spring 1.0.0 → 1.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,6 +20,8 @@ module Spring
20
20
  @spring_commands = spring_commands || Spring::Client::COMMANDS.dup
21
21
  @application_commands = application_commands || Spring.commands.dup
22
22
 
23
+ @spring_commands.delete_if { |k, v| k.start_with?("-") }
24
+
23
25
  @application_commands["rails"] = @spring_commands.delete("rails")
24
26
  end
25
27
 
@@ -46,19 +48,9 @@ module Spring
46
48
  spring_commands.merge application_commands
47
49
  end
48
50
 
49
- def description_for_command(command)
50
- if command.respond_to?(:description)
51
- command.description
52
- elsif command.respond_to?(:exec_name)
53
- "Runs the #{command.exec_name} command"
54
- else
55
- "No description given."
56
- end
57
- end
58
-
59
51
  def display(name, command)
60
- if description = description_for_command(command)
61
- " #{name.ljust(max_name_width)} #{description}"
52
+ if command.description
53
+ " #{name.ljust(max_name_width)} #{command.description}"
62
54
  end
63
55
  end
64
56
 
@@ -24,9 +24,7 @@ module Spring
24
24
  else
25
25
  require "spring/configuration"
26
26
  ARGV.shift
27
- Object.const_set(:APP_PATH, Spring.application_root_path.join("config/application").to_s)
28
- require Spring.application_root_path.join("config/boot")
29
- require "rails/commands"
27
+ load Dir.glob(Spring.application_root_path.join("{bin,script}/rails")).first
30
28
  end
31
29
  end
32
30
  end
@@ -0,0 +1,11 @@
1
+ require "spring/version"
2
+
3
+ module Spring
4
+ module Client
5
+ class Version < Command
6
+ def call
7
+ puts "Spring version #{Spring::VERSION}"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,83 @@
1
+ require "spring/configuration"
2
+
3
+ module Spring
4
+ class CommandWrapper
5
+ attr_reader :name, :command
6
+
7
+ def initialize(name, command = nil)
8
+ @name = name
9
+ @command = command
10
+ @setup = false
11
+ end
12
+
13
+ def description
14
+ if command.respond_to?(:description)
15
+ command.description
16
+ else
17
+ "Runs the #{name} command"
18
+ end
19
+ end
20
+
21
+ def setup?
22
+ @setup
23
+ end
24
+
25
+ def setup
26
+ if !setup? && command.respond_to?(:setup)
27
+ command.setup
28
+ @setup = true
29
+ return true
30
+ else
31
+ @setup = true
32
+ return false
33
+ end
34
+ end
35
+
36
+ def call
37
+ if command.respond_to?(:call)
38
+ command.call
39
+ else
40
+ $0 = exec
41
+ load exec
42
+ end
43
+ end
44
+
45
+ def gem_name
46
+ if command.respond_to?(:gem_name)
47
+ command.gem_name
48
+ else
49
+ exec_name
50
+ end
51
+ end
52
+
53
+ def exec_name
54
+ if command.respond_to?(:exec_name)
55
+ command.exec_name
56
+ else
57
+ name
58
+ end
59
+ end
60
+
61
+ def binstub
62
+ Spring.application_root_path.join(binstub_name)
63
+ end
64
+
65
+ def binstub_name
66
+ "bin/#{name}"
67
+ end
68
+
69
+ def exec
70
+ if binstub.exist?
71
+ binstub.to_s
72
+ else
73
+ Gem.bin_path(gem_name, exec_name)
74
+ end
75
+ end
76
+
77
+ def env(args)
78
+ if command.respond_to?(:env)
79
+ command.env(args)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -1,4 +1,5 @@
1
1
  require "spring/watcher"
2
+ require "spring/command_wrapper"
2
3
 
3
4
  module Spring
4
5
  @commands = {}
@@ -7,8 +8,8 @@ module Spring
7
8
  attr_reader :commands
8
9
  end
9
10
 
10
- def self.register_command(name, klass)
11
- commands[name] = klass
11
+ def self.register_command(name, command = nil)
12
+ commands[name] = CommandWrapper.new(name, command)
12
13
  end
13
14
 
14
15
  def self.command?(name)
@@ -3,8 +3,7 @@ module Spring
3
3
  class Rails
4
4
  def call
5
5
  ARGV.unshift command_name
