Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2018-12-26 14:26:59 -05:00
parent 4111d21e0e
commit c169119895
6 changed files with 27 additions and 31 deletions

View file

@ -3,4 +3,4 @@
require 'utils/deprecation/errors'
require 'utils/deprecation/config_file'
require 'utils/deprecation/deprecator'
require 'utils/deprecation/global_method'
require 'utils/deprecation/global_method'

View file

@ -5,7 +5,6 @@ require 'inspec/globals'
module Inspec
module Deprecation
class ConfigFile
GroupEntry = Struct.new(:name, :action, :prefix, :suffix, :exit_status, :occurences)
# What actions may you specify to be taken when a deprecation is encountered?
@ -20,7 +19,7 @@ module Inspec
:warn,
].freeze
VALID_GROUP_FIELDS = [ 'action', 'suffix', 'prefix', 'exit_status'].freeze
VALID_GROUP_FIELDS = %w{action suffix prefix exit_status}.freeze
attr_reader :groups, :unknown_group_action
@ -29,7 +28,7 @@ module Inspec
begin
@raw_data = JSON.parse(io.read)
rescue JSON::ParserError => e
raise Inspec::Deprecation::MalformedConfigFileError.new("Could not parse deprecation config file: #{e.message}")
raise Inspec::Deprecation::MalformedConfigFileError, "Could not parse deprecation config file: #{e.message}"
end
@groups = {}
@ -42,7 +41,7 @@ module Inspec
def open_default_config_io
default_path = File.join(Inspec.src_root, 'etc', 'deprecations.json')
unless File.exist?(default_path)
raise Inspec::Deprecation::MalformedConfigError.new( "Missing deprecation config file: #{default_path}")
raise Inspec::Deprecation::MalformedConfigError, "Missing deprecation config file: #{default_path}"
end
File.open(default_path)
end
@ -55,10 +54,10 @@ module Inspec
validate_unknown_group_action
unless @raw_data.key?('groups')
raise Inspec::Deprecation::InvalidConfigFileError.new('Missing groups field')
raise Inspec::Deprecation::InvalidConfigFileError, 'Missing groups field'
end
unless @raw_data['groups'].is_a?(Hash)
raise Inspec::Deprecation::InvalidConfigFileError.new('Groups field must be a Hash')
raise Inspec::Deprecation::InvalidConfigFileError, 'Groups field must be a Hash'
end
@raw_data['groups'].each do |group_name, group_info|
validate_group_entry(group_name, group_info)
@ -67,17 +66,17 @@ module Inspec
def validate_file_version
unless @raw_data.key?('file_version')
raise Inspec::Deprecation::InvalidConfigFileError.new('Missing file_version field')
raise Inspec::Deprecation::InvalidConfigFileError, 'Missing file_version field'
end
unless @raw_data['file_version'] == '1.0.0'
raise Inspec::Deprecation::InvalidConfigFileError.new("Unrecognized file_version '#{@raw_data['file_version']}' - supported versions: 1.0.0")
raise Inspec::Deprecation::InvalidConfigFileError, "Unrecognized file_version '#{@raw_data['file_version']}' - supported versions: 1.0.0"
end
end
def validate_unknown_group_action
seen = (@raw_data['unknown_group_action'] || @unknown_group_action).to_sym
unless VALID_ACTIONS.include?(seen)
raise Inspec::Deprecation::UnrecognizedActionError.new("Unrecognized action for unknown groups '#{seen}' - supported actions: #{VALID_ACTIONS.map(&:to_s).join(', ')}")
raise Inspec::Deprecation::UnrecognizedActionError, "Unrecognized action for unknown groups '#{seen}' - supported actions: #{VALID_ACTIONS.map(&:to_s).join(', ')}"
end
@unknown_group_action = seen
end
@ -85,7 +84,7 @@ module Inspec
def validate_group_entry(name, opts)
opts.each do |seen_field, _value|
unless VALID_GROUP_FIELDS.include?(seen_field)
raise Inspec::Deprecation::InvalidConfigFileError.new("Unrecognized field for group '#{name}' - saw '#{seen_field}', supported fields: #{VALID_GROUP_FIELDS.map(&:to_s).join(', ')}")
raise Inspec::Deprecation::InvalidConfigFileError, "Unrecognized field for group '#{name}' - saw '#{seen_field}', supported fields: #{VALID_GROUP_FIELDS.map(&:to_s).join(', ')}"
end
end
@ -93,7 +92,7 @@ module Inspec
opts['action'] = (opts['action'] || :warn).to_sym
unless VALID_ACTIONS.include?(opts['action'])
raise Inspec::Deprecation::UnrecognizedActionError.new("Unrecognized action for group '#{name}' - saw '#{opts['action']}', supported actions: #{VALID_ACTIONS.map(&:to_s).join(', ')}")
raise Inspec::Deprecation::UnrecognizedActionError, "Unrecognized action for group '#{name}' - saw '#{opts['action']}', supported actions: #{VALID_ACTIONS.map(&:to_s).join(', ')}"
end
entry.action = opts['action']

View file

