mirror of
https://github.com/inspec/inspec
synced 2024-11-10 23:24:18 +00:00
Merge pull request #191 from chef/fix-lib
Merged change 5d3d542e-8b53-4d78-93c7-ee6b0d9017f0 From review branch fix-lib into master Signed-off-by: chartmann <chartmann@chef.io>
This commit is contained in:
commit
9ba79fc516
4 changed files with 44 additions and 14 deletions
|
@ -14,7 +14,7 @@ require 'rspec/its'
|
|||
require 'inspec/rspec_json_formatter'
|
||||
|
||||
module Inspec
|
||||
class Runner
|
||||
class Runner # rubocop:disable Metrics/ClassLength
|
||||
attr_reader :tests, :backend, :rules
|
||||
def initialize(conf = {})
|
||||
@rules = {}
|
||||
|
@ -47,11 +47,14 @@ module Inspec
|
|||
# retrieve the raw ruby code of all tests
|
||||
items = tests.map do |test|
|
||||
Inspec::Targets.resolve(test)
|
||||
end
|
||||
end.flatten
|
||||
|
||||
tests = items.find_all { |i| i[:type] == :test }
|
||||
libs = items.find_all { |i| i[:type] == :library }
|
||||
|
||||
# add all tests (raw) to the runtime
|
||||
items.flatten.each do |item|
|
||||
add_content(item[:content], item[:ref], item[:line])
|
||||
tests.flatten.each do |test|
|
||||
add_content(test, libs)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -59,12 +62,18 @@ module Inspec
|
|||
Inspec::ProfileContext.new(@profile_id, @backend)
|
||||
end
|
||||
|
||||
def add_content(content, source, line = nil)
|
||||
def add_content(test, libs)
|
||||
content = test[:content]
|
||||
return if content.nil? || content.empty?
|
||||
|
||||
# evaluate all tests
|
||||
# load all libraries
|
||||
ctx = create_context
|
||||
ctx.load(content, source, line || 1)
|
||||
libs.each do |lib|
|
||||
ctx.load(lib[:content].to_s, lib[:ref], lib[:line] || 1)
|
||||
end
|
||||
|
||||
# evaluate the test content
|
||||
ctx.load(content, test[:ref], test[:line] || 1)
|
||||
|
||||
# process the resulting rules
|
||||
ctx.rules.each do |rule_id, rule|
|
||||
|
|
|
@ -9,10 +9,15 @@ module Inspec::Targets
|
|||
paths.include?('test') && paths.include?('metadata.rb')
|
||||
end
|
||||
|
||||
def get_libraries(paths)
|
||||
paths.find_all do |path|
|
||||
path.start_with?('libraries') && path.end_with?('.rb')
|
||||
end
|
||||
end
|
||||
|
||||
def get_filenames(paths)
|
||||
paths.find_all do |path|
|
||||
(path.start_with?('test') || path.start_with?('lib')) &&
|
||||
path.end_with?('.rb')
|
||||
path.start_with?('test') && path.end_with?('.rb')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,12 +8,21 @@ module Inspec::Targets
|
|||
File.file?(target) and target.end_with?('.rb')
|
||||
end
|
||||
|
||||
def resolve(target)
|
||||
def resolve(target, opts = {})
|
||||
base = opts[:base_folder]
|
||||
path = base.nil? ? target : File.join(base, target)
|
||||
{
|
||||
content: File.read(target),
|
||||
ref: target,
|
||||
content: File.read(path),
|
||||
type: opts[:as] || :test,
|
||||
ref: path,
|
||||
}
|
||||
end
|
||||
|
||||
def resolve_all(targets, opts = {})
|
||||
Array(targets).map do |target|
|
||||
resolve(target, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Inspec::Targets.add_module('file', FileHelper.new)
|
||||
|
|
|
@ -26,9 +26,16 @@ module Inspec::Targets
|
|||
# get all test file contents
|
||||
file_handler = Inspec::Targets.modules['file']
|
||||
raw_files = helper.get_filenames(files)
|
||||
raw_files.map do |f|
|
||||
file_handler.resolve(File.join(target, f))
|
||||
tests = file_handler.resolve_all(raw_files, base_folder: target)
|
||||
|
||||
libs = []
|
||||
if helper.respond_to? :get_libraries
|
||||
raw_libs = helper.get_libraries(files)
|
||||
libs = file_handler.resolve_all(raw_libs,
|
||||
base_folder: target, as: :library)
|
||||
end
|
||||
|
||||
libs + tests
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue