Class: InspecRspecCli::Control
- Inherits:
-
Object
- Object
- InspecRspecCli::Control
- Includes:
- Comparable
- Defined in:
- lib/inspec/rspec_json_formatter.rb
Overview
This class wraps a control hash object to provide a useful inteface for maintaining the associated profile, ids, results, title, etc.
Constant Summary collapse
- STATUS_TYPES =
{ 'unknown' => -3, 'passed' => -2, 'skipped' => -1, 'minor' => 1, 'major' => 2, 'failed' => 2.5, 'critical' => 3, }.freeze
Instance Attribute Summary collapse
-
#control ⇒ Object
(also: #as_hash)
readonly
Returns the value of attribute control.
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
We are interested in comparing controls against other controls.
- #add_example(example) ⇒ Object
- #anonymous? ⇒ Boolean
- #examples ⇒ Object
- #id ⇒ Object
-
#initialize(control, profile) ⇒ Control
constructor
A new instance of Control.
- #profile_id ⇒ Object
-
#summary ⇒ Object
Return summary of the control which is usually a title with fails and skips.
- #summary_indicator ⇒ Object
-
#title ⇒ Object
Determine title for control given current_control.
Constructor Details
#initialize(control, profile) ⇒ Control
Returns a new instance of Control.
643 644 645 646 647 |
# File 'lib/inspec/rspec_json_formatter.rb', line 643 def initialize(control, profile) @control = control @profile = profile summary_calculation_is_needed end |
Instance Attribute Details
#control ⇒ Object (readonly) Also known as: as_hash
Returns the value of attribute control.
649 650 651 |
# File 'lib/inspec/rspec_json_formatter.rb', line 649 def control @control end |
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
649 650 651 |
# File 'lib/inspec/rspec_json_formatter.rb', line 649 def profile @profile end |
Instance Method Details
#<=>(other) ⇒ Object
We are interested in comparing controls against other controls. It is important to compare their id values and the id values of their profiles. In the event that a control has the same id in a different profile we do not want them to be considered the same.
Controls are never ordered so we don’t care about the remaining implementation of the spaceship operator.
737 738 739 740 741 742 743 |
# File 'lib/inspec/rspec_json_formatter.rb', line 737 def <=>(other) if id == other.id && profile_id == other.profile_id 0 else -1 end end |
#add_example(example) ⇒ Object
674 675 676 677 678 679 680 681 682 683 684 685 |
# File 'lib/inspec/rspec_json_formatter.rb', line 674 def add_example(example) control[:id] = example[:id] control[:profile_id] = example[:profile_id] example[:status_type] = status_type(example) example.delete(:id) example.delete(:profile_id) control[:results] ||= [] control[:results].push(example) summary_calculation_is_needed end |
#anonymous? ⇒ Boolean
657 658 659 |
# File 'lib/inspec/rspec_json_formatter.rb', line 657 def anonymous? control[:id].to_s.start_with? '(generated from ' end |
#examples ⇒ Object
665 666 667 |
# File 'lib/inspec/rspec_json_formatter.rb', line 665 def examples control[:results] end |
#id ⇒ Object
653 654 655 |
# File 'lib/inspec/rspec_json_formatter.rb', line 653 def id control[:id] end |
#profile_id ⇒ Object
661 662 663 |
# File 'lib/inspec/rspec_json_formatter.rb', line 661 def profile_id control[:profile_id] end |
#summary ⇒ Object
Return summary of the control which is usually a title with fails and skips
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 |
# File 'lib/inspec/rspec_json_formatter.rb', line 712 def summary calculate_summary! if summary_calculation_needed? suffix = if examples.length == 1 # Single test - be nice and just print the exception message if the test # failed. No need to say "1 failed". examples[0][:message].to_s else [ !fails.empty? ? "#{fails.uniq.length} failed" : nil, !skips.empty? ? "#{skips.uniq.length} skipped" : nil, ].compact.join(' ') end suffix == '' ? title : title + ' (' + suffix + ')' end |
#summary_indicator ⇒ Object
669 670 671 672 |
# File 'lib/inspec/rspec_json_formatter.rb', line 669 def summary_indicator calculate_summary! if summary_calculation_needed? STATUS_TYPES.key(@summary_status) end |
#title ⇒ Object
Determine title for control given current_control. Called from current_control_summary.
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
# File 'lib/inspec/rspec_json_formatter.rb', line 689 def title title = control[:title] if title title elsif examples.length == 1 # If it's an anonymous control, just go with the only description # available for the underlying test. examples[0][:code_desc].to_s elsif examples.empty? # Empty control block - if it's anonymous, there's nothing we can do. # Is this case even possible? 'Empty anonymous control' else # Multiple tests - but no title. Do our best and generate some form of # identifier or label or name. title = (examples.map { |example| example[:code_desc] }).join('; ') max_len = MULTI_TEST_CONTROL_SUMMARY_MAX_LEN title = title[0..(max_len-1)] + '...' if title.length > max_len title end end |