spring 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,164 +1,42 @@
1
1
  # encoding: utf-8
2
- require 'helper'
3
- require 'io/wait'
2
+ require "helper"
3
+ require "acceptance/helper"
4
+ require "io/wait"
4
5
  require "timeout"
5
6
  require "spring/sid"
6
- require "spring/env"
7
+ require "spring/client"
7
8
 
8
9
  class AppTest < ActiveSupport::TestCase
9
10
  DEFAULT_SPEEDUP = 0.8
10
- DEFAULT_TIMEOUT = ENV['CI'] ? 30 : 10
11
11
 
12
12
  def rails_version
13
13
  ENV['RAILS_VERSION'] || '~> 4.0.0'
14
14
  end
15
15
 
16
- def rails_3?
17
- rails_version.split(" ").last =~ /^3/
16
+ def generator
17
+ @@generator ||= Spring::Test::ApplicationGenerator.new(rails_version)
18
18
  end
19
19
 
20
- def app_root
21
- Pathname.new("#{TEST_ROOT}/apps/rails-#{rails_version.scan(/\d/)[0..1].join("-")}")
22
- end
23
-
24
- def gem_home
25
- app_root.join "vendor/gems/#{RUBY_VERSION}"
26
- end
27
-
28
- def user_home
29
- app_root.join "user_home"
30
- end
31
-
32
- def spring
33
- gem_home.join "bin/spring"
34
- end
35
-
36
- def spring_env
37
- @spring_env ||= Spring::Env.new(app_root)
38
- end
39
-
40
- def stdout
41
- @stdout ||= IO.pipe
42
- end
43
-
44
- def stderr
45
- @stderr ||= IO.pipe
46
- end
47
-
48
- def log_file
49
- @log_file ||= File.open("/tmp/spring.log", "w+")
50
- end
51
-
52
- def env
53
- @env ||= {
54
- "GEM_HOME" => gem_home.to_s,
55
- "GEM_PATH" => "",
56
- "HOME" => user_home.to_s,
57
- "RAILS_ENV" => nil,
58
- "RACK_ENV" => nil,
59
- "SPRING_LOG" => log_file.path
60
- }
61
- end
62
-
63
- def app_run(command, opts = {})
64
- start_time = Time.now
65
-
66
- Bundler.with_clean_env do
67
- Process.spawn(
68
- env,
69
- command.to_s,
70
- out: stdout.last,
71
- err: stderr.last,
72
- in: :close,
73
- chdir: app_root.to_s,
74
- )
75
- end
76
-
77
- _, status = Timeout.timeout(opts.fetch(:timeout, DEFAULT_TIMEOUT)) { Process.wait2 }
78
-
79
- if pid = spring_env.pid
80
- @server_pid = pid
81
- lines = `ps -A -o ppid= -o pid= | egrep '^\\s*#{@server_pid}'`.lines
82
- @application_pids = lines.map { |l| l.split.last.to_i }
83
- end
84
-
85
- output = read_streams
86
- puts dump_streams(command, output) if ENV["SPRING_DEBUG"]
87
-
88
- @times << (Time.now - start_time) if @times
89
-
90
- output.merge(status: status, command: command)
91
- rescue Timeout::Error => e
92
- raise e, "Output:\n\n#{dump_streams(command, read_streams)}"
93
- end
94
-
95
- def read_streams
96
- {
97
- stdout: read_stream(stdout.first),
98
- stderr: read_stream(stderr.first),
99
- log: read_stream(log_file)
100
- }
101
- end
102
-
103
- def read_stream(stream)
104
- output = ""
105
- while IO.select([stream], [], [], 0.5) && !stream.eof?
106
- output << stream.readpartial(10240)
107
- end
108
- output
109
- end
110
-
111
- def dump_streams(command, streams)
112
- output = "$ #{command}\n"
113
-
114
- streams.each do |name, stream|
115
- unless stream.chomp.empty?
116
- output << "--- #{name} ---\n"
117
- output << "#{stream.chomp}\n"
118
- end
119
- end
120
-
121
- output << "\n"
122
- output
123
- end
124
-
125
- def alive?(pid)
126
- Process.kill 0, pid
127
- true
128
- rescue Errno::ESRCH
129
- false
130
- end
131
-
132
- def await_reload
133
- raise "no pid" if @application_pids.nil? || @application_pids.empty?
134
-
135
- Timeout.timeout(DEFAULT_TIMEOUT) do
136
- sleep 0.1 while @application_pids.any? { |p| alive?(p) }
137
- end
138
- end
139
-
140
- def debug(artifacts)
141
- artifacts = artifacts.dup
142
- artifacts.delete :status
143
- dump_streams(artifacts.delete(:command), artifacts)
20
+ def app
21
+ @app ||= Spring::Test::Application.new("#{TEST_ROOT}/apps/tmp")
144
22
  end
