Skip to content

Commit

Permalink
Standardize testing helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexB52 committed Aug 12, 2024
1 parent 8189b05 commit ed6ae03
Show file tree
Hide file tree
Showing 70 changed files with 785 additions and 629 deletions.
1 change: 1 addition & 0 deletions bin/test/bundler-app
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions bin/test/git-ruby
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions bin/test/hanami-app
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions bin/test/rails-app
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions bin/test/reset_helpers
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions bin/test/rspec-rails
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions bin/test/rspec-ruby
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions bin/test/ruby-app
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions bin/test/ruby-bare
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions features/bundler-app/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Empty file.
3 changes: 2 additions & 1 deletion features/bundler-app/retest/retest_test.rb
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
14 changes: 7 additions & 7 deletions features/bundler-app/retest/retest_test/file_changes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand All @@ -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')

Expand All @@ -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')

Expand All @@ -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 'test/bundler_app/test_foo.rb'

Expand All @@ -45,7 +45,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 'lib/bundler_app/foo.rb'
assert_match <<~EXPECTED, @output.read
Expand All @@ -70,7 +70,7 @@ 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
Expand Down
28 changes: 7 additions & 21 deletions features/bundler-app/retest/support/output_file.rb
Original file line number Diff line number Diff line change
@@ -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
59 changes: 59 additions & 0 deletions features/bundler-app/retest/support/test_helper.rb
Original file line number Diff line number Diff line change
@@ -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
40 changes: 0 additions & 40 deletions features/bundler-app/retest/test_helper.rb

This file was deleted.

Empty file.
3 changes: 3 additions & 0 deletions features/git-ruby/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions features/git-ruby/retest/retest_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'test_helper'
require 'retest'
require_relative 'support/test_helper'
require 'minitest/autorun'

$stdout.sync = true
Expand All @@ -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...
Expand Down Expand Up @@ -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
Expand Down
28 changes: 7 additions & 21 deletions features/git-ruby/retest/support/output_file.rb
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit ed6ae03

Please sign in to comment.