Alter split code to handle comments containing the word 'control', and rename some vars

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2021-04-06 13:58:41 -04:00
parent d6e63fce6e
commit 4dbee6b7bd
2 changed files with 19 additions and 15 deletions

View file

@ -234,7 +234,7 @@ module Inspec
# Wipe out waived controls
def filter_waived_controls
cfg = Inspec::Config.cached
return self.tests unless cfg["filter_waived_controls"]
return tests unless cfg["filter_waived_controls"]
## Find the waivers file
# - TODO: cli_opts and instance_variable_get could be exposed
@ -255,37 +255,39 @@ module Inspec
end
waived_control_ids << waiver_content.keys
end
control_id_regex = "(#{waived_control_ids.join('|')})"
waived_control_id_regex = "(#{waived_control_ids.join("|")})"
## Purge tests (this could be doone in next block for performance)
## TODO: implement earlier with pure AST and pure autocorrect AST
purged_tests = {}
filtered_tests = {}
if cfg["retain_waiver_data"]
# VERY EXPERIMENTAL, but an empty describe block at the top level
# of the control blocks evaluation of ruby code until later-term
# waivers (behind the scenes this tells RSpec to hold on and use its internals to lazy load the code). This allows current waiver-data (e.g. skips) to still
# be processed and rendered
tests.each do |key, value|
cleared_tests = value.split("control ").collect do |element|
tests.each do |control_filename, source_code|
cleared_tests = source_code.scan(/control\s+['"].+?['"].+?(?=(?:control\s+['"].+?['"])|\z)/m).collect do |element|
next if element.blank?
if element&.match?(control_id_regex)
if element&.match?(waived_control_id_regex)
splitlines = element.split("\n")
splitlines[0] + "\ndescribe '---' do\n" + splitlines[1..-1].join("\n") + "\nend\n"
else
element
end
end.join("control ")
purged_tests[key] = cleared_tests
end.join("")
filtered_tests[control_filename] = cleared_tests
end
else
tests.each do |key, value|
cleared_tests = value.split("control ").select do |element|
!element&.match?(control_id_regex)
end.join("control ")
purged_tests[key] = cleared_tests
tests.each do |control_filename, source_code|
cleared_tests = source_code.scan(/control\s+['"].+?['"].+?(?=(?:control\s+['"].+?['"])|\z)/m).select do |control_code|
!control_code.match?(waived_control_id_regex)
end.join("")
filtered_tests[control_filename] = cleared_tests
end
end
purged_tests
filtered_tests
end
# This creates the list of controls provided in the --controls options which need to be include

View file

@ -5,6 +5,8 @@ control '01_my_broken_control' do
end
end
# This is a comment mentioning a control word
control '02_my_working_but_waived_control' do
describe true do
it { should eq true }