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.



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

def initialize(output)
  super(output)

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

Instance Attribute Details

#backendObject

Returns the value of attribute backend.



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

def backend
  @backend
end

#enhanced_outcomesObject

Returns the value of attribute enhanced_outcomes.



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

def enhanced_outcomes
  @enhanced_outcomes
end

#run_dataObject

Returns the value of attribute run_data.



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

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.



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

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.



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

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

#get_control_checks_count_mapObject



104
105
106
# File 'lib/inspec/formatters/base.rb', line 104

def get_control_checks_count_map
  @control_checks_count_map
end

#get_controls_countObject



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

def get_controls_count
  @all_controls_count
end

#resultsObject

Return all the collected output to the caller



110
111
112
# File 'lib/inspec/formatters/base.rb', line 110

def results
  run_data
end

#set_control_checks_count_map(mapping) ⇒ Object



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

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



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

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.



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
79
80
81
# File 'lib/inspec/formatters/base.rb', line 40

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_enhanced_outcomes_to_controls if enhanced_outcomes

  # 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