Class: TestRunner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ TestRunner

Returns a new instance of TestRunner.



4
5
6
7
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 4

def initialize(env)
  @shell = env[:shell]
  @all_engines_param = env[:all_engines]
end

Instance Attribute Details

#all_engines_paramObject (readonly)

Returns the value of attribute all_engines_param.



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

def all_engines_param
  @all_engines_param
end

#shellObject (readonly)

Returns the value of attribute shell.



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

def shell
  @shell
end

Instance Method Details

#named_test_runner(working_dir) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 70

def named_test_runner(working_dir)
  if test_runner_type(working_dir) == :spring
    # hack:
    # Add some options for using spring.
    #
    #"bin/testunit"
    "ruby -I test"
  else
    "ruby -I test"
  end
end

#run(tests) ⇒ Object

this could use some love



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 10

def run(tests)
  if tests.is_one_test_method?
    run_method(tests.first)
  elsif tests.in_one_file? && tests.all? {|t| t.is_method? }
    shell.notify "Multiple matches in same file. Running that file."
    run_files(tests)
  elsif tests.in_one_file?
    shell.notify "Matched one file."
    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."
    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

#run_across_engines(tests) ⇒ Object



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

def run_across_engines(tests)
  shell.notify "\nfinding test runners"
  tests.working_dirs.each do |engine_dir|
    test_files = tests.relative_paths_in(engine_dir).join(' ')
    test_runner = test_collection_runner(engine_dir)

    shell.enqueue("#{test_runner} #{test_files}", dir: engine_dir)
  end
  shell.exec_queue
end

#run_across_engines?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 91

def run_across_engines?
  all_engines_param || shell.confirm?("Test files are in multiple engines.  Run them all?")
end

#run_files(tests) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 36

def run_files(tests)
  begin
    test_runner = test_collection_runner(tests.working_dir)
    test_files = tests.relative_paths.join(' ')

    shell.exec("#{test_runner} #{test_files}", dir: tests.working_dir)
  rescue TestCollection::MultipleWorkingDirectoriesError => e
    if run_across_engines?
      run_across_engines(tests)
    end
  end
end

#run_method(test) ⇒ Object



30
31
32
33
34
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 30

def run_method(test)
  test_runner = named_test_runner(test.working_dir)

  shell.exec("#{test_runner} #{test.relative_path} --name=#{test.test_name}", dir: test.working_dir)
end

#test_collection_runner(working_dir) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 82

def test_collection_runner(working_dir)
  if test_runner_type(working_dir) == :spring
    #"bin/testunit"
    %{ruby -I test -e 'ARGV.each { |file| require(Dir.pwd + "/" + file) }'}
  else
    %{ruby -I test -e 'ARGV.each { |file| require(Dir.pwd + "/" + file) }'}
  end
end

#test_runner_type(working_dir) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 60

def test_runner_type(working_dir)
  if shell.run("ls bin", dir: working_dir).split("\n").include? 'testunit'
    :spring
  else
    :ruby
  end
rescue ShellRunner::CommandFailureError
  :ruby
end