class Minitest::PathExpander
Minitest’s PathExpander to find and filter tests.
Constants
- TM
-
Simple TestMethod (abbr
TM) Data object.
Public Instance Methods
Source
# File lib/minitest/path_expander.rb, line 307 def all_tests Minitest.seed = 42 # minor hack to deal with runnable_methods shuffling Minitest::Runnable.runnables .to_h { |k| ms = k.runnable_methods .sort .map { |m| TM.new k, m.to_sym } .sort_by { |t| [t.path, t.line_s] } [k, ms] } .reject { |k, v| v.empty? } end
Find and return all known tests as a hash of klass => [TM…] pairs.
Source
# File lib/minitest/path_expander.rb, line 355 def handle_missing_tests? tests _tests = tests.values.flatten not_found = by_line .flat_map { |f, ls| ls.map { |l| [f, l] } } .reject { |f, l| _tests.any? { |t| t.path == f and t.include? l } } unless not_found.empty? then by_path = all_tests.values.flatten.group_by(&:path) puts puts "ERROR: test(s) not found at:" not_found.each do |f, l| puts " %s:%s" % [f, l] puts puts "Did you mean?" puts l = l.begin if l.is_a? Range by_path[f] and by_path[f] .sort_by { |m| (m.line_s - l).abs } .first(2) .each do |m| puts " %-30s (dist=%+d) (%s)" % [m, m.line_s - l, m.name] end puts end $stdout.flush $stderr.flush true end end
Handle the case where a line number doesn’t match any known tests. Returns true to signal that running should stop.
Source
# File lib/minitest/path_expander.rb, line 292 def post_process return if by_line.empty? tests = tests_by_class exit! 1 if handle_missing_tests? tests test_res = tests_to_regexp tests self.args << "-n" << "/#{test_res.join "|"}/" end
Add additional arguments to args to handle path:line argument filtering
Source
# File lib/minitest/path_expander.rb, line 274 def process_flags flags flags.reject { |flag| # all hits are truthy, so this works out well case flag when /^-I(.*)/ then $LOAD_PATH.prepend(*$1.split(/:/)) when /^-d/ then $DEBUG = true when /^-w/ then $VERBOSE = true else false end } end
Overrides PathExpander#process_flags to filter out ruby flags from minitest flags. Only supports -I<paths>, -d, and -w for ruby.
Source
# File lib/minitest/path_expander.rb, line 323 def tests_by_class all_tests .transform_values { |ms| ms.select { |m| bl = by_line[m.path] not bl or bl.any? { |l| m.include? l } } } .reject { |k, v| v.empty? } end
Returns a hash mapping Minitest runnable classes to TMs
Source
# File lib/minitest/path_expander.rb, line 338 def tests_to_regexp tests tests # { k1 => [Test(a), ...} .transform_values { |tms| tms.map(&:name) } # { k1 => %w[a, b], ...} .map { |k, ns| # [ "k1#(?:a|b)", "k2#c", ...] if ns.size > 1 then ns.map! { |n| Regexp.escape n } "%s#\(?:%s\)" % [Regexp.escape(k.name), ns.join("|")] else "%s#%s" % [Regexp.escape(k.name), ns.first] end } end
Converts tests to an array of “klass#(methods+)” regexps to be used for test selection.