Skip to content

(MAINT) Add CI workflow #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Sep 29, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
(MAINT) Rubocop - (spec) improve output capture
  • Loading branch information
chelnak committed Sep 28, 2022
commit d2e993713b79c2c5d410fa74f1e87b9c95341dfb
76 changes: 45 additions & 31 deletions spec/unit/puppet-strings/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,50 +228,59 @@ class klass(Integer $param1, $param2, String $param3 = hi) inherits foo::bar {

RSpec.shared_examples 'correct JSON' do
it 'includes data for Puppet Classes' do
puppet_class_json = YARD::Registry.all(:puppet_class).sort_by!(&:name).map!(&:to_hash).to_json
expected = YARD::Registry.all(:puppet_class).sort_by!(&:name).map!(&:to_hash).to_json

expect(json_output).to include_json(puppet_class_json)
actual = capture_output { described_class.render(nil) }
expect(actual[:stdout]).to include_json(expected)
end

it 'includes data for Puppet Data Types' do
data_types_json = YARD::Registry.all(:puppet_data_type).sort_by!(&:name).map!(&:to_hash).to_json
expect(json_output).to include_json(data_types_json)
expected = YARD::Registry.all(:puppet_data_type).sort_by!(&:name).map!(&:to_hash).to_json

actual = capture_output { described_class.render(nil) }
expect(actual[:stdout]).to include_json(expected)
end

it 'includes data for Puppet Defined Types' do
defined_types_json = YARD::Registry.all(:puppet_defined_type).sort_by!(&:name).map!(&:to_hash).to_json
expected = YARD::Registry.all(:puppet_defined_type).sort_by!(&:name).map!(&:to_hash).to_json

expect(json_output).to include_json(defined_types_json)
actual = capture_output { described_class.render(nil) }
expect(actual[:stdout]).to include_json(expected)
end

it 'includes data for Puppet Resource Types' do
resource_types_json = YARD::Registry.all(:puppet_type).sort_by!(&:name).map!(&:to_hash).to_json
expected = YARD::Registry.all(:puppet_type).sort_by!(&:name).map!(&:to_hash).to_json

expect(json_output).to include_json(resource_types_json)
actual = capture_output { described_class.render(nil) }
expect(actual[:stdout]).to include_json(expected)
end

it 'includes data for Puppet Providers' do
providers_json = YARD::Registry.all(:puppet_provider).sort_by!(&:name).map!(&:to_hash).to_json
expected = YARD::Registry.all(:puppet_provider).sort_by!(&:name).map!(&:to_hash).to_json

expect(json_output).to include_json(providers_json)
actual = capture_output { described_class.render(nil) }
expect(actual[:stdout]).to include_json(expected)
end

it 'includes data for Puppet Functions', if: TEST_PUPPET_FUNCTIONS do
puppet_functions_json = YARD::Registry.all(:puppet_function).sort_by!(&:name).map!(&:to_hash).to_json
expected = YARD::Registry.all(:puppet_function).sort_by!(&:name).map!(&:to_hash).to_json

expect(json_output).to include_json(puppet_functions_json)
actual = capture_output { described_class.render(nil) }
expect(actual[:stdout]).to include_json(expected)
end

it 'includes data for Puppet Tasks' do
puppet_tasks_json = YARD::Registry.all(:puppet_task).sort_by!(&:name).map!(&:to_hash).to_json
expected = YARD::Registry.all(:puppet_task).sort_by!(&:name).map!(&:to_hash).to_json

expect(json_output).to include_json(puppet_tasks_json)
actual = capture_output { described_class.render(nil) }
expect(actual[:stdout]).to include_json(expected)
end

it 'includes data for Puppet Plans', if: TEST_PUPPET_PLANS do
puppet_plans_json = YARD::Registry.all(:puppet_plan).sort_by!(&:name).map!(&:to_hash).to_json
expected = YARD::Registry.all(:puppet_plan).sort_by!(&:name).map!(&:to_hash).to_json

expect(json_output).to include_json(puppet_plans_json)
actual = capture_output { described_class.render(nil) }
expect(actual[:stdout]).to include_json(expected)
end
end

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

describe 'rendering JSON to stdout' do
let(:json_output) { @json_output }

before(:each) do
output = StringIO.new

old_stdout = $stdout
$stdout = output

described_class.render(nil)

$stdout = old_stdout

@json_output = output.string
end

include_examples 'correct JSON'
end
end

# Helper method to capture stdout and stderr from a block
# Source: https://gist.github.com/herrphon/2d2ebbf23c86a10aa955
#
# @param [Proc] block The block to capture output from
# @return [Hash] A hash containing the captured output
def capture_output(&_block)
begin
$stdout = StringIO.new
$stderr = StringIO.new
yield
result = {}
result[:stdout] = $stdout.string
result[:stderr] = $stderr.string
ensure
$stdout = STDOUT
$stderr = STDERR
end
result
end