make syntax binding to scope programmable

Signed-off-by: Dominik Richter <dominik@vulcanosec.com>
This commit is contained in:
Dominik Richter 2015-06-19 20:11:26 +02:00
parent 2c912d2fbe
commit f64f15ee6b

View file

@ -3,61 +3,103 @@
# license: All rights reserved # license: All rights reserved
require 'vulcano/base_rule' require 'vulcano/base_rule'
class VulcanoRule < VulcanoBaseRule module Vulcano
include Serverspec::Helper::Type class Rule < VulcanoBaseRule
extend Serverspec::Helper::Type include Serverspec::Helper::Type
include RSpec::Core::DSL extend Serverspec::Helper::Type
include RSpec::Core::DSL
def describe(sth, &block) # Override RSpec methods to add
r = VulcanoRule.describe(sth, &block) # IDs to each example group
set_rspec_ids(r) # 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 end
def method_missing(m, *a, &b) def require_rules id, &block
VulcanoRule.__send__(m, *a, &b) files = get_spec_files_for_profile id
end end
def self.registry def include_rules id, &block
@rules ||= {} files = get_spec_files_for_profile id
# files.each do |file|
# eval(File::read(file), file, 1)
# end
end end
private private
def set_rspec_ids(obj) def get_spec_files_for_profile id
obj.examples.each {|ex| base_path = '/etc/vulcanosec/tests'
ex.metadata[:id] = @id path = File.join( base_path, id )
} # find all files to be included
obj.children.each {|c| files = []
set_rspec_ids(c) if File::directory? path
} # include all library paths, if they exist
end libdir = File::join(path, 'lib')
end if File::directory? libdir and !$LOAD_PATH.include?(libdir)
$LOAD_PATH.unshift(libdir)
def rule id, &block end
existing = VulcanoRule.registry[id] files = Dir[File.join(path, 'spec','*_spec.rb')]
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)
end end
files = Dir[File.join(path, 'spec','*_spec.rb')] return files
end end
# include all files
files.each do |file| end
eval(File::read(file))
module Vulcano::DSLHelper
def self.bind_dsl(scope)
(class << scope; self; end).class_exec do
include Vulcano::DSL
end
end end
end end
::Vulcano::DSLHelper.bind_dsl(self)