Browse files

Refactor autotest / rspec / bundler codes

- --skip-bundler beats 'autotest/bundler'
  • Loading branch information...
1 parent 8aca7e0 commit bac86b15800963842f9f7b5c3bdc41e5cdca126f @dchelimsky dchelimsky committed Jan 22, 2011
Showing with 69 additions and 54 deletions.
  1. +38 −27 lib/autotest/rspec2.rb
  2. +31 −27 spec/autotest/rspec_spec.rb
View
65 lib/autotest/rspec2.rb
@@ -5,16 +5,15 @@ class RSpecCommandError < StandardError; end
class Autotest::Rspec2 < Autotest
- attr_reader :cli_args, :skip_bundler
+ attr_reader :cl_args, :skip_bundler
alias_method :skip_bundler?, :skip_bundler
- SPEC_PROGRAM = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin', 'rspec'))
+ SPEC_PROGRAM = File.expand_path('../../../bin/rspec', __FILE__)
def initialize
super()
- @cli_args = ARGV.dup << "--tty"
- @skip_bundler = @cli_args.delete("--skip-bundler")
- ARGV.clear
+ @cl_args = ARGV.dup << "--tty"
+ @skip_bundler = @cl_args.delete("--skip-bundler")
clear_mappings
setup_rspec_project_mappings
@@ -47,8 +46,20 @@ def consolidate_failures(failed)
end
def make_test_cmd(files_to_test)
- if File.exist?('./Gemfile') && prefix !~ /bundle exec/ && !skip_bundler?
- RSpec.warn_deprecation <<-WARNING
+ warn_about_bundler if rspec_wants_bundler? && !autotest_wants_bundler?
+ files_to_test.empty? ? '' :
+ "#{prefix}#{ruby}#{suffix} -S #{SPEC_PROGRAM} #{cl_args.join(' ')} #{normalize(files_to_test).keys.flatten.map { |f| "'#{f}'"}.join(' ')}"
+ end
+
+ def normalize(files_to_test)
+ files_to_test.keys.inject({}) do |result, filename|
+ result[File.expand_path(filename)] = []
+ result
+ end
+ end
+
+ def warn_about_bundler
+ RSpec.warn_deprecation <<-WARNING
****************************************************************************
DEPRECATION WARNING: you are using deprecated behaviour that will be removed
@@ -57,45 +68,45 @@ def make_test_cmd(files_to_test)
RSpec's autotest extension is relying on the presence of a Gemfile in the
project root directory to generate a command including 'bundle exec'.
-You have two options to disable this message:
+You have two options to suppress this message:
If you want to include 'bundle exec' in the command, add a .autotest file to
the project root with the following content:
require 'autotest/bundler'
-If you prefer to skip 'bundle exec', pass the --skip-bundler to autotest as
-an extra argument, like this:
+If you want to _not_include 'bundle exec' in the command, pass --skip-bundler
+to autotest as an extra argument, like this:
autotest -- --skip-bundler
*****************************************************************
WARNING
- end
- files_to_test.empty? ? '' :
- "#{prefix}#{bundle_exec}#{ruby} #{require_rubygems}-S #{SPEC_PROGRAM} #{cli_args.join(' ')} #{normalize(files_to_test).keys.flatten.map { |f| "'#{f}'"}.join(' ')}"
end
- def bundle_exec
- if using_bundler? && prefix !~ /bundle exec/
- "bundle exec "
- else
- ""
- end
+ alias_method :autotest_prefix, :prefix
+
+ def rspec_prefix
+ (rspec_wants_bundler? && !autotest_wants_bundler?) ? "bundle exec " : ""
end
- def require_rubygems
- using_bundler? ? "" : defined?(:Gem) ? "-rrubygems " : " "
+ def prefix
+ skip_bundler? ? "#{rspec_prefix}#{autotest_prefix}".gsub("bundle exec","") : "#{rspec_prefix}#{autotest_prefix}"
end
- def normalize(files_to_test)
- files_to_test.keys.inject({}) do |result, filename|
- result[File.expand_path(filename)] = []
- result
- end
+ def autotest_wants_bundler?
+ autotest_prefix =~ /bundle exec/
+ end
+
+ def suffix
+ using_bundler? ? "" : defined?(:Gem) ? " -rrubygems" : ""
+ end
+
+ def rspec_wants_bundler?
+ gemfile? && !skip_bundler?
end
def using_bundler?
- gemfile? unless skip_bundler?
+ rspec_wants_bundler? || autotest_wants_bundler?
end
def gemfile?
View
58 spec/autotest/rspec_spec.rb
@@ -14,30 +14,32 @@
after { ARGV.clear; ARGV.concat(@orig_argv) }
context "with ARGV" do
-
it "passes ARGV to command" do
ARGV.concat(%w[-c -f d])
- rspec_autotest.cli_args.should eq(%w[-c -f d --tty])
+ rspec_autotest.cl_args.should eq(%w[-c -f d --tty])
end
- it "extracts --skip-bundler" do
- ARGV.concat(%w[--skip-bundler])
- rspec_autotest.cli_args.should eq(%w[--tty])
- end
+ context "with --skip-bundler" do
+ before do
+ ARGV.concat(%w[--skip-bundler])
+ end
+ it "extracts --skip-bundler" do
+ rspec_autotest.cl_args.should eq(%w[--tty])
+ end
- it "excludes bundler from command with --skip-bundler" do
- ARGV.concat(%w[--skip-bundler])
- rspec_autotest.make_test_cmd({'a' => 'b'}).should_not match(/bundle exec/)
+ it "sets skip_bundler? true" do
+ rspec_autotest.skip_bundler?.should be_true
+ end
end
end
- describe "warning about bundler" do
+ describe "handling bundler" do
context "gemfile, no prefix" do
before do
File.stub(:exist?).with("./Gemfile") { true }
end
- it "warns" do
+ it "warns of deprecation of implicit inclusion of 'bundle exec'" do
RSpec.should_receive(:warn_deprecation)
rspec_autotest.make_test_cmd({})
end
@@ -100,6 +102,24 @@
end
end
+ context "gemfile, prefix, --skip-bundler" do
+ before do
+ File.stub(:exist?).with("./Gemfile") { true }
+ rspec_autotest.prefix = "bundle exec "
+ rspec_autotest.stub(:skip_bundler?) { true }
+ end
+
+ it "does not warn" do
+ RSpec.should_not_receive(:warn_deprecation)
+ rspec_autotest.make_test_cmd({'a' => 'b'})
+ end
+
+ it "does not include bundle exec" do
+ rspec_autotest.
+ make_test_cmd({'a' => 'b'}).should_not match(/bundle exec/)
+ end
+ end
+
context "no gemfile, no prefix" do
before do
File.stub(:exist?).with("./Gemfile") { false }
@@ -222,20 +242,4 @@
rspec_autotest.normalize(@files_to_test).should have(1).file
end
end
-
- describe "ruby command" do
- context "using bundler" do
- it "returns 'bundle exec'" do
- File.stub(:exist?).with("./Gemfile") { true }
- rspec_autotest.make_test_cmd({'a' => 'b'}).should =~ /bundle exec .*ruby/
- end
- end
-
- context "not using bundler" do
- it "returns the ruby command generated by Autotest" do
- File.stub(:exist?).with("./Gemfile") { false }
- rspec_autotest.make_test_cmd({'a' => 'b'}).should_not =~ /bundle exec/
- end
- end
- end
end

0 comments on commit bac86b1

Please sign in to comment.