Refactor to DRY up repeated implementation of input()

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2019-11-06 11:13:18 -05:00
parent b776b84603
commit 11438d5d3c
3 changed files with 36 additions and 21 deletions

View file

@ -1,5 +1,10 @@
require "inspec/input_dsl_helpers"
module Inspec
class DescribeBase
include Inspec::InputDslHelpers
def initialize(action)
@action = action
@checks = []
@ -23,18 +28,7 @@ module Inspec
end
def input(input_name, options = {})
profile_id = __profile_id
# TODO: DRY up - remainder of this code is identical to input() in rspec_extensions.rb
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
input_with_profile_id(__profile_id, input_name, options)
end
def input_object(name)

View file

@ -0,0 +1,26 @@
require "inspec/input_registry"
module Inspec
# A mixin to provide implementations for the input() DSL methods
module InputDslHelpers
# Find or create an input, explicitly named by a profile ID and
# input name. Evaluate the input and return the value.
# @param [String] Profile ID
# @param [String] Input Name
# @param [Hash] Input options - see input docs on website
# @returns [Object] Input value
def input_with_profile_id(profile_id, input_name, options)
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
end
end
end

View file

@ -1,6 +1,7 @@
require "inspec/input_registry"
require "inspec/plugin/v2"
require "rspec/core/example_group"
require "inspec/input_dsl_helpers"
# Any additions to RSpec::Core::ExampleGroup (the RSpec class behind describe blocks) should go here.
@ -82,18 +83,12 @@ module Inspec
end
class RSpec::Core::ExampleGroup
include Inspec::InputDslHelpers
# This DSL method allows us to access the values of inputs within InSpec tests
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
input_with_profile_id(profile_id, input_name, options)
end
define_example_method :input