Removed the last of the metaprogramming from ControlEvalContext.

* Merged CEC.create args into CEC#initialize
* Converted the only call site of CEC.create to just call new.
* Added profile_context and resources_dsl accessors to CEC.

Signed-off-by: Ryan Davis <zenspider@chef.io>
This commit is contained in:
Ryan Davis 2019-12-10 18:07:42 -08:00
parent f6407fb01b
commit 6426feeeb4
3 changed files with 24 additions and 50 deletions

View file

@ -15,34 +15,13 @@ module Inspec
include Inspec::DSL
include Inspec::DSL::RequireOverride
class << self
attr_accessor :profile_context_owner
attr_accessor :profile_id
attr_accessor :resources_dsl
end
# Creates the heart of the control eval context:
#
# An instantiated object which has all resources registered to it
# and exposes them to the test file.
#
# @param profile_context [Inspec::ProfileContext]
# @param outer_dsl [OuterDSLClass]
# @return [ProfileContextClass]
def self.create(profile_context, resources_dsl)
klass = Class.new self
klass.include resources_dsl
klass.profile_context_owner = profile_context
klass.profile_id = profile_context.profile_id
klass.resources_dsl = resources_dsl
klass
end
attr_accessor :skip_file
attr_accessor :profile_context
attr_accessor :resources_dsl
def initialize(backend, conf, dependencies, require_loader, skip_only_if_eval)
def initialize(profile_context, resources_dsl, backend, conf, dependencies, require_loader, skip_only_if_eval)
@profile_context = profile_context
@resources_dsl = resources_dsl
@backend = backend
@conf = conf
@dependencies = dependencies
@ -50,24 +29,20 @@ module Inspec
@skip_file_message = nil
@skip_file = false
@skip_only_if_eval = skip_only_if_eval
extend resources_dsl # TODO: remove? push to method_missing?
end
alias profile_context_owner profile_context
def profile_id
profile_context.profile_id
end
def to_s
"Control Evaluation Context (#{profile_name})"
end
def profile_context_owner
self.class.profile_context_owner
end
def profile_id
self.class.profile_id
end
def resources_dsl
self.class.resources_dsl
end
def title(arg)
profile_context_owner.set_header(:title, arg)
end
@ -134,12 +109,12 @@ module Inspec
unless profile_context_owner.profile_supports_platform?
platform = inspec.platform
msg = "Profile `#{profile_context_owner.profile_id}` is not supported on platform #{platform.name}/#{platform.release}."
msg = "Profile `#{profile_id}` is not supported on platform #{platform.name}/#{platform.release}."
::Inspec::Rule.set_skip_rule(control, true, msg)
end
unless profile_context_owner.profile_supports_inspec_version?
msg = "Profile `#{profile_context_owner.profile_id}` is not supported on InSpec version (#{Inspec::VERSION})."
msg = "Profile `#{profile_id}` is not supported on InSpec version (#{Inspec::VERSION})."
::Inspec::Rule.set_skip_rule(control, true, msg)
end

View file

@ -56,10 +56,14 @@ module Inspec
end
def control_eval_context
@control_eval_context ||= begin
ctx = Inspec::ControlEvalContext.create(self, to_resources_dsl)
ctx.new(@backend, @conf, dependencies, @require_loader, @skip_only_if_eval)
end
@control_eval_context ||=
Inspec::ControlEvalContext.new(self,
to_resources_dsl,
@backend,
@conf,
dependencies,
@require_loader,
@skip_only_if_eval)
end
def reload_dsl

View file

@ -28,15 +28,10 @@ describe Inspec::ControlEvalContext do
let(:backend) { mock }
let(:profile_context) { Inspec::ProfileContext.new("test-profile", backend, {}) }
let(:eval_context) do
c = Inspec::ControlEvalContext.create(profile_context, resource_dsl)
# Options that are mocked below are:
# backend, conf, dependencies, require_loader, and skip_only_if_eval
# See: `lib/inspec/control_eval_context.rb` for more details
c.new(backend, {}, mock, mock, false)
end
it "accepts a context and a resource_dsl" do
Inspec::ControlEvalContext.create(profile_context, resource_dsl)
Inspec::ControlEvalContext.new(profile_context, resource_dsl, backend, {}, mock, mock, false)
end
it "provides rules with access to the given DSL" do