Class: InspecRspecJson

Inherits:
InspecRspecMiniJson show all
Defined in:
lib/inspec/rspec_json_formatter.rb

Overview

rubocop:disable Metrics/ClassLength

Direct Known Subclasses

InspecRspecCli

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from InspecRspecMiniJson

#dump_summary

Constructor Details

#initialize(*args) ⇒ InspecRspecJson

Returns a new instance of InspecRspecJson.



100
101
102
103
104
105
106
# File 'lib/inspec/rspec_json_formatter.rb', line 100

def initialize(*args)
  super(*args)
  @profiles = []
  # Will be valid after "start" state is reached.
  @profiles_info = nil
  @backend = nil
end

Instance Attribute Details

#backend=(value) ⇒ Object (writeonly)

Sets the attribute backend

Parameters:

  • value

    the value to set the attribute backend to.



98
99
100
# File 'lib/inspec/rspec_json_formatter.rb', line 98

def backend=(value)
  @backend = value
end

Instance Method Details

#add_profile(profile) ⇒ Object

Called by the runner during example collection.



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

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

#controls_summaryObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/inspec/rspec_json_formatter.rb', line 143

def controls_summary
  failed = 0
  skipped = 0
  passed = 0
  critical = 0
  major = 0
  minor = 0

  @control_tests.each do |control|
    next if control[:id].start_with? '(generated from '
    next unless control[:results]
    if control[:results].any? { |r| r[:status] == 'failed' }
      failed += 1
      if control[:impact] >= 0.7
        critical += 1
      elsif control[:impact] >= 0.4
        major += 1
      else
        minor += 1
      end
    elsif control[:results].any? { |r| r[:status] == 'skipped' }
      skipped += 1
    else
      passed += 1
    end
  end

  total = failed + passed + skipped

  { 'total' => total,
    'failed' => {
      'total' => failed,
      'critical' => critical,
      'major' => major,
      'minor' => minor,
    },
    'skipped' => skipped,
    'passed' => passed }
end

#dump_one_example(example, control) ⇒ Object



121
122
123
124
125
126
# File 'lib/inspec/rspec_json_formatter.rb', line 121

def dump_one_example(example, control)
  control[:results] ||= []
  example.delete(:id)
  example.delete(:profile_id)
  control[:results].push(example)
end

#start(_notification) ⇒ Object

Called after all examples have been collected but before rspec test execution has begun.



115
116
117
118
119
# File 'lib/inspec/rspec_json_formatter.rb', line 115

def start(_notification)
  # Note that the default profile may have no name - therefore
  # the hash may have a valid nil => entry.
  @profiles_info = @profiles.map(&:info!).map(&:dup)
end

#stop(notification) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/inspec/rspec_json_formatter.rb', line 128

def stop(notification)
  super(notification)
  examples = @output_hash.delete(:controls)
  missing = []

  examples.each do |example|
    control = example2control(example, @profiles_info)
    next missing.push(example) if control.nil?
    dump_one_example(example, control)
  end

  @output_hash[:profiles] = @profiles_info
  @output_hash[:other_checks] = missing
end

#tests_summaryObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/inspec/rspec_json_formatter.rb', line 183

def tests_summary
  total = 0
  failed = 0
  skipped = 0
  passed = 0

  all_tests = @anonymous_tests + @control_tests
  all_tests.each do |control|
    next unless control[:results]
    control[:results].each do |result|
      if result[:status] == 'failed'
        failed += 1
      elsif result[:status] == 'skipped'
        skipped += 1
      else
        passed += 1
      end
    end
  end

  { 'total' => total, 'failed' => failed, 'skipped' => skipped, 'passed' => passed }
end