add inspec objects (not exposed by default)

This commit is contained in:
Dominik Richter 2016-03-31 19:40:17 +01:00
parent d5bc997ce4
commit 8150a67e4a
8 changed files with 235 additions and 0 deletions

11
lib/inspec/objects.rb Normal file
View file

@ -0,0 +1,11 @@
# encoding: utf-8
module Inspec
autoload :Control, 'inspec/objects/control'
autoload :EachLoop, 'inspec/objects/each_loop'
autoload :List, 'inspec/objects/list'
autoload :OrTest, 'inspec/objects/or_test'
autoload :RubyHelper, 'inspec/objects/ruby_helper'
autoload :Test, 'inspec/objects/test'
autoload :Value, 'inspec/objects/value'
end

View file

@ -0,0 +1,35 @@
# encoding:utf-8
module Inspec
class Control
attr_accessor :id, :title, :desc, :impact, :tests
def initialize
@tests = []
end
def add_test(t)
@tests.push(t)
end
def to_hash
{ id: id, title: title, desc: desc, impact: impact, tests: tests.map(&:to_hash) }
end
def to_ruby
res = ["control #{id.inspect} do"]
res.push " title #{title.inspect}" unless title.to_s.empty?
res.push " desc #{desc.inspect}" unless desc.to_s.empty?
res.push " impact #{impact}" unless impact.nil?
tests.each { |t| res.push(indent(t.to_ruby, 2)) }
res.push 'end'
res.join("\n")
end
private
def indent(txt, d)
dt = ' '*d
dt + txt.gsub("\n", "\n"+dt)
end
end
end

View file

@ -0,0 +1,32 @@
# encoding:utf-8
module Inspec
class EachLoop < List
attr_reader :tests
def initialize
super
@tests = []
end
def add_test(t = nil)
t ||= Test.new
t.qualifier[0] = ['entry']
@tests.push(t)
t
end
def negate!
@tests.each(&:negate!)
end
def to_hash
{ qualifier: qualifier, test: @test }
end
def to_ruby
obj = super
all_tests = @tests.map(&:to_ruby).join("\n")
format("%s.each do |entry|\n %s\nend", obj, all_tests)
end
end
end

View file

@ -0,0 +1,15 @@
# encoding:utf-8
module Inspec
class List < Value
def map
fail 'Inspec::List.map needs to be called with a block' unless block_given?
t = List.new
t.qualifier = [['x']]
yield(t)
return if t.qualifier == [['x']]
@qualifier.push(['map', "{ |x| #{t.to_ruby} }"])
self
end
end
end

View file

@ -0,0 +1,22 @@
# encoding:utf-8
module Inspec
class OrTest
def initialize(tests)
@tests = tests
end
def skip
nil
end
def to_ruby
format("describe.one do\n %s\nend",
@tests.map(&:to_ruby).join("\n"))
end
def to_hash
{ describe_one: @tests.map(&:to_hash) }
end
end
end

View file

@ -0,0 +1,15 @@
# encoding: utf-8
module Inspec
module RubyHelper
def ruby_qualifier(q)
if q.length <= 1
q[0]
elsif q[0] == 'map' && q.length == 2
q[0] + ' ' + q[1]
else
q[0] + '(' + q[1..-1].map(&:inspect).join(', ') + ')'
end
end
end
end

View file

@ -0,0 +1,78 @@
# encoding:utf-8
module Inspec
class Test
attr_accessor :qualifier, :matcher, :expectation, :skip, :negated, :variables
include RubyHelper
def initialize
@qualifier = []
@negated = false
@variables = []
end
def negate!
@negated = !@negated
end
def to_ruby
return rb_skip if !skip.nil?
rb_describe
end
def to_hash
{ qualifier: qualifier, matcher: matcher, expectation: expectation, skip: skip, negated: negated }
end
def resource
@resource ||=
if qualifier.empty? || qualifier[0].empty? || qualifier[0][0].empty?
nil
else
qualifier[0][0]
end
end
def remove_expectation
remove_instance_variable(:@expectation)
end
private
def describe_chain
return nil if @qualifier.empty?
resource = (@qualifier.length > 1) ? @qualifier[0..-2] : [@qualifier[0]]
res = resource.map { |q| ruby_qualifier(q) }.join('.')
xres = nil
if @qualifier.length > 1
last = @qualifier[-1]
if last.length == 1
xres = last[0]
else
res += '.' + ruby_qualifier(last)
end
end
[res, xres]
end
def rb_describe
vars = variables.map(&:to_ruby).join("\n")
vars += "\n" unless vars.empty?
res, xtra = describe_chain
itsy = xtra.nil? ? 'it' : 'its(' + xtra.to_sym.inspect + ')'
naughty = @negated ? '_not' : ''
xpect = defined?(@expectation) ? expectation.inspect : ''
format("%sdescribe %s do\n %s { should%s %s %s }\nend",
vars, res, itsy, naughty, matcher, xpect)
end
def rb_skip
dc = describe_chain
obj = dc.nil? ? skip.inspect : dc[0]
format("describe %s do\n skip %s\nend", obj, skip.inspect)
end
end
end

View file

@ -0,0 +1,27 @@
# encoding:utf-8
module Inspec
class Value
include ::Inspec::RubyHelper
attr_accessor :qualifier
attr_accessor :skip
attr_accessor :variable
def initialize(qualifiers = [])
@qualifier = qualifiers
@variable = nil
end
def to_ruby
res = @variable.nil? ? '' : "#{@variable} = "
res + @qualifier.map { |x| ruby_qualifier(x) }.join('.')
end
def name_variable(cache = [])
@variable = Array('a'..'z').find { |x| !cache.include?(x) }
cache.push(@variable)
@variable
end
end
end