6
- Object.const_set(:APP_PATH, ::Rails.root.join('config/application'))
7
- require "rails/commands"
6
+ load Dir.glob(::Rails.root.join("{bin,script}/rails")).first
8
7
  end
9
8
 
10
9
  def description
@@ -23,10 +23,6 @@ module Spring
23
23
 
24
24
  nil
25
25
  end
26
-
27
- def exec_name
28
- "rake"
29
- end
30
26
  end
31
27
 
32
28
  Spring.register_command "rake", Rake.new
@@ -82,13 +82,7 @@ module Spring
82
82
  end
83
83
 
84
84
  def rails_env_for(args, default_rails_env)
85
- command = Spring.command(args.first)
86
-
87
- if command.respond_to?(:env)
88
- env = command.env(args.drop(1))
89
- end
90
-
91
- env || default_rails_env
85
+ Spring.command(args.first).env(args.drop(1)) || default_rails_env
92
86
  end
93
87
 
94
88
  # Boot the server into the process group of the current session.
@@ -1,3 +1,3 @@
1
1
  module Spring
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0.beta1"
3
3
  end
@@ -1,146 +1,30 @@
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
20
+ def app
21
+ @app ||= Spring::Test::Application.new("#{TEST_ROOT}/apps/tmp")
138
22
  end
139
23
 
140
24
  def debug(artifacts)
141
25
  artifacts = artifacts.dup
142
26
  artifacts.delete :status
143
- dump_streams(artifacts.delete(:command), artifacts)
27
+ app.dump_streams(artifacts.delete(:command), artifacts)
144
28
  end
145
29
 
146
30
  def assert_output(artifacts, expected)
@@ -151,13 +35,13 @@ class AppTest < ActiveSupport::TestCase
151
35
  end
152
36
 
153
37
  def assert_success(command, expected_output = nil)
154
- artifacts = app_run(*Array(command))
38
+ artifacts = app.run(*Array(command))
155
39
  assert artifacts[:status].success?, "expected successful exit status\n\n#{debug(artifacts)}"
156
40
  assert_output artifacts, expected_output if expected_output
157
41
  end
158
42
 
159
43
  def assert_failure(command, expected_output = nil)
160
- artifacts = app_run(*Array(command))
44
+ artifacts = app.run(*Array(command))
161
45
  assert !artifacts[:status].success?, "expected unsuccessful exit status\n\n#{debug(artifacts)}"
162
46
  assert_output artifacts, expected_output if expected_output
163
47
  end
@@ -166,286 +50,236 @@ class AppTest < ActiveSupport::TestCase
166
50
  if ENV['CI']
167
51
  yield
168
52
  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
- )
53
+ app.with_timing do
54
+ yield
55
+ assert app.timing_ratio < ratio, "#{app.last_time} was not less than #{ratio} of #{app.first_time}"
201
56
  end
202
-
203
- File.write(
204
- "#{app_root}/Gemfile",
205
- File.read("#{app_root}/Gemfile").sub("https://rubygems.org", "http://rubygems.org")
206
- )
207
57
  end
208
58
  end
209
59
 
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]
60
+ def assert_app_reloaded
61
+ assert_success app.spring_test_command
218
62
 
219
- unless File.exist?(@controller)
220
- assert_success "bundle exec rails g scaffold post title:string"
221
- end
63
+ File.write(app.application_config, app.application_config.read + <<-CODE)
64
+ class Foo
65
+ def self.omg
66
+ raise "omg"
67
+ end
68
+ end
69
+ CODE
70
+ File.write(app.test, app.test.read.sub("get :index", "Foo.omg"))
222
71
 
223
- assert_success "bundle exec rake db:migrate db:test:clone"
224
- @@installed = true
72
+ app.await_reload
73
+ assert_failure app.spring_test_command, stdout: "RuntimeError: omg"
225
74
  end
226
75
 
227
- @@installed = false
228
-
229
76
  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)
77
+ generator.generate_if_missing
78
+ generator.install_spring
79
+ generator.copy_to(app.root)
237
80
  end
238
81
 
239
82
  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")
83
+ app.stop_spring
244
84
  end
245
85
 
246
86
  test "basic" do
247
87
  assert_speedup do
248
- 2.times { app_run spring_test_command }
88
+ 2.times { app.run app.spring_test_command }
249
89
  end
250
90
  end
251
91
 
