Skip to content

Commit

Permalink
make code nicer to read by using stripper heredoc
Browse files Browse the repository at this point in the history
using RUBY instead of CODE also adds highlighting in some editors
  • Loading branch information
grosser committed Aug 3, 2015
1 parent 7280284 commit e3388c6
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions lib/spring/test/acceptance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "timeout"
require "spring/sid"
require "spring/client"
require "active_support/core_ext/string/strip"

module Spring
module Test
Expand Down Expand Up @@ -111,13 +112,13 @@ def assert_speedup(ratio = DEFAULT_SPEEDUP)
test "app gets reloaded when preloaded files change" do
assert_success app.spring_test_command

File.write(app.application_config, app.application_config.read + <<-CODE)
File.write(app.application_config, app.application_config.read + <<-RUBY.strip_heredoc)
class Foo
def self.omg
raise "omg"
end
end
CODE
RUBY
File.write(app.test, app.test.read.sub("get :index", "Foo.omg"))

app.await_reload
Expand Down Expand Up @@ -160,7 +161,7 @@ def self.omg
# Start spring before setting up the command, to test that it gracefully upgrades itself
assert_success "bin/rails runner ''"

File.write(app.spring_config, <<-CODE)
File.write(app.spring_config, <<-RUBY.strip_heredoc)
class CustomCommand
def call
puts "omg"
Expand All @@ -172,7 +173,7 @@ def exec_name
end
Spring.register_command "custom", CustomCommand.new
CODE
RUBY

assert_success "bin/spring custom", stdout: "omg"

Expand Down Expand Up @@ -200,46 +201,48 @@ def exec_name
end

test "binstub upgrade" do
File.write(app.path("bin/rake"), <<CODE)
#!/usr/bin/env ruby
if !Process.respond_to?(:fork) || Gem::Specification.find_all_by_name("spring").empty?
exec "bundle", "exec", "rake", *ARGV
else
ARGV.unshift "rake"
load Gem.bin_path("spring", "spring")
end
CODE

File.write(app.path("bin/rails"), <<CODE)
#!/usr/bin/env ruby
if !Process.respond_to?(:fork) || Gem::Specification.find_all_by_name("spring").empty?
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
else
ARGV.unshift "rails"
load Gem.bin_path("spring", "spring")
end
CODE
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
#!/usr/bin/env ruby
if !Process.respond_to?(:fork) || Gem::Specification.find_all_by_name("spring").empty?
exec "bundle", "exec", "rake", *ARGV
else
ARGV.unshift "rake"
load Gem.bin_path("spring", "spring")
end
RUBY

File.write(app.path("bin/rails"), <<-RUBY.strip_heredoc)
#!/usr/bin/env ruby
if !Process.respond_to?(:fork) || Gem::Specification.find_all_by_name("spring").empty?
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
else
ARGV.unshift "rails"
load Gem.bin_path("spring", "spring")
end
RUBY

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

assert_equal app.path("bin/rake").read, <<CODE
#!/usr/bin/env ruby
#{Spring::Client::Binstub::LOADER.strip}
require 'bundler/setup'
load Gem.bin_path('rake', 'rake')
CODE
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

assert_equal app.path("bin/rails").read, <<CODE
#!/usr/bin/env ruby
#{Spring::Client::Binstub::LOADER.strip}
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
CODE
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 "after fork callback" do
Expand Down Expand Up @@ -278,17 +281,17 @@ def exec_name
end

test "setting env vars with rake" do
File.write(app.path("lib/tasks/env.rake"), <<-'CODE')
File.write(app.path("lib/tasks/env.rake"), <<-RUBY.strip_heredoc)
task :print_rails_env => :environment do
puts Rails.env
end
task :print_env do
ENV.each { |k, v| puts "#{k}=#{v}" }
ENV.each { |k, v| puts "\#{k}=\#{v}" }
end
task(:default).clear.enhance [:print_rails_env]
CODE
RUBY

assert_success "bin/rake RAILS_ENV=test print_rails_env", stdout: "test"
assert_success "bin/rake FOO=bar print_env", stdout: "FOO=bar"
Expand All @@ -305,15 +308,15 @@ def exec_name
end

test "changing the Gemfile works when spring calls into itself" do
File.write(app.path("script.rb"), <<-CODE)
File.write(app.path("script.rb"), <<-RUBY.strip_heredoc)
gemfile = Rails.root.join("Gemfile")
File.write(gemfile, "\#{gemfile.read}gem 'devise'\\n")
Bundler.with_clean_env do
system(#{app.env.inspect}, "bundle install")
end
output = `\#{Rails.root.join('bin/rails')} runner 'require "devise"; puts "done";'`
exit output == "done\n"
CODE
RUBY

assert_success [%(bin/rails runner 'load Rails.root.join("script.rb")'), timeout: 60]
end
Expand Down

0 comments on commit e3388c6

Please sign in to comment.