Skip to content

Commit

Permalink
Feature - Bin commands (#67)
Browse files Browse the repository at this point in the history
* Extract Command from Options class

* Move tests from options to command class

* Refactor Command class

* Cleanup Options class

* Update command retrievel in executable

* Prototype RSpec command

* Prototype Rails command

* Remove keyword init from Struct because it is a 2.5 ruby feature

* Check for rails setup within gemflock file

* Prototype Rake command

* Prototype Ruby command

* Move setup command to their own file

* Fix rails feature specs to use bin/rails instead

* Refactor command tests
  • Loading branch information
AlexB52 authored Apr 27, 2021
1 parent f05137f commit 750483b
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 140 deletions.
2 changes: 1 addition & 1 deletion bin/debug
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end

program = Retest::Program.new(
repository: Retest::Repository.new(files: Retest::VersionControl.files),
runner: Retest::Runner.for(options.command)
runner: Retest::Runner.for(Retest::Command.for_options(options))
)

if options.params[:diff]
Expand Down
2 changes: 1 addition & 1 deletion exe/retest
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end

program = Retest::Program.new(
repository: Retest::Repository.new(files: Retest::VersionControl.files),
runner: Retest::Runner.for(options.command)
runner: Retest::Runner.for(Retest::Command.for_options(options))
)

if options.params[:diff]
Expand Down
8 changes: 4 additions & 4 deletions features/rails-app/retest/retest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_with_no_command
@output, @pid = launch_retest 'retest'

assert_match <<~OUTPUT, @output.read
Setup identified: [RAILS]. Using command: 'bundle exec rails test <test>'
Setup identified: [RAILS]. Using command: 'bin/rails test <test>'
Launching Retest...
Ready to refactor! You can make file changes now
OUTPUT
Expand All @@ -79,7 +79,7 @@ def test_with_no_command_all
@output, @pid = launch_retest 'retest --all'

assert_match <<~OUTPUT, @output.read
Setup identified: [RAILS]. Using command: 'bundle exec rails test'
Setup identified: [RAILS]. Using command: 'bin/rails test'
Launching Retest...
Ready to refactor! You can make file changes now
OUTPUT
Expand All @@ -89,7 +89,7 @@ def test_with_auto_flag
@output, @pid = launch_retest 'retest --auto'

assert_match <<~OUTPUT, @output.read
Setup identified: [RAILS]. Using command: 'bundle exec rails test <test>'
Setup identified: [RAILS]. Using command: 'bin/rails test <test>'
Launching Retest...
Ready to refactor! You can make file changes now
OUTPUT
Expand All @@ -99,7 +99,7 @@ def test_with_auto_flag_all
@output, @pid = launch_retest 'retest --auto --all'

assert_match <<~OUTPUT, @output.read
Setup identified: [RAILS]. Using command: 'bundle exec rails test'
Setup identified: [RAILS]. Using command: 'bin/rails test'
Launching Retest...
Ready to refactor! You can make file changes now
OUTPUT
Expand Down
1 change: 1 addition & 0 deletions lib/retest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "retest/options"
require "retest/version_control"
require "retest/setup"
require "retest/command"

module Retest
class Error < StandardError; end
Expand Down
77 changes: 77 additions & 0 deletions lib/retest/command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require_relative 'command/rails'
require_relative 'command/rake'
require_relative 'command/rspec'
require_relative 'command/ruby'

module Retest
class Command
extend Forwardable

def self.for_options(options)
new(options: options).command
end

def self.for_setup(setup)
new(setup: setup).command
end

def_delegator :setup, :type
def_delegators :options, :params, :full_suite?, :auto?

attr_accessor :options, :setup
def initialize(options: Options.new, setup: Setup.new, output_stream: STDOUT)
@options = options
@setup = setup
@output_stream = output_stream
end

def command
return default_command if auto?
options_command || default_command
end

def options_command
return params[:command] if params[:command]

if params[:rspec] then rspec_command
elsif params[:rails] then rails_command
elsif params[:ruby] then ruby_command
elsif params[:rake] then rake_command
else
end
end

def setup_command
case type
when :rake then rake_command
when :rspec then rspec_command
when :rails then rails_command
when :ruby then ruby_command
else ruby_command
end
end

def default_command
@output_stream.puts "Setup identified: [#{type.upcase}]. Using command: '#{setup_command}'"
setup_command
end

private

def rspec_command
Rspec.command(all: full_suite?)
end

def rails_command
Rails.command(all: full_suite?)
end

def rake_command
Rake.command(all: full_suite?)
end

def ruby_command
Ruby.command(all: full_suite?)
end
end
end
22 changes: 22 additions & 0 deletions lib/retest/command/rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Retest
class Command
Rails = Struct.new(:all, :bin_file) do
def self.command(all:, bin_file: File.exist?('bin/rails'))
new(all, bin_file).command
end

def command
return "#{root_command} <test>" unless all
root_command
end

private

def root_command
return 'bin/rails test' if bin_file

'bundle exec rails test'
end
end
end
end
22 changes: 22 additions & 0 deletions lib/retest/command/rake.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Retest
class Command
Rake = Struct.new(:all, :bin_file) do
def self.command(all:, bin_file: File.exist?('bin/rake'))
new(all, bin_file).command
end

def command
return "#{root_command} TEST=<test>" unless all
root_command
end

private

def root_command
return 'bin/rake test' if bin_file

'bundle exec rake test'
end
end
end
end
22 changes: 22 additions & 0 deletions lib/retest/command/rspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Retest
class Command
Rspec = Struct.new(:all, :bin_file) do
def self.command(all:, bin_file: File.exist?('bin/rspec'))
new(all, bin_file).command
end

def command
return "#{root_command} <test>" unless all
root_command
end

private

def root_command
return 'bin/rspec' if bin_file

'bundle exec rspec'
end
end
end
end
13 changes: 13 additions & 0 deletions lib/retest/command/ruby.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Retest
class Command
Ruby = Struct.new(:all, :bin_file) do
def self.command(all: false, bin_file: false)
new(false, false).command
end

def command
'bundle exec ruby <test>'
end
end
end
end
54 changes: 7 additions & 47 deletions lib/retest/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,8 @@ def self.command(args)
new(args).command
end

def initialize(args = [], output_stream: STDOUT, setup: Setup)
def initialize(args = [])
self.args = args
@output_stream = output_stream
@setup = setup
end

def command
return params[:command] if params[:command]

if params[:rspec] then rspec_command
elsif params[:rake] then rake_command
elsif params[:rails] then rails_command
elsif params[:ruby] then ruby_command
elsif params[:auto] then default_command
else default_command
end
end

def args=(args)
Expand All @@ -138,45 +124,19 @@ def help?
params[:help]
end

private

def full_suite?
params[:all]
end

def default_command
choose_command @setup.type, command_for(@setup.type)
end

def command_for(type)
case type
when :rspec then rspec_command
when :rails then rails_command
when :rake then rake_command
when :ruby then ruby_command
else ruby_command
end
def auto?
return true if no_options_passed?
params[:auto]
end

def choose_command(type, command)
@output_stream.puts "Setup identified: [#{type.upcase}]. Using command: '#{command}'"
command
end

def rspec_command
full_suite? ? ALL_RSPEC_COMMAND : RSPEC_COMMAND
end

def rails_command
full_suite? ? ALL_RAILS_COMMAND : RAILS_COMMAND
end

def rake_command
full_suite? ? ALL_RAKE_COMMAND : RAKE_COMMAND
end
private

def ruby_command
RUBY_COMMAND
def no_options_passed?
params.to_h.values.compact.uniq == [false]
end
end
end
2 changes: 1 addition & 1 deletion lib/retest/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def rspec?
end

def rails?
File.exist? 'bin/rails'
has_gem? 'rails'
end

def rake?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class AutoFlagTest < MiniTest::Test
def setup
@setup = SetupFake.new
@output_stream = StringIO.new
@subject = Options.new(['--auto'], output_stream: @output_stream, setup: @setup)

@subject = Command.new(
options: Options.new(['--auto']),
setup: @setup,
output_stream: @output_stream)
end

def read_ouput
Expand Down Expand Up @@ -64,7 +68,11 @@ class AutoAllFlagTest < MiniTest::Test
def setup
@setup = SetupFake.new
@output_stream = StringIO.new
@subject = Options.new(['--auto', '--all'], output_stream: @output_stream, setup: @setup)

@subject = Command.new(
options: Options.new(['--auto', '--all']),
setup: @setup,
output_stream: @output_stream)
end

def read_ouput
Expand Down
55 changes: 55 additions & 0 deletions test/retest/command/setup_commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module Retest
class Command
class RspecTest < MiniTest::Test
def test_command
assert_equal 'bin/rspec', Rspec.command(all: true, bin_file: true)
assert_equal 'bundle exec rspec', Rspec.command(all: true, bin_file: false)
assert_equal 'bin/rspec <test>', Rspec.command(all: false, bin_file: true)
assert_equal 'bundle exec rspec <test>', Rspec.command(all: false, bin_file: false)

# take into account gem repository which doesn't have a bin/rspec file
assert_equal 'bundle exec rspec <test>', Rspec.command(all: false)
assert_equal 'bundle exec rspec', Rspec.command(all: true)
end
end

class RailsTest < MiniTest::Test
def test_command
assert_equal 'bin/rails test', Rails.command(all: true, bin_file: true)
assert_equal 'bundle exec rails test', Rails.command(all: true, bin_file: false)
assert_equal 'bin/rails test <test>', Rails.command(all: false, bin_file: true)
assert_equal 'bundle exec rails test <test>', Rails.command(all: false, bin_file: false)

# take into account gem repository which doesn't have a bin file
assert_equal 'bundle exec rails test <test>', Rails.command(all: false)
assert_equal 'bundle exec rails test', Rails.command(all: true)
end
end

class RakeTest < MiniTest::Test
def test_command
assert_equal 'bin/rake test', Rake.command(all: true, bin_file: true)
assert_equal 'bundle exec rake test', Rake.command(all: true, bin_file: false)
assert_equal 'bin/rake test TEST=<test>', Rake.command(all: false, bin_file: true)
assert_equal 'bundle exec rake test TEST=<test>', Rake.command(all: false, bin_file: false)

# take into account gem repository which doesn't have a bin file
assert_equal 'bundle exec rake test TEST=<test>', Rake.command(all: false)
assert_equal 'bundle exec rake test', Rake.command(all: true)
end
end

class RubyTest < MiniTest::Test
def test_command
assert_equal 'bundle exec ruby <test>', Ruby.command(all: true, bin_file: true)
assert_equal 'bundle exec ruby <test>', Ruby.command(all: true, bin_file: false)
assert_equal 'bundle exec ruby <test>', Ruby.command(all: false, bin_file: true)
assert_equal 'bundle exec ruby <test>', Ruby.command(all: false, bin_file: false)

# take into account gem repository which doesn't have a bin file
assert_equal 'bundle exec ruby <test>', Ruby.command(all: false)
assert_equal 'bundle exec ruby <test>', Ruby.command(all: true)
end
end
end
end
Loading

0 comments on commit 750483b

Please sign in to comment.