Merge pull request #5026 from inspec/cw/silence-deprecations

CLI Option to silence deprecations
This commit is contained in:
James Stocks 2020-06-05 14:10:50 +01:00 committed by GitHub
commit d5f825e716
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 0 deletions

View file

@ -155,6 +155,9 @@ module Inspec
desc: "Show progress while executing tests."
option :distinct_exit, type: :boolean, default: true,
desc: "Exit with code 101 if any tests fail, and 100 if any are skipped (default). If disabled, exit 0 on skips and 1 for failures."
option :silence_deprecations, type: :array,
banner: "[all]|[GROUP GROUP...]",
desc: "Suppress deprecation warnings. See install_dir/etc/deprecations.json for list of GROUPs or use 'all'."
end
def self.format_platform_info(params: {}, indent: 0, color: 39)

View file

@ -1,6 +1,7 @@
require "stringio"
require "json"
require "inspec/globals"
require "inspec/config"
module Inspec
module Deprecation
@ -32,6 +33,7 @@ module Inspec
@groups = {}
@unknown_group_action = :warn
validate!
silence_deprecations_from_cli
end
private
@ -45,6 +47,25 @@ module Inspec
File.open(default_path)
end
def silence_deprecations_from_cli
# Read --silence-deprecations CLI option
cfg = Inspec::Config.cached
return unless cfg[:silence_deprecations]
groups_to_silence = cfg[:silence_deprecations]
silence_all = groups_to_silence.include?("all")
groups.each do |group_name, group|
# Only silence things that warn. Don't silence things that exit;
# those harsher measures are usually protecting removed code and ignoring
# and continuing regardless would be perilous and lead to errors.
if %i{warn fail_control}.include?(group.action) &&
(silence_all || groups_to_silence.include?(group_name.to_s))
group.action = :ignore
end
end
end
#====================================================================================================#
# Validation
#====================================================================================================#

View file

@ -180,4 +180,30 @@ describe "Deprecation Facility Behavior" do
end
end
end
describe "when desprecations are silenced via the CLI" do
let(:profile_name) { "typical" }
describe "when a specific deprecation is silenced" do
# Run two controls with two different deprecation groups, and silence one
let(:control_flag) { "--controls deprecate_warn_mode deprecate_fail_mode --silence-deprecations a_group_that_will_fail" }
it "silences the specified deprecation but not others" do
# stderr will still contain the warning deprecation
_(run_result.stderr).must_include "DEPRECATION: This should warn"
# control 0, result 1 will fail but not contain a deprecation warning
_(json_result[1]["status"]).must_equal "failed"
_(json_result[1]["message"]).wont_include "DEPRECATION"
end
end
describe "when all deprecations are silenced with 'all'" do
# Run two controls with two different deprecation groups, and silence all deprecations with the "all" param
let(:control_flag) { "--controls deprecate_warn_mode deprecate_fail_mode --silence-deprecations all" }
it "silences all deprecations explicitly" do
# stderr will not contain the warning deprecation
_(run_result.stderr).wont_include "DEPRECATION: This should warn"
# control 0, result 1 will fail but not contain a deprecation warning
_(json_result[1]["status"]).must_equal "failed"
_(json_result[1]["message"]).wont_include "DEPRECATION"
end
end
end
end