Class: M::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/m/executor.rb

Instance Method Summary collapse

Constructor Details

#initialize(testable) ⇒ Executor

Returns a new instance of Executor.



9
10
11
# File 'lib/m/executor.rb', line 9

def initialize testable
  @testable = testable
end

Instance Method Details

#executeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/m/executor.rb', line 13

def execute
  # Locate tests to run that may be inside of this line. There could be more than one!
  tests_to_run = tests.within testable.lines

  # If we found any tests,
  if tests_to_run.size.positive?
    # assemble the regexp to run these tests,
    test_names = tests_to_run.map { |test| Regexp.escape(test.name) }.join("|")

    # set up the args needed for the runner
    test_arguments = ["-n", "/^(#{test_names})$/"]

    # directly run the tests from here and exit with the status of the tests passing or failing
    runner.run test_arguments + testable.passthrough_options
  elsif tests.size.positive?
    # Otherwise we found no tests on this line, so you need to pick one.
    message = "No tests found on line #{testable.lines.join ", "}. Valid tests to run:\n\n"

    # For every test ordered by line number,
    # spit out the test name and line number where it starts,
    tests. do |test|
      message << "#{format "%0#{tests.column_size}s", test.name}: m #{testable.file}:#{test.start_line}\n"
    end

    # Spit out helpful message and bail
    warn message
    false
  else
    # There were no tests at all
    message = "There were no tests found.\n\n"
    warn message
    false
  end
end