Class: Inspec::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/runner.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf = {}) ⇒ Runner

Returns a new instance of Runner.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/inspec/runner.rb', line 17

def initialize(conf = {})
  @rules = {}
  @profile_id = conf[:id]
  @conf = conf.dup
  @conf[:logger] ||= Logger.new(nil)

  @test_collector = @conf.delete(:test_collector) || begin
    require 'inspec/runner_rspec'
    RunnerRspec.new(@conf)
  end

  configure_transport
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



16
17
18
# File 'lib/inspec/runner.rb', line 16

def backend
  @backend
end

#rulesObject (readonly)

Returns the value of attribute rules.



16
17
18
# File 'lib/inspec/runner.rb', line 16

def rules
  @rules
end

Instance Method Details

#add_content(test, libs) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/inspec/runner.rb', line 89

def add_content(test, libs)
  content = test[:content]
  return if content.nil? || content.empty?

  # load all libraries
  ctx = create_context
  libs.each do |lib|
    ctx.load(lib[:content].to_s, lib[:ref], lib[:line] || 1)
    ctx.reload_dsl
  end

  # evaluate the test content
  ctx.load(content, test[:ref], test[:line] || 1)

  # process the resulting rules
  ctx.rules.each do |rule_id, rule|
    register_rule(rule_id, rule)
  end
end

#add_test_profile(test, ignore_supports = false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/inspec/runner.rb', line 47

def add_test_profile(test, ignore_supports = false)
  assets = Inspec::Targets.resolve(test, @conf)
  meta_assets = assets.find_all { |a| a[:type] == :metadata }
  metas = meta_assets.map do |x|
    Inspec::Metadata.from_ref(x[:ref], x[:content], @profile_id, @conf[:logger])
  end
  metas.each do |meta|
    return [] unless ignore_supports || meta.supports_transport?(@backend)
  end
  assets
end

#add_tests(tests, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/inspec/runner.rb', line 59

def add_tests(tests, options = {})
  # retrieve the raw ruby code of all tests
  items = tests.map do |test|
    add_test_profile(test, options[:ignore_supports])
  end.flatten

  tests = items.find_all { |i| i[:type] == :test }
  libs = items.find_all { |i| i[:type] == :library }

  # Ensure each test directory exists on the $LOAD_PATH. This
  # will ensure traditional RSpec-isms like `require 'spec_helper'`
  # continue to work.
  tests.flatten.each do |test|
    # do not load path for virtual files, eg. from zip
    if !test[:ref].nil?
      test_directory = File.dirname(test[:ref])
      $LOAD_PATH.unshift test_directory unless $LOAD_PATH.include?(test_directory)
    end
  end

  # add all tests (raw) to the runtime
  tests.flatten.each do |test|
    add_content(test, libs)
  end
end

#configure_transportObject



43
44
45
# File 'lib/inspec/runner.rb', line 43

def configure_transport
  @backend = Inspec::Backend.create(@conf)
end

#create_contextObject



85
86
87
# File 'lib/inspec/runner.rb', line 85

def create_context
  Inspec::ProfileContext.new(@profile_id, @backend)
end

#normalize_map(hm) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/inspec/runner.rb', line 35

def normalize_map(hm)
  res = {}
  hm.each {|k, v|
    res[k.to_s] = v
  }
  res
end

#run(with = nil) ⇒ Object



109
110
111
# File 'lib/inspec/runner.rb', line 109

def run(with = nil)
  @test_collector.run(with)
end

#testsObject



31
32
33
# File 'lib/inspec/runner.rb', line 31

def tests
  @test_collector.tests
end