Skip to content

Commit

Permalink
V2/refactor test helper (#235)
Browse files Browse the repository at this point in the history
* Enable new test helper for all feature specs
* Refactor all feature specs
  • Loading branch information
AlexB52 authored Dec 8, 2024
1 parent 5a9f98c commit c2744b9
Show file tree
Hide file tree
Showing 38 changed files with 732 additions and 577 deletions.
2 changes: 1 addition & 1 deletion bin/test/bundler-app
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FOLDER="features/bundler-app"

bundle install
bundle exec rake build
# cp -R features/support features/bundler-app/retest
cp -R features/support features/bundler-app/retest
ls -t pkg | head -n1 | xargs -I {} mv pkg/{} "$FOLDER/retest.gem"

if [[ "$1" == "--no-build" ]]; then
Expand Down
2 changes: 1 addition & 1 deletion bin/test/ruby-app
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

bundle install
bundle exec rake build
# cp -R features/support features/ruby-app/retest
cp -R features/support features/ruby-app/retest
ls -t pkg | head -n1 | xargs -I {} mv pkg/{} features/ruby-app/retest.gem
docker compose -f features/ruby-app/docker-compose.yml up --build --exit-code-from retest
11 changes: 4 additions & 7 deletions features/bundler-app/retest/support/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@

module RetestHelper
# COMMAND
def launch_retest(command, sleep_seconds: Float(ENV.fetch('LAUNCH_SLEEP_SECONDS', 1.5)))
def launch_retest(command, sleep_for: Float(ENV.fetch('LAUNCH_SLEEP_SECONDS', 1.5)))
require 'open3'
@input, @output, @stderr, @wait_thr = Open3.popen3(command)
@pid = @wait_thr[:pid]
sleep sleep_seconds
sleep sleep_for
end

def end_retest
@input&.close
@stderr&.close
@output&.close
if @pid
Process.kill('SIGHUP', @pid)
Process.detach(@pid)
end
@wait_thr.exit
end

# ASSERTIONS
Expand All @@ -42,7 +39,7 @@ def read_output(output = @output)
result = ""
loop do
result += output.read_nonblock(1024)
rescue IO::WaitReadable
rescue IO::WaitReadable, EOFError
break
end

Expand Down
33 changes: 17 additions & 16 deletions features/git-ruby/retest/retest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

$stdout.sync = true

include FileHelper

class FileChangesTest < Minitest::Test
include RetestHelper

def setup
@command = 'retest --ruby'
end
Expand All @@ -18,14 +18,16 @@ def teardown
def test_start_retest
launch_retest @command

assert_match <<~EXPECTED, @output.read
assert_output_matches <<~EXPECTED
Launching Retest...
Ready to refactor! You can make file changes now
EXPECTED
end
end

class GitChangesTest < Minitest::Test
include RetestHelper

def setup
`git config --global init.defaultBranch main`
`git config --global --add safe.directory /usr/src/app`
Expand All @@ -45,25 +47,24 @@ def teardown
end

def test_diffs_from_other_branch
delete_file('lib/to_be_deleted.rb')
rename_file('lib/to_be_renamed.rb', 'lib/renamed.rb')
rename_file('lib/to_be_renamed_with_test_file.rb', 'lib/renamed_with_test_file.rb')
rename_file('test/to_be_renamed_with_test_file_test.rb', 'test/renamed_with_test_file_test.rb')
create_file('lib/created.rb', should_sleep: false)
create_file('lib/created_with_test_file.rb', should_sleep: false)
create_file('test/created_with_test_file_test.rb', should_sleep: false)
delete_file('lib/to_be_deleted.rb', sleep_for: 0)
rename_file('lib/to_be_renamed.rb', 'lib/renamed.rb', sleep_for: 0)
rename_file('lib/to_be_renamed_with_test_file.rb', 'lib/renamed_with_test_file.rb', sleep_for: 0)
rename_file('test/to_be_renamed_with_test_file_test.rb', 'test/renamed_with_test_file_test.rb', sleep_for: 0)
create_file('lib/created.rb', sleep_for: 0)
create_file('lib/created_with_test_file.rb', sleep_for: 0)
create_file('test/created_with_test_file_test.rb', sleep_for: 0)

`git add .`
`git commit -m "Rename, Add and Remove files"`

launch_retest 'retest --diff=main --ruby'
sleep 2

assert_match <<~EXPECTED, @output.read
Tests selected:
- test/created_with_test_file_test.rb
- test/renamed_with_test_file_test.rb
- test/to_be_renamed_test.rb
assert_output_matches <<~EXPECTED
Tests selected:
- test/created_with_test_file_test.rb
- test/renamed_with_test_file_test.rb
- test/to_be_renamed_test.rb
EXPECTED
end
end
21 changes: 0 additions & 21 deletions features/git-ruby/retest/support/output_file.rb

This file was deleted.

101 changes: 70 additions & 31 deletions features/git-ruby/retest/support/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,98 @@
require_relative 'output_file'
# Can be updated to all feature repositories with
# $ bin/test/reset_helpers

module FileHelper
def default_sleep_seconds
Float(ENV.fetch('DEFAULT_SLEEP_SECONDS', 1))
module RetestHelper
# COMMAND
def launch_retest(command, sleep_for: Float(ENV.fetch('LAUNCH_SLEEP_SECONDS', 1.5)))
require 'open3'
@input, @output, @stderr, @wait_thr = Open3.popen3(command)
@pid = @wait_thr[:pid]
sleep sleep_for
end

def launch_sleep_seconds
Float(ENV.fetch('LAUNCH_SLEEP_SECONDS', 1.5))
def end_retest
@input&.close
@stderr&.close
@output&.close
@wait_thr.exit
end

# ASSERTIONS
def assert_output_matches(*expectations, max_retries: 5)
retries = 0
wait_for = 0.1
output = ""
begin
output += read_output
expectations.each { |expectation| assert_match(expectation, output) }
rescue Minitest::Assertion => e
raise e if retries >= max_retries
retries += 1
sleep_seconds = wait_for ** -(wait_for * retries)
sleep sleep_seconds
retry
end
end

def wait(sleep_seconds: default_sleep_seconds)
sleep sleep_seconds
# OUTPUT
def read_output(output = @output)
result = ""
loop do
result += output.read_nonblock(1024)
rescue IO::WaitReadable, EOFError
break
end

if block_given?
yield result
else
result
end
end

def modify_file(path, sleep_seconds: default_sleep_seconds)
# INPUT
def write_input(command, input: @input, sleep_for: 0.1)
input.write(command)
wait(sleep_for)
end

# FILE CHANGES
def modify_file(path, sleep_for: default_sleep_seconds)
return unless File.exist? path

old_content = File.read(path)
File.open(path, 'w') { |file| file.write old_content }

sleep sleep_seconds
wait(sleep_for)
end

def create_file(path, should_sleep: true, sleep_seconds: default_sleep_seconds)
File.open(path, "w").tap(&:close)

sleep sleep_seconds if should_sleep
def create_file(path, content: "", sleep_for: default_sleep_seconds)
File.open(path, "w") { |f| f.write(content) }
wait(sleep_for)
end

def delete_file(path)
def delete_file(path, sleep_for: 0)
return unless File.exist? path

File.delete path
wait(sleep_for)
end

def rename_file(path, new_path)
def rename_file(path, new_path, sleep_for: 0)
return unless File.exist? path

File.rename path, new_path
wait(sleep_for)
end
end

def launch_retest(command, sleep_seconds: launch_sleep_seconds)
@rd, @input = IO.pipe
@output = OutputFile.new
@pid = Process.spawn command, out: @output.path, in: @rd
sleep sleep_seconds
end
def default_sleep_seconds
Float(ENV.fetch('DEFAULT_SLEEP_SECONDS', 1))
end

def launch_sleep_seconds
Float(ENV.fetch('LAUNCH_SLEEP_SECONDS', 1.5))
end

def end_retest(file = nil, pid = nil)
@output&.delete
@rd&.close
@input&.close
if @pid
Process.kill('SIGHUP', @pid)
Process.detach(@pid)
def wait(sleep_for = default_sleep_seconds)
sleep sleep_for
end
end
23 changes: 14 additions & 9 deletions features/hanami-app/retest/retest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

$stdout.sync = true

include FileHelper

class MatchingTestsCommandTest < Minitest::Test
include RetestHelper

def setup
@command = "retest --rake"
end
Expand All @@ -18,7 +18,7 @@ def teardown
def test_start_retest
launch_retest @command

assert_match <<~EXPECTED, @output.read
assert_output_matches <<~EXPECTED
Launching Retest...
Ready to refactor! You can make file changes now
EXPECTED
Expand All @@ -29,12 +29,15 @@ def test_modify_a_file

modify_file 'apps/web/controllers/books/create.rb'

assert_match "Test file: spec/web/controllers/books/create_spec.rb", @output.read
assert_match "4 runs, 7 assertions, 0 failures, 0 errors, 0 skips", @output.read
assert_output_matches(
"Test file: spec/web/controllers/books/create_spec.rb",
"4 runs, 7 assertions, 0 failures, 0 errors, 0 skips")
end
end

class AllTestsCommandTest < Minitest::Test
include RetestHelper

def setup
@command = 'retest --rake --all'
end
Expand All @@ -46,7 +49,7 @@ def teardown
def test_start_retest
launch_retest @command

assert_match <<~EXPECTED, @output.read
assert_output_matches <<~EXPECTED
Launching Retest...
Ready to refactor! You can make file changes now
EXPECTED
Expand All @@ -57,19 +60,21 @@ def test_modify_a_file

modify_file 'apps/web/controllers/books/create.rb'

assert_match "15 runs, 27 assertions, 0 failures, 0 errors, 1 skips", @output.read
assert_output_matches "15 runs, 27 assertions, 0 failures, 0 errors, 1 skips"
end
end

class AutoFlagTest < Minitest::Test
include RetestHelper

def teardown
end_retest
end

def test_with_no_command
launch_retest 'retest'

assert_match <<~OUTPUT, @output.read
assert_output_matches <<~OUTPUT
Setup identified: [RAKE]. Using command: 'bundle exec rake test TEST=<test>'
Watcher: [LISTEN]
Launching Retest...
Expand All @@ -80,7 +85,7 @@ def test_with_no_command
def test_with_no_command_all
launch_retest 'retest --all'

assert_match <<~OUTPUT, @output.read
assert_output_matches <<~OUTPUT
Setup identified: [RAKE]. Using command: 'bundle exec rake test'
Watcher: [LISTEN]
Launching Retest...
Expand Down
21 changes: 0 additions & 21 deletions features/hanami-app/retest/support/output_file.rb

This file was deleted.

Loading

0 comments on commit c2744b9

Please sign in to comment.