Class: TConsole::MiniTestUnit

Inherits:
MiniTest::Unit
  • Object
show all
Defined in:
lib/tconsole/minitest_handler.rb

Overview

Custom minitest runner for tconsole

Constant Summary collapse

COLOR_MAP =
{
  "S" => ::Term::ANSIColor.cyan,
  "E" => ::Term::ANSIColor.red,
  "F" => ::Term::ANSIColor.red,
  "P" => ::Term::ANSIColor.green
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match_patterns, config) ⇒ MiniTestUnit

Returns a new instance of MiniTestUnit.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tconsole/minitest_handler.rb', line 60

def initialize(match_patterns, config)
  self.match_patterns = match_patterns
  self.match_patterns = [] unless self.match_patterns.is_a?(Array)

  self.config = config
  self.results = TConsole::TestResult.new

  self.passes = 0

  results.suite_counts = config.cached_suite_counts
  results.elements = config.cached_elements

  super()

  # We do this since plugins like turn may have tweaked it
  @@out = $stdout
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



58
59
60
# File 'lib/tconsole/minitest_handler.rb', line 58

def config
  @config
end

#interruptedObject

Returns the value of attribute interrupted.



58
59
60
# File 'lib/tconsole/minitest_handler.rb', line 58

def interrupted
  @interrupted
end

#match_patternsObject

Returns the value of attribute match_patterns.



58
59
60
# File 'lib/tconsole/minitest_handler.rb', line 58

def match_patterns
  @match_patterns
end

#passesObject

Returns the value of attribute passes.



58
59
60
# File 'lib/tconsole/minitest_handler.rb', line 58

def passes
  @passes
end

#resultsObject

Returns the value of attribute results.



58
59
60
# File 'lib/tconsole/minitest_handler.rb', line 58

def results
  @results
end

Instance Method Details

#_run_anything(type) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tconsole/minitest_handler.rb', line 78

def _run_anything(type)
  suites = TestCase.send "#{type}_suites"
  return if suites.empty?

  start = Time.now

  @test_count, @assertion_count = 0, 0
  sync = output.respond_to? :"sync=" # stupid emacs
  old_sync, output.sync = output.sync, true if sync

  results = _run_suites(suites, type)

  @test_count      = results.inject(0) { |sum, (tc, _)| sum + tc }
  @assertion_count = results.inject(0) { |sum, (_, ac)| sum + ac }

  output.sync = old_sync if sync

  t = Time.now - start

  puts
  puts
  puts "Finished #{type}s in %.6fs, %.4f tests/s, %.4f assertions/s." %
    [t, test_count / t, assertion_count / t]

  report.each_with_index do |msg, i|
    puts "\n%3d) %s" % [i + 1, msg]
  end

  puts

  status
end

#_run_suite(suite, type) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/tconsole/minitest_handler.rb', line 142

def _run_suite(suite, type)
  @last_suite ||= nil
  @failed_fast ||= false

  assertions = suite.send("#{type}_methods").map do |method|
    skip = false

    # Get our unique id for this particular element
    id = results.add_element(suite, method)
    suite_id = results.elements[suite.to_s]

    # If we're using failed fast mode and we already failed, just return
    skip = true if @failed_fast || interrupted

    # If we've got match patterns, see if this matches them
    if !match_patterns.empty?
      match = match_patterns.find do |pattern|
        pattern == suite.to_s || pattern == "#{suite.to_s}##{method.to_s}" || pattern == suite_id.to_s || pattern == id
      end

      skip = true unless !match.nil?
    end

    if skip
      nil
    else
      inst = suite.new method
      inst._assertions = 0

      # Print the suite name if needed
      unless @last_suite == suite
        print("\n\n", ::Term::ANSIColor.cyan, suite, ::Term::ANSIColor.reset,
              ::Term::ANSIColor.magenta, " #{suite_id}", ::Term::ANSIColor.reset, "\n")
        @last_suite = suite
      end

      @start_time = Time.now
      result = inst.run self
      time = Time.now - @start_time
      results.add_timing(suite, method, time)

      if result == "."
        result = "P"
        self.passes += 1
      end

      results.failures << id unless result == "P" || result == "S"

      if config.fail_fast && result != "P" && result != "S"
        @failed_fast = true
      end

      output = "#{result} #{method}"

      print COLOR_MAP[result], " #{output}", ::Term::ANSIColor.reset, " #{"%0.6f" % time }s ",
        ::Term::ANSIColor.magenta, "#{id}", ::Term::ANSIColor.reset, "\n"

      if @failed_fast
        print "\n", COLOR_MAP["E"], "Halting tests because of failure.", ::Term::ANSIColor.reset, "\n"
      end

      inst._assertions
    end
  end

  return assertions.select { |n| !n.nil? }.size, assertions.inject(0) { |sum, n| n.nil? ? sum + 0 : sum + n }
end

#puke(klass, meth, e) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/tconsole/minitest_handler.rb', line 210

def puke(klass, meth, e)
  id = results.elements["#{klass}##{meth}"]

  e = case e
      when MiniTest::Skip then
        @skips += 1
        results.skip_count += 1
        ["S", COLOR_MAP["S"] + "Skipped:\n#{klass}##{meth} (#{id})" + ::Term::ANSIColor.reset + " [#{location e}]:\n#{e.message}\n"]
      when MiniTest::Assertion then
        @failures += 1
        results.failure_count += 1
        ["F", COLOR_MAP["F"] + "Failure:\n#{klass}##{meth} (#{id})" + ::Term::ANSIColor.reset + " [#{location e}]:\n#{e.message}\n"]
      else
        @errors += 1
        results.error_count += 1

        filtered_backtrace = Util.filter_backtrace(e.backtrace)
        backtrace_text = MiniTest::filter_backtrace(filtered_backtrace).join "\n    "

        ["E", COLOR_MAP["E"] + "Error:\n#{klass}##{meth} (#{id}):\n" + ::Term::ANSIColor.reset + "#{e.class}: #{e.message}\n    #{backtrace_text}\n"]
      end
  @report << e[1]
  e[0]
end

#status(io = self.output) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/tconsole/minitest_handler.rb', line 111

def status(io = self.output)

  if test_count == 0
    if !match_patterns.empty?
      puts ::Term::ANSIColor.yellow("No tests were executed because no tests matching `#{match_patterns.join(", ")}` were found.")
    else
      puts ::Term::ANSIColor.yellow("No tests were executed.")
    end
  else
    format = "%d tests, %d assertions, "

    format << COLOR_MAP["P"] if passes > 0
    format << "%d passes, "
    format << ::Term::ANSIColor.reset if passes > 0

    format << COLOR_MAP["F"] if failures > 0
    format << "%d failures, "
    format << ::Term::ANSIColor.reset if failures > 0

    format << COLOR_MAP["E"] if errors > 0
    format << "%d errors, "
    format << ::Term::ANSIColor.reset if errors > 0

    format << COLOR_MAP["S"] if skips > 0
    format << "%d skips"
    format << ::Term::ANSIColor.reset if skips > 0

    io.puts format % [test_count, assertion_count, passes, failures, errors, skips]
  end
end