Skip to content

Commit

Permalink
Fix uncaught Gem::LoadError in binstub loader
Browse files Browse the repository at this point in the history
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 rails#447.
  • Loading branch information
felixbuenemann committed Nov 18, 2015
1 parent eb2255b commit 1fea946
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
6 changes: 3 additions & 3 deletions lib/spring/client/binstub.rb
Expand Up @@ -13,10 +13,9 @@ class Binstub < Command
# should cause the "unsprung" version of the command to run.
LOADER = <<CODE
begin
spring_bin_path = File.expand_path('../spring', __FILE__)
load spring_bin_path
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.end_with? spring_bin_path, 'spring/binstub'
raise unless e.message.include?('spring')
end
CODE

Expand Down Expand Up @@ -51,6 +50,7 @@ class Binstub < Command
BINSTUB_VARIATIONS = Regexp.union [
%{begin\n load File.expand_path("../spring", __FILE__)\nrescue LoadError\nend\n},
%{begin\n load File.expand_path('../spring', __FILE__)\nrescue LoadError\nend\n},
%{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},
LOADER
]

Expand Down
41 changes: 25 additions & 16 deletions lib/spring/test/acceptance_test.rb
Expand Up @@ -275,6 +275,13 @@ def exec_name
end

test "binstub upgrade with new binstub variations" do
expected = <<-RUBY.gsub(/^ /, "")
#!/usr/bin/env ruby
#{Spring::Client::Binstub::LOADER.strip}
require 'bundler/setup'
load Gem.bin_path('rake', 'rake')
RUBY

# older variation with double quotes
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
#!/usr/bin/env ruby
Expand All @@ -286,36 +293,38 @@ def exec_name
load Gem.bin_path('rake', 'rake')
RUBY

assert_success "bin/spring binstub rake", stdout: "bin/rake: upgraded"
assert_equal expected, app.path("bin/rake").read

# newer variation with single quotes
File.write(app.path("bin/rails"), <<-RUBY.strip_heredoc)
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError
end
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
require 'bundler/setup'
load Gem.bin_path('rake', 'rake')
RUBY

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

expected = <<-RUBY.gsub(/^ /, "")
# newer variation which checks end of exception message
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
#!/usr/bin/env ruby
#{Spring::Client::Binstub::LOADER.strip}
begin
spring_bin_path = File.expand_path('../spring', __FILE__)
load spring_bin_path
rescue LoadError => e
raise unless e.message.end_with? spring_bin_path, 'spring/binstub'
end
require 'bundler/setup'
load Gem.bin_path('rake', 'rake')
RUBY
assert_equal expected, app.path("bin/rake").read

expected = <<-RUBY.gsub(/^ /, "")
#!/usr/bin/env ruby
#{Spring::Client::Binstub::LOADER.strip}
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
RUBY
assert_equal expected, app.path("bin/rails").read
assert_success "bin/spring binstub rake", stdout: "bin/rake: upgraded"
assert_equal expected, app.path("bin/rake").read
end

test "binstub remove with new binstub variations" do
Expand Down

0 comments on commit 1fea946

Please sign in to comment.