Class: Specwrk::CLIReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/specwrk/cli_reporter.rb

Instance Method Summary collapse

Instance Method Details

#clientObject



119
120
121
# File 'lib/specwrk/cli_reporter.rb', line 119

def client
  @client ||= Client.new
end

#colorizerObject



123
124
125
# File 'lib/specwrk/cli_reporter.rb', line 123

def colorizer
  ::RSpec::Core::Formatters::ConsoleCodes
end

#example_countObject



107
108
109
# File 'lib/specwrk/cli_reporter.rb', line 107

def example_count
  report_data.dig(:examples).length
end

#failure_countObject



95
96
97
# File 'lib/specwrk/cli_reporter.rb', line 95

def failure_count
  report_data.dig(:meta, :failures)
end

#failure_reruns_linesObject



67
68
69
70
71
72
73
# File 'lib/specwrk/cli_reporter.rb', line 67

def failure_reruns_lines
  @failure_reruns_lines ||= report_data.dig(:examples).values.map do |example|
    next unless example[:status] == "failed"

    ["rspec #{example[:file_path]}:#{example[:line_number]}", "# #{example[:full_description]}"]
  end.compact
end

#flake_countObject



111
112
113
# File 'lib/specwrk/cli_reporter.rb', line 111

def flake_count
  report_data.dig(:flakes).length
end

#flake_reruns_linesObject



75
76
77
78
79
80
81
# File 'lib/specwrk/cli_reporter.rb', line 75

def flake_reruns_lines
  @flake_reruns_lines ||= report_data.dig(:flakes).map do |example_id, count|
    example = report_data.dig(:examples, example_id)

    ["rspec #{example[:file_path]}:#{example[:line_number]}", "# #{example[:full_description]}. Failed #{RSpec::Core::Formatters::Helpers.pluralize(count, "time")} before passing."]
  end.compact
end

#pending_countObject



99
100
101
# File 'lib/specwrk/cli_reporter.rb', line 99

def pending_count
  report_data.dig(:meta, :pending)
end

#reportObject



13
14
15
16
17
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
# File 'lib/specwrk/cli_reporter.rb', line 13

def report
  unless Client.connect?
    puts colorizer.wrap("\nCannot connect to server to generate report. Assuming failure.", :red)
    return 1
  end

  puts "\nFinished in #{Specwrk.human_readable_duration total_duration} " \
                      "(total execution time of #{Specwrk.human_readable_duration total_run_time})\n"

  if flake_count.positive?
    puts "\nFlaked examples:\n\n"
    flake_reruns_lines.each { |(command, description)| print "#{colorizer.wrap(command, :magenta)} #{colorizer.wrap(description, :cyan)}\n" }
    puts ""
  end

  client.shutdown

  if failure_count.positive?
    puts colorizer.wrap(totals_line, :red)

    puts "\nFailed examples:\n\n"
    failure_reruns_lines.each { |(command, description)| print "#{colorizer.wrap(command, :red)} #{colorizer.wrap(description, :cyan)}\n" }
    puts ""

    1
  elsif unexecuted_count.positive?
    puts colorizer.wrap(totals_line, :red)

    1
  elsif pending_count.positive?
    puts colorizer.wrap(totals_line, :yellow)
    0
  else
    puts colorizer.wrap(totals_line, :green)
    0
  end
rescue Specwrk::UnhandledResponseError => e
  puts colorizer.wrap("\nCannot report, #{e.message}.", :red)

  client.shutdown

  1
end

#report_dataObject



83
84
85
# File 'lib/specwrk/cli_reporter.rb', line 83

def report_data
  @report_data ||= client.report
end

#total_durationObject



87
88
89
# File 'lib/specwrk/cli_reporter.rb', line 87

def total_duration
  Time.parse(report_data.dig(:meta, :last_finished_at)) - Time.parse(report_data.dig(:meta, :first_started_at))
end

#total_flakesObject



115
116
117
# File 'lib/specwrk/cli_reporter.rb', line 115

def total_flakes
  @total_flakes ||= report_data.dig(:flakes).values.sum
end

#total_run_timeObject



91
92
93
# File 'lib/specwrk/cli_reporter.rb', line 91

def total_run_time
  report_data.dig(:meta, :total_run_time)
end

#totals_lineObject



57
58
59
60
61
62
63
64
65
# File 'lib/specwrk/cli_reporter.rb', line 57

def totals_line
  summary = RSpec::Core::Formatters::Helpers.pluralize(example_count, "example") +
    ", " + RSpec::Core::Formatters::Helpers.pluralize(failure_count, "failure")
  summary += ", #{pending_count} pending" if pending_count.positive?
  summary += ", #{RSpec::Core::Formatters::Helpers.pluralize(flake_count, "example")} flaked #{RSpec::Core::Formatters::Helpers.pluralize(total_flakes, "time")}" if flake_count.positive?
  summary += ". #{RSpec::Core::Formatters::Helpers.pluralize(unexecuted_count, "example")} not executed" if unexecuted_count.positive?

  summary
end

#unexecuted_countObject



103
104
105
# File 'lib/specwrk/cli_reporter.rb', line 103

def unexecuted_count
  report_data.dig(:meta, :unexecuted)
end