Class: TestLauncher::Frameworks::Minitest::Searcher

Inherits:
Base::Searcher show all
Defined in:
lib/test_launcher/frameworks/minitest.rb

Constant Summary collapse

MultipleByLineMatches =
Class.new(BaseError)

Instance Attribute Summary

Attributes inherited from Base::Searcher

#raw_searcher

Instance Method Summary collapse

Methods inherited from Base::Searcher

#examples, #grep, #initialize, #test_files

Constructor Details

This class inherits a constructor from TestLauncher::Frameworks::Base::Searcher

Instance Method Details

#by_line(file_pattern, line_number) ⇒ Object



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
# File 'lib/test_launcher/frameworks/minitest.rb', line 27

def (file_pattern, line_number)
  files = test_files(file_pattern)
  return [] unless files.any?
  raise multiple_files_error if files.size > 1
  #
  file = files.first
  grep_results = raw_searcher.grep(example_name_regex, file_pattern: file)

  if grep_results.empty?
    # the file exists, but doesn't appear to contain any tests...
    # we'll try to run it anyway
    return [file: file]
  end

  best_result =
    grep_results
      .select {|r| line_number >= r[:line_number]}
      .min_by {|r| line_number - r[:line_number]}

  if best_result
    example_name =
      if match = best_result[:line].match(/def\s+(?<name>test_[\w\?]+)/)
        match[:name]
      elsif match = best_result[:line].match(/test\s+['"](?<name>.*)['"]\s+do/)
        "test_#{match[:name]}"
      end

    [{
      file: best_result[:file],
      example_name: example_name,
      line_number: best_result[:line_number]
    }]
  else
    # line number outside of example. Run whole file
    [{
      file: grep_results.first[:file]
    }]
  end
end