Skip to content

Commit b3adbcd

Browse files
committed
Use only one runner
1 parent 0235e6f commit b3adbcd

File tree

24 files changed

+291
-517
lines changed

24 files changed

+291
-517
lines changed

exe/retest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ end
2626
prompt = Retest::Prompt.new
2727
repository = Retest::Repository.new(files: Retest::VersionControl.files, prompt: prompt)
2828
command = Retest::Command.for_options(options)
29-
runner = Retest::Runners.runner_for(command)
29+
runner = Retest::Runner.new(command)
3030
sounds = Retest::Sounds.for(options)
3131

3232
# All test runner
3333
all_test_command = Retest::Command.for_options(options.merge(%w[--all]), stdout: nil)
34-
all_test_runner = Retest::Runners.runner_for(all_test_command.to_s)
34+
all_test_runner = Retest::Runner.new(all_test_command)
3535
all_test_runner.add_observer(sounds)
3636

3737
sounds.play(:start)

features/bundler-app/retest/retest_test/file_changes_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def test_creating_a_new_file
5050

5151
create_file 'lib/bundler_app/foo.rb'
5252
assert_match <<~EXPECTED, @output.read
53-
404 - Test File Not Found
54-
Retest could not find a matching test file to run.
53+
FileNotFound - Retest could not find a matching test file to run.
5554
EXPECTED
5655

5756
create_file 'test/bundler_app/test_foo.rb'

features/rspec-ruby/retest/retest_test/file_changes_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def test_creating_a_new_file
5050

5151
create_file 'foo.rb'
5252
assert_match <<~EXPECTED, @output.read
53-
404 - Test File Not Found
54-
Retest could not find a matching test file to run.
53+
FileNotFound - Retest could not find a matching test file to run.
5554
EXPECTED
5655

5756
create_file 'foo_spec.rb'

features/ruby-app/retest/retest_test/file_changes_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def test_creating_a_new_file
5050

5151
create_file 'foo.rb'
5252
assert_match <<~EXPECTED, @output.read
53-
404 - Test File Not Found
54-
Retest could not find a matching test file to run.
53+
FileNotFound - Retest could not find a matching test file to run.
5554
EXPECTED
5655

5756
create_file 'foo_test.rb'

features/ruby-bare/retest/retest_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ def test_creating_a_new_file
7474

7575
create_file 'foo.rb'
7676
assert_match <<~EXPECTED, @output.read
77-
404 - Test File Not Found
78-
Retest could not find a matching test file to run.
77+
FileNotFound - Retest could not find a matching test file to run.
7978
EXPECTED
8079

8180
create_file 'foo_test.rb'

features/ruby-bare/retest/scenarios/force_polling.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def test_creating_a_new_file
5050

5151
create_file 'foo.rb'
5252
assert_match <<~EXPECTED, @output.read
53-
404 - Test File Not Found
54-
Retest could not find a matching test file to run.
53+
FileNotFound - Retest could not find a matching test file to run.
5554
EXPECTED
5655

5756
create_file 'foo_test.rb'

lib/retest.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require 'observer'
55

66
require "retest/version"
7-
require "retest/runners"
7+
require "retest/runner"
88
require "retest/repository"
99
require "retest/matching_options"
1010
require "retest/options"

lib/retest/runner.rb

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
require_relative "runner/cached_test_file"
2+
3+
module Retest
4+
class Runner
5+
include Observable
6+
include CachedTestFile
7+
8+
attr_accessor :command, :stdout
9+
def initialize(command, stdout: $stdout)
10+
@stdout = stdout
11+
@command = command
12+
end
13+
14+
def ==(obj)
15+
return false unless obj.command
16+
17+
command.to_s == obj.command.to_s && self.class == obj.class
18+
end
19+
20+
def run(changed_files: [], test_files: [])
21+
changed_file = changed_files.is_a?(Array) ? changed_files.first : changed_files
22+
test_file = test_files.is_a?(Array) ? test_files.first : test_files
23+
24+
self.cached_test_file = test_file
25+
26+
if command.to_s.include?('<changed>')
27+
if changed_file.nil?
28+
print_changed_file_not_found
29+
return
30+
end
31+
log("Changed File Selected: #{changed_file}")
32+
end
33+
34+
if command.to_s.include?('<test>')
35+
if cached_test_file.nil?
36+
print_test_file_not_found
37+
return
38+
end
39+
log("Test File Selected: #{cached_test_file}")
40+
end
41+
42+
system_run command.to_s
43+
.gsub('<test>', cached_test_file.to_s)
44+
.gsub('<changed>', changed_file.to_s)
45+
end
46+
47+
def run_all_tests(tests_string)
48+
log("Test Files Selected: #{tests_string}")
49+
system_run command.to_s.gsub('<test>', tests_string)
50+
end
51+
52+
def sync(added:, removed:)
53+
purge_test_file(removed)
54+
end
55+
56+
def running?
57+
@running
58+
end
59+
60+
private
61+
62+
def system_run(command)
63+
@running = true
64+
result = system(command) ? :tests_pass : :tests_fail
65+
changed
66+
notify_observers(result)
67+
@running = false
68+
end
69+
70+
def log(message)
71+
stdout.puts(message)
72+
end
73+
74+
def print_changed_file_not_found
75+
log(<<~ERROR)
76+
FileNotFound - Retest could not find a changed file to run.
77+
ERROR
78+
end
79+
80+
def print_test_file_not_found
81+
log(<<~ERROR)
82+
FileNotFound - Retest could not find a matching test file to run.
83+
ERROR
84+
end
85+
end
86+
end

lib/retest/runner/cached_test_file.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Retest
2+
module CachedTestFile
3+
def cached_test_file
4+
@cached_test_file
5+
end
6+
7+
def cached_test_file=(value)
8+
@cached_test_file = value || @cached_test_file
9+
end
10+
11+
def purge_test_file(purged)
12+
return if purged.empty?
13+
14+
if purged.is_a?(Array) && purged.include?(cached_test_file)
15+
@cached_test_file = nil
16+
elsif purged.is_a?(String) && purged == cached_test_file
17+
@cached_test_file = nil
18+
end
19+
end
20+
end
21+
end

lib/retest/runners.rb

Lines changed: 0 additions & 23 deletions
This file was deleted.

lib/retest/runners/cached_test_file.rb

Lines changed: 0 additions & 23 deletions
This file was deleted.

lib/retest/runners/change_runner.rb

Lines changed: 0 additions & 24 deletions
This file was deleted.

lib/retest/runners/runner.rb

Lines changed: 0 additions & 48 deletions
This file was deleted.

lib/retest/runners/test_runner.rb

Lines changed: 0 additions & 42 deletions
This file was deleted.

lib/retest/runners/variable_runner.rb

Lines changed: 0 additions & 42 deletions
This file was deleted.

test/retest/runners/observable_runner.rb renamed to test/retest/runner/observable_runner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Retest
2-
module Runners
2+
class Runner
33
module OversableRunnerTests
44
def test_publishes_event_after_running_command
55
observer = MiniTest::Mock.new

0 commit comments

Comments
 (0)