Class: InspecRspecCli

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

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: Control

Constant Summary collapse

MULTI_TEST_CONTROL_SUMMARY_MAX_LEN =
60

Instance Attribute Summary

Attributes inherited from InspecRspecJson

#backend

Instance Method Summary collapse

Methods inherited from InspecRspecJson

#add_profile, #stop

Methods inherited from InspecRspecMiniJson

#dump_summary, #stop

Constructor Details

#initialize(*args) ⇒ InspecRspecCli

Returns a new instance of InspecRspecCli.



338
339
340
341
342
343
# File 'lib/inspec/rspec_json_formatter.rb', line 338

def initialize(*args)
  @current_control = nil
  @all_controls = []
  @profile_printed = false
  super(*args)
end

Instance Method Details

#close(_notification) ⇒ Object

This is the last method is invoked through the formatter interface. Because the profile we may have some remaining anonymous examples so we want to display them as well as a summary of the profile and test stats.



405
406
407
408
409
410
411
412
413
414
# File 'lib/inspec/rspec_json_formatter.rb', line 405

def close(_notification)
  # when the profile has no controls or examples it will not have been printed.
  # then we want to ensure we print all the profiles
  print_last_control_with_examples unless last_control_is_anonymous?
  output.puts ''
  print_anonymous_examples_associated_with_last_profile
  print_profiles_without_examples
  print_profile_summary
  print_tests_summary
end

#format_example(example) ⇒ Object

This method is called through the RSpec Formatter interface for every example found in the test suite.

Within #format_example we are getting an example and:

* if this is an example, within a control, within a profile then we want
  to display the profile header, display the control, and then display
  the example.
* if this is another example, within the same control, within the same
  profile we want to display the example.
* if this is an example that does not map to a control (anonymous) then
  we want to store it for later to displayed at the end of a profile.


358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/inspec/rspec_json_formatter.rb', line 358

def format_example(example)
  example_data = super(example)
  control = create_or_find_control(example_data)

  # If we are switching to a new control then we want to print the control
  # we were previously collecting examples unless the last control is
  # anonymous (no control). Anonymous controls and their examples are handled
  # later on the profile change.

  if switching_to_new_control?(control)
    print_last_control_with_examples unless last_control_is_anonymous?
  end

  store_last_control(control)

  # Each profile may have zero or more anonymous examples. These are examples
  # that defined in a profile but outside of a control. They may be defined
  # at the start, in-between, or end of list of examples. To display them
  # at the very end of a profile, which means we have to wait for the profile
  # to change to know we are done with a profile.

  if switching_to_new_profile?(control.profile)
    output.puts ''
    print_anonymous_examples_associated_with_last_profile
    clear_anonymous_examples_associated_with_last_profile
  end

  print_profile(control.profile)
  store_last_profile(control.profile)

  # The anonymous controls should be added to a hash that we will display
  # when we are done examining all the examples within this profile.

  if control.anonymous?
    add_anonymous_example_within_this_profile(control.as_hash)
  end

  @all_controls.push(control.as_hash)
  example_data
end