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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
# frozen_string_literal: true
RSpec.describe "bundle binstubs <gem>" do
context "when the gem exists in the lockfile" do
it "sets up the binstub" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G
bundle "binstubs rack"
expect(bundled_app("bin/rackup")).to exist
end
it "does not install other binstubs" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
gem "rails"
G
bundle "binstubs rails"
expect(bundled_app("bin/rackup")).not_to exist
expect(bundled_app("bin/rails")).to exist
end
it "does install multiple binstubs" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
gem "rails"
G
bundle "binstubs rails rack"
expect(bundled_app("bin/rackup")).to exist
expect(bundled_app("bin/rails")).to exist
end
it "allows installing all binstubs" do
install_gemfile! <<-G
source "file://#{gem_repo1}"
gem "rails"
G
bundle! :binstubs, :all => true
expect(bundled_app("bin/rails")).to exist
expect(bundled_app("bin/rake")).to exist
end
it "displays an error when used without any gem" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G
bundle "binstubs"
expect(exitstatus).to eq(1) if exitstatus
expect(out).to include("`bundle binstubs` needs at least one gem to run.")
end
it "displays an error when used with --all and gems" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G
bundle "binstubs rack", :all => true
expect(last_command).to be_failure
expect(last_command.bundler_err).to include("Cannot specify --all with specific gems")
end
context "when generating bundle binstub outside bundler" do
it "should abort" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G
bundle "binstubs rack"
File.open("bin/bundle", "wb") do |file|
file.print "OMG"
end
sys_exec "bin/rackup"
expect(last_command.stderr).to include("was not generated by Bundler")
end
end
context "the bundle binstub" do
before do
if system_bundler_version == :bundler
system_gems :bundler
elsif system_bundler_version
build_repo4 do
build_gem "bundler", system_bundler_version do |s|
s.executables = "bundle"
s.bindir = "exe"
s.write "exe/bundle", "puts %(system bundler #{system_bundler_version}\\n\#{ARGV.inspect})"
end
end
system_gems "bundler-#{system_bundler_version}", :gem_repo => gem_repo4
end
build_repo2 do
build_gem "prints_loaded_gems", "1.0" do |s|
s.executables = "print_loaded_gems"
s.bindir = "exe"
s.write "exe/print_loaded_gems", <<-R
specs = Gem.loaded_specs.values.reject {|s| Bundler.rubygems.spec_default_gem?(s) }
puts specs.map(&:full_name).sort.inspect
R
end
end
install_gemfile! <<-G
source "file://#{gem_repo2}"
gem "rack"
gem "prints_loaded_gems"
G
bundle! "binstubs bundler rack prints_loaded_gems"
end
# When environment has a same version of bundler as default gems.
# `system_gems "bundler-x.y.z"` will detect system binstub.
# We need to avoid it by virtual version of bundler.
let(:system_bundler_version) { Gem::Version.new(Bundler::VERSION).bump.to_s }
context "when system bundler was used" do
# Support master branch of bundler
if ENV["BUNDLER_SPEC_SUB_VERSION"]
let(:system_bundler_version) { Bundler::VERSION }
end
before do
gemfile <<-G
source "file:///Users/colby/Projects/bundler/tmp/gems/remote2"
gem "rack"
gem "prints_loaded_gems"
G
lockfile <<-G
GEM
remote: file:///Users/colby/Projects/bundler/tmp/gems/remote2/
specs:
prints_loaded_gems (1.0)
rack (1.2)
PLATFORMS
ruby
DEPENDENCIES
prints_loaded_gems
rack
BUNDLED WITH
#{system_bundler_version}
G
end
it "runs bundler" do
sys_exec! "#{bundled_app("bin/bundle")} install"
expect(out).to eq %(system bundler #{system_bundler_version}\n["install"])
end
end
context "when BUNDLER_VERSION is set" do
let(:system_bundler_version) { Bundler::VERSION }
it "runs the correct version of bundler" do
sys_exec "BUNDLER_VERSION='999.999.999' #{bundled_app("bin/bundle")} install"
expect(exitstatus).to eq(42) if exitstatus
expect(last_command.stderr).to include("Activating bundler (999.999.999) failed:").
and include("To install the version of bundler this project requires, run `gem install bundler -v '999.999.999'`")
end
end
context "when a lockfile exists with a locked bundler version" do
let(:system_bundler_version) { Bundler::VERSION }
it "runs the correct version of bundler when the version is newer" do
lockfile lockfile.gsub(system_bundler_version, "999.999.999")
sys_exec "#{bundled_app("bin/bundle")} install"
expect(exitstatus).to eq(42) if exitstatus
expect(last_command.stderr).to include("Activating bundler (999.999.999) failed:").
and include("To install the version of bundler this project requires, run `gem install bundler -v '999.999.999'`")
end
it "runs the correct version of bundler when the version is older" do
simulate_bundler_version "55"
lockfile lockfile.gsub(system_bundler_version, "44.0")
sys_exec "#{bundled_app("bin/bundle")} install"
expect(exitstatus).to eq(42) if exitstatus
expect(last_command.stderr).to include("Activating bundler (44.0) failed:").
and include("To install the version of bundler this project requires, run `gem install bundler -v '44.0'`")
end
it "runs the correct version of bundler when the version is a pre-release" do
simulate_bundler_version "55"
lockfile lockfile.gsub(system_bundler_version, "2.12.0.a")
sys_exec "#{bundled_app("bin/bundle")} install"
expect(exitstatus).to eq(42) if exitstatus
expect(last_command.stderr).to include("Activating bundler (2.12.0.a) failed:").
and include("To install the version of bundler this project requires, run `gem install bundler -v '2.12.0.a'`")
end
end
context "when update --bundler is called" do
before { lockfile.gsub(system_bundler_version, "1.1.1") }
it "calls through to the latest bundler version" do
sys_exec! "#{bundled_app("bin/bundle")} update --bundler"
expect(last_command.stdout).to eq %(system bundler #{system_bundler_version}\n["update", "--bundler"])
end
it "calls through to the explicit bundler version" do
sys_exec "#{bundled_app("bin/bundle")} update --bundler=999.999.999"
expect(exitstatus).to eq(42) if exitstatus
expect(last_command.stderr).to include("Activating bundler (999.999.999) failed:").
and include("To install the version of bundler this project requires, run `gem install bundler -v '999.999.999'`")
end
end
context "without a lockfile" do
it "falls back to the latest installed bundler" do
FileUtils.rm bundled_app("Gemfile.lock")
sys_exec! bundled_app("bin/bundle").to_s
expect(out).to eq "system bundler #{system_bundler_version}\n[]"
end
end
context "using another binstub" do
let(:system_bundler_version) { :bundler }
it "loads all gems" do
sys_exec! bundled_app("bin/print_loaded_gems").to_s
# RG < 2.0.14 didn't have a `Gem::Specification#default_gem?`
# This is dirty detection for old RG versions.
if File.dirname(Bundler.load.specs["bundler"][0].loaded_from) =~ %r{specifications/default}
expect(out).to eq %(["prints_loaded_gems-1.0", "rack-1.2"])
else
expect(out).to eq %(["bundler-#{Bundler::VERSION}", "prints_loaded_gems-1.0", "rack-1.2"])
end
end
context "when requesting a different bundler version" do
before { lockfile lockfile.gsub(Bundler::VERSION, "999.999.999") }
it "attempts to load that version", :ruby_repo do
sys_exec bundled_app("bin/rackup").to_s
expect(exitstatus).to eq(42) if exitstatus
expect(last_command.stderr).to include("Activating bundler (999.999.999) failed:").
and include("To install the version of bundler this project requires, run `gem install bundler -v '999.999.999'`")
end
end
end
end
it "installs binstubs from git gems" do
FileUtils.mkdir_p(lib_path("foo/bin"))
FileUtils.touch(lib_path("foo/bin/foo"))
build_git "foo", "1.0", :path => lib_path("foo") do |s|
s.executables = %w[foo]
end
install_gemfile <<-G
gem "foo", :git => "#{lib_path("foo")}"
G
bundle "binstubs foo"
expect(bundled_app("bin/foo")).to exist
end
it "installs binstubs from path gems" do
FileUtils.mkdir_p(lib_path("foo/bin"))
FileUtils.touch(lib_path("foo/bin/foo"))
build_lib "foo", "1.0", :path => lib_path("foo") do |s|
s.executables = %w[foo]
end
install_gemfile <<-G
gem "foo", :path => "#{lib_path("foo")}"
G
bundle "binstubs foo"
expect(bundled_app("bin/foo")).to exist
end
it "sets correct permissions for binstubs" do
with_umask(0o002) do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G
bundle "binstubs rack"
binary = bundled_app("bin/rackup")
expect(File.stat(binary).mode.to_s(8)).to eq("100775")
end
end
context "when using --shebang" do
it "sets the specified shebang for the the binstub" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G
bundle "binstubs rack --shebang jruby"
expect(File.open("bin/rackup").gets).to eq("#!/usr/bin/env jruby\n")
end
end
end
context "when the gem doesn't exist" do
it "displays an error with correct status" do
install_gemfile <<-G
source "file://#{gem_repo1}"
G
bundle "binstubs doesnt_exist"
expect(exitstatus).to eq(7) if exitstatus
expect(out).to include("Could not find gem 'doesnt_exist'.")
end
end
context "--path" do
it "sets the binstubs dir" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G
bundle "binstubs rack --path exec"
expect(bundled_app("exec/rackup")).to exist
end
it "setting is saved for bundle install", :bundler => "< 3" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
gem "rails"
G
bundle! "binstubs rack", forgotten_command_line_options([:path, :bin] => "exec")
bundle! :install
expect(bundled_app("exec/rails")).to exist
end
end
context "with --standalone option" do
before do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G
end
it "generates a standalone binstub" do
bundle! "binstubs rack --standalone"
expect(bundled_app("bin/rackup")).to exist
end
it "generates a binstub that does not depend on rubygems or bundler" do
bundle! "binstubs rack --standalone"
expect(File.read(bundled_app("bin/rackup"))).to_not include("Gem.bin_path")
end
context "when specified --path option" do
it "generates a standalone binstub at the given path" do
bundle! "binstubs rack --standalone --path foo"
expect(bundled_app("foo/rackup")).to exist
end
end
end
context "when the bin already exists" do
it "doesn't overwrite and warns" do
FileUtils.mkdir_p(bundled_app("bin"))
File.open(bundled_app("bin/rackup"), "wb") do |file|
file.print "OMG"
end
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G
bundle "binstubs rack"
expect(bundled_app("bin/rackup")).to exist
expect(File.read(bundled_app("bin/rackup"))).to eq("OMG")
expect(out).to include("Skipped rackup")
expect(out).to include("overwrite skipped stubs, use --force")
end
context "when using --force" do
it "overwrites the binstub" do
FileUtils.mkdir_p(bundled_app("bin"))
File.open(bundled_app("bin/rackup"), "wb") do |file|
file.print "OMG"
end
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G
bundle "binstubs rack --force"
expect(bundled_app("bin/rackup")).to exist
expect(File.read(bundled_app("bin/rackup"))).not_to eq("OMG")
end
end
end
context "when the gem has no bins" do
it "suggests child gems if they have bins" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack-obama"
G
bundle "binstubs rack-obama"
expect(out).to include("rack-obama has no executables")
expect(out).to include("rack has: rackup")
end
it "works if child gems don't have bins" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "actionpack"
G
bundle "binstubs actionpack"
expect(out).to include("no executables for the gem actionpack")
end
it "works if the gem has development dependencies" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "with_development_dependency"
G
bundle "binstubs with_development_dependency"
expect(out).to include("no executables for the gem with_development_dependency")
end
end
context "when BUNDLE_INSTALL is specified" do
it "performs an automatic bundle install" do
gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G
bundle "config auto_install 1"
bundle "binstubs rack"
expect(out).to include("Installing rack 1.0.0")
expect(the_bundle).to include_gems "rack 1.0.0"
end
it "does nothing when already up to date" do
install_gemfile <<-G
source "file://#{gem_repo1}"
gem "rack"
G
bundle "config auto_install 1"
bundle "binstubs rack", :env => { "BUNDLE_INSTALL" => 1 }
expect(out).not_to include("Installing rack 1.0.0")
end
end
end
|