252
92
  test "help message when called without arguments" do
253
- assert_success spring, stdout: 'Usage: spring COMMAND [ARGS]'
93
+ assert_success "bin/spring", stdout: 'Usage: spring COMMAND [ARGS]'
254
94
  end
255
95
 
256
96
  test "test changes are picked up" do
257
97
  assert_speedup do
258
- assert_success spring_test_command, stdout: "0 failures"
98
+ assert_success app.spring_test_command, stdout: "0 failures"
259
99
 
260
- File.write(@test, @test_contents.sub("get :index", "raise 'omg'"))
261
- assert_failure spring_test_command, stdout: "RuntimeError: omg"
100
+ File.write(app.test, app.test.read.sub("get :index", "raise 'omg'"))
101
+ assert_failure app.spring_test_command, stdout: "RuntimeError: omg"
262
102
  end
263
103
  end
264
104
 
265
105
  test "code changes are picked up" do
266
106
  assert_speedup do
267
- assert_success spring_test_command, stdout: "0 failures"
107
+ assert_success app.spring_test_command, stdout: "0 failures"
268
108
 
269
- File.write(@controller, @controller_contents.sub("@posts = Post.all", "raise 'omg'"))
270
- assert_failure spring_test_command, stdout: "RuntimeError: omg"
109
+ File.write(app.controller, app.controller.read.sub("@posts = Post.all", "raise 'omg'"))
110
+ assert_failure app.spring_test_command, stdout: "RuntimeError: omg"
271
111
  end
272
112
  end
273
113
 
274
114
  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")
115
+ File.write(app.path("config/initializers/load_posts_controller.rb"), "PostsController\n")
278
116
 
279
- assert_speedup do
280
- assert_success spring_test_command, stdout: "0 failures"
117
+ assert_speedup do
118
+ assert_success app.spring_test_command, stdout: "0 failures"
281
119
 
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)
120
+ File.write(app.controller, app.controller.read.sub("@posts = Post.all", "raise 'omg'"))
121
+ assert_failure app.spring_test_command, stdout: "RuntimeError: omg"
287
122
  end
288
123
  end
289
124
 
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
125
  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"
126
+ app.env["RAILS_ENV"] = "test"
127
+ assert_success "bin/rails runner 'puts Spring.watcher.class'", stdout: "Polling"
314
128
  assert_app_reloaded
315
129
  end
316
130
 
317
131
  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]
132
+ File.write(app.gemfile, "#{app.gemfile.read}gem 'listen', '~> 1.0'")
133
+ File.write(app.spring_config, "Spring.watch_method = :listen")
134
+ app.bundle
326
135
 
327
- env["RAILS_ENV"] = "test"
328
- assert_success "#{spring} rails runner 'puts Spring.watcher.class'", stdout: "Listen"
329
-
330
- assert_app_reloaded
331
- ensure
332
- File.write(gemfile, gemfile_contents)
333
- assert_success "bundle check"
334
- end
136
+ app.env["RAILS_ENV"] = "test"
137
+ assert_success "bin/rails runner 'puts Spring.watcher.class'", stdout: "Listen"
138
+ assert_app_reloaded
335
139
  end
336
140
 
337
141
  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)
142
+ config = app.application_config.read
341
143
 
342
- assert_success spring_test_command
144
+ assert_success app.spring_test_command
343
145
 
344
- File.write(application, application_contents + "\nomg")
345
- await_reload
146
+ File.write(app.application_config, "#{config}\nomg")
147
+ app.await_reload
346
148
 
347
- assert_failure spring_test_command
149
+ assert_failure app.spring_test_command
348
150
 
349
- File.write(application, application_contents)
350
- assert_success spring_test_command
351
- ensure
352
- File.write(application, application_contents)
353
- end
151
+ File.write(app.application_config, config)
152
+ assert_success app.spring_test_command
354
153
  end
355
154
 
356
155
  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"
156
+ app.run app.spring_test_command
157
+ assert app.spring_env.server_running?, "The server should be running but it isn't"
359
158
 
360
- assert_success "#{spring} stop"
361
- assert !spring_env.server_running?, "The server should not be running but it is"
159
+ assert_success "bin/spring stop"
160
+ assert !app.spring_env.server_running?, "The server should not be running but it is"
362
161
  end
363
162
 
364
163
  test "custom commands" do