145
23
 
146
24
  def assert_output(artifacts, expected)
147
25
  expected.each do |stream, output|
148
26
  assert artifacts[stream].include?(output),
149
- "expected #{stream} to include '#{output}'.\n\n#{debug(artifacts)}"
27
+ "expected #{stream} to include '#{output}'.\n\n#{app.debug(artifacts)}"
150
28
  end
151
29
  end
152
30
 
153
31
  def assert_success(command, expected_output = nil)
154
- artifacts = app_run(*Array(command))
155
- assert artifacts[:status].success?, "expected successful exit status\n\n#{debug(artifacts)}"
32
+ artifacts = app.run(*Array(command))
33
+ assert artifacts[:status].success?, "expected successful exit status\n\n#{app.debug(artifacts)}"
156
34
  assert_output artifacts, expected_output if expected_output
157
35
  end
158
36
 
159
37
  def assert_failure(command, expected_output = nil)
160
- artifacts = app_run(*Array(command))
161
- assert !artifacts[:status].success?, "expected unsuccessful exit status\n\n#{debug(artifacts)}"
38
+ artifacts = app.run(*Array(command))
39
+ assert !artifacts[:status].success?, "expected unsuccessful exit status\n\n#{app.debug(artifacts)}"
162
40
  assert_output artifacts, expected_output if expected_output
163
41
  end
164
42
 
@@ -166,286 +44,236 @@ class AppTest < ActiveSupport::TestCase
166
44
  if ENV['CI']
167
45
  yield
168
46
  else
169
- @times = []
170
- yield
171
- assert (@times.last / @times.first) < ratio, "#{@times.last} was not less than #{ratio} of #{@times.first}"
172
- @times = nil
173
- end
174
- end
175
-
176
- def spring_test_command
177
- "#{spring} #{rails_3? ? 'testunit' : 'rake test'} #{@test}"
178
- end
179
-
180
- def generate_app
181
- Bundler.with_clean_env do
182
- # Sporadic SSL errors keep causing test failures so there are anti-SSL workarounds here
183
-
184
- assert system("(gem list rails --installed --version '#{rails_version}' || " \
185
- "gem install rails --clear-sources --source http://rubygems.org --version '#{rails_version}') > /dev/null")
186
-
187
- # Have to shell out otherwise bundler prevents us finding the gem
188
- version = `ruby -e 'puts Gem::Specification.find_by_name("rails", "#{rails_version}").version'`.chomp
189
-
190
- assert system("rails _#{version}_ new #{app_root} --skip-bundle --skip-javascript --skip-sprockets > /dev/null")
191
-
192
- FileUtils.mkdir_p(gem_home)
193
- FileUtils.mkdir_p(user_home)
194
- FileUtils.rm_rf("#{app_root}/test/performance/")
195
-
196
- if rails_3?
197
- File.write(
198
- "#{app_root}/Gemfile",
199
- File.read("#{app_root}/Gemfile") + "gem 'spring-commands-testunit'\n"
200
- )
47
+ app.with_timing do
48
+ yield
49
+ assert app.timing_ratio < ratio, "#{app.last_time} was not less than #{ratio} of #{app.first_time}"
201
50
  end
202
-
203
- File.write(
204
- "#{app_root}/Gemfile",
205
- File.read("#{app_root}/Gemfile").sub("https://rubygems.org", "http://rubygems.org")
206
- )
207
51
  end
208
52
  end
209
53
 
210
- def install
211
- generate_app unless app_root.exist?
212
-
213
- assert system("gem build spring.gemspec 2>/dev/null 1>/dev/null")
214
-
215
- assert_success ["gem install ../../../spring-#{Spring::VERSION}.gem", timeout: nil]
216
- assert_success ["(gem list bundler | grep bundler) || gem install bundler", timeout: nil]
217
- assert_success ["bundle check || bundle update", timeout: nil]
54
+ def assert_app_reloaded
55
+ assert_success app.spring_test_command
218
56
 
