Added impact setting option in only_if

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>
This commit is contained in:
Nikita Mathur 2022-07-12 16:31:32 +05:30
parent 7cbaa892cc
commit 84c14a505a
3 changed files with 31 additions and 5 deletions

View file

@ -16,6 +16,7 @@ module Inspec
attr_reader :__waiver_data attr_reader :__waiver_data
attr_accessor :resource_dsl attr_accessor :resource_dsl
attr_reader :__profile_id attr_reader :__profile_id
attr_accessor :impact
def initialize(id, profile_id, resource_dsl, opts, &block) def initialize(id, profile_id, resource_dsl, opts, &block)
@impact = nil @impact = nil
@ -133,10 +134,11 @@ module Inspec
# #
# @param [Type] &block returns true if tests are added, false otherwise # @param [Type] &block returns true if tests are added, false otherwise
# @return [nil] # @return [nil]
def only_if(message = nil) def only_if(message = nil, impact: nil)
return unless block_given? return unless block_given?
return if @__skip_only_if_eval == true return if @__skip_only_if_eval == true
self.impact = impact if impact && !yield
@__skip_rule[:result] ||= !yield @__skip_rule[:result] ||= !yield
@__skip_rule[:type] = :only_if @__skip_rule[:type] = :only_if
@__skip_rule[:message] = message @__skip_rule[:message] = message

View file

@ -60,3 +60,19 @@ control "tmp-5.0" do
it { should cmp "e.1" } it { should cmp "e.1" }
end end
end end
# Example of setting impact using code and marking it N/A
control "tmp-6.0.1" do
only_if(impact: 0.0) { false }
describe file("/tmp") do
it { should be_directory }
end
end
# Example of setting impact using code and not marked as N/A
control "tmp-6.0.2" do
only_if(impact: 0.5) { false }
describe file("/tmp") do
it { should be_directory }
end
end

View file

@ -1340,14 +1340,14 @@ EOT
end end
it "should show enhanced_outcomes for skipped tests in controls" do it "should show enhanced_outcomes for skipped tests in controls" do
_(run_result.stdout).must_include "3 skipped" _(run_result.stdout).must_include "5 skipped"
_(run_result.stdout).must_include "2 controls not reviewed" _(run_result.stdout).must_include "3 controls not reviewed"
_(run_result.stdout).must_include "N/R" _(run_result.stdout).must_include "N/R"
end end
it "should show enhanced_outcomes for controls with impact 0" do it "should show enhanced_outcomes for controls with impact 0" do
_(run_result.stdout).must_include "3 skipped" _(run_result.stdout).must_include "5 skipped"
_(run_result.stdout).must_include "2 controls not applicable" _(run_result.stdout).must_include "3 controls not applicable"
_(run_result.stdout).must_include "N/A" _(run_result.stdout).must_include "N/A"
end end
@ -1364,6 +1364,14 @@ EOT
it "should show enhanced_outcomes for passed controls" do it "should show enhanced_outcomes for passed controls" do
_(run_result.stdout).must_include "1 successful control" _(run_result.stdout).must_include "1 successful control"
end end
it "should mark control as N/A using zero impact from only_if" do
_(run_result.stdout).must_include "N/A tmp-6.0.1"
end
it "should not mark control as N/A using non-zeo impact from only_if" do
_(run_result.stdout).must_include "N/R tmp-6.0.2"
end
end end
describe "when running profile with enhanced_outcomes option and yaml reporter" do describe "when running profile with enhanced_outcomes option and yaml reporter" do