1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
# frozen_string_literal: true
require "spec_helper"
describe "bundle exec" do
before :each do
system_gems "rack-1.0.0", "rack-0.9.1"
end
it "activates the correct gem" do
gemfile <<-G
gem "rack", "0.9.1"
G
bundle "exec rackup"
expect(out).to eq("0.9.1")
end
it "works when the bins are in ~/.bundle" do
install_gemfile <<-G
gem "rack"
G
bundle "exec rackup"
expect(out).to eq("1.0.0")
end
it "works when running from a random directory" do
install_gemfile <<-G
gem "rack"
G
bundle "exec 'cd #{tmp("gems")} && rackup'"
expect(out).to eq("1.0.0")
end
it "works when exec'ing something else" do
install_gemfile 'gem "rack"'
bundle "exec echo exec"
expect(out).to eq("exec")
end
it "works when exec'ing to ruby" do
install_gemfile 'gem "rack"'
bundle "exec ruby -e 'puts %{hi}'"
expect(out).to eq("hi")
end
it "accepts --verbose" do
install_gemfile 'gem "rack"'
bundle "exec --verbose echo foobar"
expect(out).to eq("foobar")
end
it "passes --verbose to command if it is given after the command" do
install_gemfile 'gem "rack"'
bundle "exec echo --verbose"
expect(out).to eq("--verbose")
end
it "handles --keep-file-descriptors" do
require "tempfile"
bundle_bin = File.expand_path("../../../exe/bundle", __FILE__)
command = Tempfile.new("io-test")
command.sync = true
command.write <<-G
if ARGV[0]
IO.for_fd(ARGV[0].to_i)
else
require 'tempfile'
io = Tempfile.new("io-test-fd")
args = %W[#{Gem.ruby} -I#{lib} #{bundle_bin} exec --keep-file-descriptors #{Gem.ruby} #{command.path} \#{io.to_i}]
args << { io.to_i => io } if RUBY_VERSION >= "2.0"
exec(*args)
end
G
install_gemfile ""
sys_exec("#{Gem.ruby} #{command.path}")
if Bundler.current_ruby.ruby_2?
expect(out).to eq("")
else
expect(out).to eq("Ruby version #{RUBY_VERSION} defaults to keeping non-standard file descriptors on Kernel#exec.")
end
expect(err).to eq("")
end
it "accepts --keep-file-descriptors" do
install_gemfile ""
bundle "exec --keep-file-descriptors echo foobar"
expect(err).to eq("")
end
it "can run a command named --verbose" do
install_gemfile 'gem "rack"'
File.open("--verbose", "w") do |f|
f.puts "#!/bin/sh"
f.puts "echo foobar"
end
File.chmod(0744, "--verbose")
with_path_as(".") do
bundle "exec -- --verbose"
end
expect(out).to eq("foobar")
end
it "handles different versions in different bundles" do
build_repo2 do
build_gem "rack_two", "1.0.0" do |s|
s.executables = "rackup"
end
end
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack", "0.9.1"
G
Dir.chdir bundled_app2 do
install_gemfile bundled_app2("Gemfile"), <<-G
source "file://#{gem_repo2}"
gem "rack_two", "1.0.0"
G
end
bundle "exec rackup", :expect_err => true
expect(out).to eq("0.9.1")
expect(err).to match("deprecated")
Dir.chdir bundled_app2 do
bundle "exec rackup"
expect(out).to eq("1.0.0")
end
end
it "handles gems installed with --without" do
install_gemfile <<-G, :without => :middleware
source "file://#{gem_repo1}"
gem "rack" # rack 0.9.1 and 1.0 exist
group :middleware do
gem "rack_middleware" # rack_middleware depends on rack 0.9.1
end
G
bundle "exec rackup"
expect(out).to eq("0.9.1")
should_not_be_installed "rack_middleware 1.0"
end
it "does not duplicate already exec'ed RUBYOPT" do
install_gemfile <<-G
gem "rack"
G
rubyopt = ENV["RUBYOPT"]
rubyopt = "-rbundler/setup #{rubyopt}"
bundle "exec 'echo $RUBYOPT'"
expect(out).to have_rubyopts(rubyopt)
bundle "exec 'echo $RUBYOPT'", :env => { "RUBYOPT" => rubyopt }
expect(out).to have_rubyopts(rubyopt)
end
it "does not duplicate already exec'ed RUBYLIB" do
install_gemfile <<-G
gem "rack"
G
rubylib = ENV["RUBYLIB"]
rubylib = "#{rubylib}".split(File::PATH_SEPARATOR).unshift "#{bundler_path}"
rubylib = rubylib.uniq.join(File::PATH_SEPARATOR)
bundle "exec 'echo $RUBYLIB'"
expect(out).to eq(rubylib)
bundle "exec 'echo $RUBYLIB'", :env => { "RUBYLIB" => rubylib }
expect(out).to eq(rubylib)
end
it "errors nicely when the argument doesn't exist" do
install_gemfile <<-G
gem "rack"
G
bundle "exec foobarbaz"
expect(exitstatus).to eq(127) if exitstatus
expect(out).to include("bundler: command not found: foobarbaz")
expect(out).to include("Install missing gem executables with `bundle install`")
end
it "errors nicely when the argument is not executable" do
install_gemfile <<-G
gem "rack"
G
bundle "exec touch foo"
bundle "exec ./foo"
expect(exitstatus).to eq(126) if exitstatus
expect(out).to include("bundler: not executable: ./foo")
end
it "errors nicely when no arguments are passed" do
install_gemfile <<-G
gem "rack"
G
bundle "exec"
expect(exitstatus).to eq(128) if exitstatus
expect(out).to include("bundler: exec needs a command to run")
end
describe "with gem executables" do
describe "run from a random directory" do
before(:each) do
install_gemfile <<-G
gem "rack"
G
end
it "works when unlocked" do
bundle "exec 'cd #{tmp("gems")} && rackup'"
expect(out).to eq("1.0.0")
end
it "works when locked" do
should_be_locked
bundle "exec 'cd #{tmp("gems")} && rackup'"
expect(out).to eq("1.0.0")
end
end
describe "from gems bundled via :path" do
before(:each) do
build_lib "fizz", :path => home("fizz") do |s|
s.executables = "fizz"
end
install_gemfile <<-G
gem "fizz", :path => "#{File.expand_path(home("fizz"))}"
G
end
it "works when unlocked" do
bundle "exec fizz"
expect(out).to eq("1.0")
end
it "works when locked" do
should_be_locked
bundle "exec fizz"
expect(out).to eq("1.0")
end
end
describe "from gems bundled via :git" do
before(:each) do
build_git "fizz_git" do |s|
s.executables = "fizz_git"
end
install_gemfile <<-G
gem "fizz_git", :git => "#{lib_path("fizz_git-1.0")}"
G
end
it "works when unlocked" do
bundle "exec fizz_git"
expect(out).to eq("1.0")
end
it "works when locked" do
should_be_locked
bundle "exec fizz_git"
expect(out).to eq("1.0")
end
end
describe "from gems bundled via :git with no gemspec" do
before(:each) do
build_git "fizz_no_gemspec", :gemspec => false do |s|
s.executables = "fizz_no_gemspec"
end
install_gemfile <<-G
gem "fizz_no_gemspec", "1.0", :git => "#{lib_path("fizz_no_gemspec-1.0")}"
G
end
it "works when unlocked" do
bundle "exec fizz_no_gemspec"
expect(out).to eq("1.0")
end
it "works when locked" do
should_be_locked
bundle "exec fizz_no_gemspec"
expect(out).to eq("1.0")
end
end
end
it "performs an automatic bundle install" do
gemfile <<-G
source "file://#{gem_repo1}"
gem "rack", "0.9.1"
gem "foo"
G
bundle "config auto_install 1"
bundle "exec rackup"
expect(out).to include("Installing foo 1.0")
end
describe "with gems bundled via :path with invalid gemspecs" do
it "outputs the gemspec validation errors", :rubygems => ">= 1.7.2" do
build_lib "foo"
gemspec = lib_path("foo-1.0").join("foo.gemspec").to_s
File.open(gemspec, "w") do |f|
f.write <<-G
Gem::Specification.new do |s|
s.name = 'foo'
s.version = '1.0'
s.summary = 'TODO: Add summary'
s.authors = 'Me'
end
G
end
install_gemfile <<-G, :expect_err => true
gem "foo", :path => "#{lib_path("foo-1.0")}"
G
bundle "exec irb", :expect_err => true
expect(err).to match("The gemspec at #{lib_path("foo-1.0").join("foo.gemspec")} is not valid")
expect(err).to match('"TODO" is not a summary')
end
end
describe "with gems bundled for deployment" do
it "works when calling bundler from another script" do
gemfile <<-G
module Monkey
def bin_path(a,b,c)
raise Gem::GemNotFoundException.new('Fail')
end
end
Bundler.rubygems.extend(Monkey)
G
bundle "install --deployment"
bundle "exec ruby -e '`../../exe/bundler -v`; puts $?.success?'"
expect(out).to match("true")
end
end
context "`load`ing a ruby file instead of `exec`ing" do
let(:path) { bundled_app("ruby_executable") }
let(:shebang) { "#!/usr/bin/env ruby" }
let(:executable) { <<-RUBY.gsub(/^ */, "").strip }
#{shebang}
require "rack"
puts "EXEC: \#{caller.grep(/load/).empty? ? 'exec' : 'load'}"
puts "ARGS: \#{$0} \#{ARGV.join(' ')}"
puts "RACK: \#{RACK}"
RUBY
before do
path.open("w") {|f| f << executable }
path.chmod(0755)
install_gemfile <<-G
gem "rack"
G
end
let(:exec) { "EXEC: load" }
let(:args) { "ARGS: #{path} arg1 arg2" }
let(:rack) { "RACK: 1.0.0" }
let(:exit_code) { 0 }
let(:expected) { [exec, args, rack].join("\n") }
let(:expected_err) { "" }
subject { bundle "exec #{path} arg1 arg2", :expect_err => true }
shared_examples_for "it runs" do
it "like a normally executed executable like a normally executed executable" do
subject
expect(exitstatus).to eq(exit_code) if exitstatus
expect(err).to eq(expected_err)
expect(out).to eq(expected)
end
end
it_behaves_like "it runs"
context "the executable exits explicitly" do
let(:executable) { super() << "\nexit #{exit_code}\nputs 'POST_EXIT'\n" }
context "with exit 0" do
it_behaves_like "it runs"
end
context "with exit 99" do
let(:exit_code) { 99 }
it_behaves_like "it runs"
end
end
context "the executable raises" do
let(:executable) { super() << "\nraise 'ERROR'" }
let(:exit_code) { 1 }
let(:expected) { super() << "\nbundler: failed to load command: #{path} (#{path})" }
let(:expected_err) do
"RuntimeError: ERROR\n #{path}:7" +
(Bundler.current_ruby.ruby_18? ? "" : ":in `<top (required)>'")
end
it_behaves_like "it runs"
end
context "when the file uses the current ruby shebang" do
let(:shebang) { "#!#{Gem.ruby}" }
it_behaves_like "it runs"
end
context "when Bundler.setup fails" do
before do
gemfile <<-G
gem 'rack', '2'
G
ENV["BUNDLER_FORCE_TTY"] = "true"
end
let(:exit_code) { Bundler::GemNotFound.new.status_code }
let(:expected) { <<-EOS.strip }
\e[31mCould not find gem 'rack (= 2)' in any of the gem sources listed in your Gemfile or available on this machine.\e[0m
\e[33mRun `bundle install` to install missing gems.\e[0m
EOS
it_behaves_like "it runs"
end
end
end
|