219
- unless File.exist?(@controller)
220
- assert_success "bundle exec rails g scaffold post title:string"
221
- end
57
+ File.write(app.application_config, app.application_config.read + <<-CODE)
58
+ class Foo
59
+ def self.omg
60
+ raise "omg"
61
+ end
62
+ end
63
+ CODE
64
+ File.write(app.test, app.test.read.sub("get :index", "Foo.omg"))
222
65
 
223
- assert_success "bundle exec rake db:migrate db:test:clone"
224
- @@installed = true
66
+ app.await_reload
67
+ assert_failure app.spring_test_command, stdout: "RuntimeError: omg"
225
68
  end
226
69
 
227
- @@installed = false
228
-
229
70
  setup do
230
- @test = "#{app_root}/test/#{rails_3? ? 'functional' : 'controllers'}/posts_controller_test.rb"
231
- @controller = "#{app_root}/app/controllers/posts_controller.rb"
232
-
233
- install unless @@installed
234
-
235
- @test_contents = File.read(@test)
236
- @controller_contents = File.read(@controller)
71
+ generator.generate_if_missing
72
+ generator.install_spring
73
+ generator.copy_to(app.root)
237
74
  end
238
75
 
239
76
  teardown do
240
- app_run "#{spring} stop"
241
- File.write(@test, @test_contents)
242
- File.write(@controller, @controller_contents)
243
- FileUtils.rm_f("#{app_root}/config/spring.rb")
77
+ app.stop_spring
244
78
  end
245
79
 
246
80
  test "basic" do
247
81
  assert_speedup do
248
- 2.times { app_run spring_test_command }
82
+ 2.times { app.run app.spring_test_command }
249
83
  end
250
84
  end
251
85
 
252
86
  test "help message when called without arguments" do
253
- assert_success spring, stdout: 'Usage: spring COMMAND [ARGS]'
87
+ assert_success "bin/spring", stdout: 'Usage: spring COMMAND [ARGS]'
254
88
  end
255
89
 
256
90
  test "test changes are picked up" do
257
91
  assert_speedup do
258
- assert_success spring_test_command, stdout: "0 failures"
92
+ assert_success app.spring_test_command, stdout: "0 failures"
259
93
 
260
- File.write(@test, @test_contents.sub("get :index", "raise 'omg'"))
261
- assert_failure spring_test_command, stdout: "RuntimeError: omg"
94
+ File.write(app.test, app.test.read.sub("get :index", "raise 'omg'"))
95
+ assert_failure app.spring_test_command, stdout: "RuntimeError: omg"
262
96
  end
263
97
  end
264
98
 
265
99
  test "code changes are picked up" do
266
100
  assert_speedup do
267
- assert_success spring_test_command, stdout: "0 failures"
101
+ assert_success app.spring_test_command, stdout: "0 failures"
268
102
 
269
- File.write(@controller, @controller_contents.sub("@posts = Post.all", "raise 'omg'"))
270
- assert_failure spring_test_command, stdout: "RuntimeError: omg"
103
+ File.write(app.controller, app.controller.read.sub("@posts = Post.all", "raise 'omg'"))
104
+ assert_failure app.spring_test_command, stdout: "RuntimeError: omg"
271
105
  end
272
106
  end
273
107
 
274
108
  test "code changes in pre-referenced app files are picked up" do
275
- begin
276
- initializer = "#{app_root}/config/initializers/load_posts_controller.rb"
277
- File.write(initializer, "PostsController\n")
109
+ File.write(app.path("config/initializers/load_posts_controller.rb"), "PostsController\n")
278
110
 
279
- assert_speedup do
280
- assert_success spring_test_command, stdout: "0 failures"
111
+ assert_speedup do
112
+ assert_success app.spring_test_command, stdout: "0 failures"
281
113
 
