Skip to content

Commit

Permalink
Prompt asking a question (#212)
Browse files Browse the repository at this point in the history
* Add actions for v2.0 branch
* Cleanup Prompt methods
* Test question_ask flag
* Refactor helper methods
  • Loading branch information
AlexB52 authored Aug 11, 2024
1 parent 1f3df1d commit cd8ab0b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
branches: [ main, v2.0 ]

jobs:
unit-tests:
Expand Down
46 changes: 26 additions & 20 deletions lib/retest/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,34 @@ module Retest
class Prompt
include Observable

def self.ask_which_test_to_use(path, files)
new.ask_which_test_to_use(path, files)
end

def self.puts(*args)
new.puts(*args)
end

attr_accessor :input, :output
def initialize(input: nil, output: nil)
@input = input || $stdin
@output = output || $stdout
@question_asked = false
end

def ask_which_test_to_use(path, files)
changed
notify_observers(:question)
options = options(files)
def question_asked?
@question_asked
end

output.puts(<<~QUESTION)
We found few tests matching: #{path}
def ask_which_test_to_use(path, files)
ask_question do
changed
notify_observers(:question)
options = options(files)

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

def puts(*args)
Expand All @@ -42,6 +40,14 @@ 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
42 changes: 36 additions & 6 deletions test/retest/prompt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,39 @@ def test_ask_which_test_to_use
Which file do you want to use?
Enter the file number now:
>
>\s
EXPECTED
end

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

@subject.input = BlockingInput.new

th = Thread.new do
@subject.ask_which_test_to_use("app/models/valuation/holdings.rb", files)
end

wait_until { assert @subject.question_asked? }

@subject.input.puts("1\n")

assert_equal "test/models/schedule/holdings_test.rb", th.value
refute @subject.question_asked?
end

def test_read_output
@subject.output.puts "hello world\n"

assert_equal "hello world\n", @subject.read_output
end

def test_puts
out, _ = capture_subprocess_io { Prompt.puts "hello world\n" }
assert_equal "hello world\n", out
end

def test_observers_receive_correct_update_on_ask_which_test_to_use
observer = FakeObserver.new

Expand All @@ -79,5 +97,17 @@ def test_observers_receive_correct_update_on_ask_which_test_to_use

assert_includes observer.notepad, MethodCall.new(name: :update, args: [:question])
end

def test_ask_question
refute @subject.question_asked?
@subject.ask_question do
assert @subject.question_asked?
end
refute @subject.question_asked?
end

def test_question_flag_when_asking_for_file

end
end
end
39 changes: 38 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)

require "retest"
require "byebug"
require "minitest/autorun"

WAITING_TIME = 0.0001

def wait(time = WAITING_TIME)
sleep time
end

FakeFS = Struct.new(:files) do
def exist?(value)
files.include? value
end
end
end

class BlockingInput
def initialize
@io = StringIO.new
end

def puts(value)
@io = StringIO.new(value)
end

def gets
loop do
wait
line = @io.gets.to_s
break line unless line.empty?
end
end
end

def wait_until(max_attempts: 10)
attempts = 0
begin
yield
rescue Minitest::Assertion => e
raise e if attempts >= max_attempts
wait
attempts += 1
end
end

0 comments on commit cd8ab0b

Please sign in to comment.