First pass on renaming attribute->input, unit test passes

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2019-02-11 22:39:25 -05:00
parent 46415dfbff
commit a1982a5f8b
4 changed files with 100 additions and 100 deletions

View file

@ -18,29 +18,29 @@ module Inspec
class ConfigError::MalformedJson < ConfigError; end class ConfigError::MalformedJson < ConfigError; end
class ConfigError::Invalid < ConfigError; end class ConfigError::Invalid < ConfigError; end
class Attribute class Input
class Error < Inspec::Error; end class Error < Inspec::Error; end
class ValidationError < Error class ValidationError < Error
attr_accessor :attribute_name attr_accessor :input_name
attr_accessor :attribute_value attr_accessor :input_value
attr_accessor :attribute_type attr_accessor :input_type
end end
class TypeError < Error class TypeError < Error
attr_accessor :attribute_type attr_accessor :input_type
end end
class RequiredError < Error class RequiredError < Error
attr_accessor :attribute_name attr_accessor :input_name
end end
end end
class AttributeRegistry class InputRegistry
class Error < Inspec::Error; end class Error < Inspec::Error; end
class ProfileError < Error class ProfileError < Error
attr_accessor :profile_name attr_accessor :profile_name
end end
class AttributeError < Error class InputError < Error
attr_accessor :profile_name attr_accessor :profile_name
attr_accessor :attribute_name attr_accessor :input_name
end end
end end

View file

@ -62,7 +62,7 @@ module Inspec
list[profile][name] list[profile][name]
else else
list[profile] = {} unless profile_exist?(profile) list[profile] = {} unless profile_exist?(profile)
list[profile][name] = Inspec::Attribute.new(name, options) list[profile][name] = Inspec::Input.new(name, options)
end end
end end

View file

@ -3,7 +3,7 @@
require 'utils/deprecation' require 'utils/deprecation'
module Inspec module Inspec
class Attribute class Input
attr_accessor :name attr_accessor :name
VALID_TYPES = %w{ VALID_TYPES = %w{
@ -37,7 +37,7 @@ module Inspec
end end
def to_s def to_s
"Attribute '#{@name}' does not have a value. Skipping test." "Input '#{@name}' does not have a value. Skipping test."
end end
end end
@ -45,9 +45,9 @@ module Inspec
@name = name @name = name
@opts = options @opts = options
if @opts.key?(:default) if @opts.key?(:default)
Inspec.deprecate(:attrs_value_replaces_default, "attribute name: '#{name}'") Inspec.deprecate(:attrs_value_replaces_default, "input name: '#{name}'")
if @opts.key?(:value) if @opts.key?(:value)
Inspec::Log.warn "Attribute #{@name} created using both :default and :value options - ignoring :default" Inspec::Log.warn "Input #{@name} created using both :default and :value options - ignoring :default"
@opts.delete(:default) @opts.delete(:default)
else else
@opts[:value] = @opts.delete(:default) @opts[:value] = @opts.delete(:default)
@ -104,7 +104,7 @@ module Inspec
end end
def to_s def to_s
"Attribute #{@name} with #{@value}" "Input #{@name} with #{@value}"
end end
private private
@ -115,9 +115,9 @@ module Inspec
# value will be set already if a secrets file was passed in # value will be set already if a secrets file was passed in
if (!@opts.key?(:default) && value.nil?) || (@opts[:default].nil? && value.nil?) if (!@opts.key?(:default) && value.nil?) || (@opts[:default].nil? && value.nil?)
error = Inspec::Attribute::RequiredError.new error = Inspec::Input::RequiredError.new
error.attribute_name = @name error.input_name = @name
raise error, "Attribute '#{error.attribute_name}' is required and does not have a value." raise error, "Input '#{error.input_name}' is required and does not have a value."
end end
end end
@ -129,9 +129,9 @@ module Inspec
} }
type = abbreviations[type] if abbreviations.key?(type) type = abbreviations[type] if abbreviations.key?(type)
if !VALID_TYPES.include?(type) if !VALID_TYPES.include?(type)
error = Inspec::Attribute::TypeError.new error = Inspec::Input::TypeError.new
error.attribute_type = type error.input_type = type
raise error, "Type '#{error.attribute_type}' is not a valid attribute type." raise error, "Type '#{error.input_type}' is not a valid input type."
end end
type type
end end
@ -168,11 +168,11 @@ module Inspec
end end
if invalid_type == true if invalid_type == true
error = Inspec::Attribute::ValidationError.new error = Inspec::Input::ValidationError.new
error.attribute_name = @name error.input_name = @name
error.attribute_value = value error.input_value = value
error.attribute_type = type error.input_type = type
raise error, "Attribute '#{error.attribute_name}' with value '#{error.attribute_value}' does not validate to type '#{error.attribute_type}'." raise error, "Input '#{error.input_name}' with value '#{error.input_value}' does not validate to type '#{error.input_type}'."
end end
end end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

