Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  * Cleanup runner and commands
  * Remove all_test_runner
  * Refactor format batch of Ruby and Rake
  • Loading branch information
AlexB52 committed Nov 24, 2024
1 parent 2b58ccf commit 7db3766
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 144 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
1 change: 1 addition & 0 deletions lib/retest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

module Retest
class Error < StandardError; end
class FileNotFound < StandardError; end

def self.listen(options, listener: Listen)
listener.to('.', only: options.extension, relative: true, force_polling: options.force_polling?) do |modified, added, removed|
Expand Down
26 changes: 20 additions & 6 deletions lib/retest/command/base.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
module Retest
class Command
class MultipleTestsNotSupported < StandardError; end

class Base
def initialize(all: false, file_system: FileSystem, command: nil)
@file_system = file_system
@all = all
@command = command
end

def changed_type?
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

def test_type?
def has_test?
to_s.include?('<test>')
end

def changed_type?
!has_test? && has_changed?
end

def test_type?
has_test? && !has_changed?
end

def variable_type?
test_type? && changed_type?
has_test? && has_changed?
end

def hardcoded_type?
!test_type? && !changed_type?
!has_test? && !has_changed?
end

def to_s
raise NotImplementedError
@command
end

def format_batch(*files)
raise NotImplementedError
raise MultipleTestsNotSupported, "Multiple test files run not supported for '#{to_s}'"
end

private
Expand Down
9 changes: 1 addition & 8 deletions lib/retest/command/hardcoded.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
module Retest
class Command
class Hardcoded < Base
def to_s
@command
end

def format_batch(*files)
end
end
class Hardcoded < Base; end
end
end
15 changes: 10 additions & 5 deletions lib/retest/command/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ module Retest
class Command
class Rails < Base
def to_s
return "#{root_command} <test>" unless all
root_command
if all
root_command
else
"#{root_command} <test>"
end
end

def format_batch(*files)
Expand All @@ -13,9 +16,11 @@ def format_batch(*files)
private

def root_command
return 'bin/rails test' if file_system.exist? 'bin/rails'

'bundle exec rails test'
if file_system.exist? 'bin/rails'
'bin/rails test'
else
'bundle exec rails test'
end
end
end
end
Expand Down
17 changes: 11 additions & 6 deletions lib/retest/command/rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ module Retest
class Command
class Rake < Base
def to_s
return "#{root_command} TEST=<test>" unless all
root_command
if all
root_command
else
"#{root_command} TEST=<test>"
end
end

def format_batch(*files)
files.size > 1 ? "\"{#{files.join(',')}}\"" : files.first
files.size > 1 ? %Q{"{#{files.join(',')}}"} : files.first
end

private

def root_command
return 'bin/rake test' if file_system.exist? 'bin/rake'

'bundle exec rake test'
if file_system.exist? 'bin/rake'
'bin/rake test'
else
'bundle exec rake test'
end
end
end
end
Expand Down
15 changes: 10 additions & 5 deletions lib/retest/command/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ module Retest
class Command
class Rspec < Base
def to_s
return "#{root_command} <test>" unless all
root_command
if all
root_command
else
"#{root_command} <test>"
end
end

def format_batch(*files)
Expand All @@ -13,9 +16,11 @@ def format_batch(*files)
private

def root_command
return 'bin/rspec' if file_system.exist? 'bin/rspec'

'bundle exec rspec'
if file_system.exist? 'bin/rspec'
'bin/rspec'
else
'bundle exec rspec'
end
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/retest/command/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module Retest
class Command
class Ruby < Base
def format_batch(*files)
%Q{-e "#{files.map { |file| "require './#{file}';" }.join}"}
end

def to_s
if file_system.exist? 'Gemfile.lock'
'bundle exec ruby <test>'
else
'ruby <test>'
end
end

def format_batch(*files)
files.size > 1 ? %Q{-e "#{files.map { |file| "require './#{file}';" }.join}"} : files.first
end
end
end
end
17 changes: 9 additions & 8 deletions lib/retest/program.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@ def run(file, force_run: false)
return
end

test_file = repository.find_test(file) if runner.command.test_type?
test_file = if runner.has_test?
repository.find_test(file)
end

runner.run changed_files: [file], test_files: [test_file]
end

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
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
Loading

0 comments on commit 7db3766

Please sign in to comment.