diff --git a/bin/test/bundler-app b/bin/test/bundler-app index 98ffb4af..f5f2bc61 100755 --- a/bin/test/bundler-app +++ b/bin/test/bundler-app @@ -2,5 +2,6 @@ bundle install bundle exec rake build +cp -R features/support features/bundler-app/retest ls -t pkg | head -n1 | xargs -I {} mv pkg/{} features/bundler-app/retest.gem docker compose -f features/bundler-app/docker-compose.yml up --build --exit-code-from retest \ No newline at end of file diff --git a/bin/test/git-ruby b/bin/test/git-ruby index 86236da9..ac4ecfad 100755 --- a/bin/test/git-ruby +++ b/bin/test/git-ruby @@ -2,5 +2,6 @@ bundle install bundle exec rake build +cp -R features/support features/git-ruby/retest ls -t pkg | head -n1 | xargs -I {} mv pkg/{} features/git-ruby/retest.gem docker compose -f features/git-ruby/docker-compose.yml up --build --exit-code-from retest \ No newline at end of file diff --git a/bin/test/hanami-app b/bin/test/hanami-app index 94703317..0338bfca 100755 --- a/bin/test/hanami-app +++ b/bin/test/hanami-app @@ -2,6 +2,7 @@ bundle install bundle exec rake build +cp -R features/support features/hanami-app/retest ls -t pkg | head -n1 | xargs -I {} mv pkg/{} features/hanami-app/retest.gem docker compose -f features/hanami-app/docker-compose.yml build docker compose -f features/hanami-app/docker-compose.yml run retest sh bin/test_setup diff --git a/bin/test/rails-app b/bin/test/rails-app index 23725950..b3161bd6 100755 --- a/bin/test/rails-app +++ b/bin/test/rails-app @@ -2,5 +2,6 @@ bundle install bundle exec rake build +cp -R features/support features/rails-app/retest ls -t pkg | head -n1 | xargs -I {} mv pkg/{} features/rails-app/retest.gem docker compose -f features/rails-app/docker-compose.yml up --build --exit-code-from retest \ No newline at end of file diff --git a/bin/test/reset_helpers b/bin/test/reset_helpers new file mode 100755 index 00000000..243253f7 --- /dev/null +++ b/bin/test/reset_helpers @@ -0,0 +1,13 @@ +#!/usr/bin/env ruby +%w[ + ruby-app + ruby-bare + git-ruby + hanami-app + rails-app + rspec-rails + rspec-ruby + bundler-app +].each do |folder| + `cp -R ./features/support ./features/#{folder}/retest` +end \ No newline at end of file diff --git a/bin/test/rspec-rails b/bin/test/rspec-rails index 7f86e8e6..6790d7d8 100755 --- a/bin/test/rspec-rails +++ b/bin/test/rspec-rails @@ -2,5 +2,6 @@ bundle install bundle exec rake build +cp -R features/support features/rspec-rails/retest ls -t pkg | head -n1 | xargs -I {} mv pkg/{} features/rspec-rails/retest.gem docker compose -f features/rspec-rails/docker-compose.yml up --build --exit-code-from retest \ No newline at end of file diff --git a/bin/test/rspec-ruby b/bin/test/rspec-ruby index 9edf26c3..a067e8af 100755 --- a/bin/test/rspec-ruby +++ b/bin/test/rspec-ruby @@ -2,5 +2,6 @@ bundle install bundle exec rake build +cp -R features/support features/rspec-ruby/retest ls -t pkg | head -n1 | xargs -I {} mv pkg/{} features/rspec-ruby/retest.gem docker compose -f features/rspec-ruby/docker-compose.yml up --build --exit-code-from retest \ No newline at end of file diff --git a/bin/test/ruby-app b/bin/test/ruby-app index 7cb3c384..135554a0 100755 --- a/bin/test/ruby-app +++ b/bin/test/ruby-app @@ -2,5 +2,6 @@ bundle install bundle exec rake build +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 \ No newline at end of file diff --git a/bin/test/ruby-bare b/bin/test/ruby-bare index 66b87a0b..2e0e844e 100755 --- a/bin/test/ruby-bare +++ b/bin/test/ruby-bare @@ -2,5 +2,6 @@ bundle install bundle exec rake build +cp -R features/support features/ruby-bare/retest ls -t pkg | head -n1 | xargs -I {} mv pkg/{} features/ruby-bare/retest.gem docker compose -f features/ruby-bare/docker-compose.yml up --build --exit-code-from retest \ No newline at end of file diff --git a/features/bundler-app/docker-compose.yml b/features/bundler-app/docker-compose.yml index 268ec9b4..c474455f 100644 --- a/features/bundler-app/docker-compose.yml +++ b/features/bundler-app/docker-compose.yml @@ -3,4 +3,7 @@ services: build: . volumes: - .:/usr/src/app + environment: + - DEFAULT_SLEEP_SECONDS=1 + - LAUNCH_SLEEP_SECONDS=1.5 command: ruby retest/retest_test.rb diff --git a/features/bundler-app/retest/retest_test.rb b/features/bundler-app/retest/retest_test.rb index e821ec7d..3c22c4e4 100644 --- a/features/bundler-app/retest/retest_test.rb +++ b/features/bundler-app/retest/retest_test.rb @@ -1,4 +1,5 @@ -require_relative 'test_helper' +require 'retest' +require_relative 'support/test_helper' require 'minitest/autorun' require_relative 'retest_test/file_changes_test' diff --git a/features/bundler-app/retest/retest_test/file_changes_test.rb b/features/bundler-app/retest/retest_test/file_changes_test.rb index c2f10933..b632b561 100644 --- a/features/bundler-app/retest/retest_test/file_changes_test.rb +++ b/features/bundler-app/retest/retest_test/file_changes_test.rb @@ -4,11 +4,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_start_retest - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~EXPECTED, @output.read Launching Retest... @@ -17,7 +17,7 @@ def test_start_retest end def test_modifying_existing_file - @output, @pid = launch_retest @command + launch_retest @command modify_file('lib/bundler_app/bottles.rb') @@ -26,7 +26,7 @@ def test_modifying_existing_file end def test_modifying_existing_test_file - @output, @pid = launch_retest @command + launch_retest @command modify_file('test/bundler_app/test_bottles.rb') @@ -35,17 +35,18 @@ def test_modifying_existing_test_file end def test_creating_a_new_test_file - @output, @pid = launch_retest @command + launch_retest @command create_file 'test/bundler_app/test_foo.rb' assert_match "Test File Selected: test/bundler_app/test_foo.rb", @output.read + ensure delete_file 'test/bundler_app/test_foo.rb' end def test_creating_a_new_file - @output, @pid = launch_retest @command + launch_retest @command create_file 'lib/bundler_app/foo.rb' assert_match <<~EXPECTED, @output.read @@ -62,6 +63,7 @@ def test_creating_a_new_file modify_file('lib/bundler_app/foo.rb') assert_match "Test File Selected: test/bundler_app/test_foo.rb", @output.read + ensure delete_file 'lib/bundler_app/foo.rb' delete_file 'test/bundler_app/test_foo.rb' end @@ -70,11 +72,12 @@ def test_untracked_file create_file 'lib/bundler_app/foo.rb', should_sleep: false create_file 'test/bundler_app/test_foo.rb', should_sleep: false - @output, @pid = launch_retest @command + launch_retest @command modify_file 'lib/bundler_app/foo.rb' assert_match "Test File Selected: test/bundler_app/test_foo.rb", @output.read + ensure delete_file 'lib/bundler_app/foo.rb' delete_file 'test/bundler_app/test_foo.rb' end diff --git a/features/bundler-app/retest/support/output_file.rb b/features/bundler-app/retest/support/output_file.rb index ec3cd8b6..c58f646e 100644 --- a/features/bundler-app/retest/support/output_file.rb +++ b/features/bundler-app/retest/support/output_file.rb @@ -1,35 +1,21 @@ -require 'securerandom' - class OutputFile - attr_reader :id - + attr_reader :output def initialize - @id = SecureRandom.hex(10) - create_file + @output = Tempfile.new end def path - "tmp/output-#{id}.log" + @output.path end - alias :to_s :path def read - return unless File.exist?(path) - - File.read(path).split('').last + @output.rewind + @output.read.split('').last end def delete - return unless File.exist?(path) - - File.delete(path) + @output.close + @output.unlink end alias :clear :delete - - private - - def create_file - Dir.mkdir('tmp') unless Dir.exist?('tmp') - File.open(path, "w") - end end diff --git a/features/bundler-app/retest/support/test_helper.rb b/features/bundler-app/retest/support/test_helper.rb new file mode 100644 index 00000000..2b41621f --- /dev/null +++ b/features/bundler-app/retest/support/test_helper.rb @@ -0,0 +1,59 @@ +require_relative 'output_file' + +module FileHelper + 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 wait(sleep_seconds: default_sleep_seconds) + sleep sleep_seconds + end + + def modify_file(path, sleep_seconds: 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 + 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 + end + + def delete_file(path) + return unless File.exist? path + + File.delete path + end + + def rename_file(path, new_path) + return unless File.exist? path + + File.rename path, new_path + 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 end_retest(file = nil, pid = nil) + @output&.delete + @rd&.close + @input&.close + if @pid + Process.kill('SIGHUP', @pid) + Process.detach(@pid) + end +end diff --git a/features/bundler-app/retest/test_helper.rb b/features/bundler-app/retest/test_helper.rb deleted file mode 100644 index f0dbe46d..00000000 --- a/features/bundler-app/retest/test_helper.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'retest' -require_relative 'support/output_file' - -module FileHelper - def modify_file(path) - return unless File.exist? path - - old_content = File.read(path) - File.open(path, 'w') { |file| file.write old_content } - - sleep 1 - end - - def create_file(path, should_sleep: true) - File.open(path, "w").tap(&:close) - - sleep 1 if should_sleep - end - - def delete_file(path) - return unless File.exist? path - - File.delete path - end -end - -def launch_retest(command) - file = OutputFile.new - pid = Process.spawn command, out: file.path - sleep 1.5 - [file, pid] -end - -def end_retest(file, pid) - file&.delete - if pid - Process.kill('SIGHUP', pid) - Process.detach(pid) - end -end \ No newline at end of file diff --git a/features/git-ruby/docker-compose.yml b/features/git-ruby/docker-compose.yml index 268ec9b4..c474455f 100644 --- a/features/git-ruby/docker-compose.yml +++ b/features/git-ruby/docker-compose.yml @@ -3,4 +3,7 @@ services: build: . volumes: - .:/usr/src/app + environment: + - DEFAULT_SLEEP_SECONDS=1 + - LAUNCH_SLEEP_SECONDS=1.5 command: ruby retest/retest_test.rb diff --git a/features/git-ruby/retest/retest_test.rb b/features/git-ruby/retest/retest_test.rb index 4ff24c40..058c035f 100644 --- a/features/git-ruby/retest/retest_test.rb +++ b/features/git-ruby/retest/retest_test.rb @@ -1,4 +1,5 @@ -require_relative 'test_helper' +require 'retest' +require_relative 'support/test_helper' require 'minitest/autorun' $stdout.sync = true @@ -11,11 +12,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_start_retest - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~EXPECTED, @output.read Launching Retest... @@ -55,7 +56,7 @@ def test_diffs_from_other_branch `git add .` `git commit -m "Rename, Add and Remove files"` - @output, @pid = launch_retest 'retest --diff=main --ruby' + launch_retest 'retest --diff=main --ruby' sleep 2 assert_match <<~EXPECTED, @output.read diff --git a/features/git-ruby/retest/support/output_file.rb b/features/git-ruby/retest/support/output_file.rb index fd8e1092..c58f646e 100644 --- a/features/git-ruby/retest/support/output_file.rb +++ b/features/git-ruby/retest/support/output_file.rb @@ -1,35 +1,21 @@ -require 'securerandom' - class OutputFile - attr_reader :id - + attr_reader :output def initialize - @id = SecureRandom.hex(10) - create_file + @output = Tempfile.new end def path - "tmp/output-#{id}.log" + @output.path end - alias :to_s :path def read - return unless File.exists?(path) - - File.read(path).split('').last + @output.rewind + @output.read.split('').last end def delete - return unless File.exists?(path) - - File.delete(path) + @output.close + @output.unlink end alias :clear :delete - - private - - def create_file - Dir.mkdir('tmp') unless Dir.exist?('tmp') - File.open(path, "w") - end end diff --git a/features/git-ruby/retest/support/test_helper.rb b/features/git-ruby/retest/support/test_helper.rb new file mode 100644 index 00000000..2b41621f --- /dev/null +++ b/features/git-ruby/retest/support/test_helper.rb @@ -0,0 +1,59 @@ +require_relative 'output_file' + +module FileHelper + 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 wait(sleep_seconds: default_sleep_seconds) + sleep sleep_seconds + end + + def modify_file(path, sleep_seconds: 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 + 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 + end + + def delete_file(path) + return unless File.exist? path + + File.delete path + end + + def rename_file(path, new_path) + return unless File.exist? path + + File.rename path, new_path + 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 end_retest(file = nil, pid = nil) + @output&.delete + @rd&.close + @input&.close + if @pid + Process.kill('SIGHUP', @pid) + Process.detach(@pid) + end +end diff --git a/features/git-ruby/retest/test_helper.rb b/features/git-ruby/retest/test_helper.rb deleted file mode 100644 index 385dbc61..00000000 --- a/features/git-ruby/retest/test_helper.rb +++ /dev/null @@ -1,46 +0,0 @@ -require 'retest' -require_relative 'support/output_file' - -module FileHelper - def modify_file(path) - return unless File.exist? path - - old_content = File.read(path) - File.open(path, 'w') { |file| file.write old_content } - - sleep 1 - end - - def create_file(path, should_sleep: true) - File.open(path, "w").tap(&:close) - - sleep 1 if should_sleep - end - - def delete_file(path) - return unless File.exist? path - - File.delete path - end - - def rename_file(path, new_path) - return unless File.exist? path - - File.rename path, new_path - end -end - -def launch_retest(command) - file = OutputFile.new - pid = Process.spawn command, out: file.path - sleep 1.5 - [file, pid] -end - -def end_retest(file, pid) - file&.delete - if pid - Process.kill('SIGHUP', pid) - Process.detach(pid) - end -end \ No newline at end of file diff --git a/features/hanami-app/docker-compose.yml b/features/hanami-app/docker-compose.yml index 268ec9b4..bfecb0bf 100644 --- a/features/hanami-app/docker-compose.yml +++ b/features/hanami-app/docker-compose.yml @@ -3,4 +3,7 @@ services: build: . volumes: - .:/usr/src/app + environment: + - DEFAULT_SLEEP_SECONDS=5 + - LAUNCH_SLEEP_SECONDS=1.5 command: ruby retest/retest_test.rb diff --git a/features/hanami-app/retest/retest_test.rb b/features/hanami-app/retest/retest_test.rb index aa0e9dbf..5f385522 100644 --- a/features/hanami-app/retest/retest_test.rb +++ b/features/hanami-app/retest/retest_test.rb @@ -1,4 +1,5 @@ -require_relative 'test_helper' +require 'retest' +require_relative 'support/test_helper' require 'minitest/autorun' $stdout.sync = true @@ -11,11 +12,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_start_retest - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~EXPECTED, @output.read Launching Retest... @@ -24,7 +25,7 @@ def test_start_retest end def test_modify_a_file - @output, @pid = launch_retest @command + launch_retest @command modify_file 'apps/web/controllers/books/create.rb' @@ -39,11 +40,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_start_retest - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~EXPECTED, @output.read Launching Retest... @@ -52,7 +53,7 @@ def test_start_retest end def test_modify_a_file - @output, @pid = launch_retest @command + launch_retest @command modify_file 'apps/web/controllers/books/create.rb' @@ -62,11 +63,11 @@ def test_modify_a_file class AutoFlagTest < Minitest::Test def teardown - end_retest @output, @pid + end_retest end def test_with_no_command - @output, @pid = launch_retest 'retest' + launch_retest 'retest' assert_match <<~OUTPUT, @output.read Setup identified: [RAKE]. Using command: 'bundle exec rake test TEST=' @@ -76,7 +77,7 @@ def test_with_no_command end def test_with_no_command_all - @output, @pid = launch_retest 'retest --all' + launch_retest 'retest --all' assert_match <<~OUTPUT, @output.read Setup identified: [RAKE]. Using command: 'bundle exec rake test' diff --git a/features/hanami-app/retest/support/output_file.rb b/features/hanami-app/retest/support/output_file.rb index ec3cd8b6..c58f646e 100644 --- a/features/hanami-app/retest/support/output_file.rb +++ b/features/hanami-app/retest/support/output_file.rb @@ -1,35 +1,21 @@ -require 'securerandom' - class OutputFile - attr_reader :id - + attr_reader :output def initialize - @id = SecureRandom.hex(10) - create_file + @output = Tempfile.new end def path - "tmp/output-#{id}.log" + @output.path end - alias :to_s :path def read - return unless File.exist?(path) - - File.read(path).split('').last + @output.rewind + @output.read.split('').last end def delete - return unless File.exist?(path) - - File.delete(path) + @output.close + @output.unlink end alias :clear :delete - - private - - def create_file - Dir.mkdir('tmp') unless Dir.exist?('tmp') - File.open(path, "w") - end end diff --git a/features/hanami-app/retest/support/test_helper.rb b/features/hanami-app/retest/support/test_helper.rb new file mode 100644 index 00000000..2b41621f --- /dev/null +++ b/features/hanami-app/retest/support/test_helper.rb @@ -0,0 +1,59 @@ +require_relative 'output_file' + +module FileHelper + 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 wait(sleep_seconds: default_sleep_seconds) + sleep sleep_seconds + end + + def modify_file(path, sleep_seconds: 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 + 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 + end + + def delete_file(path) + return unless File.exist? path + + File.delete path + end + + def rename_file(path, new_path) + return unless File.exist? path + + File.rename path, new_path + 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 end_retest(file = nil, pid = nil) + @output&.delete + @rd&.close + @input&.close + if @pid + Process.kill('SIGHUP', @pid) + Process.detach(@pid) + end +end diff --git a/features/hanami-app/retest/test_helper.rb b/features/hanami-app/retest/test_helper.rb deleted file mode 100644 index 3798d664..00000000 --- a/features/hanami-app/retest/test_helper.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'retest' -require_relative 'support/output_file' - -module FileHelper - def modify_file(path) - return unless File.exist? path - - old_content = File.read(path) - File.open(path, 'w') { |file| file.write old_content } - - sleep 5 - end - - def create_file(path, should_sleep: true) - File.open(path, "w").tap(&:close) - - sleep 5 if should_sleep - end - - def delete_file(path) - return unless File.exist? path - - File.delete path - end -end - -def launch_retest(command) - file = OutputFile.new - pid = Process.spawn command, out: file.path - sleep 1.5 - [file, pid] -end - -def end_retest(file, pid) - file&.delete - if pid - Process.kill('SIGHUP', pid) - Process.detach(pid) - end -end \ No newline at end of file diff --git a/features/rails-app/docker-compose.yml b/features/rails-app/docker-compose.yml index 268ec9b4..6e3ef76c 100644 --- a/features/rails-app/docker-compose.yml +++ b/features/rails-app/docker-compose.yml @@ -3,4 +3,7 @@ services: build: . volumes: - .:/usr/src/app + environment: + - DEFAULT_SLEEP_SECONDS=10 + - LAUNCH_SLEEP_SECONDS=1.5 command: ruby retest/retest_test.rb diff --git a/features/rails-app/retest/retest_test.rb b/features/rails-app/retest/retest_test.rb index e1e3cba9..cee31998 100644 --- a/features/rails-app/retest/retest_test.rb +++ b/features/rails-app/retest/retest_test.rb @@ -1,4 +1,5 @@ -require_relative 'test_helper' +require 'retest' +require_relative 'support/test_helper' require 'minitest/autorun' $stdout.sync = true @@ -11,11 +12,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_start_retest - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~EXPECTED, @output.read Launching Retest... @@ -24,7 +25,7 @@ def test_start_retest end def test_modify_a_file - @output, @pid = launch_retest @command + launch_retest @command modify_file 'app/models/post.rb' @@ -39,11 +40,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_start_retest - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~EXPECTED, @output.read Launching Retest... @@ -52,7 +53,7 @@ def test_start_retest end def test_modify_a_file - @output, @pid = launch_retest @command + launch_retest @command modify_file 'app/models/post.rb' @@ -62,11 +63,11 @@ def test_modify_a_file class AutoFlagTest < Minitest::Test def teardown - end_retest @output, @pid + end_retest end def test_with_no_command - @output, @pid = launch_retest 'retest' + launch_retest 'retest' assert_match <<~OUTPUT, @output.read Setup identified: [RAILS]. Using command: 'bin/rails test ' @@ -76,7 +77,7 @@ def test_with_no_command end def test_with_no_command_all - @output, @pid = launch_retest 'retest --all' + launch_retest 'retest --all' assert_match <<~OUTPUT, @output.read Setup identified: [RAILS]. Using command: 'bin/rails test' @@ -120,8 +121,8 @@ def test_diffs_from_other_branch `git add .` `git commit -m "Scaffold books"` - @output, @pid = launch_retest 'retest --diff=main' - sleep 10 + launch_retest 'retest --diff=main' + wait assert_match <<~EXPECTED, @output.read Setup identified: [RAILS]. Using command: 'bin/rails test ' diff --git a/features/rails-app/retest/support/output_file.rb b/features/rails-app/retest/support/output_file.rb index ec3cd8b6..c58f646e 100644 --- a/features/rails-app/retest/support/output_file.rb +++ b/features/rails-app/retest/support/output_file.rb @@ -1,35 +1,21 @@ -require 'securerandom' - class OutputFile - attr_reader :id - + attr_reader :output def initialize - @id = SecureRandom.hex(10) - create_file + @output = Tempfile.new end def path - "tmp/output-#{id}.log" + @output.path end - alias :to_s :path def read - return unless File.exist?(path) - - File.read(path).split('').last + @output.rewind + @output.read.split('').last end def delete - return unless File.exist?(path) - - File.delete(path) + @output.close + @output.unlink end alias :clear :delete - - private - - def create_file - Dir.mkdir('tmp') unless Dir.exist?('tmp') - File.open(path, "w") - end end diff --git a/features/rails-app/retest/support/test_helper.rb b/features/rails-app/retest/support/test_helper.rb new file mode 100644 index 00000000..2b41621f --- /dev/null +++ b/features/rails-app/retest/support/test_helper.rb @@ -0,0 +1,59 @@ +require_relative 'output_file' + +module FileHelper + 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 wait(sleep_seconds: default_sleep_seconds) + sleep sleep_seconds + end + + def modify_file(path, sleep_seconds: 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 + 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 + end + + def delete_file(path) + return unless File.exist? path + + File.delete path + end + + def rename_file(path, new_path) + return unless File.exist? path + + File.rename path, new_path + 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 end_retest(file = nil, pid = nil) + @output&.delete + @rd&.close + @input&.close + if @pid + Process.kill('SIGHUP', @pid) + Process.detach(@pid) + end +end diff --git a/features/rails-app/retest/test_helper.rb b/features/rails-app/retest/test_helper.rb deleted file mode 100644 index 2187eb56..00000000 --- a/features/rails-app/retest/test_helper.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'retest' -require_relative 'support/output_file' - -module FileHelper - def modify_file(path) - return unless File.exist? path - - old_content = File.read(path) - File.open(path, 'w') { |file| file.write old_content } - - sleep 10 - end - - def create_file(path, should_sleep: true) - File.open(path, "w").tap(&:close) - - sleep 10 if should_sleep - end - - def delete_file(path) - return unless File.exist? path - - File.delete path - end -end - -def launch_retest(command) - file = OutputFile.new - pid = Process.spawn command, out: file.path - sleep 1.5 - [file, pid] -end - -def end_retest(file, pid) - file&.delete - if pid - Process.kill('SIGHUP', pid) - Process.detach(pid) - end -end \ No newline at end of file diff --git a/features/rspec-rails/docker-compose.yml b/features/rspec-rails/docker-compose.yml index 268ec9b4..6e3ef76c 100644 --- a/features/rspec-rails/docker-compose.yml +++ b/features/rspec-rails/docker-compose.yml @@ -3,4 +3,7 @@ services: build: . volumes: - .:/usr/src/app + environment: + - DEFAULT_SLEEP_SECONDS=10 + - LAUNCH_SLEEP_SECONDS=1.5 command: ruby retest/retest_test.rb diff --git a/features/rspec-rails/retest/retest_test.rb b/features/rspec-rails/retest/retest_test.rb index 481af7be..11c19ba1 100644 --- a/features/rspec-rails/retest/retest_test.rb +++ b/features/rspec-rails/retest/retest_test.rb @@ -1,4 +1,5 @@ -require_relative 'test_helper' +require 'retest' +require_relative 'support/test_helper' require 'minitest/autorun' $stdout.sync = true @@ -11,11 +12,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_start_retest - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~EXPECTED, @output.read Launching Retest... @@ -24,7 +25,7 @@ def test_start_retest end def test_modify_a_file - @output, @pid = launch_retest @command + launch_retest @command modify_file 'app/models/post.rb' @@ -39,11 +40,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_start_retest - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~EXPECTED, @output.read Launching Retest... @@ -52,7 +53,7 @@ def test_start_retest end def test_modify_a_file - @output, @pid = launch_retest @command + launch_retest @command modify_file 'app/models/post.rb', sleep_seconds: 15 @@ -62,11 +63,11 @@ def test_modify_a_file class AutoFlagTest < Minitest::Test def teardown - end_retest @output, @pid + end_retest end def test_with_no_command - @output, @pid = launch_retest 'retest' + launch_retest 'retest' assert_match <<~OUTPUT, @output.read Setup identified: [RSPEC]. Using command: 'bundle exec rspec ' @@ -76,7 +77,7 @@ def test_with_no_command end def test_with_no_command_all - @output, @pid = launch_retest 'retest --all' + launch_retest 'retest --all' assert_match <<~OUTPUT, @output.read Setup identified: [RSPEC]. Using command: 'bundle exec rspec' diff --git a/features/rspec-rails/retest/support/output_file.rb b/features/rspec-rails/retest/support/output_file.rb index ec3cd8b6..c58f646e 100644 --- a/features/rspec-rails/retest/support/output_file.rb +++ b/features/rspec-rails/retest/support/output_file.rb @@ -1,35 +1,21 @@ -require 'securerandom' - class OutputFile - attr_reader :id - + attr_reader :output def initialize - @id = SecureRandom.hex(10) - create_file + @output = Tempfile.new end def path - "tmp/output-#{id}.log" + @output.path end - alias :to_s :path def read - return unless File.exist?(path) - - File.read(path).split('').last + @output.rewind + @output.read.split('').last end def delete - return unless File.exist?(path) - - File.delete(path) + @output.close + @output.unlink end alias :clear :delete - - private - - def create_file - Dir.mkdir('tmp') unless Dir.exist?('tmp') - File.open(path, "w") - end end diff --git a/features/rspec-rails/retest/support/test_helper.rb b/features/rspec-rails/retest/support/test_helper.rb new file mode 100644 index 00000000..2b41621f --- /dev/null +++ b/features/rspec-rails/retest/support/test_helper.rb @@ -0,0 +1,59 @@ +require_relative 'output_file' + +module FileHelper + 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 wait(sleep_seconds: default_sleep_seconds) + sleep sleep_seconds + end + + def modify_file(path, sleep_seconds: 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 + 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 + end + + def delete_file(path) + return unless File.exist? path + + File.delete path + end + + def rename_file(path, new_path) + return unless File.exist? path + + File.rename path, new_path + 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 end_retest(file = nil, pid = nil) + @output&.delete + @rd&.close + @input&.close + if @pid + Process.kill('SIGHUP', @pid) + Process.detach(@pid) + end +end diff --git a/features/rspec-rails/retest/test_helper.rb b/features/rspec-rails/retest/test_helper.rb deleted file mode 100644 index b8f6c692..00000000 --- a/features/rspec-rails/retest/test_helper.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'retest' -require_relative 'support/output_file' - -module FileHelper - def modify_file(path, sleep_seconds: 10) - return unless File.exist? path - - old_content = File.read(path) - File.open(path, 'w') { |file| file.write old_content } - - sleep sleep_seconds - end - - def create_file(path, should_sleep: true) - File.open(path, "w").tap(&:close) - - sleep 10 if should_sleep - end - - def delete_file(path) - return unless File.exist? path - - File.delete path - end -end - -def launch_retest(command) - file = OutputFile.new - pid = Process.spawn command, out: file.path - sleep 1.5 - [file, pid] -end - -def end_retest(file, pid) - file&.delete - if pid - Process.kill('SIGHUP', pid) - Process.detach(pid) - end -end \ No newline at end of file diff --git a/features/rspec-ruby/docker-compose.yml b/features/rspec-ruby/docker-compose.yml index 268ec9b4..c474455f 100644 --- a/features/rspec-ruby/docker-compose.yml +++ b/features/rspec-ruby/docker-compose.yml @@ -3,4 +3,7 @@ services: build: . volumes: - .:/usr/src/app + environment: + - DEFAULT_SLEEP_SECONDS=1 + - LAUNCH_SLEEP_SECONDS=1.5 command: ruby retest/retest_test.rb diff --git a/features/rspec-ruby/retest/retest_test.rb b/features/rspec-ruby/retest/retest_test.rb index 5bb3bcc9..421cb470 100644 --- a/features/rspec-ruby/retest/retest_test.rb +++ b/features/rspec-ruby/retest/retest_test.rb @@ -1,4 +1,5 @@ -require_relative 'test_helper' +require 'retest' +require_relative 'support/test_helper' require 'minitest/autorun' require_relative 'retest_test/file_changes_test' require_relative 'retest_test/flags_test' diff --git a/features/rspec-ruby/retest/retest_test/file_changes_test.rb b/features/rspec-ruby/retest/retest_test/file_changes_test.rb index c758cd23..2fe24a89 100644 --- a/features/rspec-ruby/retest/retest_test/file_changes_test.rb +++ b/features/rspec-ruby/retest/retest_test/file_changes_test.rb @@ -4,11 +4,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_start_retest - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~EXPECTED, @output.read Launching Retest... @@ -17,7 +17,7 @@ def test_start_retest end def test_modifying_existing_file - @output, @pid = launch_retest @command + launch_retest @command modify_file('lib/bottles.rb') @@ -26,7 +26,7 @@ def test_modifying_existing_file end def test_modifying_existing_test_file - @output, @pid = launch_retest @command + launch_retest @command modify_file('spec/bottles_spec.rb') @@ -35,7 +35,7 @@ def test_modifying_existing_test_file end def test_creating_a_new_test_file - @output, @pid = launch_retest @command + launch_retest @command create_file 'foo_spec.rb' @@ -46,7 +46,7 @@ def test_creating_a_new_test_file end def test_creating_a_new_file - @output, @pid = launch_retest @command + launch_retest @command create_file 'foo.rb' assert_match <<~EXPECTED, @output.read @@ -72,7 +72,7 @@ def test_untracked_file create_file 'foo.rb', should_sleep: false create_file 'foo_spec.rb', should_sleep: false - @output, @pid = launch_retest @command + launch_retest @command modify_file 'foo.rb' assert_match "Test File Selected: foo_spec.rb", @output.read diff --git a/features/rspec-ruby/retest/retest_test/flags_test.rb b/features/rspec-ruby/retest/retest_test/flags_test.rb index 2ba8350e..c882ae16 100644 --- a/features/rspec-ruby/retest/retest_test/flags_test.rb +++ b/features/rspec-ruby/retest/retest_test/flags_test.rb @@ -3,11 +3,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_with_no_command - @output, @pid = launch_retest 'retest' + launch_retest 'retest' assert_match <<~OUTPUT, @output.read Setup identified: [RSPEC]. Using command: 'bundle exec rspec ' diff --git a/features/rspec-ruby/retest/support/output_file.rb b/features/rspec-ruby/retest/support/output_file.rb index ec3cd8b6..c58f646e 100644 --- a/features/rspec-ruby/retest/support/output_file.rb +++ b/features/rspec-ruby/retest/support/output_file.rb @@ -1,35 +1,21 @@ -require 'securerandom' - class OutputFile - attr_reader :id - + attr_reader :output def initialize - @id = SecureRandom.hex(10) - create_file + @output = Tempfile.new end def path - "tmp/output-#{id}.log" + @output.path end - alias :to_s :path def read - return unless File.exist?(path) - - File.read(path).split('').last + @output.rewind + @output.read.split('').last end def delete - return unless File.exist?(path) - - File.delete(path) + @output.close + @output.unlink end alias :clear :delete - - private - - def create_file - Dir.mkdir('tmp') unless Dir.exist?('tmp') - File.open(path, "w") - end end diff --git a/features/rspec-ruby/retest/support/test_helper.rb b/features/rspec-ruby/retest/support/test_helper.rb new file mode 100644 index 00000000..2b41621f --- /dev/null +++ b/features/rspec-ruby/retest/support/test_helper.rb @@ -0,0 +1,59 @@ +require_relative 'output_file' + +module FileHelper + 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 wait(sleep_seconds: default_sleep_seconds) + sleep sleep_seconds + end + + def modify_file(path, sleep_seconds: 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 + 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 + end + + def delete_file(path) + return unless File.exist? path + + File.delete path + end + + def rename_file(path, new_path) + return unless File.exist? path + + File.rename path, new_path + 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 end_retest(file = nil, pid = nil) + @output&.delete + @rd&.close + @input&.close + if @pid + Process.kill('SIGHUP', @pid) + Process.detach(@pid) + end +end diff --git a/features/rspec-ruby/retest/test_helper.rb b/features/rspec-ruby/retest/test_helper.rb deleted file mode 100644 index f0dbe46d..00000000 --- a/features/rspec-ruby/retest/test_helper.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'retest' -require_relative 'support/output_file' - -module FileHelper - def modify_file(path) - return unless File.exist? path - - old_content = File.read(path) - File.open(path, 'w') { |file| file.write old_content } - - sleep 1 - end - - def create_file(path, should_sleep: true) - File.open(path, "w").tap(&:close) - - sleep 1 if should_sleep - end - - def delete_file(path) - return unless File.exist? path - - File.delete path - end -end - -def launch_retest(command) - file = OutputFile.new - pid = Process.spawn command, out: file.path - sleep 1.5 - [file, pid] -end - -def end_retest(file, pid) - file&.delete - if pid - Process.kill('SIGHUP', pid) - Process.detach(pid) - end -end \ No newline at end of file diff --git a/features/ruby-app/Gemfile b/features/ruby-app/Gemfile index c4cff67a..d0c3b25a 100644 --- a/features/ruby-app/Gemfile +++ b/features/ruby-app/Gemfile @@ -1,2 +1,3 @@ source 'https://rubygems.org' gem 'minitest', '~> 5.4' +gem 'byebug' \ No newline at end of file diff --git a/features/ruby-app/Gemfile.lock b/features/ruby-app/Gemfile.lock index 6bdfd6c6..fd16f24d 100644 --- a/features/ruby-app/Gemfile.lock +++ b/features/ruby-app/Gemfile.lock @@ -1,13 +1,15 @@ GEM remote: https://rubygems.org/ specs: + byebug (11.1.3) minitest (5.14.0) PLATFORMS ruby DEPENDENCIES + byebug minitest (~> 5.4) BUNDLED WITH - 2.1.4 + 2.4.21 diff --git a/features/ruby-app/docker-compose.yml b/features/ruby-app/docker-compose.yml index 268ec9b4..c474455f 100644 --- a/features/ruby-app/docker-compose.yml +++ b/features/ruby-app/docker-compose.yml @@ -3,4 +3,7 @@ services: build: . volumes: - .:/usr/src/app + environment: + - DEFAULT_SLEEP_SECONDS=1 + - LAUNCH_SLEEP_SECONDS=1.5 command: ruby retest/retest_test.rb diff --git a/features/ruby-app/retest/retest_test.rb b/features/ruby-app/retest/retest_test.rb index 0de43cb7..f48ba94a 100644 --- a/features/ruby-app/retest/retest_test.rb +++ b/features/ruby-app/retest/retest_test.rb @@ -1,4 +1,5 @@ -require_relative 'test_helper' +require 'retest' +require_relative 'support/test_helper' require 'minitest/autorun' require_relative 'retest_test/file_changes_test' require_relative 'retest_test/flags_test' diff --git a/features/ruby-app/retest/retest_test/file_changes_test.rb b/features/ruby-app/retest/retest_test/file_changes_test.rb index 3b9e1c96..0e6ea7a7 100644 --- a/features/ruby-app/retest/retest_test/file_changes_test.rb +++ b/features/ruby-app/retest/retest_test/file_changes_test.rb @@ -4,11 +4,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_start_retest - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~EXPECTED, @output.read Launching Retest... @@ -17,7 +17,7 @@ def test_start_retest end def test_modifying_existing_file - @output, @pid = launch_retest @command + launch_retest @command modify_file('lib/bottles.rb') @@ -26,7 +26,7 @@ def test_modifying_existing_file end def test_modifying_existing_test_file - @output, @pid = launch_retest @command + launch_retest @command modify_file('test/bottles_test.rb') @@ -35,17 +35,18 @@ def test_modifying_existing_test_file end def test_creating_a_new_test_file - @output, @pid = launch_retest @command + launch_retest @command create_file 'foo_test.rb' assert_match "Test File Selected: foo_test.rb", @output.read + ensure delete_file 'foo_test.rb' end def test_creating_a_new_file - @output, @pid = launch_retest @command + launch_retest @command create_file 'foo.rb' assert_match <<~EXPECTED, @output.read @@ -62,6 +63,7 @@ def test_creating_a_new_file modify_file('foo.rb') assert_match "Test File Selected: foo_test.rb", @output.read + ensure delete_file 'foo.rb' delete_file 'foo_test.rb' end @@ -70,11 +72,12 @@ def test_untracked_file create_file 'foo.rb', should_sleep: false create_file 'foo_test.rb', should_sleep: false - @output, @pid = launch_retest @command + launch_retest @command modify_file 'foo.rb' assert_match "Test File Selected: foo_test.rb", @output.read + ensure delete_file 'foo.rb' delete_file 'foo_test.rb' end diff --git a/features/ruby-app/retest/retest_test/flags_test.rb b/features/ruby-app/retest/retest_test/flags_test.rb index 37c3df66..8fd86f64 100644 --- a/features/ruby-app/retest/retest_test/flags_test.rb +++ b/features/ruby-app/retest/retest_test/flags_test.rb @@ -3,11 +3,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_with_no_command - @output, @pid = launch_retest 'retest' + launch_retest 'retest' assert_match <<~OUTPUT, @output.read Setup identified: [RUBY]. Using command: 'bundle exec ruby ' diff --git a/features/ruby-app/retest/retest_test/matching_unmatching_command_test.rb b/features/ruby-app/retest/retest_test/matching_unmatching_command_test.rb index f80ecc0a..e9356174 100644 --- a/features/ruby-app/retest/retest_test/matching_unmatching_command_test.rb +++ b/features/ruby-app/retest/retest_test/matching_unmatching_command_test.rb @@ -4,58 +4,27 @@ def setup end def teardown - end_retest @output, @pid + end_retest delete_file('test/other_bottles_test.rb') end def test_not_displaying_options_on_unmatching_command - @output, @pid = launch_retest "retest 'echo there was no command'" + launch_retest "retest 'echo there was no command'" modify_file('lib/bottles.rb') 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 - stdin = Tempfile.new - stdin.write "2\n" - pid, _, stdout = launch_retest('retest --ruby', input: stdin) + launch_retest('retest --ruby') create_file 'foo_test.rb' - assert_match "Test File Selected: foo_test.rb", stdout.tap(&:rewind).read + assert_match "Test File Selected: foo_test.rb", @output.read modify_file('lib/bottles.rb') - assert_match <<~EXPECTED.chomp, stdout.tap(&:rewind).read + assert_match <<~EXPECTED.chomp, @output.read We found few tests matching: lib/bottles.rb [0] - test/bottles_test.rb @@ -64,11 +33,15 @@ def test_displaying_options_on_matching_command 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 + @input.write "2\n" + wait + + assert_match "Test File Selected: foo_test.rb", @output.read + ensure - end_retest(pid: pid, input: stdin, output: stdout) + delete_file 'foo_test.rb' end end diff --git a/features/ruby-app/retest/support/output_file.rb b/features/ruby-app/retest/support/output_file.rb index ec3cd8b6..c58f646e 100644 --- a/features/ruby-app/retest/support/output_file.rb +++ b/features/ruby-app/retest/support/output_file.rb @@ -1,35 +1,21 @@ -require 'securerandom' - class OutputFile - attr_reader :id - + attr_reader :output def initialize - @id = SecureRandom.hex(10) - create_file + @output = Tempfile.new end def path - "tmp/output-#{id}.log" + @output.path end - alias :to_s :path def read - return unless File.exist?(path) - - File.read(path).split('').last + @output.rewind + @output.read.split('').last end def delete - return unless File.exist?(path) - - File.delete(path) + @output.close + @output.unlink end alias :clear :delete - - private - - def create_file - Dir.mkdir('tmp') unless Dir.exist?('tmp') - File.open(path, "w") - end end diff --git a/features/ruby-app/retest/support/test_helper.rb b/features/ruby-app/retest/support/test_helper.rb new file mode 100644 index 00000000..2b41621f --- /dev/null +++ b/features/ruby-app/retest/support/test_helper.rb @@ -0,0 +1,59 @@ +require_relative 'output_file' + +module FileHelper + 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 wait(sleep_seconds: default_sleep_seconds) + sleep sleep_seconds + end + + def modify_file(path, sleep_seconds: 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 + 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 + end + + def delete_file(path) + return unless File.exist? path + + File.delete path + end + + def rename_file(path, new_path) + return unless File.exist? path + + File.rename path, new_path + 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 end_retest(file = nil, pid = nil) + @output&.delete + @rd&.close + @input&.close + if @pid + Process.kill('SIGHUP', @pid) + Process.detach(@pid) + end +end diff --git a/features/ruby-app/retest/test_helper.rb b/features/ruby-app/retest/test_helper.rb deleted file mode 100644 index f0dbe46d..00000000 --- a/features/ruby-app/retest/test_helper.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'retest' -require_relative 'support/output_file' - -module FileHelper - def modify_file(path) - return unless File.exist? path - - old_content = File.read(path) - File.open(path, 'w') { |file| file.write old_content } - - sleep 1 - end - - def create_file(path, should_sleep: true) - File.open(path, "w").tap(&:close) - - sleep 1 if should_sleep - end - - def delete_file(path) - return unless File.exist? path - - File.delete path - end -end - -def launch_retest(command) - file = OutputFile.new - pid = Process.spawn command, out: file.path - sleep 1.5 - [file, pid] -end - -def end_retest(file, pid) - file&.delete - if pid - Process.kill('SIGHUP', pid) - Process.detach(pid) - end -end \ No newline at end of file diff --git a/features/ruby-bare/docker-compose.yml b/features/ruby-bare/docker-compose.yml index 268ec9b4..c474455f 100644 --- a/features/ruby-bare/docker-compose.yml +++ b/features/ruby-bare/docker-compose.yml @@ -3,4 +3,7 @@ services: build: . volumes: - .:/usr/src/app + environment: + - DEFAULT_SLEEP_SECONDS=1 + - LAUNCH_SLEEP_SECONDS=1.5 command: ruby retest/retest_test.rb diff --git a/features/ruby-bare/retest/flags/help.rb b/features/ruby-bare/retest/flags/help.rb index 942fe2ac..b29fe0ba 100644 --- a/features/ruby-bare/retest/flags/help.rb +++ b/features/ruby-bare/retest/flags/help.rb @@ -1,6 +1,10 @@ class TestHelpFlag < Minitest::Test + def teardown + end_retest + end + def test_help - @output, @pid = launch_retest 'retest --help' + launch_retest 'retest --help' assert_match <<~EXPECTED, @output.read Usage: retest [OPTIONS] [COMMAND] @@ -10,7 +14,7 @@ def test_help end def test_help_short_flag - @output, @pid = launch_retest 'retest -h' + launch_retest 'retest -h' assert_match <<~EXPECTED, @output.read Usage: retest [OPTIONS] [COMMAND] diff --git a/features/ruby-bare/retest/flags/version.rb b/features/ruby-bare/retest/flags/version.rb index 33bfdde1..6a1d4376 100644 --- a/features/ruby-bare/retest/flags/version.rb +++ b/features/ruby-bare/retest/flags/version.rb @@ -1,12 +1,16 @@ class TestVersionFlag < Minitest::Test + def teardown + end_retest + end + def test_version - @output, @pid = launch_retest 'retest --version' + launch_retest 'retest --version' assert_match /^1\.\d+\.\d+/, @output.read end def test_version_short_flag - @output, @pid = launch_retest 'retest -v' + launch_retest 'retest -v' assert_match /^1\.\d+\.\d+/, @output.read end diff --git a/features/ruby-bare/retest/retest_test.rb b/features/ruby-bare/retest/retest_test.rb index e72d1cd3..c525f9da 100644 --- a/features/ruby-bare/retest/retest_test.rb +++ b/features/ruby-bare/retest/retest_test.rb @@ -1,4 +1,5 @@ -require_relative 'test_helper' +require 'retest' +require_relative 'support/test_helper' require 'minitest/autorun' require_relative 'scenarios/auto_flag.rb' @@ -28,11 +29,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_start_retest - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~EXPECTED, @output.read Launching Retest... @@ -41,7 +42,7 @@ def test_start_retest end def test_modifying_existing_file - @output, @pid = launch_retest @command + launch_retest @command modify_file('program.rb') @@ -50,7 +51,7 @@ def test_modifying_existing_file end def test_modifying_existing_test_file - @output, @pid = launch_retest @command + launch_retest @command modify_file('program_test.rb') @@ -59,7 +60,7 @@ def test_modifying_existing_test_file end def test_creating_a_new_test_file - @output, @pid = launch_retest @command + launch_retest @command create_file 'foo_test.rb' @@ -69,7 +70,7 @@ def test_creating_a_new_test_file end def test_creating_a_new_file - @output, @pid = launch_retest @command + launch_retest @command create_file 'foo.rb' assert_match <<~EXPECTED, @output.read @@ -94,7 +95,7 @@ def test_untracked_file create_file 'foo.rb', should_sleep: false create_file 'foo_test.rb', should_sleep: false - @output, @pid = launch_retest @command + launch_retest @command modify_file 'foo.rb' assert_match "Test File Selected: foo_test.rb", @output.read diff --git a/features/ruby-bare/retest/scenarios/auto_flag.rb b/features/ruby-bare/retest/scenarios/auto_flag.rb index 1d5a7b3f..3cf701d7 100644 --- a/features/ruby-bare/retest/scenarios/auto_flag.rb +++ b/features/ruby-bare/retest/scenarios/auto_flag.rb @@ -1,6 +1,10 @@ class AutoFlag < Minitest::Test + def teardown + end_retest + end + def test_start_retest - @output, @pid = launch_retest 'retest' + launch_retest 'retest' assert_match <<~OUTPUT, @output.read Setup identified: [RUBY]. Using command: 'ruby ' @@ -11,7 +15,5 @@ def test_start_retest modify_file('program.rb') assert_match "Test File Selected: program_test.rb", @output.read - - end_retest @output, @pid end end \ No newline at end of file diff --git a/features/ruby-bare/retest/scenarios/changed_and_test_placeholders.rb b/features/ruby-bare/retest/scenarios/changed_and_test_placeholders.rb index 83e24d4d..1f3172b9 100644 --- a/features/ruby-bare/retest/scenarios/changed_and_test_placeholders.rb +++ b/features/ruby-bare/retest/scenarios/changed_and_test_placeholders.rb @@ -4,11 +4,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_file_modification - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~OUTPUT, @output.read Launching Retest... diff --git a/features/ruby-bare/retest/scenarios/changed_placeholder.rb b/features/ruby-bare/retest/scenarios/changed_placeholder.rb index 211e03e1..f681e3a8 100644 --- a/features/ruby-bare/retest/scenarios/changed_placeholder.rb +++ b/features/ruby-bare/retest/scenarios/changed_placeholder.rb @@ -4,11 +4,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_file_modification - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~OUTPUT, @output.read Launching Retest... diff --git a/features/ruby-bare/retest/scenarios/custom_extensions.rb b/features/ruby-bare/retest/scenarios/custom_extensions.rb index 996a86ed..20063230 100644 --- a/features/ruby-bare/retest/scenarios/custom_extensions.rb +++ b/features/ruby-bare/retest/scenarios/custom_extensions.rb @@ -4,7 +4,7 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_custom_extension @@ -12,7 +12,7 @@ def test_custom_extension create_file 'foo.rb', should_sleep: false create_file 'foo_test.rb', should_sleep: false - @output, @pid = launch_retest @command + launch_retest @command modify_file 'foo.rb' assert_match <<~EXPECTED, @output.read @@ -23,6 +23,7 @@ def test_custom_extension modify_file 'foo.txt' assert_match "I captured a change", @output.read + ensure delete_file 'foo.rb' delete_file 'foo_test.rb' delete_file 'foo.txt' diff --git a/features/ruby-bare/retest/scenarios/force_polling.rb b/features/ruby-bare/retest/scenarios/force_polling.rb index 036a9610..d2231775 100644 --- a/features/ruby-bare/retest/scenarios/force_polling.rb +++ b/features/ruby-bare/retest/scenarios/force_polling.rb @@ -4,11 +4,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_start_retest - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~EXPECTED, @output.read Launching Retest with polling method... @@ -17,7 +17,7 @@ def test_start_retest end def test_modifying_existing_file - @output, @pid = launch_retest @command + launch_retest @command modify_file('program.rb') @@ -26,7 +26,7 @@ def test_modifying_existing_file end def test_modifying_existing_test_file - @output, @pid = launch_retest @command + launch_retest @command modify_file('program_test.rb') @@ -35,17 +35,18 @@ def test_modifying_existing_test_file end def test_creating_a_new_test_file - @output, @pid = launch_retest @command + launch_retest @command create_file 'foo_test.rb' assert_match "Test File Selected: foo_test.rb", @output.read + ensure delete_file 'foo_test.rb' end def test_creating_a_new_file - @output, @pid = launch_retest @command + launch_retest @command create_file 'foo.rb' assert_match <<~EXPECTED, @output.read @@ -62,6 +63,7 @@ def test_creating_a_new_file modify_file('foo.rb') assert_match "Test File Selected: foo_test.rb", @output.read + ensure delete_file 'foo.rb' delete_file 'foo_test.rb' end @@ -70,11 +72,12 @@ def test_untracked_file create_file 'foo.rb', should_sleep: false create_file 'foo_test.rb', should_sleep: false - @output, @pid = launch_retest @command + launch_retest @command modify_file 'foo.rb' assert_match "Test File Selected: foo_test.rb", @output.read + ensure delete_file 'foo.rb' delete_file 'foo_test.rb' end diff --git a/features/ruby-bare/retest/scenarios/interruptions.rb b/features/ruby-bare/retest/scenarios/interruptions.rb index 57d18f00..78a2e165 100644 --- a/features/ruby-bare/retest/scenarios/interruptions.rb +++ b/features/ruby-bare/retest/scenarios/interruptions.rb @@ -1,10 +1,10 @@ class GracefulExitWhenInterrupting < Minitest::Test def teardown - end_retest @output, @pid + end_retest end def test_interruption - @output, @pid = launch_retest 'retest --ruby' + launch_retest 'retest --ruby' assert_match <<~EXPECTED, @output.read Launching Retest... @@ -12,7 +12,7 @@ def test_interruption EXPECTED Process.kill("INT", @pid) - sleep 1 + wait assert_match <<~EXPECTED, @output.read Goodbye diff --git a/features/ruby-bare/retest/scenarios/multiple_commands.rb b/features/ruby-bare/retest/scenarios/multiple_commands.rb index 62be067d..ad0eeec0 100644 --- a/features/ruby-bare/retest/scenarios/multiple_commands.rb +++ b/features/ruby-bare/retest/scenarios/multiple_commands.rb @@ -4,11 +4,11 @@ def setup end def teardown - end_retest @output, @pid + end_retest end def test_file_modification - @output, @pid = launch_retest @command + launch_retest @command assert_match <<~OUTPUT, @output.read Launching Retest... diff --git a/features/ruby-bare/retest/support/output_file.rb b/features/ruby-bare/retest/support/output_file.rb index ec3cd8b6..c58f646e 100644 --- a/features/ruby-bare/retest/support/output_file.rb +++ b/features/ruby-bare/retest/support/output_file.rb @@ -1,35 +1,21 @@ -require 'securerandom' - class OutputFile - attr_reader :id - + attr_reader :output def initialize - @id = SecureRandom.hex(10) - create_file + @output = Tempfile.new end def path - "tmp/output-#{id}.log" + @output.path end - alias :to_s :path def read - return unless File.exist?(path) - - File.read(path).split('').last + @output.rewind + @output.read.split('').last end def delete - return unless File.exist?(path) - - File.delete(path) + @output.close + @output.unlink end alias :clear :delete - - private - - def create_file - Dir.mkdir('tmp') unless Dir.exist?('tmp') - File.open(path, "w") - end end diff --git a/features/ruby-bare/retest/support/test_helper.rb b/features/ruby-bare/retest/support/test_helper.rb new file mode 100644 index 00000000..2b41621f --- /dev/null +++ b/features/ruby-bare/retest/support/test_helper.rb @@ -0,0 +1,59 @@ +require_relative 'output_file' + +module FileHelper + 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 wait(sleep_seconds: default_sleep_seconds) + sleep sleep_seconds + end + + def modify_file(path, sleep_seconds: 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 + 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 + end + + def delete_file(path) + return unless File.exist? path + + File.delete path + end + + def rename_file(path, new_path) + return unless File.exist? path + + File.rename path, new_path + 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 end_retest(file = nil, pid = nil) + @output&.delete + @rd&.close + @input&.close + if @pid + Process.kill('SIGHUP', @pid) + Process.detach(@pid) + end +end diff --git a/features/ruby-bare/retest/test_helper.rb b/features/ruby-bare/retest/test_helper.rb deleted file mode 100644 index f0dbe46d..00000000 --- a/features/ruby-bare/retest/test_helper.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'retest' -require_relative 'support/output_file' - -module FileHelper - def modify_file(path) - return unless File.exist? path - - old_content = File.read(path) - File.open(path, 'w') { |file| file.write old_content } - - sleep 1 - end - - def create_file(path, should_sleep: true) - File.open(path, "w").tap(&:close) - - sleep 1 if should_sleep - end - - def delete_file(path) - return unless File.exist? path - - File.delete path - end -end - -def launch_retest(command) - file = OutputFile.new - pid = Process.spawn command, out: file.path - sleep 1.5 - [file, pid] -end - -def end_retest(file, pid) - file&.delete - if pid - Process.kill('SIGHUP', pid) - Process.detach(pid) - end -end \ No newline at end of file diff --git a/features/support/output_file.rb b/features/support/output_file.rb new file mode 100644 index 00000000..c58f646e --- /dev/null +++ b/features/support/output_file.rb @@ -0,0 +1,21 @@ +class OutputFile + attr_reader :output + def initialize + @output = Tempfile.new + end + + def path + @output.path + end + + def read + @output.rewind + @output.read.split('').last + end + + def delete + @output.close + @output.unlink + end + alias :clear :delete +end diff --git a/features/support/test_helper.rb b/features/support/test_helper.rb new file mode 100644 index 00000000..2b41621f --- /dev/null +++ b/features/support/test_helper.rb @@ -0,0 +1,59 @@ +require_relative 'output_file' + +module FileHelper + 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 wait(sleep_seconds: default_sleep_seconds) + sleep sleep_seconds + end + + def modify_file(path, sleep_seconds: 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 + 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 + end + + def delete_file(path) + return unless File.exist? path + + File.delete path + end + + def rename_file(path, new_path) + return unless File.exist? path + + File.rename path, new_path + 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 end_retest(file = nil, pid = nil) + @output&.delete + @rd&.close + @input&.close + if @pid + Process.kill('SIGHUP', @pid) + Process.detach(@pid) + end +end