@ -5,16 +5,15 @@ require 'inspec/log'
module Inspec
module Deprecation
class Deprecator
attr_reader :config, :groups
# This is used only in functional testing
def self.set_class_test_cfg_io(io)
@@test_cfg_io = io
def self.class_test_cfg_io(io)
@@test_cfg_io = io # rubocop: disable Style/ClassVars
end
def initialize(opts = {})
@@test_cfg_io ||= nil
@@test_cfg_io ||= nil # rubocop: disable Style/ClassVars
@config = Inspec::Deprecation::ConfigFile.new(opts[:config_io] || @@test_cfg_io)
@groups = @config.groups
end
@ -32,7 +31,7 @@ module Inspec
private
def annotate_stack_information(opts)
stack = caller_locations(1,25)
stack = caller_locations(1, 25)
# Attempt to give a meaningful stack location of the place
# where the deprecated functionality was used. This is likely
@ -54,14 +53,14 @@ module Inspec
prefix += ' ' unless prefix.empty?
suffix = ' ' + suffix unless suffix.empty?
suffix += (' (used at ' + opts[:used_at_stack_frame].path+ ':' + opts[:used_at_stack_frame].lineno.to_s + ')' ) if opts.key?(:used_at_stack_frame)
suffix += (' (used at ' + opts[:used_at_stack_frame].path+ ':' + opts[:used_at_stack_frame].lineno.to_s + ')') if opts.key?(:used_at_stack_frame)
'DEPRECATION: ' + prefix + message + suffix
end
def called_from_control?
# Heuristics for determining if the deprecation is coming from within a control
stack = caller_locations(10,45)
stack = caller_locations(10, 45)
# Within a control block, that is actually an RSpec:ExampleGroup
stack.each do |frame|
@ -75,7 +74,7 @@ module Inspec
# Inspec::Log.debug("Ignoring deprecation message from #{opts[:caller][:path]}:#{opts[:caller][:lineno]}" )
end
def handle_log_action(message, level, group)
def handle_log_action(message, level, _group)
case level
when :warn
Inspec::Log.warn message
@ -92,10 +91,9 @@ module Inspec
handle_log_action(message, :error, group)
end
def handle_fail_control_action(message, group)
if called_from_control?
raise Inspec::Exceptions::ResourceFailed.new(message)
raise Inspec::Exceptions::ResourceFailed, message
else
handle_warn_action(message, group)
end
@ -106,7 +104,6 @@ module Inspec
status = (group ? group[:exit_status] : :fatal_deprecation) || :fatal_deprecation
Inspec::UI.new.exit(status)
end
end
end
end
end

View file

@ -11,4 +11,4 @@ module Inspec
class UnrecognizedActionError < InvalidConfigFileError; end
class UnrecognizedOutputStreamError < InvalidConfigFileError; end
end
end
end

View file

@ -5,4 +5,4 @@ module Inspec
deprecator = opts.delete(:deprecator) || Inspec::Deprecation::Deprecator.new
deprecator.handle_deprecation(group, msg, opts)
end
end
end

View file

@ -19,7 +19,7 @@ class DeprecationTester < Inspec.resource(1)
EOC
def fail_me
Inspec::Deprecation::Deprecator.set_class_test_cfg_io(StringIO.new(DEPRECATION_CFG))
Inspec::Deprecation::Deprecator.class_test_cfg_io(StringIO.new(DEPRECATION_CFG))
#deprecate(:a_group_that_will_fail, 'This should fail')
Inspec.deprecate(:a_group_that_will_fail, 'This should fail')
@ -27,7 +27,7 @@ class DeprecationTester < Inspec.resource(1)
end
def exit_me_default_code
Inspec::Deprecation::Deprecator.set_class_test_cfg_io(StringIO.new(DEPRECATION_CFG))
Inspec::Deprecation::Deprecator.class_test_cfg_io(StringIO.new(DEPRECATION_CFG))
#deprecate(:a_group_that_will_exit, 'This should exit')
Inspec.deprecate(:a_group_that_will_exit, 'This should exit')
@ -35,7 +35,7 @@ class DeprecationTester < Inspec.resource(1)
end
def exit_me_explicit_code
Inspec::Deprecation::Deprecator.set_class_test_cfg_io(StringIO.new(DEPRECATION_CFG))
Inspec::Deprecation::Deprecator.class_test_cfg_io(StringIO.new(DEPRECATION_CFG))
#deprecate(:a_group_that_will_exit_with_a_code, 'This should exit')
Inspec.deprecate(:a_group_that_will_exit_with_a_code, 'This should exit')
@ -43,7 +43,7 @@ class DeprecationTester < Inspec.resource(1)
end
def ignore_me
Inspec::Deprecation::Deprecator.set_class_test_cfg_io(StringIO.new(DEPRECATION_CFG))
Inspec::Deprecation::Deprecator.class_test_cfg_io(StringIO.new(DEPRECATION_CFG))
#deprecate(:an_ignored_group, 'This should be ignored')
Inspec.deprecate(:an_ignored_group, 'This should be ignored')
@ -51,7 +51,7 @@ class DeprecationTester < Inspec.resource(1)
end
def warn_me
Inspec::Deprecation::Deprecator.set_class_test_cfg_io(StringIO.new(DEPRECATION_CFG))
Inspec::Deprecation::Deprecator.class_test_cfg_io(StringIO.new(DEPRECATION_CFG))
#deprecate(:a_group_that_will_warn, 'This should warn')
Inspec.deprecate(:a_group_that_will_warn, 'This should warn')