From 77ce1b65594f9f61c751c1f02c784142e4fab58a Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Thu, 5 Dec 2019 16:13:31 -0800 Subject: [PATCH 1/2] Wire up backend to rspec metadata, allowing for example groups to use resources Fixes #628. Signed-off-by: Ryan Davis --- lib/inspec/rspec_extensions.rb | 15 +++++++-------- lib/inspec/runner.rb | 2 ++ lib/inspec/runner_rspec.rb | 27 +++++++++++++++++---------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/lib/inspec/rspec_extensions.rb b/lib/inspec/rspec_extensions.rb index 4f675ebd6..65face2aa 100644 --- a/lib/inspec/rspec_extensions.rb +++ b/lib/inspec/rspec_extensions.rb @@ -11,14 +11,13 @@ module Inspec module DescribeDslLazyLoader # Support for Describe DSL plugins def method_missing(method_name, *arguments, &block) - # TODO: need a backend available at the describe level... class method somewhere? - # # see if it is a resource first - # begin - # resource = Inspec::DSL.method_missing_resource(:where_is_backend?, method_name, *arguments) - # return resource if resource - # rescue LoadError - # # pass through - # end + begin + backend = metadata[:backend] # populated via Inspec::Runner#add_resource + resource = Inspec::DSL.method_missing_resource(backend, method_name, *arguments) + return resource if resource + rescue LoadError + # pass through + end # Check to see if there is a describe_dsl plugin activator hook with the method name registry = Inspec::Plugin::V2::Registry.instance diff --git a/lib/inspec/runner.rb b/lib/inspec/runner.rb index 863036dab..3a5cb2da8 100644 --- a/lib/inspec/runner.rb +++ b/lib/inspec/runner.rb @@ -307,6 +307,8 @@ module Inspec def add_resource(method_name, arg, opts, block) case method_name when "describe" + opts = { backend: @test_collector.backend }.merge opts + @test_collector.example_group(*arg, opts, &block) when "expect" block.example_group diff --git a/lib/inspec/runner_rspec.rb b/lib/inspec/runner_rspec.rb index 3f5135eb6..00e4a375d 100644 --- a/lib/inspec/runner_rspec.rb +++ b/lib/inspec/runner_rspec.rb @@ -10,6 +10,8 @@ require "inspec/rspec_extensions" module Inspec class RunnerRspec + attr_accessor :formatter + def initialize(conf) @conf = conf @formatter = nil @@ -22,19 +24,26 @@ module Inspec # @param [Type] &block the block associated with this example group # @return [RSpecExampleGroup] def example_group(*args, &block) + # NOTE: this RUNS immediately RSpec::Core::ExampleGroup.describe(*args, &block) end + def formatters + RSpec.configuration.formatters.grep(Inspec::Formatters::Base) + end + # Add a full profile to the runner. Only pulls in metadata # # @param [Inspec::Profile] profile # @return [nil] def add_profile(profile) - RSpec.configuration.formatters - .find_all { |c| c.is_a?(Inspec::Formatters::Base) } - .each do |fmt| - fmt.add_profile(profile) - end + formatters.each do |fmt| + fmt.add_profile(profile) + end + end + + def backend + formatters.first.backend end # Configure the backend of the runner. @@ -42,11 +51,9 @@ module Inspec # @param [Inspec::Backend] backend # @return [nil] def backend=(backend) - RSpec.configuration.formatters - .find_all { |c| c.is_a?(Inspec::Formatters::Base) } - .each do |fmt| - fmt.backend = backend - end + formatters.each do |fmt| + fmt.backend = backend + end end # Add an example group to the list of registered tests. From a14e7548ec89ddfd74b588349a5325fa2f9ee6c5 Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Thu, 5 Dec 2019 16:16:51 -0800 Subject: [PATCH 2/2] Clean up super stale dependency commentary. Signed-off-by: Ryan Davis --- inspec.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inspec.gemspec b/inspec.gemspec index e00334a4d..abea19dd3 100644 --- a/inspec.gemspec +++ b/inspec.gemspec @@ -23,11 +23,11 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.4" - spec.add_dependency "train", "~> 3.0" # Inspec 4 must have train 2+; 3+ if we include train-winrm + spec.add_dependency "train", "~> 3.0" # Train plugins we ship with InSpec spec.add_dependency "train-habitat", "~> 0.1" spec.add_dependency "train-aws", "~> 0.1" - spec.add_dependency "train-winrm", "~> 0.2" # Requires train 3+ + spec.add_dependency "train-winrm", "~> 0.2" # Implementation dependencies spec.add_dependency "chef-telemetry", "~> 1.0"