Class: Inspec::Formatters::Base

Inherits:
RSpec::Core::Formatters::BaseFormatter
  • Object
show all
Defined in:
lib/inspec/formatters/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
18
19
20
# File 'lib/inspec/formatters/base.rb', line 11

def initialize(output)
  super(output)

  @run_data = {}
  @profiles = []
  @profiles_info = nil
  @backend = nil
  @all_controls_count = nil
  @control_checks_count_map = {}
end

Instance Attribute Details

#backendObject

Returns the value of attribute backend.



9
10
11
# File 'lib/inspec/formatters/base.rb', line 9

def backend
  @backend
end

#run_dataObject

Returns the value of attribute run_data.



9
10
11
# File 'lib/inspec/formatters/base.rb', line 9

def run_data
  @run_data
end

Instance Method Details

#add_profile(profile) ⇒ Object

Add the current profile to the list of executed profiles. Called by the runner during example collection.



82
83
84
# File 'lib/inspec/formatters/base.rb', line 82

def add_profile(profile)
  @profiles.push(profile)
end

#dump_summary(summary) ⇒ Object

RSpec Override: #dump_summary

Supply run summary data, such as the InSpec version and the total duration.



25
26
27
28
29
30
31
# File 'lib/inspec/formatters/base.rb', line 25

def dump_summary(summary)
  run_data[:version] = Inspec::VERSION
  run_data[:statistics] = {
    duration: summary.duration,
    controls: statistics,
  }
end

#get_control_checks_count_mapObject



101
102
103
# File 'lib/inspec/formatters/base.rb', line 101

def get_control_checks_count_map
  @control_checks_count_map
end

#get_controls_countObject



97
98
99
# File 'lib/inspec/formatters/base.rb', line 97

def get_controls_count
  @all_controls_count
end

#resultsObject

Return all the collected output to the caller



107
108
109
# File 'lib/inspec/formatters/base.rb', line 107

def results
  run_data
end

#set_control_checks_count_map(mapping) ⇒ Object



93
94
95
# File 'lib/inspec/formatters/base.rb', line 93

def set_control_checks_count_map(mapping)
  @control_checks_count_map = mapping
end

#set_controls_count(controls_count) ⇒ Object

These control count related methods are called via runner rspec library of inspec And these are used within streaming plugins to determine end of control Start of control count related methods



89
90
91
# File 'lib/inspec/formatters/base.rb', line 89

def set_controls_count(controls_count)
  @all_controls_count = controls_count
end

#stop(notification) ⇒ Object

RSpec Override: #stop

Called at the end of a complete RSpec run. We use this to map tests to controls and flesh out the rest of the run_data hash to include details about the run, the platform, etc.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/inspec/formatters/base.rb', line 38

def stop(notification)
  # This might be a bit confusing. The results are not actually organized
  # by control. It is organized by test. So if a control has 3 tests, the
  # output will have 3 control entries, each one with the same control id
  # and different test results. An rspec example maps to an inspec test.
  run_data[:controls] = notification.examples.map do |example|
    format_example(example).tap do |hash|
      e = example.exception
      next unless e

      if example.[:sensitive]
        hash[:message] = "*** sensitive output suppressed ***"
      else
        hash[:message] = exception_message(e)
      end

      next if e.is_a? RSpec::Expectations::ExpectationNotMetError

      hash[:exception] = e.class.name
      hash[:backtrace] = e.backtrace
    end
  end

  # include any tests that were run that were not part of a control
  run_data[:other_checks] = examples_without_controls
  examples_with_controls.each do |example|
    control = example2control(example)
    move_example_into_control(example, control)
  end

  # flesh out the profiles key with additional profile information
  run_data[:profiles] = profiles_info

  # add the platform information for this particular target
  run_data[:platform] = {
    name: platform(:name),
    release: platform(:release),
    target: backend_target,
    target_id: platform(:uuid),
  }
end