282
- File.write(@controller, @controller_contents.sub("@posts = Post.all", "raise 'omg'"))
283
- assert_failure spring_test_command, stdout: "RuntimeError: omg"
284
- end
285
- ensure
286
- FileUtils.rm_f(initializer)
114
+ File.write(app.controller, app.controller.read.sub("@posts = Post.all", "raise 'omg'"))
115
+ assert_failure app.spring_test_command, stdout: "RuntimeError: omg"
287
116
  end
288
117
  end
289
118
 
290
- def assert_app_reloaded
291
- application = "#{app_root}/config/application.rb"
292
- application_contents = File.read(application)
293
-
294
- assert_success spring_test_command
295
-
296
- File.write(application, application_contents + <<-CODE)
297
- class Foo
298
- def self.omg
299
- raise "omg"
300
- end
301
- end
302
- CODE
303
- File.write(@test, @test_contents.sub("get :index", "Foo.omg"))
304
-
305
- await_reload
306
- assert_failure spring_test_command, stdout: "RuntimeError: omg"
307
- ensure
308
- File.write(application, application_contents)
309
- end
310
-
311
119
  test "app gets reloaded when preloaded files change (polling watcher)" do
312
- env["RAILS_ENV"] = "test"
313
- assert_success "#{spring} rails runner 'puts Spring.watcher.class'", stdout: "Polling"
120
+ app.env["RAILS_ENV"] = "test"
121
+ assert_success "bin/rails runner 'puts Spring.watcher.class'", stdout: "Polling"
314
122
  assert_app_reloaded
315
123
  end
316
124
 
317
125
  test "app gets reloaded when preloaded files change (listen watcher)" do
318
- begin
319
- gemfile = app_root.join("Gemfile")
320
- gemfile_contents = gemfile.read
321
- File.write(gemfile, gemfile_contents + "\ngem 'listen', '~> 1.0'")
322
-
323
- File.write("#{app_root}/config/spring.rb", "Spring.watch_method = :listen")
324
-
325
- assert_success ["bundle install", timeout: nil]
326
-
327
- env["RAILS_ENV"] = "test"
328
- assert_success "#{spring} rails runner 'puts Spring.watcher.class'", stdout: "Listen"
126
+ File.write(app.gemfile, "#{app.gemfile.read}gem 'listen', '~> 1.0'")
127
+ File.write(app.spring_config, "Spring.watch_method = :listen")
128
+ app.bundle
329
129
 
330
- assert_app_reloaded
331
- ensure
332
- File.write(gemfile, gemfile_contents)
333
- assert_success "bundle check"
334
- end
130
+ app.env["RAILS_ENV"] = "test"
131
+ assert_success "bin/rails runner 'puts Spring.watcher.class'", stdout: "Listen"
132
+ assert_app_reloaded
335
133
  end
336
134
 
337
135
  test "app recovers when a boot-level error is introduced" do
338
- begin
339
- application = "#{app_root}/config/application.rb"
340
- application_contents = File.read(application)
136
+ config = app.application_config.read
341
137
 
342
- assert_success spring_test_command
138
+ assert_success app.spring_test_command
343
139
 
344
- File.write(application, application_contents + "\nomg")
345
- await_reload
140
+ File.write(app.application_config, "#{config}\nomg")
141
+ app.await_reload
346
142
 
347
- assert_failure spring_test_command
143
+ assert_failure app.spring_test_command
348
144
 
349
- File.write(application, application_contents)
350
- assert_success spring_test_command
351
- ensure
352
- File.write(application, application_contents)
353
- end
145
+ File.write(app.application_config, config)
146
+ assert_success app.spring_test_command
354
147
  end
355
148
 
356
149
  test "stop command kills server" do
357
- app_run spring_test_command
358
- assert spring_env.server_running?, "The server should be running but it isn't"
150
+ app.run app.spring_test_command
151
+ assert app.spring_env.server_running?, "The server should be running but it isn't"
359
152
 
360
- assert_success "#{spring} stop"
361
- assert !spring_env.server_running?, "The server should not be running but it is"
153
+ assert_success "bin/spring stop"
154
+ assert !app.spring_env.server_running?, "The server should not be running but it is"
362
155
  end
363
156
 
364
157
  test "custom commands" do
365
- File.write("#{app_root}/config/spring.rb", <<-CODE)
158
+ File.write(app.spring_config, <<-CODE)
366
159
  class CustomCommand
367
160
  def call
368
161
  puts "omg"
369
162
  end
