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.



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

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

  configure_output
  configure_transport
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



18
19
20
# File 'lib/inspec/runner.rb', line 18

def backend
  @backend
end

#rulesObject (readonly)

Returns the value of attribute rules.



18
19
20
# File 'lib/inspec/runner.rb', line 18

def rules
  @rules
end

#testsObject (readonly)

Returns the value of attribute tests.



18
19
20
# File 'lib/inspec/runner.rb', line 18

def tests
  @tests
end

Instance Method Details

#add_content(test, libs) ⇒ Object



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

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)
  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(ctx, rule_id, rule)
  end
end

#add_tests(tests) ⇒ Object



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

def add_tests(tests)
  # retrieve the raw ruby code of all tests
  items = tests.map do |test|
    Inspec::Targets.resolve(test)
  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
  tests.flatten.each do |test|
    add_content(test, libs)
  end
end

#configure_outputObject



38
39
40
# File 'lib/inspec/runner.rb', line 38

def configure_output
  RSpec.configuration.add_formatter(@conf['format'] || 'progress')
end

#configure_transportObject



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

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

#create_contextObject



61
62
63
# File 'lib/inspec/runner.rb', line 61

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

#normalize_map(hm) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/inspec/runner.rb', line 30

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

#runObject



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

def run
  run_with(RSpec::Core::Runner.new(nil))
end

#run_with(rspec_runner) ⇒ Object



88
89
90
# File 'lib/inspec/runner.rb', line 88

def run_with(rspec_runner)
  rspec_runner.run_specs(@tests.ordered_example_groups)
end