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, pure_regex: false) ⇒ TestMethodRunner

Returns a new instance of TestMethodRunner.



12
13
14
15
16
17
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 12

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

Instance Attribute Details

#regexObject (readonly)

Returns the value of attribute regex.



11
12
13
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 11

def regex
  @regex
end

#repoObject (readonly)

Returns the value of attribute repo.



11
12
13
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 11

def repo
  @repo
end

#shellObject (readonly)

Returns the value of attribute shell.



11
12
13
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 11

def shell
  @shell
end

#test_runnerObject (readonly)

Returns the value of attribute test_runner.



11
12
13
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 11

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

.run_file_with_match(regex, env) ⇒ Object



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

def self.run_file_with_match(regex, env)
  new(env, pure_regex: true).run(regex)
end

Instance Method Details

#find_tests_by_nameObject



49
50
51
52
53
54
55
56
57
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 49

def find_tests_by_name
  test_def_regex = @pure_regex ? 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



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
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 19

def run(regex)
  @regex = regex

  if tests.empty?
    shell.notify "Could not find matching test method."
    return false
  elsif tests.size == 1
    if @pure_regex
      test_runner.run_files(tests)
    else
      test_runner.run_method(tests.first)
    end
  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



45
46
47
# File 'lib/sfb_scripts/test_running/test_method_runner.rb', line 45

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