From 50bad247b9c87c5a136d0657aba9f660ae984e8e Mon Sep 17 00:00:00 2001 From: Alexandre Barret Date: Sun, 11 Aug 2024 20:58:16 +1200 Subject: [PATCH] Refactor helper methods --- test/retest/prompt_test.rb | 32 ++++-------------------------- test/test_helper.rb | 40 +++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/test/retest/prompt_test.rb b/test/retest/prompt_test.rb index cc3ed779..b542e565 100644 --- a/test/retest/prompt_test.rb +++ b/test/retest/prompt_test.rb @@ -49,21 +49,6 @@ def test_ask_which_test_to_use EXPECTED end - class ListeningIO - attr_accessor :io - def initialize - @io = StringIO.new - end - - def gets - loop do - sleep 0.0001 - line = @io.gets.to_s - break line unless line.empty? - end - end - end - def test_question_asked_when_asking_question files = %w( test/models/taxation/holdings_test.rb @@ -73,26 +58,17 @@ def test_question_asked_when_asking_question test/lib/csv_report/holdings_test.rb ) - @subject.input = stdin = ListeningIO.new + @subject.input = stdin = BlockingInput.new th = Thread.new do @subject.ask_which_test_to_use("app/models/valuation/holdings.rb", files) end - attempts = 0 - begin - assert @subject.question_asked? - rescue Minitest::Assertion => e - raise e if attempts >= 10 - sleep 0.0001 - attempts += 1 - end - - stdin.io = StringIO.new("1\n") - th.join + wait_until { assert @subject.question_asked? } - refute @subject.question_asked? + stdin.puts("1\n") assert_equal "test/models/schedule/holdings_test.rb", th.value + refute @subject.question_asked? end def test_read_output diff --git a/test/test_helper.rb b/test/test_helper.rb index 806f8ff6..0a9dca22 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,10 +1,48 @@ $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 \ No newline at end of file +end + +class BlockingInput + attr_accessor :io + 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 +