diff --git a/exe/retest b/exe/retest index a5962af3..54ecf982 100755 --- a/exe/retest +++ b/exe/retest @@ -41,8 +41,8 @@ program = Retest::Program.new( # === LOGGING === case command -when Retest::Command::Rspec then puts "Setup: [RSPEC]" when Retest::Command::Rails then puts "Setup: [RAILS]" +when Retest::Command::Rspec then puts "Setup: [RSPEC]" when Retest::Command::Rake then puts "Setup: [RAKE]" when Retest::Command::Ruby then puts "Setup: [RUBY]" else puts "Setup: [UNKNOWN]" @@ -52,7 +52,6 @@ puts "Command: '#{command}'" # === DIFF ACTION === if options.params[:diff] - puts program.diff(options.params[:diff]) return end diff --git a/features/rails-app/bin/retest b/features/rails-app/bin/retest new file mode 100755 index 00000000..a78c59c8 --- /dev/null +++ b/features/rails-app/bin/retest @@ -0,0 +1,3 @@ +#!/usr/bin/env sh + +bin/rails test $@ diff --git a/features/rails-app/retest/retest_test.rb b/features/rails-app/retest/retest_test.rb index 6ad4ed77..ab2fec36 100644 --- a/features/rails-app/retest/retest_test.rb +++ b/features/rails-app/retest/retest_test.rb @@ -4,7 +4,13 @@ $stdout.sync = true -class MatchingTestsCommandTest < Minitest::Test +class SetupTest < Minitest::Test + def test_repository_setup + assert_equal :rails, Retest::Setup.new.type + end +end + +class TestRailsOption < Minitest::Test include RetestHelper def setup @@ -18,16 +24,39 @@ def teardown def test_start_retest launch_retest @command - assert_output_matches <<~EXPECTED + assert_output_matches <<~OUTPUT + Setup: [RAILS] + Command: 'bin/rails test ' + Watcher: [LISTEN] + Launching Retest... Ready to refactor! You can make file changes now - EXPECTED + OUTPUT + end +end - write_input("\n") # Trigger last command when no command was run +class TestDefaultCommand < Minitest::Test + include RetestHelper - assert_output_matches <<~EXPECTED - Error - Not enough information to run a command. Please trigger a run first. - EXPECTED + def setup + @command = 'retest' + end + + def teardown + end_retest + end + + def test_start_retest + launch_retest @command + + assert_output_matches <<~OUTPUT + Setup: [RAILS] + Command: 'bin/rails test ' + Watcher: [LISTEN] + + Launching Retest... + Ready to refactor! You can make file changes now + OUTPUT end def test_modify_a_file @@ -39,13 +68,25 @@ def test_modify_a_file "Test file: test/models/post_test.rb", "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips") end + + def test_interactive_commands + launch_retest @command + + assert_output_matches("Ready to refactor! You can make file changes now") + + write_input("\n") # Trigger last command when no command was run + + assert_output_matches <<~EXPECTED + Error - Not enough information to run a command. Please trigger a run first. + EXPECTED + end end -class AllTestsCommandTest < Minitest::Test +class TestAllRailsCommand < Minitest::Test include RetestHelper def setup - @command = 'retest --rails --all' + @command = 'retest --all' end def teardown @@ -55,10 +96,20 @@ def teardown def test_start_retest launch_retest @command - assert_output_matches <<~EXPECTED + assert_output_matches <<~OUTPUT + Setup: [RAILS] + Command: 'bin/rails test' + Watcher: [LISTEN] + Launching Retest... Ready to refactor! You can make file changes now - EXPECTED + OUTPUT + end + + def test_interactive_commands + launch_retest @command + + assert_output_matches("Ready to refactor! You can make file changes now") write_input("\n") # Trigger all command when no command was run @@ -74,47 +125,59 @@ def test_modify_a_file end end -class AutoFlagTest < Minitest::Test +class TestRailsAliasCommand < Minitest::Test include RetestHelper def teardown end_retest end - def test_with_no_command - launch_retest 'retest' + def test_start_retest_with_placeholder + launch_retest "retest 'bin/retest ' --rails" assert_output_matches <<~OUTPUT Setup: [RAILS] - Command: 'bin/rails test ' + Command: 'bin/retest ' Watcher: [LISTEN] Launching Retest... Ready to refactor! You can make file changes now OUTPUT + + write_input("\n") # Trigger last command when no command was run + + assert_output_matches("Error - Not enough information to run a command. Please trigger a run first.") + + modify_file 'app/models/post.rb' + + assert_output_matches( + "Test file: test/models/post_test.rb", + "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips") end - def test_with_no_command_all - launch_retest 'retest --all' + def test_start_retest_without_placeholder + launch_retest "retest --rails 'bin/retest'" assert_output_matches <<~OUTPUT Setup: [RAILS] - Command: 'bin/rails test' + Command: 'bin/retest' Watcher: [LISTEN] Launching Retest... Ready to refactor! You can make file changes now OUTPUT - end -end -class SetupTest < Minitest::Test - def test_repository_setup - assert_equal :rails, Retest::Setup.new.type + write_input("\n") # Trigger last command when no command was run + + assert_output_matches "8 runs, 10 assertions, 0 failures, 0 errors, 0 skips" + + modify_file 'app/models/post.rb' + + assert_output_matches "8 runs, 10 assertions, 0 failures, 0 errors, 0 skips" end end -class DiffOptionTest < Minitest::Test +class TestDiffOption < Minitest::Test include RetestHelper def setup diff --git a/features/rspec-ruby/bin/retest b/features/rspec-ruby/bin/retest new file mode 100755 index 00000000..ad671744 --- /dev/null +++ b/features/rspec-ruby/bin/retest @@ -0,0 +1,3 @@ +#!/usr/bin/env sh + +bundle exec rspec $@ \ No newline at end of file diff --git a/features/rspec-ruby/retest/retest_test.rb b/features/rspec-ruby/retest/retest_test.rb index 9ae4b9ae..8565ed1f 100644 --- a/features/rspec-ruby/retest/retest_test.rb +++ b/features/rspec-ruby/retest/retest_test.rb @@ -1,9 +1,74 @@ require 'retest' require_relative 'support/test_helper' require 'minitest/autorun' -require_relative 'retest_test/file_changes_test' -require_relative 'retest_test/flags_test' -require_relative 'retest_test/setup_test' +require_relative 'retest_test/file_changes' $stdout.sync = true +class TestDefaultCommand < Minitest::Test + include RetestHelper + include FileChanges + + def setup + @command = "retest" + end + + def teardown + end_retest + end + + def test_repository_setup + assert_equal :rspec, Retest::Setup.new.type + end + + def test_starting_details + launch_retest @command + + assert_output_matches <<~OUTPUT + Setup: [RSPEC] + Command: 'bundle exec rspec ' + Watcher: [LISTEN] + + Launching Retest... + Ready to refactor! You can make file changes now + OUTPUT + + modify_file('lib/bottles.rb') + + assert_output_matches "Test file: spec/bottles_spec.rb" + end +end + +class TestAliasCommand < Minitest::Test + include RetestHelper + include FileChanges + + def setup + @command = "retest --rspec 'bin/retest '" + end + + def teardown + end_retest + end + + def test_repository_setup + assert_equal :rspec, Retest::Setup.new.type + end + + def test_starting_details + launch_retest @command + + assert_output_matches <<~OUTPUT + Setup: [RSPEC] + Command: 'bin/retest ' + Watcher: [LISTEN] + + Launching Retest... + Ready to refactor! You can make file changes now + OUTPUT + + modify_file('lib/bottles.rb') + + assert_output_matches "Test file: spec/bottles_spec.rb" + end +end diff --git a/features/rspec-ruby/retest/retest_test/file_changes_test.rb b/features/rspec-ruby/retest/retest_test/file_changes.rb similarity index 80% rename from features/rspec-ruby/retest/retest_test/file_changes_test.rb rename to features/rspec-ruby/retest/retest_test/file_changes.rb index c2ce20f7..3e2b234b 100644 --- a/features/rspec-ruby/retest/retest_test/file_changes_test.rb +++ b/features/rspec-ruby/retest/retest_test/file_changes.rb @@ -1,22 +1,8 @@ -class FileChangesTest < Minitest::Test - include RetestHelper - - def setup - @command = 'retest --rspec' - end - - def teardown - end_retest - end - - def test_start_retest - launch_retest @command - - assert_output_matches <<~EXPECTED - Launching Retest... - Ready to refactor! You can make file changes now - EXPECTED - end +module FileChanges + # Don't forget to end_retest in the class + # def teardown + # end_retest + # end def test_modifying_existing_file launch_retest @command diff --git a/features/rspec-ruby/retest/retest_test/flags_test.rb b/features/rspec-ruby/retest/retest_test/flags_test.rb deleted file mode 100644 index 4f775dca..00000000 --- a/features/rspec-ruby/retest/retest_test/flags_test.rb +++ /dev/null @@ -1,27 +0,0 @@ -class FlagTest < Minitest::Test - include RetestHelper - - def setup - end - - def teardown - end_retest - end - - def test_with_no_command - launch_retest 'retest' - - assert_output_matches <<~OUTPUT - Setup: [RSPEC] - Command: 'bundle exec rspec ' - Watcher: [LISTEN] - - Launching Retest... - Ready to refactor! You can make file changes now - OUTPUT - - modify_file('lib/bottles.rb') - - assert_output_matches "Test file: spec/bottles_spec.rb" - end -end \ No newline at end of file diff --git a/features/rspec-ruby/retest/retest_test/setup_test.rb b/features/rspec-ruby/retest/retest_test/setup_test.rb deleted file mode 100644 index 6b797369..00000000 --- a/features/rspec-ruby/retest/retest_test/setup_test.rb +++ /dev/null @@ -1,5 +0,0 @@ -class SetupTest < Minitest::Test - def test_repository_setup - assert_equal :rspec, Retest::Setup.new.type - end -end \ No newline at end of file diff --git a/features/ruby-app/retest/retest_test.rb b/features/ruby-app/retest/retest_test.rb index ddba31f1..e28b8a47 100644 --- a/features/ruby-app/retest/retest_test.rb +++ b/features/ruby-app/retest/retest_test.rb @@ -59,12 +59,8 @@ def test_start_retest end end -class TestDefaultWatcher < Minitest::Test +class TestDefaultWatcher < Minitest::Test # only test the correct watcher is set include RetestHelper - include Setup - include ExplicitMatching - include FileChanges - include InteractiveCommands def setup @command = 'retest' diff --git a/features/ruby-app/retest/shared/interactive_commands.rb b/features/ruby-app/retest/shared/interactive_commands.rb index aa38b5e3..334d0554 100644 --- a/features/ruby-app/retest/shared/interactive_commands.rb +++ b/features/ruby-app/retest/shared/interactive_commands.rb @@ -12,6 +12,6 @@ def test_run_all_logs_proper_error_message write_input("ra\n") # Trigger run all - assert_output_matches("Command::AllTestsNotSupported - All tests run not supported for 'bundle exec ruby '") + assert_output_matches("Command::AllTestsNotSupported - All tests run not supported for Ruby command: 'bundle exec ruby '") end end diff --git a/lib/retest/command.rb b/lib/retest/command.rb index 6eec1fd0..62f42b87 100644 --- a/lib/retest/command.rb +++ b/lib/retest/command.rb @@ -1,8 +1,8 @@ require_relative 'command/base' require_relative 'command/hardcoded' +require_relative 'command/rspec' require_relative 'command/rails' require_relative 'command/rake' -require_relative 'command/rspec' require_relative 'command/ruby' module Retest @@ -29,11 +29,13 @@ def command private def options_command - if params[:command] then hardcoded_command(params[:command]) - elsif params[:rspec] then rspec_command - elsif params[:rails] then rails_command - elsif params[:ruby] then ruby_command - elsif params[:rake] then rake_command + command_input = params[:command] + + if params[:rspec] then rspec_command(command: command_input) + elsif params[:rails] then rails_command(command: command_input) + elsif params[:ruby] then ruby_command(command: command_input) + elsif params[:rake] then rake_command(command: command_input) + elsif command_input then hardcoded_command(command: command_input) end end @@ -47,24 +49,24 @@ def setup_command end end - def hardcoded_command(command) + def hardcoded_command(command:) Hardcoded.new(command: command) end - def rspec_command - Rspec.new(all: full_suite?) + def rspec_command(command: nil) + Rspec.new(all: full_suite?, command: command) end - def rails_command - Rails.new(all: full_suite?) + def rails_command(command: nil) + Rails.new(all: full_suite?, command: command) end - def rake_command - Rake.new(all: full_suite?) + def rake_command(command: nil) + Rake.new(all: full_suite?, command: command) end - def ruby_command - Ruby.new(all: full_suite?) + def ruby_command(command: nil) + Ruby.new(all: full_suite?, command: command) end end end \ No newline at end of file diff --git a/lib/retest/command/base.rb b/lib/retest/command/base.rb index f5f1a2d8..f7c70142 100644 --- a/lib/retest/command/base.rb +++ b/lib/retest/command/base.rb @@ -4,12 +4,15 @@ class MultipleTestsNotSupported < StandardError; end class AllTestsNotSupported < StandardError; end class Base - attr_reader :all, :file_system, :command + attr_reader :file_system, :command def initialize(all: false, file_system: FileSystem, command: nil) @file_system = file_system - @all = all - @command = command + @command = command || default_command(all: all) + end + + def to_s + all ? all_command : batched_command end def eql?(other) @@ -18,13 +21,13 @@ def eql?(other) alias == eql? def hash - [all, file_system, command].hash + [self.class, file_system, command].hash end def switch_to(type) case type.to_s - when 'all' then clone(all: true) - when 'one' then clone(all: false) + when 'all' then clone(command: all_command) + when 'batched' then clone(command: batched_command) else raise ArgumentError, "unknown type to switch to: #{type}" end end @@ -34,26 +37,42 @@ def hardcoded? end def has_changed? - to_s.include?('') + command.include?('') end def has_test? - to_s.include?('') - end - - def to_s - @command + command.include?('') end def format_batch(*files) - raise MultipleTestsNotSupported, "Multiple test files run not supported for '#{to_s}'" + raise_multiple_test_not_supported end private + def all + !has_test? + end + + def batched_command + raise NotImplementedError, 'must define a BATCHED command' + end + + def all_command + raise NotImplementedError, 'must define a ALL command' + end + + def default_command(all: false) + raise NotImplementedError, 'must define a DEFAULT command' + end + def clone(params = {}) self.class.new(**{ all: all, file_system: file_system, command: command }.merge(params)) end + + def raise_multiple_test_not_supported + raise MultipleTestsNotSupported, "Multiple test files run not supported for command: '#{to_s}'" + end end end end diff --git a/lib/retest/command/hardcoded.rb b/lib/retest/command/hardcoded.rb index a450dc22..c6df5989 100644 --- a/lib/retest/command/hardcoded.rb +++ b/lib/retest/command/hardcoded.rb @@ -1,12 +1,17 @@ module Retest class Command class Hardcoded < Base - def switch_to(type) - if type.to_s == 'all' - raise AllTestsNotSupported, "All tests run not supported for '#{to_s}'" - end + private - super + def all_command + command + end + + def batched_command + command + end + + def default_command(all: false) end end end diff --git a/lib/retest/command/rails.rb b/lib/retest/command/rails.rb index c1b7aa42..11fd3315 100644 --- a/lib/retest/command/rails.rb +++ b/lib/retest/command/rails.rb @@ -1,26 +1,17 @@ module Retest class Command - class Rails < Base - def to_s - if all - root_command - else - "#{root_command} " - end - end - - def format_batch(*files) - files.join(' ') - end + class Rails < Rspec private - def root_command - if file_system.exist? 'bin/rails' + def default_command(all:) + command = if file_system&.exist? 'bin/rails' 'bin/rails test' else 'bundle exec rails test' end + + all ? command : "#{command} " end end end diff --git a/lib/retest/command/rake.rb b/lib/retest/command/rake.rb index 9cb7330f..65ecc7a9 100644 --- a/lib/retest/command/rake.rb +++ b/lib/retest/command/rake.rb @@ -1,26 +1,32 @@ module Retest class Command class Rake < Base - def to_s - if all - root_command - else - "#{root_command} TEST=" - end - end - def format_batch(*files) files.size > 1 ? %Q{"{#{files.join(',')}}"} : files.first end private - def root_command - if file_system.exist? 'bin/rake' + def default_command(all: false) + command = if file_system&.exist? 'bin/rake' 'bin/rake test' else 'bundle exec rake test' end + + all ? command : "#{command} TEST=" + end + + def all_command + return command if all + + command.gsub('TEST=', '').strip + end + + def batched_command + return command unless all + + "#{command} TEST=" end end end diff --git a/lib/retest/command/rspec.rb b/lib/retest/command/rspec.rb index cdbd39b1..64ec131c 100644 --- a/lib/retest/command/rspec.rb +++ b/lib/retest/command/rspec.rb @@ -1,26 +1,32 @@ module Retest class Command class Rspec < Base - def to_s - if all - root_command - else - "#{root_command} " - end - end - def format_batch(*files) files.join(' ') end private - def root_command - if file_system.exist? 'bin/rspec' + def all_command + return command if all + + command.gsub('', '').strip + end + + def batched_command + return command unless all + + "#{command} " + end + + def default_command(all:) + command = if file_system&.exist? 'bin/rspec' 'bin/rspec' else 'bundle exec rspec' end + + all ? command : "#{command} " end end end diff --git a/lib/retest/command/ruby.rb b/lib/retest/command/ruby.rb index 8a1123ad..7610b380 100644 --- a/lib/retest/command/ruby.rb +++ b/lib/retest/command/ruby.rb @@ -1,24 +1,26 @@ module Retest class Command class Ruby < Base - def to_s - if file_system.exist? 'Gemfile.lock' + def format_batch(*files) + files.size > 1 ? %Q{-e "#{files.map { |file| "require './#{file}';" }.join}"} : files.first + end + + private + + def default_command(all: false) + if file_system&.exist? 'Gemfile.lock' 'bundle exec ruby ' else 'ruby ' end end - def format_batch(*files) - files.size > 1 ? %Q{-e "#{files.map { |file| "require './#{file}';" }.join}"} : files.first + def all_command + raise AllTestsNotSupported, "All tests run not supported for Ruby command: '#{to_s}'" end - def switch_to(type) - if type.to_s == 'all' - raise AllTestsNotSupported, "All tests run not supported for '#{to_s}'" - end - - super + def batched_command + command end end end diff --git a/lib/retest/program.rb b/lib/retest/program.rb index c8218309..94b24c3c 100644 --- a/lib/retest/program.rb +++ b/lib/retest/program.rb @@ -30,7 +30,6 @@ def run(file, force_run: false) @stdout.puts <<~HINT Forced selection enabled. Reset to default settings by typing 'r' in the interactive console. - HINT runner.run(test_files: selected_test_files) @@ -48,17 +47,13 @@ def diff(branch) raise "Git not installed" unless VersionControl::Git.installed? test_files = repository.find_tests VersionControl::Git.diff_files(branch) - run_selected(test_files) + runner.run(test_files: test_files) end def run_all runner.run_all end - def run_selected(test_files) - runner.run(test_files: test_files) - end - def clear_terminal system('clear 2>/dev/null') || system('cls 2>/dev/null') end diff --git a/lib/retest/runner.rb b/lib/retest/runner.rb index d3906127..146c7311 100644 --- a/lib/retest/runner.rb +++ b/lib/retest/runner.rb @@ -43,11 +43,12 @@ def run_all def format_instruction(changed_files: [], test_files: []) if changed_files.empty? && test_files.size >= 1 - instruction = command.switch_to(:one).to_s - tests_string = command.format_batch(*test_files) - log("Tests selected:") + new_command = command.switch_to(:batched) + + log("\nTests selected:") test_files.each { |test_file| log(" - #{test_file}") } - return instruction.gsub('', tests_string) + + return new_command.to_s.gsub('', new_command.format_batch(*test_files)) end instruction = command.to_s diff --git a/test/retest/command/command_interface.rb b/test/retest/command/command_interface.rb index a6d9448e..5fdfc0e6 100644 --- a/test/retest/command/command_interface.rb +++ b/test/retest/command/command_interface.rb @@ -4,8 +4,16 @@ module CommandInterface def test_interface assert_respond_to @subject, :format_batch assert_respond_to @subject, :to_s + assert_respond_to @subject, :switch_to assert_respond_to @subject, :has_test? assert_respond_to @subject, :has_changed? + assert_respond_to @subject, :hardcoded? + + # internals + assert_respond_to @subject, :command + assert_respond_to @subject, :file_system + assert_respond_to @subject, :hash + assert_respond_to @subject, :eql? end def test_initializatioin diff --git a/test/retest/command/hardcoded_test.rb b/test/retest/command/hardcoded_test.rb index 0672c745..41014e92 100644 --- a/test/retest/command/hardcoded_test.rb +++ b/test/retest/command/hardcoded_test.rb @@ -52,13 +52,13 @@ def test_format_with_multiple_files def test_switch_to all_command = Hardcoded.new(all: true, command: 'echo "hello world"') - one_command = Hardcoded.new(all: false, command: 'echo "hello world"') + batched_command = Hardcoded.new(all: false, command: 'echo "hello world"') - assert_raises(Command::AllTestsNotSupported) { all_command.switch_to(:all) } - assert_equal one_command, all_command.switch_to(:one) + assert_equal all_command, all_command.switch_to(:all) + assert_equal all_command, batched_command.switch_to(:all) - assert_raises(Command::AllTestsNotSupported) { one_command.switch_to(:all) } - assert_equal one_command, one_command.switch_to(:one) + assert_equal batched_command, all_command.switch_to(:batched) + assert_equal batched_command, batched_command.switch_to(:batched) end end end diff --git a/test/retest/command/rails_test.rb b/test/retest/command/rails_test.rb index 227424d0..2eecf83c 100644 --- a/test/retest/command/rails_test.rb +++ b/test/retest/command/rails_test.rb @@ -30,13 +30,48 @@ def test_format_with_multiple_files def test_switch_to all_command = Rails.new(all: true, file_system: FakeFS.new([])) - one_command = Rails.new(all: false, file_system: FakeFS.new([])) + batched_command = Rails.new(all: false, file_system: FakeFS.new([])) assert_equal all_command, all_command.switch_to(:all) - assert_equal one_command, all_command.switch_to(:one) + assert_equal batched_command, all_command.switch_to(:batched) - assert_equal all_command, one_command.switch_to(:all) - assert_equal one_command, one_command.switch_to(:one) + assert_equal all_command, batched_command.switch_to(:all) + assert_equal batched_command, batched_command.switch_to(:batched) + end + end + + class RailsWithACommandTest < MiniTest::Test + def setup + @subject = Rails.new(command: 'bin/test', file_system: nil) + end + + include CommandInterface + + def test_to_s + assert_equal 'bin/test', Rails.new(command: 'bin/test', file_system: FakeFS.new(['bin/rails'])).to_s + assert_equal 'bin/test', Rails.new(command: 'bin/test', file_system: FakeFS.new([])).to_s + + assert_equal 'bin/test ', Rails.new(command: 'bin/test ', file_system: FakeFS.new(['bin/rails'])).to_s + assert_equal 'bin/test ', Rails.new(command: 'bin/test ', file_system: FakeFS.new([])).to_s + end + + def test_format_with_one_file + assert_equal 'a/file/path.rb', @subject.format_batch('a/file/path.rb') + end + + def test_format_with_multiple_files + assert_equal 'a/file/path.rb another/file/path.rb', @subject.format_batch('a/file/path.rb', 'another/file/path.rb') + end + + def test_switch_to + batched_command = Rails.new(command: 'bin/test ') + all_command = Rails.new(command: 'bin/test') + + assert_equal all_command, all_command.switch_to(:all) + assert_equal batched_command, all_command.switch_to(:batched) + + assert_equal all_command, batched_command.switch_to(:all) + assert_equal batched_command, batched_command.switch_to(:batched) end end end diff --git a/test/retest/command/rake_test.rb b/test/retest/command/rake_test.rb index 79267e21..09f4620f 100644 --- a/test/retest/command/rake_test.rb +++ b/test/retest/command/rake_test.rb @@ -15,9 +15,6 @@ def test_to_s assert_equal 'bundle exec rake test', Rake.new(all: true, file_system: FakeFS.new([])).to_s assert_equal 'bin/rake test TEST=', Rake.new(all: false, file_system: FakeFS.new(['bin/rake'])).to_s assert_equal 'bundle exec rake test TEST=', Rake.new(all: false, file_system: FakeFS.new([])).to_s - # take into account gem repository which doesn't have a bin file - assert_equal 'bundle exec rake test TEST=', Rake.new(all: false).to_s - assert_equal 'bundle exec rake test', Rake.new(all: true).to_s end def test_format_with_one_file @@ -30,13 +27,47 @@ def test_format_with_multiple_files def test_switch_to all_command = Rake.new(all: true, file_system: FakeFS.new([])) - one_command = Rake.new(all: false, file_system: FakeFS.new([])) + batched_command = Rake.new(all: false, file_system: FakeFS.new([])) assert_equal all_command, all_command.switch_to(:all) - assert_equal one_command, all_command.switch_to(:one) + assert_equal batched_command, all_command.switch_to(:batched) - assert_equal all_command, one_command.switch_to(:all) - assert_equal one_command, one_command.switch_to(:one) + assert_equal all_command, batched_command.switch_to(:all) + assert_equal batched_command, batched_command.switch_to(:batched) + end + end + + class RakeWithACommandTest < MiniTest::Test + def setup + @subject = Rake.new(command: 'bin/test', all: true, file_system: FakeFS.new([])) + end + + include CommandInterface + + def test_to_s + assert_equal 'bin/test', Rake.new(command: 'bin/test', file_system: FakeFS.new(['bin/rake'])).to_s + assert_equal 'bin/test TEST=', Rake.new(command: 'bin/test TEST=', file_system: FakeFS.new(['bin/rake'])).to_s + assert_equal 'bin/test', Rake.new(command: 'bin/test', file_system: FakeFS.new([])).to_s + assert_equal 'bin/test TEST=', Rake.new(command: 'bin/test TEST=', file_system: FakeFS.new([])).to_s + end + + def test_format_with_one_file + assert_equal 'a/file/path.rb', @subject.format_batch('a/file/path.rb') + end + + def test_format_with_multiple_files + assert_equal '"{a/file/path.rb,another/file/path.rb}"', @subject.format_batch('a/file/path.rb', 'another/file/path.rb') + end + + def test_switch_to + all_command = Rake.new(command: 'bin/test', file_system: nil) + batched_command = Rake.new(command: 'bin/test TEST=', file_system: nil) + + assert_equal all_command, all_command.switch_to(:all) + assert_equal batched_command, all_command.switch_to(:batched) + + assert_equal all_command, batched_command.switch_to(:all) + assert_equal batched_command, batched_command.switch_to(:batched) end end end diff --git a/test/retest/command/rspec_test.rb b/test/retest/command/rspec_test.rb index fc15b594..a72b7ffb 100644 --- a/test/retest/command/rspec_test.rb +++ b/test/retest/command/rspec_test.rb @@ -30,13 +30,48 @@ def test_format_with_multiple_files def test_switch_to all_command = Rspec.new(all: true, file_system: FakeFS.new([])) - one_command = Rspec.new(all: false, file_system: FakeFS.new([])) + batched_command = Rspec.new(all: false, file_system: FakeFS.new([])) assert_equal all_command, all_command.switch_to(:all) - assert_equal one_command, all_command.switch_to(:one) + assert_equal all_command, batched_command.switch_to(:all) - assert_equal all_command, one_command.switch_to(:all) - assert_equal one_command, one_command.switch_to(:one) + assert_equal batched_command, all_command.switch_to(:batched) + assert_equal batched_command, batched_command.switch_to(:batched) + end + end + + class RspecWithACommandTest < MiniTest::Test + def setup + @subject = Rspec.new(command: 'bin/test', file_system: nil) + end + + include CommandInterface + + def test_to_s + assert_equal 'bin/test', Rspec.new(command: 'bin/test', file_system: FakeFS.new(['bin/rspec'])).to_s + assert_equal 'bin/test', Rspec.new(command: 'bin/test', file_system: FakeFS.new([])).to_s + + assert_equal 'bin/test ', Rspec.new(command: 'bin/test ', file_system: FakeFS.new(['bin/rspec'])).to_s + assert_equal 'bin/test ', Rspec.new(command: 'bin/test ', file_system: FakeFS.new([])).to_s + end + + def test_format_with_one_file + assert_equal 'a/file/path.rb', @subject.format_batch('a/file/path.rb') + end + + def test_format_with_multiple_files + assert_equal 'a/file/path.rb another/file/path.rb', @subject.format_batch('a/file/path.rb', 'another/file/path.rb') + end + + def test_switch_to + batched_command = Rspec.new(command: 'bin/test ') + all_command = Rspec.new(command: 'bin/test') + + assert_equal all_command, all_command.switch_to(:all) + assert_equal batched_command, all_command.switch_to(:batched) + + assert_equal all_command, batched_command.switch_to(:all) + assert_equal batched_command, batched_command.switch_to(:batched) end end end diff --git a/test/retest/command/ruby_test.rb b/test/retest/command/ruby_test.rb index 7244fc66..473f4646 100644 --- a/test/retest/command/ruby_test.rb +++ b/test/retest/command/ruby_test.rb @@ -32,13 +32,56 @@ def test_format_with_multiple_files def test_switch_to all_command = Ruby.new(all: true, file_system: FakeFS.new([])) - one_command = Ruby.new(all: false, file_system: FakeFS.new([])) + batched_command = Ruby.new(all: false, file_system: FakeFS.new([])) - assert_raises(Command::AllTestsNotSupported) { all_command.switch_to(:all) } - assert_equal one_command, all_command.switch_to(:one) + exception = assert_raises(Command::AllTestsNotSupported) { all_command.switch_to(:all) } + assert_equal "All tests run not supported for Ruby command: 'ruby '", exception.message + assert_equal batched_command, all_command.switch_to(:batched) - assert_raises(Command::AllTestsNotSupported) { one_command.switch_to(:all) } - assert_equal one_command, one_command.switch_to(:one) + exception = assert_raises(Command::AllTestsNotSupported) { batched_command.switch_to(:all) } + assert_equal "All tests run not supported for Ruby command: 'ruby '", exception.message + assert_equal batched_command, batched_command.switch_to(:batched) + + all_command = Ruby.new(all: true, file_system: FakeFS.new(['Gemfile.lock'])) + batched_command = Ruby.new(all: false, file_system: FakeFS.new(['Gemfile.lock'])) + + exception = assert_raises(Command::AllTestsNotSupported) { all_command.switch_to(:all) } + assert_equal "All tests run not supported for Ruby command: 'bundle exec ruby '", exception.message + assert_equal batched_command, all_command.switch_to(:batched) + + exception = assert_raises(Command::AllTestsNotSupported) { batched_command.switch_to(:all) } + assert_equal "All tests run not supported for Ruby command: 'bundle exec ruby '", exception.message + assert_equal batched_command, batched_command.switch_to(:batched) + end + end + + class RubyWithACommandTest < MiniTest::Test + def setup + @subject = Ruby.new(all: true, file_system: FakeFS.new([]), command: 'bin/test') + end + + include CommandInterface + + def test_to_s + assert_equal 'bin/test ', Ruby.new(command: 'bin/test ', all: true, file_system: FakeFS.new([])).to_s + assert_equal 'bin/test ', Ruby.new(command: 'bin/test ', all: false, file_system: FakeFS.new([])).to_s + assert_equal 'bin/test ', Ruby.new(command: 'bin/test ', all: true, file_system: FakeFS.new(['Gemfile.lock'])).to_s + assert_equal 'bin/test ', Ruby.new(command: 'bin/test ', all: false, file_system: FakeFS.new(['Gemfile.lock'])).to_s + end + + def test_format_with_one_file + assert_equal 'a/file/path.rb', @subject.format_batch('a/file/path.rb') + end + + def test_format_with_multiple_files + assert_equal %Q{-e "require './a/file/path.rb';require './another/file/path.rb';"}, @subject.format_batch('a/file/path.rb', 'another/file/path.rb') + end + + def test_switch_to + batched_command = Ruby.new(command: 'bin/test ', file_system: FakeFS.new([])) + + assert_raises(Command::AllTestsNotSupported) { batched_command.switch_to(:all) } + assert_equal batched_command, batched_command.switch_to(:batched) end end end diff --git a/test/retest/command_test.rb b/test/retest/command_test.rb index 8f8ab1b0..1f8dfb60 100644 --- a/test/retest/command_test.rb +++ b/test/retest/command_test.rb @@ -7,51 +7,64 @@ def setup @subject = Command.new end - def test_hardcoded_command - @subject.options = Options.new(['--rspec', '--all', 'hello world']) - assert_equal 'hello world', @subject.command.to_s - end - - def test_empty_options + def test_retest_repository_setup # returning the gem setup in this test @subject.options = Options.new([]) - assert_equal 'bundle exec rake test TEST=', @subject.command.to_s + + assert_equal Command::Rake.new(all: false), @subject.command + end + + def test_hardcoded_command + @subject.options = Options.new(["echo 'hello world'"]) + + assert_equal Command::Hardcoded.new(command: "echo 'hello world'"), @subject.command end def test_matching_test_options @subject.options = Options.new(['--rspec']) - assert_equal 'bundle exec rspec ', @subject.command.to_s + assert_equal Command::Rspec.new(all: false), @subject.command @subject.options = Options.new(['--rails']) - assert_equal 'bundle exec rails test ', @subject.command.to_s + assert_equal Command::Rails.new(all: false), @subject.command @subject.options = Options.new(['--ruby']) - assert_equal 'bundle exec ruby ', @subject.command.to_s + assert_equal Command::Ruby.new(all: false), @subject.command @subject.options = Options.new(['--rake']) - assert_equal 'bundle exec rake test TEST=', @subject.command.to_s + assert_equal Command::Rake.new(all: false), @subject.command end def test_all_options @subject.options = Options.new(['--rspec', '--all']) - assert_equal 'bundle exec rspec', @subject.command.to_s + assert_equal Command::Rspec.new(all: true), @subject.command @subject.options = Options.new(['--rails', '--all']) - assert_equal 'bundle exec rails test', @subject.command.to_s + assert_equal Command::Rails.new(all: true), @subject.command @subject.options = Options.new(['--rake', '--all']) - assert_equal 'bundle exec rake test', @subject.command.to_s + assert_equal Command::Rake.new(all: true), @subject.command @subject.options = Options.new(['--ruby', '--all']) - assert_equal 'bundle exec ruby ', @subject.command.to_s + assert_equal Command::Ruby.new(all: true), @subject.command end - def test_mixed_options - @subject.options = Options.new(["echo hello world", "--rails"]) - assert_equal 'echo hello world', @subject.command.to_s + def test_alias_options + @subject.options = Options.new(["--rails", "bin/test"]) + assert_equal Command::Rails.new(command: "bin/test"), @subject.command + + @subject.options = Options.new(["--rspec", "bin/test"]) + assert_equal Command::Rspec.new(command: "bin/test"), @subject.command + @subject.options = Options.new(["--rake", "bin/test"]) + assert_equal Command::Rake.new(command: "bin/test"), @subject.command + + @subject.options = Options.new(["--ruby", "bin/test"]) + assert_equal Command::Ruby.new(command: "bin/test"), @subject.command + end + + def test_invalid_options @subject.options = Options.new(["--rspec", "--rails"]) - assert_equal 'bundle exec rspec ', @subject.command.to_s + assert_equal Command::Rspec.new(all: false), @subject.command end end