Skip to content

Commit 06abf37

Browse files
committed
(MAINT) Rubocop - improve output capture
1 parent 4404403 commit 06abf37

File tree

1 file changed

+45
-31
lines changed

1 file changed

+45
-31
lines changed

spec/unit/puppet-strings/json_spec.rb

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -228,50 +228,59 @@ class klass(Integer $param1, $param2, String $param3 = hi) inherits foo::bar {
228228

229229
RSpec.shared_examples 'correct JSON' do
230230
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
232232

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)
234235
end
235236

236237
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)
239242
end
240243

241244
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
243246

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)
245249
end
246250

247251
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
249253

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)
251256
end
252257

253258
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
255260

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)
257263
end
258264

259265
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
261267

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)
263270
end
264271

265272
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
267274

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)
269277
end
270278

271279
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
273281

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)
275284
end
276285
end
277286

@@ -292,21 +301,26 @@ class klass(Integer $param1, $param2, String $param3 = hi) inherits foo::bar {
292301
end
293302

294303
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-
310304
include_examples 'correct JSON'
311305
end
312306
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

Comments
 (0)