Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ask user which tests to run #225

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PATH
observer (~> 0.1)
string-similarity (~> 2.1)
tty-option (~> 0.1)
tty-prompt (~> 0.1)

GEM
remote: https://rubygems.org/
Expand All @@ -17,12 +18,25 @@ GEM
rb-inotify (~> 0.9, >= 0.9.10)
minitest (5.15.0)
observer (0.1.2)
pastel (0.8.0)
tty-color (~> 0.5)
rake (13.0.6)
rb-fsevent (0.11.2)
rb-inotify (0.11.1)
ffi (~> 1.0)
string-similarity (2.1.0)
tty-color (0.6.0)
tty-cursor (0.7.1)
tty-option (0.3.0)
tty-prompt (0.23.1)
pastel (~> 0.8)
tty-reader (~> 0.8)
tty-reader (0.9.0)
tty-cursor (~> 0.7)
tty-screen (~> 0.8)
wisper (~> 2.0)
tty-screen (0.8.2)
wisper (2.0.1)

PLATFORMS
ruby
Expand Down
19 changes: 11 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 @@ -82,12 +77,20 @@ def run_command(input:, program:, all_test_runner:)
puts "Program has been resumed\n"
when 'e', 'exit'
Process.kill("INT", 0)
when 'f', 'force'
require 'tty-prompt'
prompt = TTY::Prompt.new
program.run_selected prompt.multi_select(
"What test files do you want to run when saving a file?",
program.repository.test_files,
filter: true, min: 1
)
when ''
puts "Running last command\n"
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 @@ -115,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
16 changes: 11 additions & 5 deletions lib/retest/program.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +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}" }
def run_all
runner.run_all
end

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

def clear_terminal
Expand Down
4 changes: 4 additions & 0 deletions lib/retest/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def find_tests(paths)
.sort
end

def test_files
files.select { |file| MatchingOptions::Path.new(file).test? }
end

def sync(added:, removed:)
add(added)
remove(removed)
Expand Down
Loading