Skip to content

Commit 1fea946

Browse files
Fix uncaught Gem::LoadError in binstub loader
The previous binstub code did not catch Gem::LoadErrors which can be generated by bundler or rubygems trying to load the spring gem. This fixes #447.
1 parent eb2255b commit 1fea946

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

lib/spring/client/binstub.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ class Binstub < Command
1313
# should cause the "unsprung" version of the command to run.
1414
LOADER = <<CODE
1515
begin
16-
spring_bin_path = File.expand_path('../spring', __FILE__)
17-
load spring_bin_path
16+
load File.expand_path('../spring', __FILE__)
1817
rescue LoadError => e
19-
raise unless e.message.end_with? spring_bin_path, 'spring/binstub'
18+
raise unless e.message.include?('spring')
2019
end
2120
CODE
2221

@@ -51,6 +50,7 @@ class Binstub < Command
5150
BINSTUB_VARIATIONS = Regexp.union [
5251
%{begin\n load File.expand_path("../spring", __FILE__)\nrescue LoadError\nend\n},
5352
%{begin\n load File.expand_path('../spring', __FILE__)\nrescue LoadError\nend\n},
53+
%{begin\n spring_bin_path = File.expand_path('../spring', __FILE__)\n load spring_bin_path\nrescue LoadError => e\n raise unless e.message.end_with? spring_bin_path, 'spring/binstub'\nend\n},
5454
LOADER
5555
]
5656

lib/spring/test/acceptance_test.rb

+25-16
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ def exec_name
275275
end
276276

277277
test "binstub upgrade with new binstub variations" do
278+
expected = <<-RUBY.gsub(/^ /, "")
279+
#!/usr/bin/env ruby
280+
#{Spring::Client::Binstub::LOADER.strip}
281+
require 'bundler/setup'
282+
load Gem.bin_path('rake', 'rake')
283+
RUBY
284+
278285
# older variation with double quotes
279286
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
280287
#!/usr/bin/env ruby
@@ -286,36 +293,38 @@ def exec_name
286293
load Gem.bin_path('rake', 'rake')
287294
RUBY
288295

296+
assert_success "bin/spring binstub rake", stdout: "bin/rake: upgraded"
297+
assert_equal expected, app.path("bin/rake").read
298+
289299
# newer variation with single quotes
290-
File.write(app.path("bin/rails"), <<-RUBY.strip_heredoc)
300+
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
291301
#!/usr/bin/env ruby
292302
begin
293303
load File.expand_path('../spring', __FILE__)
294304
rescue LoadError
295305
end
296-
APP_PATH = File.expand_path('../../config/application', __FILE__)
297-
require_relative '../config/boot'
298-
require 'rails/commands'
306+
require 'bundler/setup'
307+
load Gem.bin_path('rake', 'rake')
299308
RUBY
300309

301-
assert_success "bin/spring binstub --all", stdout: "upgraded"
310+
assert_success "bin/spring binstub rake", stdout: "bin/rake: upgraded"
311+
assert_equal expected, app.path("bin/rake").read
302312

303-
expected = <<-RUBY.gsub(/^ /, "")
313+
# newer variation which checks end of exception message
314+
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
304315
#!/usr/bin/env ruby
305-
#{Spring::Client::Binstub::LOADER.strip}
316+
begin
317+
spring_bin_path = File.expand_path('../spring', __FILE__)
318+
load spring_bin_path
319+
rescue LoadError => e
320+
raise unless e.message.end_with? spring_bin_path, 'spring/binstub'
321+
end
306322
require 'bundler/setup'
307323
load Gem.bin_path('rake', 'rake')
308324
RUBY
309-
assert_equal expected, app.path("bin/rake").read
310325

311-
expected = <<-RUBY.gsub(/^ /, "")
312-
#!/usr/bin/env ruby
313-
#{Spring::Client::Binstub::LOADER.strip}
314-
APP_PATH = File.expand_path('../../config/application', __FILE__)
315-
require_relative '../config/boot'
316-
require 'rails/commands'
317-
RUBY
318-
assert_equal expected, app.path("bin/rails").read
326+
assert_success "bin/spring binstub rake", stdout: "bin/rake: upgraded"
327+
assert_equal expected, app.path("bin/rake").read
319328
end
320329

321330
test "binstub remove with new binstub variations" do

0 commit comments

Comments
 (0)