Skip to content

Commit b583da7

Browse files
Update/remove all existing binstub variations
There are three variations of the new style spring binstub: * older with double quotes * newer with single quotes * newest which does not swallow unrelated load errors This does not handle removing the old style if/else binstub. To remove is a two-step upgrade and remove is required.
1 parent 1c130b6 commit b583da7

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

lib/spring/client/binstub.rb

+12-2
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

lib/spring/test/acceptance_test.rb

+88-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def exec_name
229229
end
230230
end
231231

232-
test "binstub upgrade" do
232+
test "binstub upgrade with old binstub" do
233233
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
234234
#!/usr/bin/env ruby
235235
@@ -274,6 +274,93 @@ def exec_name
274274
assert_equal expected, app.path("bin/rails").read
275275
end
276276

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+
277364
test "after fork callback" do
278365
File.write(app.spring_config, "Spring.after_fork { puts '!callback!' }")
279366
assert_success "bin/rails runner 'puts 2'", stdout: "!callback!\n2"

0 commit comments

Comments
 (0)