Class: Minitest::Reporters::TravisReporter

Inherits:
BaseReporter
  • Object
show all
Defined in:
lib/minitest/xs_and_os_plugin.rb

Constant Summary collapse

@@color_for_result_code =
{
    '.' => :green,
    'E' => :red,
    'F' => :red,
    '$' => :yellow
}
@@result_code_to_unicode =
{
    '.' => "\u{2714}",
    'F' => "\u{2718}",
    'E' => "\u{203C}",
    '$' => "\u{26A1}"
}
@@color =
{
    red:    31,
    green:  32,
    yellow: 33,
    blue:   34
}

Instance Method Summary collapse

Constructor Details

#initializeTravisReporter

Returns a new instance of TravisReporter.



31
32
33
34
35
# File 'lib/minitest/xs_and_os_plugin.rb', line 31

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

end

Instance Method Details

#backtrace(backtrace) ⇒ Object



144
145
146
147
148
# File 'lib/minitest/xs_and_os_plugin.rb', line 144

def backtrace(backtrace)
  backtrace = filter_backtrace(backtrace).map { |line| location(line, true) }
  return if backtrace.empty?
  indent(backtrace.join("\n")).gsub(/^(\s+)/, "\\1# ")
end

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



180
181
182
183
184
185
186
187
# File 'lib/minitest/xs_and_os_plugin.rb', line 180

def color(string, color = :default)
  if color_enabled?
    color = @@color.fetch(color, 0)
    "\e[#{color}m#{string}\e[0m"
  else
    string
  end
end

#color_enabled?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/minitest/xs_and_os_plugin.rb', line 189

def color_enabled?
  @color_enabled
end

#display_failing(result, index) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/minitest/xs_and_os_plugin.rb', line 101

def display_failing(result, index)
  backtrace = backtrace(result.failure.backtrace)
  message   = result.failure.message
  message   = message.lines.tap(&:pop).join.chomp if result.error?

  str = "\n\n"
  str << color('%4d) %s' % [index, result_name(result.name)])
  str << "\n" << color(indent(message), :red)
  str << "\n" << color(backtrace, :blue)
  io.print str
end

#display_replay_command(result) ⇒ Object



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

def display_replay_command(result)
  location, line = find_test_file(result)
  return if location.empty?

  command = if defined?(Rails) && Rails.version >= '5.0.0'
              %[bin/rails test #{location}:#{line}]
              %[rake TEST=#{location} TESTOPTS="--name=#{result.name}"]
            else
            end

  str = "\n"
  str << color(command, :red)

  io.print str
end

#display_skipped(result, index) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/minitest/xs_and_os_plugin.rb', line 113

def display_skipped(result, index)
  location = location(result.failure.location)
  str      = "\n\n"
  str << color('%4d) %s [SKIPPED]' % [index, result_name(result.name)], :yellow)
  str << "\n" << indent(color(location, :yellow))
  io.print str
end

#filter_backtrace(backtrace) ⇒ Object



159
160
161
# File 'lib/minitest/xs_and_os_plugin.rb', line 159

def filter_backtrace(backtrace)
  Minitest.backtrace_filter.filter(backtrace)
end

#find_test_file(result) ⇒ Object



137
138
139
140
141
142
# File 'lib/minitest/xs_and_os_plugin.rb', line 137

def find_test_file(result)
  location, line = result.source_location
  location       = location.gsub(%r[^.*?/((?:test|spec)/.*?)$], "\\1")

  [location, line]
end

#indent(text) ⇒ Object



97
98
99
# File 'lib/minitest/xs_and_os_plugin.rb', line 97

def indent(text)
  text.gsub(/^/, '      ')
end

#location(location, include_line_number = false) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/minitest/xs_and_os_plugin.rb', line 150

def location(location, include_line_number = false)
  regex    = include_line_number ? /^([^:]+:\d+)/ : /^([^:]+)/
  location = File.expand_path(location[regex, 1])

  return location unless location.start_with?(Dir.pwd)

  location.gsub(%r[^#{Regexp.escape(Dir.pwd)}/], '')
end

#pluralize(word, count) ⇒ Object



193
194
195
196
197
198
199
200
201
202
# File 'lib/minitest/xs_and_os_plugin.rb', line 193

def pluralize(word, count)
  case count
    when 0
      "no #{word}s"
    when 1
      "1 #{word}"
    else
      "#{count} #{word}s"
  end
end


169
170
171
172
173
174
175
176
177
178
# File 'lib/minitest/xs_and_os_plugin.rb', line 169

def print_result_code(result_code)
  result = @@result_code_to_unicode[result_code]
  colors = {
      "\u{2714}" => :green,
      "\u{2718}" => :red,
      "\u{203C}" => :red,
      "\u{26A1}" => :yellow
  }
  io.print color(result, colors[result])
end

#record(result) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/minitest/xs_and_os_plugin.rb', line 37

def record(result)
  super
  puts
  print result.class_name
  print ' '
  print self.result_name result.name
  print ' ['
  print_result_code(result.result_code)
  print ']'
end

#reportObject



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
# File 'lib/minitest/xs_and_os_plugin.rb', line 54

def report
  super
  io.sync = true

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

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

  if failing_results.any? || skipped_results.any?
    failing_results.each.with_index(1) { |result, index| display_failing(result, index) }
    skipped_results.each.with_index(failing_results.size + 1) { |result, index| display_skipped(result, index) }
  end

  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"
  end
end

#result_name(name) ⇒ Object



163
164
165
166
167
# File 'lib/minitest/xs_and_os_plugin.rb', line 163

def result_name(name)
  name
      .gsub(/^test(_\d+)?_/, '')
      .gsub(/_/, ' ')
end

#startObject



48
49
50
51
52
# File 'lib/minitest/xs_and_os_plugin.rb', line 48

def start
  super
  io.print "Run options: #{options[:args]} / "
  io.print 'Running:'
end

#statisticsObject



82
83
84
85
# File 'lib/minitest/xs_and_os_plugin.rb', line 82

def statistics
  'Finished in %.6fs, %.4f runs/s, %.4f assertions/s.' %
      [total_time, count / total_time, assertions / total_time]
end

#summaryObject

:nodoc:



87
88
89
90
91
92
93
94
95
# File 'lib/minitest/xs_and_os_plugin.rb', line 87

def summary # :nodoc:
  [
      pluralize('run', count),
      pluralize('assertion', assertions),
      pluralize('failure', failures),
      pluralize('error', errors),
      pluralize('skip', skips)
  ].join(', ')
end