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

Inherits:
Base::Searcher
  • Object
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, #test_files

Instance Method Details

#by_line(file_pattern, line_number) ⇒ Object



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

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
    [{
      file: best_result[:file],
      example_name: best_result[:line].match(/(def\s+(?<name>test_[\w\?]+)|test\s+['"](?<name>.*)['"]\s+do)/)[:name],
      line_number: best_result[:line_number]
    }]
  else
    # line number outside of example. Run whole file
    [{
      file: grep_results.first[:file]
    }]
  end
end