aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/test_unit/reporter.rb
blob: b97d6f86f28d8778348ab94a82ae479db1f6aaf3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
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
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
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
110
111
112
113
114
115
116
require "active_support/core_ext/class/attribute"
require "minitest"

module Rails
  class TestUnitReporter < Minitest::StatisticsReporter
    class_attribute :executable
    self.executable = "bin/rails test"

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

    COLOR_CODES = {
      red: 31,
      green: 32,
      yellow: 33
    }

    def record(result)
      super
      color = COLOR_CODES_FOR_RESULTS[result.result_code]

      if options[:verbose]
        io.puts color_output(format_line(result), color)
      else
        io.print color_output(result.result_code, color)
      end

      if output_inline? && result.failure && (!result.skipped? || options[:verbose])
        io.puts
        io.puts
        io.puts format_failures(result).map { |line| color_output(line, color) }
        io.puts
        io.puts format_rerun_snippet(result)
        io.puts
      end

      if fail_fast? && result.failure && !result.error? && !result.skipped?
        raise Interrupt
      end
    end

    def report
      return if output_inline? || filtered_results.empty?
      io.puts
      io.puts "Failed tests:"
      io.puts
      io.puts aggregated_results
    end

    def aggregated_results # :nodoc:
      filtered_results.map { |result| format_rerun_snippet(result) }.join "\n"
    end

    def filtered_results
      if options[:verbose]
        results
      else
        results.reject(&:skipped?)
      end
    end

    def relative_path_for(file)
      file.sub(/^#{app_root}\/?/, '')
    end

    private
      def output_inline?
        options[:output_inline]
      end

      def fail_fast?
        options[:fail_fast]
      end

      def format_line(result)
        "%s#%s = %.2f s = %s" % [result.class, result.name, result.time, result.result_code]
      end

      def format_failures(result)
        result.failures.map do |failure|
          "#{failure.result_label}:\n#{result.class}##{result.name}:\n#{failure.message}\n"
        end
      end

      def format_rerun_snippet(result)
        # Try to extract path to assertion from backtrace.
        if result.location =~ /\[(.*)\]\z/
          assertion_path = $1
        else
          assertion_path = result.method(result.name).source_location.join(':')
        end

        "#{self.executable} #{relative_path_for(assertion_path)}"
      end

      def app_root
        @app_root ||= defined?(ENGINE_ROOT) ? ENGINE_ROOT : Rails.root
      end

      def colored_output?
        options[:color] && io.respond_to?(:tty?) && io.tty?
      end

      def color_output(string, color)
        if colored_output?
          "\e[#{COLOR_CODES[color]}m#{string}\e[0m"
        else
          string
        end
      end
  end
end