From 6426feeeb41507d878e99973c38c7dcdef98ad6e Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Tue, 10 Dec 2019 18:07:42 -0800 Subject: [PATCH] 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 --- lib/inspec/control_eval_context.rb | 55 +++++-------------- lib/inspec/profile_context.rb | 12 ++-- .../profiles/control_eval_context_test.rb | 7 +-- 3 files changed, 24 insertions(+), 50 deletions(-) diff --git a/lib/inspec/control_eval_context.rb b/lib/inspec/control_eval_context.rb index 7c236e845..61e8c7adb 100644 --- a/lib/inspec/control_eval_context.rb +++ b/lib/inspec/control_eval_context.rb @@ -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 diff --git a/lib/inspec/profile_context.rb b/lib/inspec/profile_context.rb index e733b5936..294db5100 100644 --- a/lib/inspec/profile_context.rb +++ b/lib/inspec/profile_context.rb @@ -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 diff --git a/test/unit/profiles/control_eval_context_test.rb b/test/unit/profiles/control_eval_context_test.rb index 11ea47517..c1f9d5c95 100644 --- a/test/unit/profiles/control_eval_context_test.rb +++ b/test/unit/profiles/control_eval_context_test.rb @@ -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