163
+
164
+ def exec_name
165
+ "rake"
166
+ end
370
167
  end
371
168
 
372
169
  Spring.register_command "custom", CustomCommand.new
373
170
  CODE
374
171
 
375
- assert_success "#{spring} custom", stdout: "omg"
172
+ assert_success "bin/spring custom", stdout: "omg"
173
+
174
+ assert_success "bin/spring binstub custom"
175
+ assert_success "bin/custom", stdout: "omg"
176
+
177
+ app.env["DISABLE_SPRING"] = "1"
178
+ assert_success %{bin/custom -e 'puts "foo"'}, stdout: "foo"
376
179
  end
377
180
 
378
- test "binstubs" do
379
- begin
380
- FileUtils.mv "#{app_root}/bin", "#{app_root}/bin~" if File.exist?("#{app_root}/bin")
181
+ test "binstub" do
182
+ assert_success "bin/rails server --help", stdout: "Usage: rails server" # rails command fallback
381
183
 
382
- app_run "#{spring} binstub rake"
383
- assert_success "bin/rake -T", stdout: "rake db:migrate"
184
+ assert_success "#{app.spring} binstub rake", stdout: "bin/rake: spring already present"
384
185
 
385
- app_run "#{spring} binstub rake rails"
386
- assert_success "bin/rails runner 'puts %(omg)'", stdout: "omg"
387
- assert_success "bin/rails server --help", stdout: "Usage: rails server"
186
+ assert_success "#{app.spring} binstub --remove rake", stdout: "bin/rake: spring removed"
187
+ assert !app.path("bin/rake").read.include?(Spring::Client::Binstub::LOADER)
188
+ assert_success "bin/rake -T", stdout: "rake db:migrate"
189
+ end
388
190
 
