Class: TestMethodRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/sfb_scripts/test_running/test_method_runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ TestMethodRunner

Returns a new instance of TestMethodRunner.



8
9
10
11
12
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 8

def initialize(env)
  @repo = env[:repo]
  @shell = env[:shell]
  @test_runner = env[:test_runner]
end

Instance Attribute Details

#regexObject (readonly)

Returns the value of attribute regex.



7
8
9
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 7

def regex
  @regex
end

#repoObject (readonly)

Returns the value of attribute repo.



7
8
9
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 7

def repo
  @repo
end

#shellObject (readonly)

Returns the value of attribute shell.



7
8
9
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 7

def shell
  @shell
end

#test_runnerObject (readonly)

Returns the value of attribute test_runner.



7
8
9
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 7

def test_runner
  @test_runner
end

Class Method Details

.run(regex, env) ⇒ Object



3
4
5
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 3

def self.run(regex, env)
  new(env).run(regex)
end

Instance Method Details

#find_tests_by_nameObject



40
41
42
43
44
45
46
47
48
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 40

def find_tests_by_name
  test_def_regex = "^\s*def .*#{regex}.*"
  begin
    return repo.grep(test_def_regex, file_pattern: '*_test.rb')
  rescue ShellRunner::CommandFailureError
    # git grep exits with 1 if no results
    return []
  end
end

#run(regex) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 14

def run(regex)
  @regex = regex

  if tests.empty?
    shell.notify "Could not find matching test method."
    return false
  elsif tests.size == 1
    test_runner.run_method(tests.first)
  elsif tests.in_one_file?
    shell.notify "Multiple matches in same file. Running that file."
    test_runner.run_files(tests)
  elsif tests.in_one_engine? && tests.full_paths.size < 4 # hack: maybe should ask here?
    shell.notify "Multiple matches across files in same engine. Running those files."
    test_runner.run_files(tests)
  else
    shell.warn 'Found too many tests:'
    tests[0..10].each {|t| shell.notify "#{t.full_path}: #{t.test_name}" }
    shell.notify '...'
    exit
  end
end

#testsObject



36
37
38
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 36

def tests
  @test_collection ||= TestCollection.new(find_tests_by_name)
end