Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 976b935

Browse files
committed
Run autotest with 'bundle exec' if there is a Gemfile present.
1 parent 5aa6ae3 commit 976b935

File tree

2 files changed

+67
-46
lines changed

2 files changed

+67
-46
lines changed

lib/autotest/rspec2.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def make_test_cmd(files_to_test)
4242
end
4343

4444
def require_rubygems
45-
defined?(:Gem) ? "-rrubygems " : ""
45+
using_bundler? ? "" : defined?(:Gem) ? "-rrubygems " : ""
4646
end
4747

4848
def normalize(files_to_test)
@@ -52,4 +52,12 @@ def normalize(files_to_test)
5252
end
5353
end
5454

55+
def ruby
56+
using_bundler? ? "bundle exec" : super
57+
end
58+
59+
def using_bundler?
60+
File.exists?('./Gemfile')
61+
end
62+
5563
end

spec/autotest/rspec_spec.rb

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,132 @@
11
require "spec_helper"
22

33
describe Autotest::Rspec2 do
4-
describe "commands" do
5-
before(:each) do
6-
@rspec_autotest = Autotest::Rspec2.new
7-
@rspec_autotest.stub!(:ruby).and_return "ruby"
4+
let(:rspec_autotest) { Autotest::Rspec2.new }
5+
let(:spec_cmd) { File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin', 'rspec')) }
6+
let(:ruby_cmd) { "ruby" }
87

9-
@ruby = @rspec_autotest.ruby
10-
@spec_cmd = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin', 'rspec'))
8+
describe "commands" do
9+
before do
10+
rspec_autotest.stub(:ruby => ruby_cmd)
1111
files = %w[file_one file_two]
1212
@files_to_test = {
1313
files[0] => [],
1414
files[1] => []
1515
}
1616
# this is not the inner representation of Autotest!
17-
@rspec_autotest.files_to_test = @files_to_test
17+
rspec_autotest.files_to_test = @files_to_test
1818
@to_test = files.map { |f| File.expand_path(f) }.join ' '
1919
end
2020

21-
it "should make the appropriate test command" do
22-
actual = @rspec_autotest.make_test_cmd(@files_to_test)
23-
expected = /#{@ruby}.*#{@spec_cmd} (.*)/
21+
it "makes the appropriate test command" do
22+
actual_command = rspec_autotest.make_test_cmd(@files_to_test)
23+
expected_command = /#{ruby_cmd}.*#{spec_cmd} (.*)/
2424

25-
actual.should match(expected)
25+
actual_command.should match(expected_command)
2626

27-
actual =~ expected
27+
actual_command =~ expected_command
2828
$1.should =~ /#{File.expand_path('file_one')}/
2929
$1.should =~ /#{File.expand_path('file_two')}/
3030
end
3131

32-
it "should return a blank command for no files" do
33-
@rspec_autotest.make_test_cmd({}).should == ''
32+
it "returns a blank command for no files" do
33+
rspec_autotest.make_test_cmd({}).should eq('')
3434
end
3535

36-
it "should quote the paths of files to test" do
37-
cmd = @rspec_autotest.make_test_cmd(@files_to_test)
36+
it "quotes the paths of files to test" do
37+
cmd = rspec_autotest.make_test_cmd(@files_to_test)
3838
@files_to_test.keys.each do |file_to_test|
39-
cmd.should =~ /'#{File.expand_path(file_to_test)}'/
39+
cmd.should match(/'#{File.expand_path(file_to_test)}'/)
4040
end
4141
end
4242
end
4343

4444
describe "mappings" do
45-
46-
before(:each) do
45+
before do
4746
@lib_file = "lib/something.rb"
4847
@spec_file = "spec/something_spec.rb"
49-
@rspec_autotest = Autotest::Rspec2.new
50-
@rspec_autotest.hook :initialize
48+
rspec_autotest.hook :initialize
5149
end
5250

53-
it "should find the spec file for a given lib file" do
54-
@rspec_autotest.should map_specs([@spec_file]).to(@lib_file)
51+
it "finds the spec file for a given lib file" do
52+
rspec_autotest.should map_specs([@spec_file]).to(@lib_file)
5553
end
5654

57-
it "should find the spec file if given a spec file" do
58-
@rspec_autotest.should map_specs([@spec_file]).to(@spec_file)
55+
it "finds the spec file if given a spec file" do
56+
rspec_autotest.should map_specs([@spec_file]).to(@spec_file)
5957
end
6058

61-
it "should ignore files in spec dir that aren't specs" do
62-
@rspec_autotest.should map_specs([]).to("spec/spec_helper.rb")
59+
it "ignores files in spec dir that aren't specs" do
60+
rspec_autotest.should map_specs([]).to("spec/spec_helper.rb")
6361
end
6462

65-
it "should ignore untracked files (in @file)" do
66-
@rspec_autotest.should map_specs([]).to("lib/untracked_file")
63+
it "ignores untracked files (in @file)" do
64+
rspec_autotest.should map_specs([]).to("lib/untracked_file")
6765
end
6866
end
6967

7068
describe "consolidating failures" do
71-
before(:each) do
72-
@rspec_autotest = Autotest::Rspec2.new
73-
69+
before do
7470
@spec_file = "spec/autotest/some_spec.rb"
75-
@rspec_autotest.instance_variable_set("@files", {@spec_file => Time.now})
76-
@rspec_autotest.stub!(:find_files_to_test).and_return true
71+
rspec_autotest.instance_variable_set("@files", {@spec_file => Time.now})
72+
rspec_autotest.stub!(:find_files_to_test).and_return true
7773
end
7874

79-
it "should return no failures if no failures were given in the output" do
80-
@rspec_autotest.consolidate_failures([[]]).should == {}
75+
it "returns no failures if no failures were given in the output" do
76+
rspec_autotest.consolidate_failures([[]]).should == {}
8177
end
8278

83-
it "should return a hash with the spec filename => spec name for each failure or error" do
84-
@rspec_autotest.stub!(:test_files_for).and_return "spec/autotest/some_spec.rb"
79+
it "returns a hash with the spec filename => spec name for each failure or error" do
80+
rspec_autotest.stub!(:test_files_for).and_return "spec/autotest/some_spec.rb"
8581
failures = [
8682
[
8783
"false should be false",
8884
"#{@spec_file}"
8985
]
9086
]
91-
@rspec_autotest.consolidate_failures(failures).should == {
87+
rspec_autotest.consolidate_failures(failures).should == {
9288
@spec_file => ["false should be false"]
9389
}
9490
end
9591

96-
it "should not include the subject file" do
92+
it "does not include the subject file" do
9793
subject_file = "lib/autotest/some.rb"
98-
@rspec_autotest.stub!(:test_files_for).and_return "spec/autotest/some_spec.rb"
94+
rspec_autotest.stub!(:test_files_for).and_return "spec/autotest/some_spec.rb"
9995
failures = [
10096
[
10197
"false should be false",
10298
"expected: true,\n got: false (using ==)\n#{subject_file}:143:\n#{@spec_file}:203:"
10399
]
104100
]
105-
@rspec_autotest.consolidate_failures(failures).keys.should_not include(subject_file)
101+
rspec_autotest.consolidate_failures(failures).keys.should_not include(subject_file)
106102
end
107103
end
108104

109105
describe "normalizing file names" do
110-
it "should ensure that a single file appears in files_to_test only once" do
111-
@rspec_autotest = Autotest::Rspec2.new
106+
it "ensures that a single file appears in files_to_test only once" do
112107
@files_to_test = {}
113108
['filename.rb', './filename.rb', File.expand_path('filename.rb')].each do |file|
114109
@files_to_test[file] = []
115110
end
116-
@rspec_autotest.normalize(@files_to_test).should have(1).file
111+
rspec_autotest.normalize(@files_to_test).should have(1).file
112+
end
113+
end
114+
115+
describe "ruby command" do
116+
context "using bundler" do
117+
it "returns 'bundle exec'" do
118+
File.stub(:exists?).with("./Gemfile") { true }
119+
rspec_autotest.ruby.should eq("bundle exec")
120+
end
121+
end
122+
123+
context "not using bundler" do
124+
it "returns the ruby command generated by Autotest" do
125+
autotest = Autotest.new
126+
autotest_ruby_command = autotest.ruby
127+
File.stub(:exists?).with("./Gemfile") { false }
128+
rspec_autotest.ruby.should eq(autotest_ruby_command)
129+
end
117130
end
118131
end
119132
end

0 commit comments

Comments
 (0)