class TestLauncher::Frameworks::Minitest::Searcher

Constants

MultipleByLineMatches

Public Instance Methods

by_line(file_pattern, line_number) click to toggle source
# File lib/test_launcher/frameworks/minitest.rb, line 28
def by_line(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

Private Instance Methods

example_name_regex(query="") click to toggle source
# File lib/test_launcher/frameworks/minitest.rb, line 78
def example_name_regex(query="")
  if query.match(/^test_/)
    "^\s*def\s+(#{query}).*"
  else
    "^\s*(def\s+test_|test\s+['\"]).*(#{query}).*"
  end
end
file_name_pattern() click to toggle source
# File lib/test_launcher/frameworks/minitest.rb, line 74
def file_name_pattern
  "*_test.rb"
end
file_name_regex() click to toggle source
# File lib/test_launcher/frameworks/minitest.rb, line 70
def file_name_regex
  /.*_test\.rb$/
end
multiple_files_error() click to toggle source
# File lib/test_launcher/frameworks/minitest.rb, line 86
        def multiple_files_error
          MultipleByLineMatches.new(<<-MSG)
It looks like you are running a line number in a test file.

Multiple files have been found that match your query.

This case is not supported for Minitest.

Open an issue on https://github.com/petekinnecom/test_launcher if this is something you have run into at least 3 times. :)
          MSG
        end