Skip to content

Commit e2a7a3b

Browse files
committed
Refactor bundler-app helpers and test command
1 parent 21f6d65 commit e2a7a3b

File tree

4 files changed

+63
-31
lines changed

4 files changed

+63
-31
lines changed

bin/test/bundler-app

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#!/usr/bin/env bash
22

3+
FOLDER="features/bundler-app"
4+
35
bundle install
46
bundle exec rake build
5-
cp -R features/support features/bundler-app/retest
6-
ls -t pkg | head -n1 | xargs -I {} mv pkg/{} features/bundler-app/retest.gem
7-
docker compose -f features/bundler-app/docker-compose.yml up --build --exit-code-from retest
7+
# cp -R features/support features/bundler-app/retest
8+
ls -t pkg | head -n1 | xargs -I {} mv pkg/{} "$FOLDER/retest.gem"
9+
10+
if [[ "$1" == "--no-build" ]]; then
11+
docker compose -f "$FOLDER/docker-compose.yml" up --exit-code-from retest
12+
else
13+
docker compose -f "$FOLDER/docker-compose.yml" up --build --exit-code-from retest
14+
fi

features/bundler-app/retest/retest_test.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,3 @@
44
require_relative 'retest_test/file_changes_test'
55

66
$stdout.sync = true
7-
8-
include FileHelper
9-

features/bundler-app/retest/retest_test/file_changes_test.rb

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
class FileChangesTest < Minitest::Test
2+
include FileHelper
3+
include OutputHelper
4+
include CommandHelper
5+
26
def setup
37
@command = 'retest'
48
end
@@ -10,7 +14,7 @@ def teardown
1014
def test_start_retest
1115
launch_retest @command
1216

13-
assert_match <<~EXPECTED, @output.read
17+
assert_match <<~EXPECTED, read_output
1418
Launching Retest...
1519
Ready to refactor! You can make file changes now
1620
EXPECTED
@@ -21,25 +25,29 @@ def test_modifying_existing_file
2125

2226
modify_file('lib/bundler_app/bottles.rb')
2327

24-
assert_match "Test file: test/bundler_app/test_bottles.rb", @output.read
25-
assert_match "12 runs, 12 assertions, 0 failures, 0 errors, 0 skips", @output.read
28+
read_output do |output|
29+
assert_match "Test file: test/bundler_app/test_bottles.rb", output
30+
assert_match "12 runs, 12 assertions, 0 failures, 0 errors, 0 skips", output
31+
end
2632
end
2733

2834
def test_modifying_existing_test_file
2935
launch_retest @command
3036

3137
modify_file('test/bundler_app/test_bottles.rb')
3238

33-
assert_match "Test file: test/bundler_app/test_bottles.rb", @output.read
34-
assert_match "12 runs, 12 assertions, 0 failures, 0 errors, 0 skips", @output.read
39+
read_output do |output|
40+
assert_match "Test file: test/bundler_app/test_bottles.rb", output
41+
assert_match "12 runs, 12 assertions, 0 failures, 0 errors, 0 skips", output
42+
end
3543
end
3644

3745
def test_creating_a_new_test_file
3846
launch_retest @command
3947

4048
create_file 'test/bundler_app/test_foo.rb'
4149

42-
assert_match "Test file: test/bundler_app/test_foo.rb", @output.read
50+
assert_match "Test file: test/bundler_app/test_foo.rb", read_output
4351

4452
ensure
4553
delete_file 'test/bundler_app/test_foo.rb'
@@ -49,18 +57,18 @@ def test_creating_a_new_file
4957
launch_retest @command
5058

5159
create_file 'lib/bundler_app/foo.rb'
52-
assert_match <<~EXPECTED, @output.read
60+
assert_match <<~EXPECTED, read_output
5361
FileNotFound - Retest could not find a matching test file to run.
5462
EXPECTED
5563

5664
create_file 'test/bundler_app/test_foo.rb'
57-
assert_match "Test file: test/bundler_app/test_foo.rb", @output.read
65+
assert_match "Test file: test/bundler_app/test_foo.rb", read_output
5866

5967
modify_file('lib/bundler_app/bottles.rb')
60-
assert_match "Test file: test/bundler_app/test_bottles.rb", @output.read
68+
assert_match "Test file: test/bundler_app/test_bottles.rb", read_output
6169

6270
modify_file('lib/bundler_app/foo.rb')
63-
assert_match "Test file: test/bundler_app/test_foo.rb", @output.read
71+
assert_match "Test file: test/bundler_app/test_foo.rb", read_output
6472

6573
ensure
6674
delete_file 'lib/bundler_app/foo.rb'
@@ -74,7 +82,7 @@ def test_untracked_file
7482
launch_retest @command
7583

7684
modify_file 'lib/bundler_app/foo.rb'
77-
assert_match "Test file: test/bundler_app/test_foo.rb", @output.read
85+
assert_match "Test file: test/bundler_app/test_foo.rb", read_output
7886

7987
ensure
8088
delete_file 'lib/bundler_app/foo.rb'

features/bundler-app/retest/support/test_helper.rb

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
require_relative 'output_file'
1+
# Can be updated to all feature repositories with
2+
# $ bin/test/reset_helpers
3+
4+
module OutputHelper
5+
def read_output(output = @output)
6+
result = ""
7+
loop do
8+
result += output.read_nonblock(1024)
9+
rescue IO::WaitReadable
10+
break
11+
end
12+
13+
if block_given?
14+
yield result
15+
else
16+
result
17+
end
18+
end
19+
end
220

321
module FileHelper
422
def default_sleep_seconds
@@ -41,19 +59,21 @@ def rename_file(path, new_path)
4159
end
4260
end
4361

44-
def launch_retest(command, sleep_seconds: launch_sleep_seconds)
45-
@rd, @input = IO.pipe
46-
@output = OutputFile.new
47-
@pid = Process.spawn command, out: @output.path, in: @rd
48-
sleep sleep_seconds
49-
end
62+
module CommandHelper
63+
def launch_retest(command, sleep_seconds: Float(ENV.fetch('LAUNCH_SLEEP_SECONDS', 1.5)))
64+
require 'open3'
65+
@input, @output, @stderr, @wait_thr = Open3.popen3(command)
66+
@pid = @wait_thr[:pid]
67+
sleep sleep_seconds
68+
end
5069

51-
def end_retest(file = nil, pid = nil)
52-
@output&.delete
53-
@rd&.close
54-
@input&.close
55-
if @pid
56-
Process.kill('SIGHUP', @pid)
57-
Process.detach(@pid)
70+
def end_retest
71+
@input&.close
72+
@stderr&.close
73+
@output&.close
74+
if @pid
75+
Process.kill('SIGHUP', @pid)
76+
Process.detach(@pid)
77+
end
5878
end
5979
end

0 commit comments

Comments
 (0)