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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(control_hash) ⇒ Control

Returns a new instance of Control.



354
355
356
# File 'lib/inspec/reporters/cli.rb', line 354

def initialize(control_hash)
  @data = control_hash
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



352
353
354
# File 'lib/inspec/reporters/cli.rb', line 352

def data
  @data
end

Instance Method Details

#anonymous?Boolean

Returns:

  • (Boolean)


378
379
380
# File 'lib/inspec/reporters/cli.rb', line 378

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

#failure_countObject



425
426
427
# File 'lib/inspec/reporters/cli.rb', line 425

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

#idObject



358
359
360
# File 'lib/inspec/reporters/cli.rb', line 358

def id
  data[:id]
end

#impactObject



370
371
372
# File 'lib/inspec/reporters/cli.rb', line 370

def impact
  data[:impact]
end

#impact_stringObject



399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/inspec/reporters/cli.rb', line 399

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"
  else
    "failed"
  end
end

#impact_string_for_result(result) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
# File 'lib/inspec/reporters/cli.rb', line 413

def impact_string_for_result(result)
  if result[:status] == "skipped"
    "skipped"
  elsif result[:status] == "passed"
    "passed"
  elsif impact.nil?
    "unknown"
  else
    "failed"
  end
end

#resultsObject



366
367
368
# File 'lib/inspec/reporters/cli.rb', line 366

def results
  data[:results]
end

#skipped_countObject



429
430
431
# File 'lib/inspec/reporters/cli.rb', line 429

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

#source_locationObject



374
375
376
# File 'lib/inspec/reporters/cli.rb', line 374

def source_location
  data[:source_location]
end

#titleObject



362
363
364
# File 'lib/inspec/reporters/cli.rb', line 362

def title
  data[:title]
end

#title_for_reportObject



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/inspec/reporters/cli.rb', line 382

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