Skip to content

Fix upgrade/remove with new binstub variations #445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/spring/client/binstub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class Binstub < Command

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

BINSTUB_VARIATIONS = Regexp.union [
%{begin\n load File.expand_path("../spring", __FILE__)\nrescue LoadError\nend\n},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these 2 lines look pretty similar ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah the quotes ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double vs. single quotes.

%{begin\n load File.expand_path('../spring', __FILE__)\nrescue LoadError\nend\n},
LOADER
]

class Item
attr_reader :command, :existing

Expand All @@ -74,8 +80,12 @@ def add
fallback = nil if fallback.include?("exec")
generate(fallback)
status "upgraded"
elsif existing =~ /load .*spring/
elsif existing.include?(LOADER)
status "spring already present"
elsif existing =~ BINSTUB_VARIATIONS
upgraded = existing.sub(BINSTUB_VARIATIONS, LOADER)
File.write(command.binstub, upgraded)
status "upgraded"
else
head, shebang, tail = existing.partition(SHEBANG)

Expand Down Expand Up @@ -110,7 +120,7 @@ def generate(fallback = nil)

def remove
if existing
File.write(command.binstub, existing.sub(LOADER, ""))
File.write(command.binstub, existing.sub(BINSTUB_VARIATIONS, ""))
status "spring removed"
end
end
Expand All @@ -119,7 +129,7 @@ def remove
attr_reader :bindir, :items

def self.description
"Generate spring based binstubs. Use --all to generate a binstub for all known commands."
"Generate spring based binstubs. Use --all to generate a binstub for all known commands. Use --remove to revert."
end

def self.rails_command
Expand Down
94 changes: 93 additions & 1 deletion lib/spring/test/acceptance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ def exec_name
assert_success "bin/rake -T", stdout: "rake db:migrate"
end

test "binstub remove all" do
assert_success "bin/spring binstub --remove --all"
refute File.exist?(app.path("bin/spring"))
end

test "binstub when spring gem is missing" do
without_gem "spring-#{Spring::VERSION}" do
File.write(app.gemfile, app.gemfile.read.gsub(/gem 'spring.*/, ""))
Expand All @@ -224,7 +229,7 @@ def exec_name
end
end

test "binstub upgrade" do
test "binstub upgrade with old binstub" do
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
#!/usr/bin/env ruby

Expand Down Expand Up @@ -269,6 +274,93 @@ def exec_name
assert_equal expected, app.path("bin/rails").read
end

test "binstub upgrade with new binstub variations" do
# older variation with double quotes
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
#!/usr/bin/env ruby
begin
load File.expand_path("../spring", __FILE__)
rescue LoadError
end
require 'bundler/setup'
load Gem.bin_path('rake', 'rake')
RUBY

# newer variation with single quotes
File.write(app.path("bin/rails"), <<-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'
RUBY

assert_success "bin/spring binstub --all", stdout: "upgraded"

expected = <<-RUBY.gsub(/^ /, "")
#!/usr/bin/env ruby
#{Spring::Client::Binstub::LOADER.strip}
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
end

test "binstub remove with new binstub variations" do
# older variation with double quotes
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
#!/usr/bin/env ruby
begin
load File.expand_path("../spring", __FILE__)
rescue LoadError
end
require 'bundler/setup'
load Gem.bin_path('rake', 'rake')
RUBY

# newer variation with single quotes
File.write(app.path("bin/rails"), <<-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'
RUBY

assert_success "bin/spring binstub --remove rake", stdout: "bin/rake: spring removed"
assert_success "bin/spring binstub --remove rails", stdout: "bin/rails: spring removed"

expected = <<-RUBY.strip_heredoc
#!/usr/bin/env ruby
require 'bundler/setup'
load Gem.bin_path('rake', 'rake')
RUBY
assert_equal expected, app.path("bin/rake").read

expected = <<-RUBY.strip_heredoc
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
RUBY
assert_equal expected, app.path("bin/rails").read
end

test "after fork callback" do
File.write(app.spring_config, "Spring.after_fork { puts '!callback!' }")
assert_success "bin/rails runner 'puts 2'", stdout: "!callback!\n2"
Expand Down