Class: InspecRspecJson

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

Direct Known Subclasses

InspecRspecCli

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ InspecRspecJson

Returns a new instance of InspecRspecJson.



93
94
95
96
97
98
99
# File 'lib/inspec/rspec_json_formatter.rb', line 93

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.



91
92
93
# File 'lib/inspec/rspec_json_formatter.rb', line 91

def backend=(value)
  @backend = value
end

Instance Method Details

#add_profile(profile) ⇒ Object

Called by the runner during example collection.



102
103
104
# File 'lib/inspec/rspec_json_formatter.rb', line 102

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

#dump_one_example(example, control) ⇒ Object



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

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

#dump_summary(summary) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/inspec/rspec_json_formatter.rb', line 136

def dump_summary(summary)
  super(summary)
  total = 0
  failed = 0
  skipped = 0
  passed = 0

  @profiles_info.each do |_name, profile|
    total += profile[:controls].length
    profile[:controls].each do |_control_name, control|
      next unless control[:results]
      if control[:results].any? { |r| r[:status] == 'failed' }
        failed += 1
      elsif control[:results].any? { |r| r[:status] == 'skipped' }
        skipped += 1
      else
        passed += 1
      end
    end
  end

  # TODO: provide this information in the output
end

#start(_notification) ⇒ Object

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



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

def start(_notification)
  # Note that the default profile may have no name - therefore
  # the hash may have a valid nil => entry.
  @profiles_info ||= Hash[@profiles.map { |x| profile_info(x) }]
end

#stop(notification) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/inspec/rspec_json_formatter.rb', line 121

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