Skip to content

Commit

Permalink
Add a none option when multiple possible tests are found
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexB52 committed Feb 20, 2024
1 parent 54a4976 commit bf6fec4
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,58 @@ def test_not_displaying_options_on_unmatching_command
refute_match "We found few tests matching:", @output.read
assert_match "there was no command", @output.read
end
end

# Testing a new way to launch and interact with retest
class TestMatchingUnmatchingCommand < Minitest::Test
include FileHelper

def setup
create_file('test/other_bottles_test.rb', should_sleep: false)
end

def teardown
delete_file('test/other_bottles_test.rb')
end

def launch_retest(command, input: Tempfile.new, output: Tempfile.new)
pid = Process.spawn(command, in: input, out: output)
sleep 1.5

[pid, input.tap(&:rewind), output.tap(&:rewind)]
end

def end_retest(pid:, input:, output:)
input.close
input.unlink
output.close
output.unlink
Process.kill('SIGHUP', pid)
Process.detach(pid)
end

def test_displaying_options_on_matching_command
@output, @pid = launch_retest 'retest --ruby'
stdin = Tempfile.new
stdin.write "2\n"
pid, _, stdout = launch_retest('retest --ruby', input: stdin)

modify_file('lib/bottles.rb')
create_file 'foo_test.rb'
assert_match "Test File Selected: foo_test.rb", stdout.tap(&:rewind).read

assert_match <<~EXPECTED, @output.read
modify_file('lib/bottles.rb')
assert_match <<~EXPECTED, stdout.tap(&:rewind).read
We found few tests matching: lib/bottles.rb
[0] - test/bottles_test.rb
[1] - test/other_bottles_test.rb
[2] - none
Which file do you want to use?
Enter the file number now:
EXPECTED

assert_match "Test File Selected: foo_test.rb", stdout.tap(&:rewind).read
ensure
end_retest(pid: pid, input: stdin, output: stdout)
end
end
end
16 changes: 12 additions & 4 deletions lib/retest/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ def initialize(input: nil, output: nil)
def ask_which_test_to_use(path, files)
changed
notify_observers(:question)
options = options(files)

output.puts(<<~QUESTION)
We found few tests matching: #{path}
#{list_options(files)}
#{list_options(options.keys)}
Which file do you want to use?
Enter the file number now:
QUESTION

files[input.gets.chomp.to_i]
options.values[input.gets.chomp.to_i]
end

def puts(*args)
Expand All @@ -42,8 +43,15 @@ def read_output

private

def list_options(files)
files
def options(files, blank_option: 'none')
result = {}
files.each { |file| result[file] = file }
result[blank_option] = nil # blank option last
result
end

def list_options(options)
options
.map
.with_index { |file, index| "[#{index}] - #{file}" }
.join("\n")
Expand Down
22 changes: 15 additions & 7 deletions lib/retest/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ def find_test(path)
return unless path
return if path.empty?

@path = path
cache[@path] ||= select_from MatchingOptions.for(@path, files: files)
unless cache.key?(path)
ok_to_cache, test_file = select_from path, MatchingOptions.for(path, files: files)
if ok_to_cache
cache[path] = test_file
end
end

cache[path]
end

def find_tests(paths)
Expand Down Expand Up @@ -49,12 +55,14 @@ def remove(removed)

private

def select_from(tests)
case tests.count
when 0, 1
tests.first
def select_from(path, matching_tests)
case matching_tests.count
when 0
[false, nil]
when 1
[true, matching_tests.first]
else
prompt.ask_which_test_to_use(@path, tests)
[true, prompt.ask_which_test_to_use(path, matching_tests)]
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/retest/prompt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def test_ask_which_test_to_use
[2] - test/models/holdings_test.rb
[3] - test/models/performance/holdings_test.rb
[4] - test/lib/csv_report/holdings_test.rb
[5] - none
Which file do you want to use?
Enter the file number now:
Expand Down
11 changes: 10 additions & 1 deletion test/retest/repository/multiple_test_files_with_user_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ def test_find_test_user_input_question
[0] - spec/models/billing_agent_customer_spec.rb
[1] - core/spec/models/billing_agent_customer_spec.rb
[2] - none
Which file do you want to use?
Enter the file number now:
EXPECTED
end

FakePrompt = Struct.new(:input_choice, keyword_init: true) do
def options(files)
result = {}
files.each { |file| result[file] = file }
result['none'] = nil
result
end

def ask_which_test_to_use(path, files)
files[input_choice]
options(files).values[input_choice]
end
end

Expand All @@ -46,6 +54,7 @@ def test_find_test_user_select_1
end

def test_find_test_user_select_2
# none exist
@subject.prompt = FakePrompt.new(input_choice: 2)
assert_nil @subject.find_test('app/models/billing_agent_customer.rb')
end
Expand Down
35 changes: 35 additions & 0 deletions test/retest/repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,47 @@ def test_find_test_similar_files_but_no_exact_match
[2] - test/models/holdings_test.rb
[3] - test/models/performance/holdings_test.rb
[4] - test/lib/csv_report/holdings_test.rb
[5] - none
Which file do you want to use?
Enter the file number now:
EXPECTED
end

def test_no_matches_with_multiple_possible_tests
mock_cache = {}

@subject.files = %w(
test/models/schedule/holdings_test.rb
test/models/taxation/holdings_test.rb
test/models/holdings_test.rb
test/models/performance/holdings_test.rb
test/lib/csv_report/holdings_test.rb
)

@subject.cache = mock_cache
@subject.prompt = Prompt.new(input: StringIO.new("5\n"), output: StringIO.new)

@subject.find_test('app/models/valuation/holdings.rb')

assert_equal({ 'app/models/valuation/holdings.rb' => nil }, mock_cache)
end

def test_no_matches_with_no_match
mock_cache = {}

@subject.files = %w(
bar.rb
bar_test.rb
)

@subject.cache = mock_cache

@subject.find_test('foo.rb')

assert_equal({}, mock_cache)
end

class TestFileChanged < MiniTest::Test
def setup
@subject = Repository.new
Expand Down

0 comments on commit bf6fec4

Please sign in to comment.