mirror of
https://github.com/inspec/inspec
synced 2024-11-27 15:10:44 +00:00
feature: support expect keyword in rules
This commit is contained in:
parent
f2e955eb37
commit
d66f874e1c
4 changed files with 89 additions and 6 deletions
|
@ -5,7 +5,8 @@
|
|||
module Vulcano
|
||||
module Plugins
|
||||
class Resource
|
||||
def self.name(name)
|
||||
def self.name(name = nil)
|
||||
return if name.nil?
|
||||
Vulcano::Plugins::Resource.__register(name, self)
|
||||
end
|
||||
|
||||
|
@ -39,6 +40,13 @@ module Vulcano
|
|||
def to_s
|
||||
@__resource_name__
|
||||
end
|
||||
|
||||
# Overwrite inspect to provide better output to RSpec results.
|
||||
#
|
||||
# @return [String] full name of the resource
|
||||
def inspect
|
||||
to_s
|
||||
end
|
||||
end
|
||||
|
||||
module ResourceCommon
|
||||
|
|
|
@ -4,8 +4,44 @@
|
|||
# author: Dominik Richter
|
||||
# author: Christoph Hartmann
|
||||
|
||||
require 'rspec/expectations'
|
||||
|
||||
module Vulcano
|
||||
class ExpectationTarget
|
||||
attr_reader :calls, :value, :block
|
||||
def initialize(value, &block)
|
||||
@value = value
|
||||
@block = block
|
||||
@calls = []
|
||||
end
|
||||
|
||||
def to(*args, &block)
|
||||
@calls.push([:to, args, block, caller])
|
||||
end
|
||||
|
||||
def not_to(*args, &block)
|
||||
@calls.push([:not_to, args, block, caller])
|
||||
end
|
||||
|
||||
def example_group
|
||||
that = self
|
||||
outer_clr = calls[0][3]
|
||||
RSpec::Core::ExampleGroup.describe(that.value, caller: outer_clr) do
|
||||
that.calls.each do |method, args, block, clr|
|
||||
# require "pry"; binding.pry
|
||||
it(nil, caller: clr) do
|
||||
x = expect(that.value, &that.block).method(method)
|
||||
# require "pry"; binding.pry
|
||||
x.call(*args, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Rule
|
||||
include ::RSpec::Matchers
|
||||
|
||||
def initialize(id, _opts, &block)
|
||||
@id = id
|
||||
@impact = nil
|
||||
|
@ -40,8 +76,14 @@ module Vulcano
|
|||
@desc
|
||||
end
|
||||
|
||||
def describe(sth, &block)
|
||||
@checks.push(['describe', [sth], block])
|
||||
def describe(value, &block)
|
||||
@checks.push(['describe', [value], block])
|
||||
end
|
||||
|
||||
def expect(value, &block)
|
||||
target = ExpectationTarget.new(value, &block)
|
||||
@checks.push(['expect', [value], target])
|
||||
target
|
||||
end
|
||||
|
||||
def self.merge(dst, src)
|
||||
|
|
|
@ -65,9 +65,8 @@ module Vulcano
|
|||
|
||||
# process the resulting rules
|
||||
ctx.rules.each do |rule_id, rule|
|
||||
#::Vulcano::DSL.execute_rule(rule, profile_id)
|
||||
checks = rule.instance_variable_get(:@checks)
|
||||
checks.each do |_, a, b|
|
||||
checks.each do |m, a, b|
|
||||
# resource skipping
|
||||
if !a.empty? &&
|
||||
a[0].respond_to?(:resource_skipped) &&
|
||||
|
@ -77,7 +76,15 @@ module Vulcano
|
|||
end
|
||||
else
|
||||
# add the resource
|
||||
example = RSpec::Core::ExampleGroup.describe(*a, &b)
|
||||
case m
|
||||
when 'describe'
|
||||
example = RSpec::Core::ExampleGroup.describe(*a, &b)
|
||||
when 'expect'
|
||||
example = b.example_group
|
||||
else
|
||||
fail "A rule was registered with the #{m.inspect} keyword. "\
|
||||
'which cannot be processed.'
|
||||
end
|
||||
end
|
||||
|
||||
set_rspec_ids(example, rule_id)
|
||||
|
|
|
@ -82,5 +82,31 @@ describe Vulcano::ProfileContext do
|
|||
check[2].must_be_kind_of Proc
|
||||
end
|
||||
end
|
||||
|
||||
describe 'adds a check via expect' do
|
||||
let(:cmd) {<<-EOF
|
||||
rule #{rule_id.inspect} do
|
||||
expect(os[:family]).to eq('ubuntu')
|
||||
end
|
||||
EOF
|
||||
}
|
||||
let(:check) {
|
||||
profile.load(cmd)
|
||||
rule = profile.rules[rule_id]
|
||||
rule.instance_variable_get(:@checks)[0]
|
||||
}
|
||||
|
||||
it 'registers the check with describe' do
|
||||
check[0].must_equal 'expect'
|
||||
end
|
||||
|
||||
it 'registers the check with the describe argument' do
|
||||
check[1].must_equal %w{ubuntu}
|
||||
end
|
||||
|
||||
it 'registers the check with the provided proc' do
|
||||
check[2].must_be_kind_of Vulcano::ExpectationTarget
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue