From 21a92a0c4eb023a4529a5d216d194b896f2bc159 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Sat, 16 Jan 2016 07:03:53 +0100 Subject: [PATCH] isolate rspec-dsl in profile context --- lib/inspec/profile_context.rb | 26 ++++++++++++++++---------- lib/inspec/resource.rb | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/inspec/profile_context.rb b/lib/inspec/profile_context.rb index b377e0b87..2aa88481d 100644 --- a/lib/inspec/profile_context.rb +++ b/lib/inspec/profile_context.rb @@ -4,7 +4,6 @@ require 'inspec/rule' require 'inspec/dsl' -require 'rspec/core/dsl' require 'securerandom' module Inspec @@ -25,7 +24,8 @@ module Inspec end def reload_dsl - ctx = create_context + resources_dsl = Inspec::Resource.create_dsl(@backend) + ctx = create_context(resources_dsl, rule_context(resources_dsl)) @profile_context = ctx.new end @@ -64,6 +64,19 @@ module Inspec private + # Create the context for controls. This includes all components of the DSL, + # including matchers and resources. + # + # @param [ResourcesDSL] resources_dsl which has all resources to attach + # @return [RuleContext] the inner context of rules + def rule_context(resources_dsl) + require 'rspec/core/dsl' + Class.new(Inspec::Rule) do + include RSpec::Core::DSL + include resources_dsl + end + end + # Creates the heart of the profile context: # An instantiated object which has all resources registered to it # and exposes them to the a test file. The profile context serves as a @@ -72,15 +85,8 @@ module Inspec # # @param outer_dsl [OuterDSLClass] # @return [ProfileContextClass] - def create_context + def create_context(resources_dsl, rule_class) profile_context_owner = self - resources_dsl = Inspec::Resource.create_dsl(@backend) - - # the inner working of a rule - rule_class = Class.new(Inspec::Rule) do - include RSpec::Core::DSL - include resources_dsl - end # rubocop:disable Lint/NestedMethodDefinition Class.new do diff --git a/lib/inspec/resource.rb b/lib/inspec/resource.rb index f5369fc9f..9293c8069 100644 --- a/lib/inspec/resource.rb +++ b/lib/inspec/resource.rb @@ -17,7 +17,7 @@ module Inspec # which is specified via the backend argument. # # @param backend [BackendRunner] exposing the target to resources - # @return [ResourceDSLModule] + # @return [ResourcesDSL] def self.create_dsl(backend) # need the local name, to use it in the module creation further down my_registry = registry