Module: Minitest::Allow

Defined in:
lib/minitest/allow_plugin.rb

Constant Summary collapse

VERSION =
"1.2.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#allowObject

Returns the value of attribute allow.



46
47
48
# File 'lib/minitest/allow_plugin.rb', line 46

def allow
  @allow
end

#allow_saveObject

Returns the value of attribute allow_save.



46
47
48
# File 'lib/minitest/allow_plugin.rb', line 46

def allow_save
  @allow_save
end

#allow_seenObject

Returns the value of attribute allow_seen.



46
47
48
# File 'lib/minitest/allow_plugin.rb', line 46

def allow_seen
  @allow_seen
end

Instance Method Details

#allow_resultsObject



53
54
55
56
57
# File 'lib/minitest/allow_plugin.rb', line 53

def allow_results
  self.reporters
    .grep(Minitest::StatisticsReporter)
    .map(&:results)
end

#filter_allowObject

Test runs call record, we put everything ran in allow_seen. This means allow_seen has everything RAN, regardless of pass/fail We use this intersected with the allowed file to determine what to report on… if it wasn’t seen, we don’t say add/remove at all.

‘allow` is all the stuff from the allow file. Test names and regexps that excuse failures.

‘allow_results` is an array of arrays of results: [[result, …], …]

When the run is done, we want to tell the user what tests to remove or add from the allowed list:

  • If a test passed that is in the allowed list, it should be removed.

  • If a test failed that is NOT in the allowed list, it should be added.

  • If a test failed that is matched by a regexp, it’s NAME should be removed if it is listed.



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/minitest/allow_plugin.rb', line 87

def filter_allow
  # 1. split allow into strings and regexps
  allowed_REs, allowed_names = allow.partition { |a| Regexp === a }

  allowed_names = allowed_names.map { |x| [x, x] }.to_h

  # 2. remove items from allow_results whose full_name matches the strings
  # 3. remove items from allow_results whose message matches the regexps
  # 4. remove items from allow_results whose full_name matches the regexps?

  hit = {}
  allow_results = self.allow_results
  allow_results.each do |results|
    results.delete_if { |r|
      name = r.full_name

      by_name = allowed_names[name]
      by_regx = allowed_REs.find { |re| r.failure.message =~ re || name =~ re }

      # this will add the name as bad unless hit by regexp as well
      # if hit by regex, then we want to report it as "good" so
      # the name gets removed from the allow list:

      # the same goes for when the test is listed as bad but is now skipped:

      hit[name] = true if by_name && !by_regx && !r.skipped?

      by_name || by_regx
    }
  end

  # 5. remove string and regexps that matched any of the above from allow
  self.allow -= hit.keys

  errored, failed = allow_results
    .flatten
    .reject(&:skipped?)
    .partition { |t| Minitest::UnexpectedError === t.failure }

  failed = failed.map(&:full_name)
  errors = Hash.new { |h,k| h[k] = [] }

  errored.each do |t|
    msg = t.failure.message.lines.first.chomp.gsub(/0x\h+/, "0xHHHH")

    errors[Regexp.new(Regexp.escape(msg))] << t.full_name
  end

  extra_bad = failed.uniq
  extra_bad << errors.transform_values(&:uniq) unless errors.empty?

  # 6. report new failures including regular expressions for errors

  unless extra_bad.empty? then
    io.puts
    io.puts "Bad tests that are NOT allowed:"
    Psych.dump extra_bad, io, line_width:-1
    io.puts
  end
end

#passed?Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
# File 'lib/minitest/allow_plugin.rb', line 159

def passed?
  write_allow        if allow_save
  filter_allow       if allow
  report_extra_allow if allow

  super # CompositeReporter#passed?
end

#record(result) ⇒ Object



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

def record result
  allow_seen << result.full_name
  super
end

#report_extra_allowObject



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

def report_extra_allow
  good = allow & allow_seen

  unless good.empty? then
    io.puts
    io.puts "Excluded tests that now pass:"
    Psych.dump good, io, line_width:-1
    io.puts
  end
end

#write_allowObject



59
60
61
62
63
64
65
66
67
# File 'lib/minitest/allow_plugin.rb', line 59

def write_allow
  data = allow_results
    .flatten
    .map(&:full_name)
    .uniq
    .sort

  File.write allow_save, Psych.dump(data, line_width:-1)
end