lib/inspec/runner.rb in inspec-0.14.3 vs lib/inspec/runner.rb in inspec-0.14.4

- old
+ new

@@ -11,11 +11,11 @@ require 'inspec/profile' require 'inspec/metadata' # spec requirements module Inspec - class Runner + class Runner # rubocop:disable Metrics/ClassLength extend Forwardable attr_reader :backend, :rules def initialize(conf = {}) @rules = {} @profile_id = conf[:id] @@ -94,18 +94,22 @@ def_delegator :@test_collector, :run def_delegator :@test_collector, :report private - def get_check_example(method_name, arg, block) + def block_source_info(block) + return {} if block.nil? || !block.respond_to?(:source_location) opts = {} - if !block.nil? && block.respond_to?(:source_location) - file_path, line = block.source_location - opts['file_path'] = file_path - opts['line_number'] = line - end + file_path, line = block.source_location + opts['file_path'] = file_path + opts['line_number'] = line + opts + end + def get_check_example(method_name, arg, block) + opts = block_source_info(block) + if !arg.empty? && arg[0].respond_to?(:resource_skipped) && !arg[0].resource_skipped.nil? return @test_collector.example_group(*arg, opts) do it arg[0].resource_skipped @@ -115,10 +119,20 @@ case method_name when 'describe' return @test_collector.example_group(*arg, opts, &block) when 'expect' return block.example_group + when 'describe.one' + tests = arg.map do |x| + @test_collector.example_group(x[1][0], block_source_info(x[2]), &x[2]) + end + return nil if tests.empty? + ok_tests = tests.find_all(&:run) + # return all tests if none succeeds; we will just report full failure + return tests if ok_tests.empty? + # otherwise return all working tests + return ok_tests else fail "A rule was registered with #{method_name.inspect}, "\ "which isn't understood and cannot be processed." end end @@ -126,22 +140,22 @@ end def register_rule(rule_id, rule) @rules[rule_id] = rule checks = rule.instance_variable_get(:@checks) - checks.each do |m, a, b| - # resource skipping - example = get_check_example(m, a, b) + examples = checks.map do |m, a, b| + get_check_example(m, a, b) + end.flatten.compact + examples.each do |example| # TODO: Remove this!! It is very dangerous to do this here. # The goal of this is to make the audit DSL available to all # describe blocks. Right now, these blocks are executed outside # the scope of this run, thus not gaining ony of the DSL pieces. # To circumvent this, the full DSL is attached to the example's # scope. dsl = Inspec::Resource.create_dsl(backend) example.send(:include, dsl) - @test_collector.add_test(example, rule_id) end end end end