Skip to content

Commit

Permalink
V2 - Cleanup (#236)
Browse files Browse the repository at this point in the history
* Remove unused reference
* Remove unused methods from Runner interface
* Refactor command.rb
* Clears window manually instead of each action
* Remove @clear_window setting since it's not used anymore too.
* Remove question asked flag since not used
* Update case indentation
  • Loading branch information
AlexB52 authored Dec 11, 2024
1 parent 800cf98 commit a304aac
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 184 deletions.
6 changes: 4 additions & 2 deletions exe/retest
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ end

if watcher == Retest::Watcher::Watchexec
puts "Watcher: [WATCHEXEC]"
else Retest::Watcher::Default
else
puts "Watcher: [LISTEN]"
end

Expand All @@ -71,7 +71,6 @@ end
puts "Ready to refactor! You can make file changes now"

def run_command(input:, program:)
program.clear_terminal
case input.strip
when /^file changed:\s(.*)$/
puts "File changed: #{$1}"
Expand Down Expand Up @@ -104,6 +103,8 @@ def run_command(input:, program:)
program.run_all
when /^di?f?f?\s(.*)$/
program.diff($1)
when 'c'
program.clear_terminal
when 'h', 'help'
puts <<~HELP
Expand All @@ -115,6 +116,7 @@ def run_command(input:, program:)
* 'f', 'force' # Forces a selection of test to run on every file change.
* 'r', 'reset' # Disables forced selection.
* 'd', 'diff' [GIT BRANCH] # Runs matching specs that changed from a target branch.
* 'c' # Clears window.
* 'e', 'exit' # Exits Retest.
HELP
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_start_help
* 'f', 'force' # Forces a selection of test to run on every file change.
* 'r', 'reset' # Disables forced selection.
* 'd', 'diff' [GIT BRANCH] # Runs matching specs that changed from a target branch.
* 'c' # Clears window.
* 'e', 'exit' # Exits Retest.
Type interactive command and press enter. Enter 'h' for help.
Expand Down
26 changes: 11 additions & 15 deletions lib/retest/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ def command
options_command || default_command
end

private

def options_command
if params[:command]
return hardcoded_command(params[:command])
if params[:command] then hardcoded_command(params[:command])
elsif params[:rspec] then rspec_command
elsif params[:rails] then rails_command
elsif params[:ruby] then ruby_command
elsif params[:rake] then rake_command
end
end

if params[:rspec] then rspec_command
elsif params[:rails] then rails_command
elsif params[:ruby] then ruby_command
elsif params[:rake] then rake_command
else
end
def default_command
log "Setup identified: [#{type.upcase}]. Using command: '#{setup_command}'"
setup_command
end

def setup_command
Expand All @@ -50,13 +53,6 @@ def setup_command
end
end

def default_command
log "Setup identified: [#{type.upcase}]. Using command: '#{setup_command}'"
setup_command
end

private

def log(message)
@stdout&.puts(message)
end
Expand Down
16 changes: 0 additions & 16 deletions lib/retest/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,6 @@ 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?
has_test? && has_changed?
end

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

def to_s
@command
end
Expand Down
5 changes: 1 addition & 4 deletions lib/retest/program.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ class Program
def_delegators :runner,
:run_last_command, :last_command

def initialize(runner: nil, repository: nil, clear_window: true, stdout: $stdout)
def initialize(runner: nil, repository: nil, stdout: $stdout)
@runner = runner
@repository = repository
@clear_window = clear_window
@stdout = stdout
initialize_pause(false)
initialize_forced_selection([])
Expand Down Expand Up @@ -61,8 +60,6 @@ def run_selected(test_files)
end

def clear_terminal
return unless @clear_window

system('clear 2>/dev/null') || system('cls 2>/dev/null')
end
end
Expand Down
36 changes: 11 additions & 25 deletions lib/retest/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,21 @@ def initialize(input: nil, output: nil)
@question_asked = false
end

def question_asked?
@question_asked
end

def ask_which_test_to_use(path, files)
ask_question do
changed
notify_observers(:question)
options = options(files)
changed
notify_observers(:question)
options = options(files)

output.puts(<<~QUESTION)
We found few tests matching: #{path}
output.puts(<<~QUESTION)
We found few tests matching: #{path}
#{list_options(options.keys)}
#{list_options(options.keys)}
Which file do you want to use?
Enter the file number now:
QUESTION
output.print("> ")
options.values[input.gets.to_s.chomp.to_i]
end
Which file do you want to use?
Enter the file number now:
QUESTION
output.print("> ")
options.values[input.gets.to_s.chomp.to_i]
end

def puts(*args)
Expand All @@ -40,14 +34,6 @@ def read_output
output.tap(&:rewind).read
end

def ask_question
old_question_asked = @question_asked
@question_asked = true
yield
ensure
@question_asked = old_question_asked
end

private

def options(files, blank_option: 'none')
Expand Down
4 changes: 1 addition & 3 deletions lib/retest/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ class Runner
include Observable
include CachedTestFile

def_delegators :command,
:has_changed?, :has_test?,
:changed_type?, :test_type?, :variable_type?, :harcoded_type?
def_delegators :command, :has_changed?, :has_test?

attr_accessor :command, :stdout, :last_command
def initialize(command, stdout: $stdout)
Expand Down
2 changes: 1 addition & 1 deletion lib/retest/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def type
@type ||= begin
return :ruby unless has_lock_file?

if rspec? then :rspec
if rspec? then :rspec
elsif rails? then :rails
elsif rake? then :rake
else :ruby
Expand Down
2 changes: 1 addition & 1 deletion lib/retest/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def self.for(watcher)
when 'listen' then Default
when 'watchexec' then Watchexec
when '', 'installed' then installed
else raise ArgumentError, "Unknown #{watcher}"
else raise ArgumentError, "Unknown #{watcher}"
end

unless tool.installed?
Expand Down
4 changes: 0 additions & 4 deletions test/retest/command/command_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ def test_interface
assert_respond_to @subject, :to_s
assert_respond_to @subject, :has_test?
assert_respond_to @subject, :has_changed?
assert_respond_to @subject, :test_type?
assert_respond_to @subject, :changed_type?
assert_respond_to @subject, :variable_type?
assert_respond_to @subject, :hardcoded_type?
end

def test_initializatioin
Expand Down
16 changes: 0 additions & 16 deletions test/retest/command/hardcoded_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,24 @@ def test_a_hardcoded_command_status
command = Hardcoded.new(command: 'echo "hello world"')
refute command.has_test?
refute command.has_changed?
refute command.test_type?
refute command.changed_type?
refute command.variable_type?
assert command.hardcoded_type?
end

def test_a_test_and_changed_command_status
command = Hardcoded.new(command: 'echo <test> & <changed>')
assert command.has_test?
assert command.has_changed?
refute command.test_type?
refute command.changed_type?
assert command.variable_type?
refute command.hardcoded_type?
end

def test_a_test_command_status
command = Hardcoded.new(command: 'echo <test>')
assert command.has_test?
refute command.has_changed?
assert command.test_type?
refute command.changed_type?
refute command.variable_type?
refute command.hardcoded_type?
end

def test_a_changed_command_status
command = Hardcoded.new(command: 'echo <changed>')
refute command.has_test?
assert command.has_changed?
refute command.test_type?
assert command.changed_type?
refute command.variable_type?
refute command.hardcoded_type?
end

def test_to_s
Expand Down
14 changes: 0 additions & 14 deletions test/retest/command/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ def setup

include CommandInterface

def test_type
all_cmd = Rails.new(all: true, file_system: FakeFS.new([]))
refute all_cmd.test_type?
refute all_cmd.variable_type?
refute all_cmd.changed_type?
assert all_cmd.hardcoded_type?

cmd = Rails.new(all: false, file_system: FakeFS.new([]))
assert cmd.test_type?
refute cmd.variable_type?
refute cmd.changed_type?
refute cmd.hardcoded_type?
end

def test_to_s
assert_equal 'bin/rails test', Rails.new(all: true, file_system: FakeFS.new(['bin/rails'])).to_s
assert_equal 'bundle exec rails test', Rails.new(all: true, file_system: FakeFS.new([])).to_s
Expand Down
14 changes: 0 additions & 14 deletions test/retest/command/rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ def setup

include CommandInterface

def test_type
all_cmd = Rake.new(all: true, file_system: FakeFS.new([]))
refute all_cmd.test_type?
refute all_cmd.variable_type?
refute all_cmd.changed_type?
assert all_cmd.hardcoded_type?

cmd = Rake.new(all: false, file_system: FakeFS.new([]))
assert cmd.test_type?
refute cmd.variable_type?
refute cmd.changed_type?
refute cmd.hardcoded_type?
end

def test_to_s
assert_equal 'bin/rake test', Rake.new(all: true, file_system: FakeFS.new(['bin/rake'])).to_s
assert_equal 'bundle exec rake test', Rake.new(all: true, file_system: FakeFS.new([])).to_s
Expand Down
14 changes: 0 additions & 14 deletions test/retest/command/rspec_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ def setup

include CommandInterface

def test_type
all_cmd = Rspec.new(all: true, file_system: FakeFS.new([]))
refute all_cmd.test_type?
refute all_cmd.variable_type?
refute all_cmd.changed_type?
assert all_cmd.hardcoded_type?

cmd = Rspec.new(all: false, file_system: FakeFS.new([]))
assert cmd.test_type?
refute cmd.variable_type?
refute cmd.changed_type?
refute cmd.hardcoded_type?
end

def test_to_s
assert_equal 'bin/rspec', Rspec.new(all: true, file_system: FakeFS.new(['bin/rspec'])).to_s
assert_equal 'bundle exec rspec', Rspec.new(all: true, file_system: FakeFS.new([])).to_s
Expand Down
14 changes: 0 additions & 14 deletions test/retest/command/ruby_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ def setup

include CommandInterface

def test_type
all_cmd = Ruby.new(all: true, file_system: FakeFS.new([]))
assert all_cmd.test_type?
refute all_cmd.variable_type?
refute all_cmd.changed_type?
refute all_cmd.hardcoded_type?

cmd = Ruby.new(all: false, file_system: FakeFS.new([]))
assert cmd.test_type?
refute cmd.variable_type?
refute cmd.changed_type?
refute cmd.hardcoded_type?
end

def test_to_s
assert_equal 'ruby <test>', Ruby.new(all: true, file_system: FakeFS.new(['bin/ruby'])).to_s
assert_equal 'ruby <test>', Ruby.new(all: true, file_system: FakeFS.new([])).to_s
Expand Down
2 changes: 1 addition & 1 deletion test/retest/program_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def find_test(_)
end

def setup
@subject = Program.new(repository: Repository.new, clear_window: false, stdout: StringIO.new)
@subject = Program.new(repository: Repository.new, stdout: StringIO.new)
end

def test_paused?
Expand Down
Loading

0 comments on commit a304aac

Please sign in to comment.