View file

@ -3,234 +3,234 @@
require 'helper' require 'helper'
require 'inspec/objects/input' require 'inspec/objects/input'
describe Inspec::Attribute do describe Inspec::Input do
let(:attribute) { Inspec::Attribute.new('test_attribute') } let(:input) { Inspec::Input.new('test_input') }
it 'support storing and returning false' do it 'support storing and returning false' do
attribute.value = false input.value = false
attribute.value.must_equal false input.value.must_equal false
end end
describe 'the dummy value used when value is not set' do describe 'the dummy value used when value is not set' do
it 'returns the actual value, not the dummy object, if one is assigned' do it 'returns the actual value, not the dummy object, if one is assigned' do
attribute.value = 'new_value' input.value = 'new_value'
attribute.value.must_equal 'new_value' input.value.must_equal 'new_value'
end end
it 'returns the dummy value if no value is assigned' do it 'returns the dummy value if no value is assigned' do
attribute.value.must_be_kind_of Inspec::Attribute::DEFAULT_ATTRIBUTE input.value.must_be_kind_of Inspec::Input::DEFAULT_ATTRIBUTE
attribute.value.to_s.must_equal "Attribute 'test_attribute' does not have a value. Skipping test." input.value.to_s.must_equal "Input 'test_input' does not have a value. Skipping test."
end end
it 'has a dummy value that can be called like a nested map' do it 'has a dummy value that can be called like a nested map' do
attribute.value['hello']['world'][1][2]['three'].wont_be_nil input.value['hello']['world'][1][2]['three'].wont_be_nil
end end
it 'has a dummy value that can take any nested method calls' do it 'has a dummy value that can take any nested method calls' do
attribute.value.call.some.fancy.functions.wont_be_nil input.value.call.some.fancy.functions.wont_be_nil
end end
end end
describe 'attribute with a value set' do describe 'input with a value set' do
it 'returns the user-configured value' do it 'returns the user-configured value' do
attribute = Inspec::Attribute.new('test_attribute', value: 'some_value') input = Inspec::Input.new('test_input', value: 'some_value')
attribute.value.must_equal 'some_value' input.value.must_equal 'some_value'
end end
it 'returns the user-configured value if nil is explicitly assigned' do it 'returns the user-configured value if nil is explicitly assigned' do
attribute = Inspec::Attribute.new('test_attribute', value: nil) input = Inspec::Input.new('test_input', value: nil)
attribute.value.must_be_nil input.value.must_be_nil
end end
it 'returns the user-configured value if false is explicitly assigned' do it 'returns the user-configured value if false is explicitly assigned' do
attribute = Inspec::Attribute.new('test_attribute', value: false) input = Inspec::Input.new('test_input', value: false)
attribute.value.must_equal false input.value.must_equal false
end end
it 'returns a new value if the value has been assigned by value=' do it 'returns a new value if the value has been assigned by value=' do
attribute = Inspec::Attribute.new('test_attribute', value: 'original_value') input = Inspec::Input.new('test_input', value: 'original_value')
attribute.value = 'new_value' input.value = 'new_value'
attribute.value.must_equal 'new_value' input.value.must_equal 'new_value'
end end
it 'accepts the legacy ":default" option' do it 'accepts the legacy ":default" option' do
attribute = Inspec::Attribute.new('test_attribute', default: 'a_default') input = Inspec::Input.new('test_input', default: 'a_default')
attribute.value.must_equal 'a_default' input.value.must_equal 'a_default'
end end
it 'accepts the legacy ":default" and ":value" options' do it 'accepts the legacy ":default" and ":value" options' do
attribute = Inspec::Attribute.new('test_attribute', default: 'a_default', value: 'a_value') input = Inspec::Input.new('test_input', default: 'a_default', value: 'a_value')
attribute.value.must_equal 'a_value' input.value.must_equal 'a_value'
end end
end end
describe 'validate required method' do describe 'validate required method' do
it 'does not error if a value is set' do it 'does not error if a value is set' do
attribute = Inspec::Attribute.new('test_attribute', value: 'some_value', required: true) input = Inspec::Input.new('test_input', value: 'some_value', required: true)
attribute.value.must_equal 'some_value' input.value.must_equal 'some_value'
end end
it 'does not error if a value is specified by value=' do it 'does not error if a value is specified by value=' do
attribute = Inspec::Attribute.new('test_attribute', required: true) input = Inspec::Input.new('test_input', required: true)
attribute.value = 'test_value' input.value = 'test_value'
attribute.value.must_equal 'test_value' input.value.must_equal 'test_value'
end end
it 'returns an error if no value is set' do it 'returns an error if no value is set' do
# Assigning the cli_command is needed because in check mode, we don't error # Assigning the cli_command is needed because in check mode, we don't error
# on unset attributes. This is how you tell the attribute system we are not in # on unset inputs. This is how you tell the input system we are not in
# check mode, apparently. # check mode, apparently.
Inspec::BaseCLI.inspec_cli_command = :exec Inspec::BaseCLI.inspec_cli_command = :exec
attribute = Inspec::Attribute.new('test_attribute', required: true) input = Inspec::Input.new('test_input', required: true)
ex = assert_raises(Inspec::Attribute::RequiredError) { attribute.value } ex = assert_raises(Inspec::Input::RequiredError) { input.value }
ex.message.must_match /Attribute 'test_attribute' is required and does not have a value./ ex.message.must_match /Input 'test_input' is required and does not have a value./
Inspec::BaseCLI.inspec_cli_command = nil Inspec::BaseCLI.inspec_cli_command = nil
end end
end end
describe 'validate value type method' do describe 'validate value type method' do
let(:opts) { {} } let(:opts) { {} }
let(:attribute) { Inspec::Attribute.new('test_attribute', opts) } let(:input) { Inspec::Input.new('test_input', opts) }
it 'validates a string type' do it 'validates a string type' do
opts[:type] = 'string' opts[:type] = 'string'
attribute.send(:validate_value_type, 'string') input.send(:validate_value_type, 'string')
end end
it 'returns an error if a invalid string is set' do it 'returns an error if a invalid string is set' do
opts[:type] = 'string' opts[:type] = 'string'
ex = assert_raises(Inspec::Attribute::ValidationError) { attribute.send(:validate_value_type, 123) } ex = assert_raises(Inspec::Input::ValidationError) { input.send(:validate_value_type, 123) }
ex.message.must_match /Attribute 'test_attribute' with value '123' does not validate to type 'String'./ ex.message.must_match /Input 'test_input' with value '123' does not validate to type 'String'./
end end
it 'validates a numeric type' do it 'validates a numeric type' do
opts[:type] = 'numeric' opts[:type] = 'numeric'
attribute.send(:validate_value_type, 123.33) input.send(:validate_value_type, 123.33)
end end
it 'returns an error if a invalid numeric is set' do it 'returns an error if a invalid numeric is set' do
opts[:type] = 'numeric' opts[:type] = 'numeric'
ex = assert_raises(Inspec::Attribute::ValidationError) { attribute.send(:validate_value_type, 'invalid') } ex = assert_raises(Inspec::Input::ValidationError) { input.send(:validate_value_type, 'invalid') }
ex.message.must_match /Attribute 'test_attribute' with value 'invalid' does not validate to type 'Numeric'./ ex.message.must_match /Input 'test_input' with value 'invalid' does not validate to type 'Numeric'./
end end
it 'validates a regex type' do it 'validates a regex type' do
opts[:type] = 'regex' opts[:type] = 'regex'
attribute.send(:validate_value_type, '/^\d*$/') input.send(:validate_value_type, '/^\d*$/')
end end
it 'returns an error if a invalid regex is set' do it 'returns an error if a invalid regex is set' do
opts[:type] = 'regex' opts[:type] = 'regex'
ex = assert_raises(Inspec::Attribute::ValidationError) { attribute.send(:validate_value_type, '/(.+/') } ex = assert_raises(Inspec::Input::ValidationError) { input.send(:validate_value_type, '/(.+/') }
ex.message.must_match "Attribute 'test_attribute' with value '/(.+/' does not validate to type 'Regexp'." ex.message.must_match "Input 'test_input' with value '/(.+/' does not validate to type 'Regexp'."
end end
it 'validates a array type' do it 'validates a array type' do
opts[:type] = 'Array' opts[:type] = 'Array'
value = [1, 2, 3] value = [1, 2, 3]
attribute.send(:validate_value_type, value) input.send(:validate_value_type, value)
end end
it 'returns an error if a invalid array is set' do it 'returns an error if a invalid array is set' do
opts[:type] = 'Array' opts[:type] = 'Array'
value = { a: 1, b: 2, c: 3 } value = { a: 1, b: 2, c: 3 }
ex = assert_raises(Inspec::Attribute::ValidationError) { attribute.send(:validate_value_type, value) } ex = assert_raises(Inspec::Input::ValidationError) { input.send(:validate_value_type, value) }
ex.message.must_match /Attribute 'test_attribute' with value '{:a=>1, :b=>2, :c=>3}' does not validate to type 'Array'./ ex.message.must_match /Input 'test_input' with value '{:a=>1, :b=>2, :c=>3}' does not validate to type 'Array'./
end end
it 'validates a hash type' do it 'validates a hash type' do
opts[:type] = 'Hash' opts[:type] = 'Hash'
value = { a: 1, b: 2, c: 3 } value = { a: 1, b: 2, c: 3 }
attribute.send(:validate_value_type, value) input.send(:validate_value_type, value)
end end
it 'returns an error if a invalid hash is set' do it 'returns an error if a invalid hash is set' do
opts[:type] = 'hash' opts[:type] = 'hash'
ex = assert_raises(Inspec::Attribute::ValidationError) { attribute.send(:validate_value_type, 'invalid') } ex = assert_raises(Inspec::Input::ValidationError) { input.send(:validate_value_type, 'invalid') }
ex.message.must_match /Attribute 'test_attribute' with value 'invalid' does not validate to type 'Hash'./ ex.message.must_match /Input 'test_input' with value 'invalid' does not validate to type 'Hash'./
end end
it 'validates a boolean type' do it 'validates a boolean type' do
opts[:type] = 'boolean' opts[:type] = 'boolean'
attribute.send(:validate_value_type, false) input.send(:validate_value_type, false)
attribute.send(:validate_value_type, true) input.send(:validate_value_type, true)
end end
it 'returns an error if a invalid boolean is set' do it 'returns an error if a invalid boolean is set' do
opts[:type] = 'boolean' opts[:type] = 'boolean'
ex = assert_raises(Inspec::Attribute::ValidationError) { attribute.send(:validate_value_type, 'not_true') } ex = assert_raises(Inspec::Input::ValidationError) { input.send(:validate_value_type, 'not_true') }
ex.message.must_match /Attribute 'test_attribute' with value 'not_true' does not validate to type 'Boolean'./ ex.message.must_match /Input 'test_input' with value 'not_true' does not validate to type 'Boolean'./
end end
it 'validates a any type' do it 'validates a any type' do
opts[:type] = 'any' opts[:type] = 'any'
attribute.send(:validate_value_type, false) input.send(:validate_value_type, false)
attribute.send(:validate_value_type, true) input.send(:validate_value_type, true)
attribute.send(:validate_value_type, 1) input.send(:validate_value_type, 1)
attribute.send(:validate_value_type, 'bob') input.send(:validate_value_type, 'bob')
end end
end end
describe 'validate type method' do describe 'validate type method' do
it 'converts regex to Regexp' do it 'converts regex to Regexp' do
attribute.send(:validate_type, 'regex').must_equal 'Regexp' input.send(:validate_type, 'regex').must_equal 'Regexp'
end end
it 'returns the same value if there is nothing to clean' do it 'returns the same value if there is nothing to clean' do
attribute.send(:validate_type, 'String').must_equal 'String' input.send(:validate_type, 'String').must_equal 'String'
end end
it 'returns an error if a invalid type is sent' do it 'returns an error if a invalid type is sent' do
ex = assert_raises(Inspec::Attribute::TypeError) { attribute.send(:validate_type, 'dressing') } ex = assert_raises(Inspec::Input::TypeError) { input.send(:validate_type, 'dressing') }
ex.message.must_match /Type 'Dressing' is not a valid attribute type./ ex.message.must_match /Type 'Dressing' is not a valid input type./
end end
end end
describe 'valid_regexp? method' do describe 'valid_regexp? method' do
it 'validates a string regex' do it 'validates a string regex' do
attribute.send(:valid_regexp?, '/.*/').must_equal true input.send(:valid_regexp?, '/.*/').must_equal true
end end
it 'validates a slash regex' do it 'validates a slash regex' do
attribute.send(:valid_regexp?, /.*/).must_equal true input.send(:valid_regexp?, /.*/).must_equal true
end end
it 'does not vaildate a invalid regex' do it 'does not vaildate a invalid regex' do
attribute.send(:valid_regexp?, '/.*(/').must_equal false input.send(:valid_regexp?, '/.*(/').must_equal false
end end
end end
describe 'valid_numeric? method' do describe 'valid_numeric? method' do
it 'validates a string number' do it 'validates a string number' do
attribute.send(:valid_numeric?, '123').must_equal true input.send(:valid_numeric?, '123').must_equal true
end end
it 'validates a float number' do it 'validates a float number' do
attribute.send(:valid_numeric?, 44.55).must_equal true input.send(:valid_numeric?, 44.55).must_equal true
end end
it 'validats a wrong padded number' do it 'validats a wrong padded number' do
attribute.send(:valid_numeric?, '00080').must_equal true input.send(:valid_numeric?, '00080').must_equal true
end end
it 'does not vaildate a invalid number' do it 'does not vaildate a invalid number' do
attribute.send(:valid_numeric?, '55.55.55.5').must_equal false input.send(:valid_numeric?, '55.55.55.5').must_equal false
end end
it 'does not vaildate a invalid string' do it 'does not vaildate a invalid string' do
attribute.send(:valid_numeric?, 'one').must_equal false input.send(:valid_numeric?, 'one').must_equal false
end end
it 'does not vaildate a fraction' do it 'does not vaildate a fraction' do
attribute.send(:valid_numeric?, '1/2').must_equal false input.send(:valid_numeric?, '1/2').must_equal false
end end
end end
describe 'to_ruby method' do describe 'to_ruby method' do
it 'generates the code for the attribute' do it 'generates the code for the input' do
attribute = Inspec::Attribute.new('application_port', description: 'The port my application uses', value: 80) input = Inspec::Input.new('application_port', description: 'The port my application uses', value: 80)
ruby_code = attribute.to_ruby ruby_code = input.to_ruby
ruby_code.must_include "attr_application_port = " # Should assign to a var ruby_code.must_include "attr_application_port = " # Should assign to a var
ruby_code.must_include "attribute('application_port'" # Should have the DSL call ruby_code.must_include "attribute('application_port'" # Should have the DSL call
ruby_code.must_include 'value: 80' ruby_code.must_include 'value: 80'
@ -240,7 +240,7 @@ describe Inspec::Attribute do
# Try to eval the code to verify that the generated code was valid ruby. # Try to eval the code to verify that the generated code was valid ruby.
# Note that the attribute() method is part of the DSL, so we need to # Note that the attribute() method is part of the DSL, so we need to
# alter the call into something that can respond - the constructor will do # alter the call into something that can respond - the constructor will do
ruby_code_for_eval = ruby_code.sub(/attribute\(/,'Inspec::Attribute.new(') ruby_code_for_eval = ruby_code.sub(/attribute\(/,'Inspec::Input.new(')
# This will throw exceptions if there is a problem # This will throw exceptions if there is a problem
new_attr = eval(ruby_code_for_eval) # Could use ripper! new_attr = eval(ruby_code_for_eval) # Could use ripper!