Rename 'attribute' DSL method to 'input' (#4008)

Rename 'attribute' DSL method to 'input'
This commit is contained in:
Clinton Wolfe 2019-05-06 15:58:09 -04:00 committed by GitHub
commit ec91ac9ee0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 89 additions and 39 deletions

View file

@ -6,6 +6,11 @@
"action": "warn",
"prefix": "The 'default' option for attributes is being replaced by 'value' - please use it instead."
},
"attrs_dsl": {
"action": "ignore",
"comment": "See #3853",
"prefix": "The 'attribute' DSL keyword is being replaced by 'input' - please use it instead."
},
"aws_resources_in_resource_pack": {
"comment": "See #3822",
"action": "warn",

View file

@ -26,8 +26,7 @@ module Inspec
with_resource_dsl resources_dsl
# allow attributes to be accessed within control blocks
# TODO: deprecate name, use input()
define_method :attribute do |input_name, options = {}|
define_method :input do |input_name, options = {}|
if options.empty?
# Simply an access, no event here
Inspec::InputRegistry.find_or_register_input(input_name, profile_id).value
@ -35,7 +34,7 @@ module Inspec
options[:priority] = 20
options[:provider] = :inline_control_code
evt = Inspec::Input.infer_event(options)
Inspec::InputRegistry.find_or_register_input(input_name, profile_name, event: evt).value
Inspec::InputRegistry.find_or_register_input(input_name, profile_id, event: evt).value
end
end
@ -45,6 +44,11 @@ module Inspec
Inspec::InputRegistry.find_or_register_input(input_name, profile_id)
end
define_method :attribute do |name, options = {}|
Inspec.deprecate(:attrs_dsl, "Input name: #{name}, Profile: #{profile_id}")
input(name, options)
end
# Support for Control DSL plugins.
# This is called when an unknown method is encountered
# within a control block.
@ -182,9 +186,7 @@ module Inspec
profile_context_owner.register_rule(control, &block) unless control.nil?
end
# method for inputs; import input handling
# TODO: deprecate name, use input()
define_method :attribute do |input_name, options = {}|
define_method :input do |input_name, options = {}|
if options.empty?
# Simply an access, no event here
Inspec::InputRegistry.find_or_register_input(input_name, profile_id).value
@ -192,7 +194,7 @@ module Inspec
options[:priority] = 20
options[:provider] = :inline_control_code
evt = Inspec::Input.infer_event(options)
Inspec::InputRegistry.find_or_register_input(input_name, profile_name, event: evt).value
Inspec::InputRegistry.find_or_register_input(input_name, profile_id, event: evt).value
end
end
@ -202,6 +204,11 @@ module Inspec
Inspec::InputRegistry.find_or_register_input(input_name, profile_id)
end
define_method :attribute do |name, options = {}|
Inspec.deprecate(:attrs_dsl, "Input name: #{name}, Profile: #{profile_id}")
input(name, options)
end
define_method :skip_control do |id|
profile_context_owner.unregister_rule(id)
end

View file

@ -65,14 +65,30 @@ end
class RSpec::Core::ExampleGroup
# This DSL method allows us to access the values of inputs within InSpec tests
def attribute(name)
Inspec::InputRegistry.find_or_register_input(name, self.class.metadata[:profile_id]).value
def input(input_name, options = {})
profile_id = self.class.metadata[:profile_id]
if options.empty?
# Simply an access, no event here
Inspec::InputRegistry.find_or_register_input(input_name, profile_id).value
else
options[:priority] = 20
options[:provider] = :inline_control_code
evt = Inspec::Input.infer_event(options)
Inspec::InputRegistry.find_or_register_input(input_name, profile_id, event: evt).value
end
define_example_method :attribute
def input_obj(name)
end
define_example_method :input
def input_object(name)
Inspec::InputRegistry.find_or_register_input(name, self.class.metadata[:profile_id])
end
define_example_method :input_obj
define_example_method :input_object
def attribute(name, options = {})
Inspec.deprecate(:attrs_dsl, "Input name: #{name}, Profile: #{self.class.metadata[:profile_id]}")
input(name, options)
end
define_example_method :attribute
# Here, we have to ensure our method_missing gets called prior
# to RSpec::Core::ExampleGroup.method_missing (the class method).

View file

@ -61,10 +61,14 @@ describe 'inputs' do
end
end
describe 'when accessing inputs in a variety of scopes' do
it "is able to read the inputs" do
cmd = 'exec '
cmd += File.join(inputs_profiles_path, 'scoping')
describe 'when accessing inputs in a variety of scopes using the DSL' do
it "is able to read the inputs using the input keyword" do
cmd = "exec #{inputs_profiles_path}/scoping"
result = run_inspec_process(cmd, json: true)
result.must_have_all_controls_passing
end
it "is able to read the inputs using the legacy attribute keyword" do
cmd = "exec #{inputs_profiles_path}/legacy-attributes-dsl"
result = run_inspec_process(cmd, json: true)
result.must_have_all_controls_passing
end
@ -102,8 +106,5 @@ describe 'inputs' do
result.must_have_all_controls_passing
end
end
# # TODO - add test for backwards compatibility using 'attribute' in DSL
end
end

View file

@ -0,0 +1,27 @@
# This should simply not error
unless attribute('test-01', value: 'test-01') == 'test-01'
raise 'Failed bare input access'
end
describe attribute('test-02', value: 'test-02') do
it { should cmp 'test-02' }
end
describe 'mentioning an input in a bare describe block as a redirected subject' do
subject { attribute('test-03', value: 'test-03') }
it { should cmp 'test-03' }
end
control 'test using an input inside a control block as the describe subject' do
describe attribute('test-04', value: 'test-04') do
it { should cmp 'test-04' }
end
end
control "test using inputs in the test its block" do
describe 'test-05' do
it { should cmp attribute('test-05', value: 'test-05') }
end
end
# TODO: add test for OR

View file

@ -0,0 +1,8 @@
name: legacy_attribute_dsl
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: you@example.com
license: Apache-2.0
summary: A profile using the legacy attribute() syntax in control file code
version: 0.1.0

View file

@ -1,28 +1,26 @@
# This should simply not error
unless attribute('test-01') == 'test-01'
unless input('test-01', value: 'test-01') == 'test-01'
raise 'Failed bare input access'
end
describe attribute('test-02') do
describe input('test-02', value: 'test-02') do
it { should cmp 'test-02' }
end
describe 'mentioning an input in a bare describe block as a redirected subject' do
subject { attribute('test-03') }
subject { input('test-03', value: 'test-03') }
it { should cmp 'test-03' }
end
control 'test using an input inside a control block as the describe subject' do
desc 'test the val_numeric attr'
describe attribute('test-04') do
describe input('test-04', value: 'test-04') do
it { should cmp 'test-04' }
end
end
control "test using inputs in the test its block" do
describe 'test-05' do
it { should cmp attribute('test-05') }
it { should cmp input('test-05', value: 'test-05') }
end
end

View file

@ -6,15 +6,3 @@ copyright_email: inspec@chef.io
license: Apache-2.0
summary: Profile to test reading attributes in a variety of scopes
version: 0.1.0
attributes:
- name: test-01
value: test-01
- name: test-02
value: test-02
- name: test-03
value: test-03
- name: test-04
value: test-04
- name: test-05
value: test-05