365
- File.write("#{app_root}/config/spring.rb", <<-CODE)
164
+ File.write(app.spring_config, <<-CODE)
366
165
  class CustomCommand
367
166
  def call
368
167
  puts "omg"
369
168
  end
169
+
170
+ def exec_name
171
+ "rake"
172
+ end
370
173
  end
371
174
 
372
175
  Spring.register_command "custom", CustomCommand.new
373
176
  CODE
374
177
 
375
- assert_success "#{spring} custom", stdout: "omg"
178
+ assert_success "bin/spring custom", stdout: "omg"
179
+
180
+ assert_success "bin/spring binstub custom"
181
+ assert_success "bin/custom", stdout: "omg"
182
+
183
+ app.env["DISABLE_SPRING"] = "1"
184
+ assert_success %{bin/custom -e 'puts "foo"'}, stdout: "foo"
376
185
  end
377
186
 
378
- test "binstubs" do
379
- begin
380
- FileUtils.mv "#{app_root}/bin", "#{app_root}/bin~" if File.exist?("#{app_root}/bin")
187
+ test "binstub" do
188
+ assert_success "bin/rails server --help", stdout: "Usage: rails server" # rails command fallback
381
189
 
382
- app_run "#{spring} binstub rake"
383
- assert_success "bin/rake -T", stdout: "rake db:migrate"
190
+ assert_success "#{app.spring} binstub rake", stdout: "bin/rake: spring already present"
384
191
 
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"
192
+ assert_success "#{app.spring} binstub --remove rake", stdout: "bin/rake: spring removed"
193
+ assert !app.path("bin/rake").read.include?(Spring::Client::Binstub::LOADER)
194
+ assert_success "bin/rake -T", stdout: "rake db:migrate"
195
+ end
388
196
 
389
- FileUtils.rm ["#{app_root}/bin/rails", "#{app_root}/bin/rake"]
197
+ test "binstub when spring is uninstalled" do
198
+ app.run! "gem uninstall --ignore-dependencies spring"
199
+ File.write(app.gemfile, app.gemfile.read.sub("gem 'spring-commands-testunit'\n", ""))
200
+ assert_success "bin/rake -T", stdout: "rake db:migrate"
201
+ end
390
202
 
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
203
+ test "binstub upgrade" do
204
+ File.write(app.path("bin/rake"), <<CODE)
205
+ #!/usr/bin/env ruby
206
+
207
+ if !Process.respond_to?(:fork) || Gem::Specification.find_all_by_name("spring").empty?
208
+ exec "bundle", "exec", "rake", *ARGV
209
+ else
210
+ ARGV.unshift "rake"
211
+ load Gem.bin_path("spring", "spring")
212
+ end
213
+ CODE
214
+
215
+ File.write(app.path("bin/rails"), <<CODE)
216
+ #!/usr/bin/env ruby
217
+
218
+ if !Process.respond_to?(:fork) || Gem::Specification.find_all_by_name("spring").empty?
219
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
220
+ require_relative '../config/boot'
221
+ require 'rails/commands'
222
+ else
223
+ ARGV.unshift "rails"
224
+ load Gem.bin_path("spring", "spring")
225
+ end
226
+ CODE
227
+
228
+ assert_success "bin/spring binstub --all", stdout: "upgraded"
229
+
230
+ assert_equal app.path("bin/rake").read, <<CODE
231
+ #!/usr/bin/env ruby
232
+ #{Spring::Client::Binstub::LOADER.strip}
233
+ require 'bundler/setup'
234
+ load Gem.bin_path('rake', 'rake')
235
+ CODE
236
+
237
+ assert_equal app.path("bin/rails").read, <<CODE
238
+ #!/usr/bin/env ruby
239
+ #{Spring::Client::Binstub::LOADER.strip}
240
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
241
+ require_relative '../config/boot'
242
+ require 'rails/commands'
243
+ CODE
400
244
  end
401
245
 
402
246
  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"
247
+ File.write(app.spring_config, "Spring.after_fork { puts '!callback!' }")
248
+ assert_success "bin/rails runner 'puts 2'", stdout: "!callback!\n2"
405
249
  end
406
250
 
407
251
  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
252
+ File.write("#{app.user_home}/.spring.rb", "Spring.after_fork { puts '!callback!' }")
253
+ assert_success "bin/rails runner 'puts 2'", stdout: "!callback!\n2"
414
254
  end
