Skip to content

Commit

Permalink
Set Runner last command on initialization (#241)
Browse files Browse the repository at this point in the history
* Sets runner's last command when command is runnable
* Add feature specs
  • Loading branch information
AlexB52 authored Jan 4, 2025
1 parent f2696e4 commit cc75531
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 9 deletions.
10 changes: 10 additions & 0 deletions features/rails-app/retest/retest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def test_start_retest
Launching Retest...
Ready to refactor! You can make file changes now
EXPECTED

write_input("\n") # Trigger last command when no command was run

assert_output_matches <<~EXPECTED
Error - Not enough information to run a command. Please trigger a run first.
EXPECTED
end

def test_modify_a_file
Expand Down Expand Up @@ -53,6 +59,10 @@ def test_start_retest
Launching Retest...
Ready to refactor! You can make file changes now
EXPECTED

write_input("\n") # Trigger all command when no command was run

assert_output_matches "8 runs, 10 assertions, 0 failures, 0 errors, 0 skips"
end

def test_modify_a_file
Expand Down
10 changes: 10 additions & 0 deletions features/rspec-rails/retest/retest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def test_start_retest
Launching Retest...
Ready to refactor! You can make file changes now
EXPECTED

write_input("\n") # Trigger last command when no command was run

assert_output_matches <<~EXPECTED
Error - Not enough information to run a command. Please trigger a run first.
EXPECTED
end

def test_modify_a_file
Expand Down Expand Up @@ -53,6 +59,10 @@ def test_start_retest
Launching Retest...
Ready to refactor! You can make file changes now
EXPECTED

write_input("\n") # Trigger all command when no command was run

assert_output_matches "9 examples, 0 failures"
end

def test_modify_a_file
Expand Down
6 changes: 6 additions & 0 deletions features/ruby-app/retest/retest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def test_start_retest
Launching Retest...
Ready to refactor! You can make file changes now
EXPECTED

write_input("\n") # Trigger last command when no command was run

assert_output_matches <<~EXPECTED
Error - Not enough information to run a command. Please trigger a run first.
EXPECTED
end
end

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 @@ -13,6 +13,10 @@ def clone(params = {})
self.class.new(**{ all: all, file_system: file_system, command: command }.merge(params))
end

def hardcoded?
!has_changed? && !has_test?
end

def has_changed?
to_s.include?('<changed>')
end
Expand Down
17 changes: 9 additions & 8 deletions lib/retest/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ class Runner
def initialize(command, stdout: $stdout)
@stdout = stdout
@command = command
if command.hardcoded?
self.last_command = command.to_s
end
end

def run_last_command
unless last_command
return log('Error - Not enough information to run a command. Please trigger a run first.')
end

system_run last_command
end

def run(changed_files: [], test_files: [])
self.last_command = format_instruction(changed_files: changed_files, test_files: test_files)
system_run last_command
run_last_command
rescue FileNotFound => e
log("FileNotFound - #{e.message}")
rescue Command::MultipleTestsNotSupported => e
Expand All @@ -29,7 +36,7 @@ def run(changed_files: [], test_files: [])

def run_all
self.last_command = command.clone(all: true).to_s
system_run last_command
run_last_command
end

def format_instruction(changed_files: [], test_files: [])
Expand Down Expand Up @@ -77,12 +84,6 @@ def sync(added:, removed:)

private

def print_test_file_not_found
log(<<~ERROR)
FileNotFound - Retest could not find a matching test file to run.
ERROR
end

def system_run(command)
log("\n")
result = system(command) ? :tests_pass : :tests_fail
Expand Down
48 changes: 47 additions & 1 deletion test/retest/runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ def observable_act(subject)
class RunnerTest < MiniTest::Test
def setup
@command = Command::Hardcoded.new(command: "echo 'hello world'")
@subject = Runner.new(@command)
@subject = Runner.new(@command, stdout: StringIO.new)
end

def output
@subject.stdout.string
end

def test_run
Expand Down Expand Up @@ -52,6 +56,15 @@ def test_sync_files
@subject.sync(added: 'a.rb', removed:'file_path_test.rb')
assert_nil @subject.cached_test_file
end

def test_initializes_last_command_correctly
assert_equal "echo 'hello world'", @subject.last_command

out, _ = capture_subprocess_io { @subject.run_last_command }

assert_match "hello world", out
assert_equal "\n", output
end
end

class VariableRunnerTest < MiniTest::Test
Expand Down Expand Up @@ -98,6 +111,17 @@ def test_returns_last_command

assert_match "another_file_path.rb & file_path_test.rb", out
end

def test_initializes_last_command_correctly
assert_nil @subject.last_command

out, _ = capture_subprocess_io { @subject.run_last_command }

assert_equal '', out
assert_equal(<<~EXPECTED, output)
Error - Not enough information to run a command. Please trigger a run first.
EXPECTED
end
end

class ChangeRunnerTest < MiniTest::Test
Expand All @@ -123,6 +147,17 @@ def test_run_with_a_file_found

assert_match "file_path.rb", out
end

def test_initializes_last_command_correctly
assert_nil @subject.last_command

out, _ = capture_subprocess_io { @subject.run_last_command }

assert_equal '', out
assert_equal(<<~EXPECTED, output)
Error - Not enough information to run a command. Please trigger a run first.
EXPECTED
end
end

class TestRunnerTest < MiniTest::Test
Expand Down Expand Up @@ -159,6 +194,17 @@ def test_returns_last_command
assert_match "touch file_path_test.rb", out
end

def test_initializes_last_command_correctly
assert_nil @subject.last_command

out, _ = capture_subprocess_io { @subject.run_last_command }

assert_equal '', out
assert_equal(<<~EXPECTED, output)
Error - Not enough information to run a command. Please trigger a run first.
EXPECTED
end

def test_run_multiple_tests
assert_raises(Command::MultipleTestsNotSupported) do
@subject.command = @command
Expand Down

0 comments on commit cc75531

Please sign in to comment.