Skip to content

Commit 727e4b8

Browse files
committed
Merge pull request #445 from felixbuenemann/fix-upgrade-remove-new-binstub-variations
Fix upgrade/remove with new binstub variations
2 parents 7351747 + b583da7 commit 727e4b8

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed

lib/spring/client/binstub.rb

+13-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ class Binstub < Command
4848

4949
OLD_BINSTUB = %{if !Process.respond_to?(:fork) || Gem::Specification.find_all_by_name("spring").empty?}
5050

51+
BINSTUB_VARIATIONS = Regexp.union [
52+
%{begin\n load File.expand_path("../spring", __FILE__)\nrescue LoadError\nend\n},
53+
%{begin\n load File.expand_path('../spring', __FILE__)\nrescue LoadError\nend\n},
54+
LOADER
55+
]
56+
5157
class Item
5258
attr_reader :command, :existing
5359

@@ -74,8 +80,12 @@ def add
7480
fallback = nil if fallback.include?("exec")
7581
generate(fallback)
7682
status "upgraded"
77-
elsif existing =~ /load .*spring/
83+
elsif existing.include?(LOADER)
7884
status "spring already present"
85+
elsif existing =~ BINSTUB_VARIATIONS
86+
upgraded = existing.sub(BINSTUB_VARIATIONS, LOADER)
87+
File.write(command.binstub, upgraded)
88+
status "upgraded"
7989
else
8090
head, shebang, tail = existing.partition(SHEBANG)
8191

@@ -110,7 +120,7 @@ def generate(fallback = nil)
110120

111121
def remove
112122
if existing
113-
File.write(command.binstub, existing.sub(LOADER, ""))
123+
File.write(command.binstub, existing.sub(BINSTUB_VARIATIONS, ""))
114124
status "spring removed"
115125
end
116126
end
@@ -119,7 +129,7 @@ def remove
119129
attr_reader :bindir, :items
120130

121131
def self.description
122-
"Generate spring based binstubs. Use --all to generate a binstub for all known commands."
132+
"Generate spring based binstubs. Use --all to generate a binstub for all known commands. Use --remove to revert."
123133
end
124134

125135
def self.rails_command

lib/spring/test/acceptance_test.rb

+93-1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ def exec_name
208208
assert_success "bin/rake -T", stdout: "rake db:migrate"
209209
end
210210

211+
test "binstub remove all" do
212+
assert_success "bin/spring binstub --remove --all"
213+
refute File.exist?(app.path("bin/spring"))
214+
end
215+
211216
test "binstub when spring gem is missing" do
212217
without_gem "spring-#{Spring::VERSION}" do
213218
File.write(app.gemfile, app.gemfile.read.gsub(/gem 'spring.*/, ""))
@@ -224,7 +229,7 @@ def exec_name
224229
end
225230
end
226231

227-
test "binstub upgrade" do
232+
test "binstub upgrade with old binstub" do
228233
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
229234
#!/usr/bin/env ruby
230235
@@ -269,6 +274,93 @@ def exec_name
269274
assert_equal expected, app.path("bin/rails").read
270275
end
271276

277+
test "binstub upgrade with new binstub variations" do
278+
# older variation with double quotes
279+
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
280+
#!/usr/bin/env ruby
281+
begin
282+
load File.expand_path("../spring", __FILE__)
283+
rescue LoadError
284+
end
285+
require 'bundler/setup'
286+
load Gem.bin_path('rake', 'rake')
287+
RUBY
288+
289+
# newer variation with single quotes
290+
File.write(app.path("bin/rails"), <<-RUBY.strip_heredoc)
291+
#!/usr/bin/env ruby
292+
begin
293+
load File.expand_path('../spring', __FILE__)
294+
rescue LoadError
295+
end
296+
APP_PATH = File.expand_path('../../config/application', __FILE__)
297+
require_relative '../config/boot'
298+
require 'rails/commands'
299+
RUBY
300+
301+
assert_success "bin/spring binstub --all", stdout: "upgraded"
302+
303+
expected = <<-RUBY.gsub(/^ /, "")
304+
#!/usr/bin/env ruby
305+
#{Spring::Client::Binstub::LOADER.strip}
306+
require 'bundler/setup'
307+
load Gem.bin_path('rake', 'rake')
308+
RUBY
309+
assert_equal expected, app.path("bin/rake").read
310+
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
319+
end
320+
321+
test "binstub remove with new binstub variations" do
322+
# older variation with double quotes
323+
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
324+
#!/usr/bin/env ruby
325+
begin
326+
load File.expand_path("../spring", __FILE__)
327+
rescue LoadError
328+
end
329+
require 'bundler/setup'
330+
load Gem.bin_path('rake', 'rake')
331+
RUBY
332+
333+
# newer variation with single quotes
334+
File.write(app.path("bin/rails"), <<-RUBY.strip_heredoc)
335+
#!/usr/bin/env ruby
336+
begin
337+
load File.expand_path('../spring', __FILE__)
338+
rescue LoadError
339+
end
340+
APP_PATH = File.expand_path('../../config/application', __FILE__)
341+
require_relative '../config/boot'
342+
require 'rails/commands'
343+
RUBY
344+
345+
assert_success "bin/spring binstub --remove rake", stdout: "bin/rake: spring removed"
346+
assert_success "bin/spring binstub --remove rails", stdout: "bin/rails: spring removed"
347+
348+
expected = <<-RUBY.strip_heredoc
349+
#!/usr/bin/env ruby
350+
require 'bundler/setup'
351+
load Gem.bin_path('rake', 'rake')
352+
RUBY
353+
assert_equal expected, app.path("bin/rake").read
354+
355+
expected = <<-RUBY.strip_heredoc
356+
#!/usr/bin/env ruby
357+
APP_PATH = File.expand_path('../../config/application', __FILE__)
358+
require_relative '../config/boot'
359+
require 'rails/commands'
360+
RUBY
361+
assert_equal expected, app.path("bin/rails").read
362+
end
363+
272364
test "after fork callback" do
273365
File.write(app.spring_config, "Spring.after_fork { puts '!callback!' }")
274366
assert_success "bin/rails runner 'puts 2'", stdout: "!callback!\n2"

0 commit comments

Comments
 (0)