mirror of
https://github.com/inspec/inspec
synced 2025-02-25 11:57:17 +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 Vulcano
|
||||||
module Plugins
|
module Plugins
|
||||||
class Resource
|
class Resource
|
||||||
def self.name(name)
|
def self.name(name = nil)
|
||||||
|
return if name.nil?
|
||||||
Vulcano::Plugins::Resource.__register(name, self)
|
Vulcano::Plugins::Resource.__register(name, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -39,6 +40,13 @@ module Vulcano
|
||||||
def to_s
|
def to_s
|
||||||
@__resource_name__
|
@__resource_name__
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Overwrite inspect to provide better output to RSpec results.
|
||||||
|
#
|
||||||
|
# @return [String] full name of the resource
|
||||||
|
def inspect
|
||||||
|
to_s
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module ResourceCommon
|
module ResourceCommon
|
||||||
|
|
|
@ -4,8 +4,44 @@
|
||||||
# author: Dominik Richter
|
# author: Dominik Richter
|
||||||
# author: Christoph Hartmann
|
# author: Christoph Hartmann
|
||||||
|
|
||||||
|
require 'rspec/expectations'
|
||||||
|
|
||||||
module Vulcano
|
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
|
class Rule
|
||||||
|
include ::RSpec::Matchers
|
||||||
|
|
||||||
def initialize(id, _opts, &block)
|
def initialize(id, _opts, &block)
|
||||||
@id = id
|
@id = id
|
||||||
@impact = nil
|
@impact = nil
|
||||||
|
@ -40,8 +76,14 @@ module Vulcano
|
||||||
@desc
|
@desc
|
||||||
end
|
end
|
||||||
|
|
||||||
def describe(sth, &block)
|
def describe(value, &block)
|
||||||
@checks.push(['describe', [sth], block])
|
@checks.push(['describe', [value], block])
|
||||||
|
end
|
||||||
|
|
||||||
|
def expect(value, &block)
|
||||||
|
target = ExpectationTarget.new(value, &block)
|
||||||
|
@checks.push(['expect', [value], target])
|
||||||
|
target
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.merge(dst, src)
|
def self.merge(dst, src)
|
||||||
|
|
|
@ -65,9 +65,8 @@ module Vulcano
|
||||||
|
|
||||||
# process the resulting rules
|
# process the resulting rules
|
||||||
ctx.rules.each do |rule_id, rule|
|
ctx.rules.each do |rule_id, rule|
|
||||||
#::Vulcano::DSL.execute_rule(rule, profile_id)
|
|
||||||
checks = rule.instance_variable_get(:@checks)
|
checks = rule.instance_variable_get(:@checks)
|
||||||
checks.each do |_, a, b|
|
checks.each do |m, a, b|
|
||||||
# resource skipping
|
# resource skipping
|
||||||
if !a.empty? &&
|
if !a.empty? &&
|
||||||
a[0].respond_to?(:resource_skipped) &&
|
a[0].respond_to?(:resource_skipped) &&
|
||||||
|
@ -77,7 +76,15 @@ module Vulcano
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
# add the resource
|
# 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
|
end
|
||||||
|
|
||||||
set_rspec_ids(example, rule_id)
|
set_rspec_ids(example, rule_id)
|
||||||
|
|
|
@ -82,5 +82,31 @@ describe Vulcano::ProfileContext do
|
||||||
check[2].must_be_kind_of Proc
|
check[2].must_be_kind_of Proc
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue