mirror of
https://github.com/inspec/inspec
synced 2024-11-11 07:34:15 +00:00
extract example group creation
and restore profile tests that had been completely mocked until now
This commit is contained in:
parent
dd2d93fd6f
commit
8c464965c1
6 changed files with 44 additions and 23 deletions
|
@ -38,6 +38,7 @@ module Inspec
|
|||
@runner = Runner.new(
|
||||
id: @profile_id,
|
||||
backend: :mock,
|
||||
test_collector: @options.delete(:test_collector),
|
||||
)
|
||||
@runner.add_tests([@path], @options)
|
||||
@runner.rules.each do |id, rule|
|
||||
|
|
|
@ -119,14 +119,14 @@ module Inspec
|
|||
if !arg.empty? &&
|
||||
arg[0].respond_to?(:resource_skipped) &&
|
||||
!arg[0].resource_skipped.nil?
|
||||
return RSpec::Core::ExampleGroup.describe(*arg, opts) do
|
||||
return @test_collector.example_group(*arg, opts) do
|
||||
it arg[0].resource_skipped
|
||||
end
|
||||
else
|
||||
# add the resource
|
||||
case method_name
|
||||
when 'describe'
|
||||
return RSpec::Core::ExampleGroup.describe(*arg, opts, &block)
|
||||
return @test_collector.example_group(*arg, opts, &block)
|
||||
when 'expect'
|
||||
return block.example_group
|
||||
else
|
||||
|
@ -150,7 +150,7 @@ module Inspec
|
|||
# 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 = ctx.method(:create_inner_dsl).call(backend)
|
||||
dsl = Inspec::Resource.create_dsl(backend)
|
||||
example.send(:include, dsl)
|
||||
|
||||
@test_collector.add_test(example, rule_id)
|
||||
|
|
31
lib/inspec/runner_mock.rb
Normal file
31
lib/inspec/runner_mock.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
# encoding: utf-8
|
||||
# author: Dominik Richter
|
||||
# author: Christoph Hartmann
|
||||
|
||||
module Inspec
|
||||
class RunnerMock
|
||||
attr_reader :tests
|
||||
def initialize
|
||||
@tests = []
|
||||
end
|
||||
|
||||
def add_test(example, _rule_id)
|
||||
@tests.push(example)
|
||||
end
|
||||
|
||||
def example_group(*in_args, &in_block)
|
||||
Class.new do
|
||||
def args
|
||||
in_args
|
||||
end
|
||||
def block
|
||||
in_block
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def run(_with = nil)
|
||||
puts 'uhm.... nothing or something... dunno, ask your admin'
|
||||
end
|
||||
end
|
||||
end
|
|
@ -23,6 +23,10 @@ module Inspec
|
|||
RSpec.configuration.add_formatter(@conf['format'] || 'progress')
|
||||
end
|
||||
|
||||
def example_group(*args, &block)
|
||||
RSpec::Core::ExampleGroup.describe(*args, &block)
|
||||
end
|
||||
|
||||
def add_test(example, rule_id)
|
||||
set_rspec_ids(example, rule_id)
|
||||
@tests.register(example)
|
||||
|
|
0
test/unit/mock/profiles/empty/test/.gitkeep
Normal file
0
test/unit/mock/profiles/empty/test/.gitkeep
Normal file
|
@ -5,25 +5,14 @@
|
|||
require 'helper'
|
||||
require 'inspec/profile_context'
|
||||
require 'inspec/runner'
|
||||
require 'inspec/runner_mock'
|
||||
|
||||
describe Inspec::Profile do
|
||||
before {
|
||||
# mock up the profile runner
|
||||
# TODO: try to take the real profile runner here;
|
||||
# currently it's stopped at test runner conflicts
|
||||
class Inspec::Profile::Runner
|
||||
def initialize(opts) end
|
||||
def add_tests(tests, options=nil) end
|
||||
def rules
|
||||
{}
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
let(:logger) { Minitest::Mock.new }
|
||||
let(:home) { File.dirname(__FILE__) }
|
||||
|
||||
def load_profile(name, opts = {})
|
||||
opts[:test_collector] = Inspec::RunnerMock.new
|
||||
Inspec::Profile.from_path("#{home}/mock/profiles/#{name}", opts)
|
||||
end
|
||||
|
||||
|
@ -96,13 +85,9 @@ describe Inspec::Profile do
|
|||
it 'prints ok messages and counts the rules' do
|
||||
logger.expect :info, nil, ["Checking profile in #{home}/mock/profiles/complete-profile"]
|
||||
logger.expect :info, nil, ['Metadata OK.']
|
||||
|
||||
# TODO: cannot load rspec in unit tests, therefore we get a loading warn
|
||||
# RSpec does not work with minitest tests
|
||||
logger.expect :warn, nil, ['No controls or tests were defined.']
|
||||
# we expect that this should work:
|
||||
# logger.expect :info, nil, ['Found 1 rules.']
|
||||
# logger.expect :info, nil, ['Rule definitions OK.']
|
||||
logger.expect :info, nil, ['Found 1 rules.']
|
||||
logger.expect :debug, nil, ["Verify all rules in #{home}/mock/profiles/complete-profile/controls/filesystem_spec.rb"]
|
||||
logger.expect :info, nil, ['Rule definitions OK.']
|
||||
|
||||
profile.check
|
||||
logger.verify
|
||||
|
|
Loading…
Reference in a new issue