mirror of
https://github.com/inspec/inspec
synced 2024-11-22 12:43:07 +00:00
make syntax binding to scope programmable
Signed-off-by: Dominik Richter <dominik@vulcanosec.com>
This commit is contained in:
parent
2c912d2fbe
commit
f64f15ee6b
1 changed files with 88 additions and 46 deletions
|
@ -3,61 +3,103 @@
|
|||
# license: All rights reserved
|
||||
require 'vulcano/base_rule'
|
||||
|
||||
class VulcanoRule < VulcanoBaseRule
|
||||
include Serverspec::Helper::Type
|
||||
extend Serverspec::Helper::Type
|
||||
include RSpec::Core::DSL
|
||||
module Vulcano
|
||||
class Rule < VulcanoBaseRule
|
||||
include Serverspec::Helper::Type
|
||||
extend Serverspec::Helper::Type
|
||||
include RSpec::Core::DSL
|
||||
|
||||
def describe(sth, &block)
|
||||
r = VulcanoRule.describe(sth, &block)
|
||||
set_rspec_ids(r)
|
||||
# Override RSpec methods to add
|
||||
# IDs to each example group
|
||||
# TODO: remove this once IDs are in rspec-core
|
||||
def describe(sth, &block)
|
||||
r = Rule.describe(sth, &block)
|
||||
set_rspec_ids(r)
|
||||
end
|
||||
|
||||
# redirect all regular method calls to the
|
||||
# core DSL (which is attached to the class)
|
||||
def method_missing(m, *a, &b)
|
||||
VulcanoRule.__send__(m, *a, &b)
|
||||
end
|
||||
|
||||
def self.registry
|
||||
@rules ||= {}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Attach an ID attribute to the
|
||||
# metadata of all examples
|
||||
# TODO: remove this once IDs are in rspec-core
|
||||
def set_rspec_ids(obj)
|
||||
obj.examples.each {|ex|
|
||||
ex.metadata[:id] = @id
|
||||
}
|
||||
obj.children.each {|c|
|
||||
set_rspec_ids(c)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# class Rule < ::Vulcano::Rule
|
||||
# def describe(sth, &block)
|
||||
# @examples ||= []
|
||||
# @examples.push([sth, block])
|
||||
# end
|
||||
# end
|
||||
|
||||
|
||||
module Vulcano::DSL
|
||||
|
||||
def rule id, &block
|
||||
existing = Vulcano::Rule.registry[id]
|
||||
if existing.nil?
|
||||
Vulcano::Rule.registry[id] = Vulcano::Rule.new(id, &block)
|
||||
else
|
||||
p "RULE #{id} was found: #{existing}"
|
||||
# TODO: alter existing rule
|
||||
end
|
||||
end
|
||||
|
||||
def method_missing(m, *a, &b)
|
||||
VulcanoRule.__send__(m, *a, &b)
|
||||
def require_rules id, &block
|
||||
files = get_spec_files_for_profile id
|
||||
end
|
||||
|
||||
def self.registry
|
||||
@rules ||= {}
|
||||
def include_rules id, &block
|
||||
files = get_spec_files_for_profile id
|
||||
# files.each do |file|
|
||||
# eval(File::read(file), file, 1)
|
||||
# end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_rspec_ids(obj)
|
||||
obj.examples.each {|ex|
|
||||
ex.metadata[:id] = @id
|
||||
}
|
||||
obj.children.each {|c|
|
||||
set_rspec_ids(c)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def rule id, &block
|
||||
existing = VulcanoRule.registry[id]
|
||||
if existing.nil?
|
||||
VulcanoRule.registry[id] = VulcanoRule.new(id, &block)
|
||||
else
|
||||
# TODO: alter existing rule
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def include_rules id
|
||||
base_path = '/etc/vulcanosec/tests'
|
||||
path = File.join( base_path, id )
|
||||
# find all files to be included
|
||||
files = []
|
||||
if File::directory? path
|
||||
# include all library paths, if they exist
|
||||
libdir = File::join(path, 'lib')
|
||||
if File::directory? libdir and !$LOAD_PATH.include?(libdir)
|
||||
$LOAD_PATH.unshift(libdir)
|
||||
def get_spec_files_for_profile id
|
||||
base_path = '/etc/vulcanosec/tests'
|
||||
path = File.join( base_path, id )
|
||||
# find all files to be included
|
||||
files = []
|
||||
if File::directory? path
|
||||
# include all library paths, if they exist
|
||||
libdir = File::join(path, 'lib')
|
||||
if File::directory? libdir and !$LOAD_PATH.include?(libdir)
|
||||
$LOAD_PATH.unshift(libdir)
|
||||
end
|
||||
files = Dir[File.join(path, 'spec','*_spec.rb')]
|
||||
end
|
||||
files = Dir[File.join(path, 'spec','*_spec.rb')]
|
||||
return files
|
||||
end
|
||||
# include all files
|
||||
files.each do |file|
|
||||
eval(File::read(file))
|
||||
|
||||
end
|
||||
|
||||
module Vulcano::DSLHelper
|
||||
def self.bind_dsl(scope)
|
||||
(class << scope; self; end).class_exec do
|
||||
include Vulcano::DSL
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
::Vulcano::DSLHelper.bind_dsl(self)
|
||||
|
|
Loading…
Reference in a new issue