389
- FileUtils.rm ["#{app_root}/bin/rails", "#{app_root}/bin/rake"]
191
+ test "binstub when spring is uninstalled" do
192
+ app.run! "gem uninstall --ignore-dependencies spring"
193
+ File.write(app.gemfile, app.gemfile.read.gsub(/gem 'spring.*/, ""))
194
+ assert_success "bin/rake -T", stdout: "rake db:migrate"
195
+ end
390
196
 
391
- app_run "#{spring} binstub --all"
392
- assert_success "bin/rake -T", stdout: "rake db:migrate"
393
- assert_success "bin/rails runner 'puts %(omg)'", stdout: "omg"
394
- ensure
395
- if File.exist?("#{app_root}/bin~")
396
- FileUtils.rm_rf "#{app_root}/bin"
397
- FileUtils.mv "#{app_root}/bin~", "#{app_root}/bin"
398
- end
399
- end
197
+ test "binstub upgrade" do
198
+ File.write(app.path("bin/rake"), <<CODE)
199
+ #!/usr/bin/env ruby
200
+
201
+ if !Process.respond_to?(:fork) || Gem::Specification.find_all_by_name("spring").empty?
202
+ exec "bundle", "exec", "rake", *ARGV
203
+ else
204
+ ARGV.unshift "rake"
205
+ load Gem.bin_path("spring", "spring")
206
+ end
207
+ CODE
208
+
209
+ File.write(app.path("bin/rails"), <<CODE)
210
+ #!/usr/bin/env ruby
211
+
212
+ if !Process.respond_to?(:fork) || Gem::Specification.find_all_by_name("spring").empty?
213
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
214
+ require_relative '../config/boot'
215
+ require 'rails/commands'
216
+ else
217
+ ARGV.unshift "rails"
218
+ load Gem.bin_path("spring", "spring")
219
+ end
220
+ CODE
221
+
222
+ assert_success "bin/spring binstub --all", stdout: "upgraded"
223
+
224
+ assert_equal app.path("bin/rake").read, <<CODE
225
+ #!/usr/bin/env ruby
226
+ #{Spring::Client::Binstub::LOADER.strip}
227
+ require 'bundler/setup'
228
+ load Gem.bin_path('rake', 'rake')
229
+ CODE
230
+
231
+ assert_equal app.path("bin/rails").read, <<CODE
232
+ #!/usr/bin/env ruby
233
+ #{Spring::Client::Binstub::LOADER.strip}
234
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
235
+ require_relative '../config/boot'
236
+ require 'rails/commands'
237
+ CODE
400
238
  end
401
239
 
402
240
  test "after fork callback" do
403
- File.write("#{app_root}/config/spring.rb", "Spring.after_fork { puts '!callback!' }")
404
- assert_success "#{spring} rails runner 'puts 2'", stdout: "!callback!\n2"
241
+ File.write(app.spring_config, "Spring.after_fork { puts '!callback!' }")
242
+ assert_success "bin/rails runner 'puts 2'", stdout: "!callback!\n2"
405
243
  end
406
244
 
407
245
  test "global config file evaluated" do
408
- begin
409
- File.write("#{user_home}/.spring.rb", "Spring.after_fork { puts '!callback!' }")
410
- assert_success "#{spring} rails runner 'puts 2'", stdout: "!callback!\n2"
411
- ensure
412
- FileUtils.rm_r("#{user_home}/.spring.rb")
413
- end
246
+ File.write("#{app.user_home}/.spring.rb", "Spring.after_fork { puts '!callback!' }")
247
+ assert_success "bin/rails runner 'puts 2'", stdout: "!callback!\n2"
414
248
  end
415
249
 
416
250
  test "missing config/application.rb" do
417
- begin
418
- FileUtils.mv app_root.join("config/application.rb"), app_root.join("config/application.rb.bak")
419
-
420
- assert_failure "#{spring} rake -T", stderr: "unable to find your config/application.rb"
421
- ensure
422
- FileUtils.mv app_root.join("config/application.rb.bak"), app_root.join("config/application.rb")
423
- end
251
+ app.application_config.delete
252
+ assert_failure "bin/rake -T", stderr: "unable to find your config/application.rb"
424
253
  end
425
254
 
426
255
  test "piping" do
427
- assert_success "#{spring} rake -T | grep db", stdout: "rake db:migrate"
256
+ assert_success "bin/rake -T | grep db", stdout: "rake db:migrate"
428
257
  end
429
258
 
430
259
  test "status" do
431
- assert_success "#{spring} status", stdout: "Spring is not running"
432
- app_run "#{spring} rails runner ''"
433
- assert_success "#{spring} status", stdout: "Spring is running"
260
+ assert_success "bin/spring status", stdout: "Spring is not running"
261
+ assert_success "bin/rails runner ''"
262
+ assert_success "bin/spring status", stdout: "Spring is running"
434
263
  end
435
264
 
436
265
  test "runner command sets Rails environment from command-line options" do
437
- assert_success "#{spring} rails runner -e production 'puts Rails.env'", stdout: "production"
438
- assert_success "#{spring} rails runner --environment=production 'puts Rails.env'", stdout: "production"
266
+ assert_success "bin/rails runner -e production 'puts Rails.env'", stdout: "production"
267
+ assert_success "bin/rails runner --environment=production 'puts Rails.env'", stdout: "production"
439
268
  end
440
269
 
441
270
  test "forcing rails env via environment variable" do
442
- env['RAILS_ENV'] = 'production'
443
- assert_success "#{spring} rake -p 'Rails.env'", stdout: "production"
271
+ app.env['RAILS_ENV'] = 'production'
272
+ assert_success "bin/rake -p 'Rails.env'", stdout: "production"
444
273
  end
445
274
 
446
275
  test "setting env vars with rake" do
447
- begin
448
- File.write("#{app_root}/lib/tasks/env.rake", <<-'CODE')
276
+ File.write(app.path("lib/tasks/env.rake"), <<-'CODE')
449
277
  task :print_rails_env => :environment do
450
278
  puts Rails.env
451
279
  end
@@ -455,57 +283,52 @@ class AppTest < ActiveSupport::TestCase
455
283
  end
456
284
 
457
285
  task(:default).clear.enhance [:print_rails_env]
458
- CODE
286
+ CODE
459
287
 
460
- assert_success "#{spring} rake RAILS_ENV=test print_rails_env", stdout: "test"
461
- assert_success "#{spring} rake FOO=bar print_env", stdout: "FOO=bar"
462
- assert_success "#{spring} rake", stdout: "test"
463
- ensure
464
- FileUtils.rm_f("#{app_root}/lib/tasks/env.rake")
465
- end
288
+ assert_success "bin/rake RAILS_ENV=test print_rails_env", stdout: "test"
289
+ assert_success "bin/rake FOO=bar print_env", stdout: "FOO=bar"
290
+ assert_success "bin/rake", stdout: "test"
466
291
  end
467
292
 
468
- test "changing the Gemfile restarts the server" do
469
- begin
470
- gemfile = app_root.join("Gemfile")
471
- gemfile_contents = gemfile.read
293
+ test "changing the Gemfile works" do
294
+ assert_success %(bin/rails runner 'require "sqlite3"')
472
295
 
473
- assert_success %(#{spring} rails runner 'require "sqlite3"')
296
+ File.write(app.gemfile, app.gemfile.read.sub(%{gem 'sqlite3'}, %{# gem 'sqlite3'}))
297
+ app.await_reload
474
298
 
475
- File.write(gemfile, gemfile_contents.sub(%{gem 'sqlite3'}, %{# gem 'sqlite3'}))
476
- app_run "bundle check"
299
+ assert_failure %(bin/rails runner 'require "sqlite3"'), stderr: "sqlite3"
300
+ end
477
301
 
478
- await_reload
479
- assert_failure %(#{spring} rails runner 'require "sqlite3"'), stderr: "sqlite3"
480
- ensure
481
- File.write(gemfile, gemfile_contents)
482
- assert_success "bundle check"
483
- end
302
+ test "changing the Gemfile works when spring calls into itself" do
303
+ File.write(app.path("script.rb"), <<-CODE)
304
+ gemfile = Rails.root.join("Gemfile")
305
+ File.write(gemfile, "\#{gemfile.read}gem 'devise'\\n")
306
+ Bundler.with_clean_env do
307
+ system(#{app.env.inspect}, "bundle install")
308
+ end
309
+ output = `\#{Rails.root.join('bin/rails')} runner 'require "devise"; puts "done";'`
310
+ exit output == "done\n"
311
+ CODE
312
+
313
+ assert_success [%(bin/rails runner 'load Rails.root.join("script.rb")'), timeout: 60]
484
314
  end
485
315
 
486
316
  test "changing the environment between runs" do
487
- begin
488
- application = "#{app_root}/config/application.rb"
489
- application_contents = File.read(application)
490
-
491
- File.write(application, "#{application_contents}\nENV['BAR'] = 'bar'")
317
+ File.write(app.application_config, "#{app.application_config.read}\nENV['BAR'] = 'bar'")
492
318
 
493
- env["OMG"] = "1"
494
- env["FOO"] = "1"
495
- env["RUBYOPT"] = "-rubygems"
319
+ app.env["OMG"] = "1"
320
+ app.env["FOO"] = "1"
321
+ app.env["RUBYOPT"] = "-rubygems"
496
322
 
497
- assert_success %(#{spring} rails runner 'p ENV["OMG"]'), stdout: "1"
498
- assert_success %(#{spring} rails runner 'p ENV["BAR"]'), stdout: "bar"
499
- assert_success %(#{spring} rails runner 'p ENV.key?("BUNDLE_GEMFILE")'), stdout: "true"
500
- assert_success %(#{spring} rails runner 'p ENV["RUBYOPT"]'), stdout: "bundler"
323
+ assert_success %(bin/rails runner 'p ENV["OMG"]'), stdout: "1"
324
+ assert_success %(bin/rails runner 'p ENV["BAR"]'), stdout: "bar"
325
+ assert_success %(bin/rails runner 'p ENV.key?("BUNDLE_GEMFILE")'), stdout: "true"
326
+ assert_success %(bin/rails runner 'p ENV["RUBYOPT"]'), stdout: "bundler"
501
327
 
502
- env["OMG"] = "2"
503
- env.delete "FOO"
328
+ app.env["OMG"] = "2"
329
+ app.env.delete "FOO"
504
330
 
505
- assert_success %(#{spring} rails runner 'p ENV["OMG"]'), stdout: "2"
506
- assert_success %(#{spring} rails runner 'p ENV.key?("FOO")'), stdout: "false"
507
- ensure
508
- File.write(application, application_contents)
509
- end
331
+ assert_success %(bin/rails runner 'p ENV["OMG"]'), stdout: "2"
332
+ assert_success %(bin/rails runner 'p ENV.key?("FOO")'), stdout: "false"
510
333
  end
511
334
  end