@@ -228,50 +228,59 @@ class klass(Integer $param1, $param2, String $param3 = hi) inherits foo::bar {
228
228
229
229
RSpec . shared_examples 'correct JSON' do
230
230
it 'includes data for Puppet Classes' do
231
- puppet_class_json = YARD ::Registry . all ( :puppet_class ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
231
+ expected = YARD ::Registry . all ( :puppet_class ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
232
232
233
- expect ( json_output ) . to include_json ( puppet_class_json )
233
+ actual = capture_output { described_class . render ( nil ) }
234
+ expect ( actual [ :stdout ] ) . to include_json ( expected )
234
235
end
235
236
236
237
it 'includes data for Puppet Data Types' do
237
- data_types_json = YARD ::Registry . all ( :puppet_data_type ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
238
- expect ( json_output ) . to include_json ( data_types_json )
238
+ expected = YARD ::Registry . all ( :puppet_data_type ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
239
+
240
+ actual = capture_output { described_class . render ( nil ) }
241
+ expect ( actual [ :stdout ] ) . to include_json ( expected )
239
242
end
240
243
241
244
it 'includes data for Puppet Defined Types' do
242
- defined_types_json = YARD ::Registry . all ( :puppet_defined_type ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
245
+ expected = YARD ::Registry . all ( :puppet_defined_type ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
243
246
244
- expect ( json_output ) . to include_json ( defined_types_json )
247
+ actual = capture_output { described_class . render ( nil ) }
248
+ expect ( actual [ :stdout ] ) . to include_json ( expected )
245
249
end
246
250
247
251
it 'includes data for Puppet Resource Types' do
248
- resource_types_json = YARD ::Registry . all ( :puppet_type ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
252
+ expected = YARD ::Registry . all ( :puppet_type ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
249
253
250
- expect ( json_output ) . to include_json ( resource_types_json )
254
+ actual = capture_output { described_class . render ( nil ) }
255
+ expect ( actual [ :stdout ] ) . to include_json ( expected )
251
256
end
252
257
253
258
it 'includes data for Puppet Providers' do
254
- providers_json = YARD ::Registry . all ( :puppet_provider ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
259
+ expected = YARD ::Registry . all ( :puppet_provider ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
255
260
256
- expect ( json_output ) . to include_json ( providers_json )
261
+ actual = capture_output { described_class . render ( nil ) }
262
+ expect ( actual [ :stdout ] ) . to include_json ( expected )
257
263
end
258
264
259
265
it 'includes data for Puppet Functions' , if : TEST_PUPPET_FUNCTIONS do
260
- puppet_functions_json = YARD ::Registry . all ( :puppet_function ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
266
+ expected = YARD ::Registry . all ( :puppet_function ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
261
267
262
- expect ( json_output ) . to include_json ( puppet_functions_json )
268
+ actual = capture_output { described_class . render ( nil ) }
269
+ expect ( actual [ :stdout ] ) . to include_json ( expected )
263
270
end
264
271
265
272
it 'includes data for Puppet Tasks' do
266
- puppet_tasks_json = YARD ::Registry . all ( :puppet_task ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
273
+ expected = YARD ::Registry . all ( :puppet_task ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
267
274
268
- expect ( json_output ) . to include_json ( puppet_tasks_json )
275
+ actual = capture_output { described_class . render ( nil ) }
276
+ expect ( actual [ :stdout ] ) . to include_json ( expected )
269
277
end
270
278
271
279
it 'includes data for Puppet Plans' , if : TEST_PUPPET_PLANS do
272
- puppet_plans_json = YARD ::Registry . all ( :puppet_plan ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
280
+ expected = YARD ::Registry . all ( :puppet_plan ) . sort_by! ( &:name ) . map! ( &:to_hash ) . to_json
273
281
274
- expect ( json_output ) . to include_json ( puppet_plans_json )
282
+ actual = capture_output { described_class . render ( nil ) }
283
+ expect ( actual [ :stdout ] ) . to include_json ( expected )
275
284
end
276
285
end
277
286
@@ -292,21 +301,26 @@ class klass(Integer $param1, $param2, String $param3 = hi) inherits foo::bar {
292
301
end
293
302
294
303
describe 'rendering JSON to stdout' do
295
- let ( :json_output ) { @json_output }
296
-
297
- before ( :each ) do
298
- output = StringIO . new
299
-
300
- old_stdout = $stdout
301
- $stdout = output
302
-
303
- described_class . render ( nil )
304
-
305
- $stdout = old_stdout
306
-
307
- @json_output = output . string
308
- end
309
-
310
304
include_examples 'correct JSON'
311
305
end
312
306
end
307
+
308
+ # Helper method to capture stdout and stderr from a block
309
+ # Source: https://gist.github.com/herrphon/2d2ebbf23c86a10aa955
310
+ #
311
+ # @param [Proc] block The block to capture output from
312
+ # @return [Hash] A hash containing the captured output
313
+ def capture_output ( &_block )
314
+ begin
315
+ $stdout = StringIO . new
316
+ $stderr = StringIO . new
317
+ yield
318
+ result = { }
319
+ result [ :stdout ] = $stdout. string
320
+ result [ :stderr ] = $stderr. string
321
+ ensure
322
+ $stdout = STDOUT
323
+ $stderr = STDERR
324
+ end
325
+ result
326
+ end
0 commit comments