Class: Inspec::Reporters::CLI::Control

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/reporters/cli.rb

Constant Summary collapse

IMPACT_SCORES =
{
  critical: 0.7,
  major: 0.4,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(control_hash) ⇒ Control

Returns a new instance of Control.



311
312
313
# File 'lib/inspec/reporters/cli.rb', line 311

def initialize(control_hash)
  @data = control_hash
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



309
310
311
# File 'lib/inspec/reporters/cli.rb', line 309

def data
  @data
end

Instance Method Details

#anonymous?Boolean

Returns:

  • (Boolean)


331
332
333
# File 'lib/inspec/reporters/cli.rb', line 331

def anonymous?
  id.start_with?('(generated from ')
end

#failure_countObject



386
387
388
# File 'lib/inspec/reporters/cli.rb', line 386

def failure_count
  results.select { |r| r[:status] == 'failed' }.size
end

#idObject



315
316
317
# File 'lib/inspec/reporters/cli.rb', line 315

def id
  data[:id]
end

#impactObject



327
328
329
# File 'lib/inspec/reporters/cli.rb', line 327

def impact
  data[:impact]
end

#impact_stringObject



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/inspec/reporters/cli.rb', line 352

def impact_string
  if anonymous?
    nil
  elsif impact.nil?
    'unknown'
  elsif results&.find { |r| r[:status] == 'skipped' }
    'skipped'
  elsif results.nil? || results.empty? || results.all? { |r| r[:status] == 'passed' }
    'passed'
  elsif impact >= IMPACT_SCORES[:critical]
    'critical'
  elsif impact >= IMPACT_SCORES[:major]
    'major'
  else
    'minor'
  end
end

#impact_string_for_result(result) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/inspec/reporters/cli.rb', line 370

def impact_string_for_result(result)
  if result[:status] == 'skipped'
    'skipped'
  elsif result[:status] == 'passed'
    'passed'
  elsif impact.nil?
    'unknown'
  elsif impact >= IMPACT_SCORES[:critical]
    'critical'
  elsif impact >= IMPACT_SCORES[:major]
    'major'
  else
    'minor'
  end
end

#resultsObject



323
324
325
# File 'lib/inspec/reporters/cli.rb', line 323

def results
  data[:results]
end

#skipped_countObject



390
391
392
# File 'lib/inspec/reporters/cli.rb', line 390

def skipped_count
  results.select { |r| r[:status] == 'skipped' }.size
end

#titleObject



319
320
321
# File 'lib/inspec/reporters/cli.rb', line 319

def title
  data[:title]
end

#title_for_reportObject



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/inspec/reporters/cli.rb', line 335

def title_for_report
  # if this is an anonymous control, just grab the resource title from any result entry
  return results.first[:resource_title] if anonymous?

  title_for_report = "#{id}: #{title || results.first[:resource_title]}"

  # we will not add any additional data to the title if there's only
  # zero or one test for this control.
  return title_for_report if results.nil? || results.size <= 1

  # append a failure summary if appropriate.
  title_for_report += " (#{failure_count} failed)" if failure_count > 0
  title_for_report += " (#{skipped_count} skipped)" if skipped_count > 0

  title_for_report
end