Skip to content

Commit

Permalink
Use multiple test files on the Runner
Browse files Browse the repository at this point in the history
This change also removes the need for an all_test_runner on the executable
  • Loading branch information
AlexB52 committed Nov 24, 2024
1 parent 18907cc commit 696c452
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 44 deletions.
11 changes: 3 additions & 8 deletions exe/retest
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ command = Retest::Command.for_options(options)
runner = Retest::Runner.new(command)
sounds = Retest::Sounds.for(options)

# All test runner
all_test_command = Retest::Command.for_options(options.merge(%w[--all]), stdout: nil)
all_test_runner = Retest::Runner.new(all_test_command)
all_test_runner.add_observer(sounds)

sounds.play(:start)
runner.add_observer(sounds)
prompt.add_observer(sounds)
Expand Down Expand Up @@ -69,7 +64,7 @@ Retest.listen(options) do |modified, added, removed|
end
$stdout.puts "Ready to refactor! You can make file changes now"

def run_command(input:, program:, all_test_runner:)
def run_command(input:, program:)
case input
when /^file changed:\s(.*)$/
puts "File changed: #{$1}"
Expand All @@ -95,7 +90,7 @@ def run_command(input:, program:, all_test_runner:)
program.run(nil, force_run: true)
when 'ra', 'run all'
puts "Running all tests\n"
all_test_runner.run
program.run_all
when /^di?f?f?\s(.*)$/
program.diff($1)
when 'h', 'help'
Expand Down Expand Up @@ -123,6 +118,6 @@ loop do
readable_connections = ready[0]
readable_connections.each do |conn|
data = conn.readpartial(4096)
run_command(input: data.to_s.chomp, program: program, all_test_runner: all_test_runner)
run_command(input: data.to_s.chomp, program: program)
end
end
2 changes: 1 addition & 1 deletion features/git-ruby/retest/retest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_diffs_from_other_branch
sleep 2

assert_match <<~EXPECTED, @output.read
Tests found:
Tests selected:
- test/created_with_test_file_test.rb
- test/renamed_with_test_file_test.rb
- test/to_be_renamed_test.rb
Expand Down
4 changes: 1 addition & 3 deletions features/rails-app/retest/retest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,10 @@ def test_diffs_from_other_branch

assert_match <<~EXPECTED, @output.read
Setup identified: [RAILS]. Using command: 'bin/rails test <test>'
Tests found:
Tests selected:
- test/controllers/books_controller_test.rb
- test/models/book_test.rb
- test/system/books_test.rb
Running tests...
Test Files Selected: test/controllers/books_controller_test.rb test/models/book_test.rb test/system/books_test.rb
EXPECTED

assert_match <<~EXPECTED, @output.read
Expand Down
4 changes: 4 additions & 0 deletions lib/retest/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def initialize(all: false, file_system: FileSystem, command: nil)
@command = command
end

def clone(params = {})
self.class.new(**{ all: all, file_system: file_system, command: command }.merge(params))
end

def has_changed?
to_s.include?('<changed>')
end
Expand Down
1 change: 1 addition & 0 deletions lib/retest/command/hardcoded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def to_s
end

def format_batch(*files)
raise ArgumentError, "Multiple test files run not supported for '#{to_s}'"
end
end
end
Expand Down
13 changes: 6 additions & 7 deletions lib/retest/program.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ def run(file, force_run: false)
def diff(branch)
raise "Git not installed" unless VersionControl::Git.installed?
test_files = repository.find_tests VersionControl::Git.diff_files(branch)
run_selected(test_files)
end

@stdout.puts "Tests found:"
test_files.each { |test_file| @stdout.puts " - #{test_file}" }

@stdout.puts "Running tests..."
runner.run_all_tests command.format_batch(*test_files)
def run_all
clear_terminal
runner.run_all
end

def run_selected(test_files)
@stdout.puts "Running tests..."
runner.run_all_tests command.format_batch(*test_files)
runner.run(test_files: test_files)
end

def clear_terminal
Expand Down
37 changes: 25 additions & 12 deletions lib/retest/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,33 @@ def initialize(command, stdout: $stdout)
end

def run(changed_files: [], test_files: [])
instruction = command.to_s
instruction = format_changed_files(instruction: instruction, files: changed_files)
instruction = format_test_files(instruction: instruction, files: test_files)

log("\n")
system_run instruction

system_run format_instruction(changed_files: changed_files, test_files: test_files)
rescue FileNotFound => e
log("FileNotFound - #{e.message}")
rescue ArgumentError => e
log("ArgumentError - #{e.message}")
end

def run_all
system_run command.clone(all: true).to_s
end

def format_instruction(changed_files: [], test_files: [])
if test_files.size >=2
instruction = command.clone(all: false).to_s
tests_string = command.format_batch(*test_files)
log("Tests selected:")
test_files.each { |test_file| log(" - #{test_file}") }

instruction.gsub!('<test>', tests_string)
else
instruction = command.to_s
instruction = format_changed_files(instruction: instruction, files: changed_files)
instruction = format_test_files(instruction: instruction, files: test_files)
end

log("\n")
instruction
end

def format_test_files(instruction:, files:)
Expand Down Expand Up @@ -53,11 +71,6 @@ def format_changed_files(instruction:, files:)
instruction.gsub('<changed>', changed_file)
end

def run_all_tests(tests_string)
log("Test Files Selected: #{tests_string}")
system_run command.to_s.gsub('<test>', tests_string)
end

def sync(added:, removed:)
purge_test_file(removed)
end
Expand Down
8 changes: 6 additions & 2 deletions test/retest/command/hardcoded_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ def test_to_s
end

def test_format_with_one_file
assert_nil @subject.format_batch('a/file/path.rb')
assert_raises(ArgumentError) do
@subject.format_batch('a/file/path.rb')
end
end

def test_format_with_multiple_files
assert_nil @subject.format_batch('a/file/path.rb', 'another/file/path.rb')
assert_raises(ArgumentError) do
@subject.format_batch('a/file/path.rb', 'another/file/path.rb')
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion test/retest/runner/runner_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module RunnerInterfaceTest
def test_behaviour
assert_respond_to @subject, :==
assert_respond_to @subject, :run
assert_respond_to @subject, :run_all_tests
assert_respond_to @subject, :sync
end

Expand Down
19 changes: 9 additions & 10 deletions test/retest/runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,15 @@ def test_returns_last_command
assert_match "touch file_path_test.rb", out
end

def test_run_all_tests
out, _ = capture_subprocess_io { @subject.run_all_tests('file_path.rb file_path_two.rb') }

assert_equal(<<~EXPECATIONS, @subject.stdout.string)
Test Files Selected: file_path.rb file_path_two.rb
EXPECATIONS

assert_equal(<<~EXPECATIONS, out)
touch file_path.rb file_path_two.rb
EXPECATIONS
def test_run_multiple_tests
assert_raises(ArgumentError) do
@subject.command = @command
@subject.format_instruction(test_files: ['file_path.rb', 'file_path_two.rb'])
end

@subject.command = Command::Rails.new(all: false)
instruction = @subject.format_instruction(test_files: ['file_path.rb', 'file_path_two.rb'])
assert_equal 'bundle exec rails test file_path.rb file_path_two.rb', instruction
end
end
end
Expand Down

0 comments on commit 696c452

Please sign in to comment.