From 48bc82e754f62481438f5bf459bc58df1285a7a3 Mon Sep 17 00:00:00 2001 From: Alex Pop Date: Tue, 20 Sep 2016 10:46:03 +0100 Subject: [PATCH 1/2] needed when converting profiles with inspec-scap --- Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile b/Gemfile index 10020b976..8bdad9f7e 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,7 @@ group :test do gem 'mocha', '~> 1.1' gem 'ruby-progressbar', '~> 1.8' gem 'inquirer' + gem 'nokogiri', '~> 1.6' end group :integration do From 13da437dccd94a23915540aec7aa665b91e21ced Mon Sep 17 00:00:00 2001 From: Alex Pop Date: Tue, 20 Sep 2016 17:03:43 +0100 Subject: [PATCH 2/2] Show skip_message and correct title --- lib/inspec/rspec_json_formatter.rb | 16 ++++++++++----- test/functional/inspec_exec_test.rb | 20 +++++++++++++++---- .../skippy-controls/controls/skipper.rb | 11 ++++++++++ .../mock/profiles/skippy-controls/inspec.yml | 3 +++ 4 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 test/unit/mock/profiles/skippy-controls/controls/skipper.rb create mode 100644 test/unit/mock/profiles/skippy-controls/inspec.yml diff --git a/lib/inspec/rspec_json_formatter.rb b/lib/inspec/rspec_json_formatter.rb index b8bacc641..5a85489e9 100644 --- a/lib/inspec/rspec_json_formatter.rb +++ b/lib/inspec/rspec_json_formatter.rb @@ -63,10 +63,18 @@ class InspecRspecMiniJson < RSpec::Core::Formatters::JsonFormatter private def format_example(example) + if example.metadata[:description_args].length == 0 + code_description = example.metadata[:full_description] + else + # For skipped profiles, rspec returns in full_description the skip_message as well. We don't want + # to mix the two, so we pick the full_description from the example.metadata[:example_group] hash. + code_description = example.metadata[:example_group][:description] + end + res = { id: example.metadata[:id], status: example.execution_result.status.to_s, - code_desc: example.full_description, + code_desc: code_description, } unless (pid = example.metadata[:profile_id]).nil? @@ -374,8 +382,6 @@ class InspecRspecCli < InspecRspecJson # rubocop:disable Metrics/ClassLength if res.length == 1 # Single test - be nice and just print the exception message if the test # failed. No need to say "1 failed". - fails.clear - skips.clear res[0][:message].to_s else [ @@ -425,7 +431,7 @@ class InspecRspecCli < InspecRspecJson # rubocop:disable Metrics/ClassLength end end - def print_tests + def print_tests # rubocop:disable Metrics/AbcSize @anonymous_tests.each do |control| control_result = control[:results] title = control_result[0][:code_desc].split[0..1].join(' ') @@ -438,7 +444,7 @@ class InspecRspecCli < InspecRspecJson # rubocop:disable Metrics/ClassLength test_result = test[:message] else # determine title - test_result = test[:code_desc].split[2..-1].join(' ') + test_result = test[:skip_message] || test[:code_desc].split[2..-1].join(' ') # show error message test_result += "\n" + test[:message] unless test[:message].nil? end diff --git a/test/functional/inspec_exec_test.rb b/test/functional/inspec_exec_test.rb index fcff74be0..f8a97eb9d 100644 --- a/test/functional/inspec_exec_test.rb +++ b/test/functional/inspec_exec_test.rb @@ -68,11 +68,11 @@ Test Summary: \e[32m0 successful\e[0m, \e[31m0 failures\e[0m, \e[37m0 skipped\e[ out.stdout.force_encoding(Encoding::UTF_8).must_include "Target: local://" out.stdout.force_encoding(Encoding::UTF_8).must_include "working should" out.stdout.force_encoding(Encoding::UTF_8).must_include "✔ eq \"working\"" - out.stdout.force_encoding(Encoding::UTF_8).must_include "skippy This" - out.stdout.force_encoding(Encoding::UTF_8).must_include "○ will be skipped intentionally." + out.stdout.force_encoding(Encoding::UTF_8).must_include "skippy\n" + out.stdout.force_encoding(Encoding::UTF_8).must_include "○ This will be skipped intentionally" out.stdout.force_encoding(Encoding::UTF_8).must_include "failing should" out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ eq \"as intended\"" - out.stdout.force_encoding(Encoding::UTF_8).must_include "Summary: \e[32m1 successful\e[0m, \e[31m1 failures\e[0m, \e[37m1 skipped\e[0m" + out.stdout.force_encoding(Encoding::UTF_8).must_include "Test Summary: \e[32m1 successful\e[0m, \e[31m1 failures\e[0m, \e[37m1 skipped\e[0m" end it 'executes only specified controls' do @@ -100,6 +100,18 @@ Test Summary: \e[32m0 successful\e[0m, \e[31m0 failures\e[0m, \e[37m0 skipped\e[ end end + describe 'with a profile that contains skipped controls' do + let(:out) { inspec('exec ' + File.join(profile_path, 'skippy-controls') + ' --no-create-lockfile') } + let(:json) { JSON.load(out.stdout) } + + it 'exits with an error' do + out.stdout.force_encoding(Encoding::UTF_8).must_include "skippy\e[0m\n\e[37m ○ This will be skipped super intentionally.\e[0m\n" + out.stdout.force_encoding(Encoding::UTF_8).must_include " ○ CONTROL database: MySQL Session\e[0m\n\e[37m ○ Can't run MySQL SQL checks without authentication\e[0m\n" + out.stdout.force_encoding(Encoding::UTF_8).must_include "Profile Summary: \e[32m0 successful\e[0m, \e[31m0 failures\e[0m, \e[37m2 skipped\e[0m" + out.exit_status.must_equal 0 + end + end + describe 'with a profile that is supported on this version of inspec' do let(:out) { inspec('exec ' + File.join(profile_path, 'supported_inspec') + ' --no-create-lockfile') } @@ -158,7 +170,7 @@ Test Summary: \e[32m2 successful\e[0m, \e[31m0 failures\e[0m, \e[37m0 skipped\e[ it 'should print all the results' do out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ tmp-1.0: Create /tmp directory (1 failed)\e[0m" - out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ should not be directory" + out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ should not be directory\n" out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ undefined method `should_nota'" out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ should not be directory\n expected `File /tmp.directory?` to return false, got true\e[0m" end diff --git a/test/unit/mock/profiles/skippy-controls/controls/skipper.rb b/test/unit/mock/profiles/skippy-controls/controls/skipper.rb new file mode 100644 index 000000000..7baf3209a --- /dev/null +++ b/test/unit/mock/profiles/skippy-controls/controls/skipper.rb @@ -0,0 +1,11 @@ +control 'CONTROL super' do + describe 'skippy' do + skip 'This will be skipped super intentionally.' + end +end + +control 'CONTROL database' do + describe mysql_session do + its('something') { should be 3 } + end +end diff --git a/test/unit/mock/profiles/skippy-controls/inspec.yml b/test/unit/mock/profiles/skippy-controls/inspec.yml new file mode 100644 index 000000000..968975d37 --- /dev/null +++ b/test/unit/mock/profiles/skippy-controls/inspec.yml @@ -0,0 +1,3 @@ +name: skippy-controls +title: skip skip skip +version: 1.0.0