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.



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

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

  # resets "pending examples" in reporter
  RSpec.configuration.reset

  configure_output
  configure_transport
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



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

def backend
  @backend
end

#rulesObject (readonly)

Returns the value of attribute rules.



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

def rules
  @rules
end

#testsObject (readonly)

Returns the value of attribute tests.



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

def tests
  @tests
end

Instance Method Details

#add_content(test, libs) ⇒ Object



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

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

#add_test_profile(test, ignore_supports = false) ⇒ Object



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

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



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

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_outputObject



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

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

#configure_transportObject



46
47
48
# File 'lib/inspec/runner.rb', line 46

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

#create_contextObject



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

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

#normalize_map(hm) ⇒ Object



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

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

#runObject



112
113
114
# File 'lib/inspec/runner.rb', line 112

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

#run_with(rspec_runner) ⇒ Object



116
117
118
# File 'lib/inspec/runner.rb', line 116

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