Class: RSpec::Nagios::Formatter

Inherits:
Core::Formatters::BaseFormatter
  • Object
show all
Defined in:
lib/rspec/nagios/formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ Formatter

Returns a new instance of Formatter.



5
6
7
# File 'lib/rspec/nagios/formatter.rb', line 5

def initialize(output)
  super(output)
end

Instance Method Details

#dump_summary(duration, example_count, failure_count, pending_count) ⇒ Object



9
10
11
12
# File 'lib/rspec/nagios/formatter.rb', line 9

def dump_summary(duration, example_count, failure_count, pending_count)
  super(duration, example_count, failure_count, pending_count)
  output.puts summary_line(duration, example_count, failure_count, pending_count)
end

#rounding(float, precision) ⇒ Object



14
15
16
# File 'lib/rspec/nagios/formatter.rb', line 14

def rounding(float, precision)
  return ((float * 10**precision).round.to_f) / (10**precision)
end

#summary_line(duration, example_count, failure_count, pending_count) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rspec/nagios/formatter.rb', line 18

def summary_line(duration, example_count, failure_count, pending_count)
  passing_count = example_count - failure_count
  # conformance is expressed as a percentage
  # if example_count is zero we need to avoid div by 0
  if example_count > 0
    conformance  = passing_count / example_count.to_f
    conformance *= 100
    conformance  = conformance.round
  else
    conformance  = 0
  end
  # limit duration precision to microseconds
  time = rounding(duration, 6)

  summary = 'RSPEC'
  if failure_count == 0
    summary << " OK"
  else
    summary << " Critical"
  end

  summary << " - " << pluralize(example_count, "example")
  summary << ", " << pluralize(failure_count, "failure")
  summary << ", #{pending_count} pending" if pending_count > 0
  summary << ", finished in #{time} seconds"

  summary << " | examples=#{example_count}"
  summary << " passing=#{example_count - failure_count}"
  summary << " failures=#{failure_count}"
  summary << " pending=#{pending_count}"
  summary << " conformance=#{conformance}%"
  summary << " time=#{time}s"

  if failed_examples.any?
    summary << "\n"
    summary << "#{failed_examples.map { |e| "#{e.location} #{e.full_description}" }.join("\n") }"
  end

  summary
end