415
255
 
416
256
  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
257
+ app.application_config.delete
258
+ assert_failure "bin/rake -T", stderr: "unable to find your config/application.rb"
424
259
  end
425
260
 
426
261
  test "piping" do
427
- assert_success "#{spring} rake -T | grep db", stdout: "rake db:migrate"
262
+ assert_success "bin/rake -T | grep db", stdout: "rake db:migrate"
428
263
  end
429
264
 
430
265
  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"
266
+ assert_success "bin/spring status", stdout: "Spring is not running"
267
+ assert_success "bin/rails runner ''"
268
+ assert_success "bin/spring status", stdout: "Spring is running"
434
269
  end
435
270
 
436
271
  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"
272
+ assert_success "bin/rails runner -e production 'puts Rails.env'", stdout: "production"
273
+ assert_success "bin/rails runner --environment=production 'puts Rails.env'", stdout: "production"
439
274
  end
440
275
 
441
276
  test "forcing rails env via environment variable" do
442
- env['RAILS_ENV'] = 'production'
443
- assert_success "#{spring} rake -p 'Rails.env'", stdout: "production"
277
+ app.env['RAILS_ENV'] = 'production'
278
+ assert_success "bin/rake -p 'Rails.env'", stdout: "production"
444
279
  end
445
280
 
446
281
  test "setting env vars with rake" do
447
- begin
448
- File.write("#{app_root}/lib/tasks/env.rake", <<-'CODE')
282
+ File.write(app.path("lib/tasks/env.rake"), <<-'CODE')
449
283
  task :print_rails_env => :environment do
450
284
  puts Rails.env
451
285
  end
@@ -455,57 +289,39 @@ class AppTest < ActiveSupport::TestCase
455
289
  end
456
290
 
457
291
  task(:default).clear.enhance [:print_rails_env]
458
- CODE
292
+ CODE
459
293
 
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
294
+ assert_success "bin/rake RAILS_ENV=test print_rails_env", stdout: "test"
295
+ assert_success "bin/rake FOO=bar print_env", stdout: "FOO=bar"
296
+ assert_success "bin/rake", stdout: "test"
466
297
  end
467
298
 
468
299
  test "changing the Gemfile restarts the server" do
469
- begin
470
- gemfile = app_root.join("Gemfile")
471
- gemfile_contents = gemfile.read
472
-
473
- assert_success %(#{spring} rails runner 'require "sqlite3"')
300
+ assert_success %(bin/rails runner 'require "sqlite3"')
474
301
 
475
- File.write(gemfile, gemfile_contents.sub(%{gem 'sqlite3'}, %{# gem 'sqlite3'}))
476
- app_run "bundle check"
302
+ File.write(app.gemfile, app.gemfile.read.sub(%{gem 'sqlite3'}, %{# gem 'sqlite3'}))
303
+ app.bundle
477
304
 
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
305
+ app.await_reload
306
+ assert_failure %(bin/rails runner 'require "sqlite3"'), stderr: "sqlite3"
484
307
  end
485
308
 
486
309
  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'")
310
+ File.write(app.application_config, "#{app.application_config.read}\nENV['BAR'] = 'bar'")
492
311
 
493
- env["OMG"] = "1"
494
- env["FOO"] = "1"
495
- env["RUBYOPT"] = "-rubygems"
312
+ app.env["OMG"] = "1"
313
+ app.env["FOO"] = "1"
314
+ app.env["RUBYOPT"] = "-rubygems"
496
315
 
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"
316
+ assert_success %(bin/rails runner 'p ENV["OMG"]'), stdout: "1"
317
+ assert_success %(bin/rails runner 'p ENV["BAR"]'), stdout: "bar"
318
+ assert_success %(bin/rails runner 'p ENV.key?("BUNDLE_GEMFILE")'), stdout: "true"
319
+ assert_success %(bin/rails runner 'p ENV["RUBYOPT"]'), stdout: "bundler"
501
320
 
502
- env["OMG"] = "2"
503
- env.delete "FOO"
321
+ app.env["OMG"] = "2"
322
+ app.env.delete "FOO"
504
323
 
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
324
+ assert_success %(bin/rails runner 'p ENV["OMG"]'), stdout: "2"
325
+ assert_success %(bin/rails runner 'p ENV.key?("FOO")'), stdout: "false"
510
326
  end
511
327
  end