Class: Minitest::Utils::Reporter

Inherits:
StatisticsReporter
  • Object
show all
Defined in:
lib/minitest/utils/reporter.rb

Constant Summary collapse

COLOR_FOR_RESULT_CODE =
{
  "." => :green,
  "E" => :red,
  "F" => :red,
  "S" => :yellow
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReporter

Returns a new instance of Reporter.



30
31
32
33
# File 'lib/minitest/utils/reporter.rb', line 30

def initialize(*)
  super
  @tty = io.respond_to?(:tty?) && io.tty?
end

Class Method Details

.filtersObject



19
20
21
# File 'lib/minitest/utils/reporter.rb', line 19

def self.filters
  @filters ||= []
end

Instance Method Details

#bundlerObject



298
299
300
# File 'lib/minitest/utils/reporter.rb', line 298

def bundler
  "bundle exec " if ENV.key?("BUNDLE_BIN_PATH")
end

#color(string, color = :default) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/minitest/utils/reporter.rb', line 45

def color(string, color = :default)
  if @tty
    Utils.color(string, color)
  else
    string
  end
end

#format_duration(duration_in_seconds) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/minitest/utils/reporter.rb', line 144

def format_duration(duration_in_seconds)
  duration_ns = duration_in_seconds * 1_000_000_000

  number, unit = if duration_ns < 1000
                   [duration_ns, "ns"]
                 elsif duration_ns < 1_000_000
                   [duration_ns / 1000, "μs"]
                 elsif duration_ns < 1_000_000_000
                   [duration_ns / 1_000_000, "ms"]
                 else
                   [duration_ns / 1_000_000_000, "s"]
                 end

  number =
    format("%.2f", number).gsub(/0+$/, "").delete_suffix(".")

  "#{number}#{unit}"
end


108
109
110
111
112
# File 'lib/minitest/utils/reporter.rb', line 108

def print_failing_results(results, initial_index = 1)
  results.each.with_index(initial_index) do |result, index|
    display_failing(result, index)
  end
end


114
115
116
117
118
119
120
# File 'lib/minitest/utils/reporter.rb', line 114

def print_skipped_results(results, initial_index)
  results
    .each
    .with_index(initial_index + 1) do |result, index|
    display_skipped(result, index)
  end
end


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/minitest/utils/reporter.rb', line 122

def print_slow_results
  test_results = slow_tests.take(10)

  return if Minitest.options[:hide_slow]
  return unless test_results.any?

  io.puts "\nSlow Tests:\n"

  test_results.each_with_index do |info, index|
    location = info[:source_location].join(":")
    duration = format_duration(info[:time])

    prefix = "#{index + 1}) "
    padding = " " * prefix.size

    io.puts color("#{prefix}#{info[:description]} (#{duration})",
                  :red)
    io.puts color("#{padding}#{location}", :blue)
    io.puts
  end
end

#record(result) ⇒ Object



40
41
42
43
# File 'lib/minitest/utils/reporter.rb', line 40

def record(result)
  super
  print_result_code(result.result_code)
end

#reportObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/minitest/utils/reporter.rb', line 53

def report
  super
  io.sync = true if io.respond_to?(:sync=)

  failing_results = results.reject(&:passed?).reject(&:skipped?)
  skipped_results = results.select(&:skipped?)

  write_failures_json(failing_results)
  print_failing_results(failing_results)
  if failing_results.empty?
    print_skipped_results(skipped_results, failing_results.size)
  end

  color = :green
  color = :yellow if skipped_results.any?
  color = :red if failing_results.any?

  io.print "\n\n"
  io.puts statistics
  io.puts color(summary, color)

  if failing_results.any?
    io.puts "\nFailed Tests:\n"
    failing_results.each {|result| display_replay_command(result) }
    io.puts "\n\n"
  else
    print_slow_results
  end
end

#slow_testsObject



98
99
100
101
102
103
104
105
106
# File 'lib/minitest/utils/reporter.rb', line 98

def slow_tests
  Test
    .tests
    .values
    .select { _1[:time] }
    .filter { _1[:time] > slow_threshold_for(_1) }
    .sort_by { _1[:time] }
    .reverse
end

#slow_threshold_for(test_case) ⇒ Object



94
95
96
# File 'lib/minitest/utils/reporter.rb', line 94

def slow_threshold_for(test_case)
  test_case[:slow_threshold] || Minitest.options[:slow_threshold] || 0.1
end

#startObject



35
36
37
38
# File 'lib/minitest/utils/reporter.rb', line 35

def start
  super
  io.puts "Run options: #{options[:args]}\n"
end

#write_failures_json(results) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/minitest/utils/reporter.rb', line 83

def write_failures_json(results)
  tests = results.each_with_object([]) do |result, buffer|
    buffer << find_test_info(result)[:id]
  end

  File.write(
    File.join(Dir.pwd, ".minitestfailures"),
    JSON.dump(tests)
  )
end