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
8
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 4

def initialize(env)
  @shell = env[:shell]
  @all_engines_param = env[:all_engines]
  @always_ask_before_grouping_tests = env[:always_ask_before_grouping_tests]
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

#always_ask_before_grouping_testsObject (readonly)

Returns the value of attribute always_ask_before_grouping_tests.



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

def always_ask_before_grouping_tests
  @always_ask_before_grouping_tests
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

#ask_user_which_tests_to_run(tests) ⇒ Object



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

def ask_user_which_tests_to_run(tests)
    shell.warn 'Found too many tests. Please choose which matches you would like to run:'

    tests.uniq!.each_with_index do |t, index| 
      shell.notify "(#{index}) #{t.full_path}: #{t.test_name}" 
    end

    tests_to_run = shell.get_number_list_for_question('Please enter the match numbers you would like to run(comma seperated)')

    new_tests = tests_to_run.map do |index|
      tests[index]
    end
    @always_ask_before_grouping_tests = false
    run(TestCollection.new(new_tests))
end

#named_test_runner(working_dir) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 96

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

Hack: this could use *a lot* of love this could use some love



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

def run(tests)

  if tests.empty?
    shell.warn "Unable to identify any tests."
    exit

  elsif tests.is_one_test_method?
    test = tests.first
    run_method(path: test.relative_path, name: test.test_name, dir: test.working_dir)
  elsif always_ask_before_grouping_tests
    ask_user_which_tests_to_run(tests)
  elsif tests.in_one_file? && tests.all? {|t| t.is_method? }
    shell.notify "Multiple matches in same file. Running those tests"
    test = tests.first
    run_method(path: test.relative_path, name: tests.query, dir: test.working_dir)

  elsif tests.in_one_file? #catches regex that matched a test file or other regex that matched in the body
    shell.notify "Matched one file. Running that 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
    ask_user_which_tests_to_run(tests)
  end
end

#run_across_engines(tests) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 75

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)


117
118
119
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 117

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

#run_files(tests) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 62

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(path:, name:, dir:) ⇒ Object



41
42
43
44
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 41

def run_method(path:, name:, dir:)
  test_runner = named_test_runner(dir)
  shell.exec("#{test_runner} #{path} --name=/#{name}/", dir: dir)
end

#test_collection_runner(working_dir) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 108

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



86
87
88
89
90
91
92
93
94
# File 'lib/sfb_scripts/test_running/test_runner.rb', line 86

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