spring 1.1.1 → 1.2.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,334 +0,0 @@
1
- # encoding: utf-8
2
- require "helper"
3
- require "acceptance/helper"
4
- require "io/wait"
5
- require "timeout"
6
- require "spring/sid"
7
- require "spring/client"
8
-
9
- class AppTest < ActiveSupport::TestCase
10
- DEFAULT_SPEEDUP = 0.8
11
-
12
- def rails_version
13
- ENV['RAILS_VERSION'] || '~> 4.0.0'
14
- end
15
-
16
- def generator
17
- @@generator ||= Spring::Test::ApplicationGenerator.new(rails_version)
18
- end
19
-
20
- def app
21
- @app ||= Spring::Test::Application.new("#{TEST_ROOT}/apps/tmp")
22
- end
23
-
24
- def assert_output(artifacts, expected)
25
- expected.each do |stream, output|
26
- assert artifacts[stream].include?(output),
27
- "expected #{stream} to include '#{output}'.\n\n#{app.debug(artifacts)}"
28
- end
29
- end
30
-
31
- def assert_success(command, expected_output = nil)
32
- artifacts = app.run(*Array(command))
33
- assert artifacts[:status].success?, "expected successful exit status\n\n#{app.debug(artifacts)}"
34
- assert_output artifacts, expected_output if expected_output
35
- end
36
-
37
- def assert_failure(command, expected_output = nil)
38
- artifacts = app.run(*Array(command))
39
- assert !artifacts[:status].success?, "expected unsuccessful exit status\n\n#{app.debug(artifacts)}"
40
- assert_output artifacts, expected_output if expected_output
41
- end
42
-
43
- def assert_speedup(ratio = DEFAULT_SPEEDUP)
44
- if ENV['CI']
45
- yield
46
- else
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}"
50
- end
51
- end
52
- end
53
-
54
- def assert_app_reloaded
55
- assert_success app.spring_test_command
56
-
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"))
65
-
66
- app.await_reload
67
- assert_failure app.spring_test_command, stdout: "RuntimeError: omg"
68
- end
69
-
70
- setup do
71
- generator.generate_if_missing
72
- generator.install_spring
73
- generator.copy_to(app.root)
74
- end
75
-
76
- teardown do
77
- app.stop_spring
78
- end
79
-
80
- test "basic" do
81
- assert_speedup do
82
- 2.times { app.run app.spring_test_command }
83
- end
84
- end
85
-
86
- test "help message when called without arguments" do
87
- assert_success "bin/spring", stdout: 'Usage: spring COMMAND [ARGS]'
88
- end
89
-
90
- test "test changes are picked up" do
91
- assert_speedup do
92
- assert_success app.spring_test_command, stdout: "0 failures"
93
-
94
- File.write(app.test, app.test.read.sub("get :index", "raise 'omg'"))
95
- assert_failure app.spring_test_command, stdout: "RuntimeError: omg"
96
- end
97
- end
98
-
99
- test "code changes are picked up" do
100
- assert_speedup do
101
- assert_success app.spring_test_command, stdout: "0 failures"
102
-
103
- File.write(app.controller, app.controller.read.sub("@posts = Post.all", "raise 'omg'"))
104
- assert_failure app.spring_test_command, stdout: "RuntimeError: omg"
105
- end
106
- end
107
-
108
- test "code changes in pre-referenced app files are picked up" do
109
- File.write(app.path("config/initializers/load_posts_controller.rb"), "PostsController\n")
110
-
111
- assert_speedup do
112
- assert_success app.spring_test_command, stdout: "0 failures"
113
-
114
- File.write(app.controller, app.controller.read.sub("@posts = Post.all", "raise 'omg'"))
115
- assert_failure app.spring_test_command, stdout: "RuntimeError: omg"
116
- end
117
- end
118
-
119
- test "app gets reloaded when preloaded files change (polling watcher)" do
120
- app.env["RAILS_ENV"] = "test"
121
- assert_success "bin/rails runner 'puts Spring.watcher.class'", stdout: "Polling"
122
- assert_app_reloaded
123
- end
124
-
125
- test "app gets reloaded when preloaded files change (listen watcher)" do
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
129
-
130
- app.env["RAILS_ENV"] = "test"
131
- assert_success "bin/rails runner 'puts Spring.watcher.class'", stdout: "Listen"
132
- assert_app_reloaded
133
- end
134
-
135
- test "app recovers when a boot-level error is introduced" do
136
- config = app.application_config.read
137
-
138
- assert_success app.spring_test_command
139
-
140
- File.write(app.application_config, "#{config}\nomg")
141
- app.await_reload
142
-
143
- assert_failure app.spring_test_command
144
-
145
- File.write(app.application_config, config)
146
- assert_success app.spring_test_command
147
- end
148
-
149
- test "stop command kills server" do
150
- app.run app.spring_test_command
151
- assert app.spring_env.server_running?, "The server should be running but it isn't"
152
-
153
- assert_success "bin/spring stop"
154
- assert !app.spring_env.server_running?, "The server should not be running but it is"
155
- end
156
-
157
- test "custom commands" do
158
- File.write(app.spring_config, <<-CODE)
159
- class CustomCommand
160
- def call
161
- puts "omg"
162
- end
163
-
164
- def exec_name
165
- "rake"
166
- end
167
- end
168
-
169
- Spring.register_command "custom", CustomCommand.new
170
- CODE
171
-
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"
179
- end
180
-
181
- test "binstub" do
182
- assert_success "bin/rails server --help", stdout: "Usage: rails server" # rails command fallback
183
-
184
- assert_success "#{app.spring} binstub rake", stdout: "bin/rake: spring already present"
185
-
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
190
-
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
196
-
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
238
- end
239
-
240
- test "after fork callback" do
241
- File.write(app.spring_config, "Spring.after_fork { puts '!callback!' }")
242
- assert_success "bin/rails runner 'puts 2'", stdout: "!callback!\n2"
243
- end
244
-
245
- test "global config file evaluated" do
246
- File.write("#{app.user_home}/.spring.rb", "Spring.after_fork { puts '!callback!' }")
247
- assert_success "bin/rails runner 'puts 2'", stdout: "!callback!\n2"
248
- end
249
-
250
- test "missing config/application.rb" do
251
- app.application_config.delete
252
- assert_failure "bin/rake -T", stderr: "unable to find your config/application.rb"
253
- end
254
-
255
- test "piping" do
256
- assert_success "bin/rake -T | grep db", stdout: "rake db:migrate"
257
- end
258
-
259
- test "status" do
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"
263
- end
264
-
265
- test "runner command sets Rails environment from command-line options" do
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"
268
- end
269
-
270
- test "forcing rails env via environment variable" do
271
- app.env['RAILS_ENV'] = 'production'
272
- assert_success "bin/rake -p 'Rails.env'", stdout: "production"
273
- end
274
-
275
- test "setting env vars with rake" do
276
- File.write(app.path("lib/tasks/env.rake"), <<-'CODE')
277
- task :print_rails_env => :environment do
278
- puts Rails.env
279
- end
280
-
281
- task :print_env do
282
- ENV.each { |k, v| puts "#{k}=#{v}" }
283
- end
284
-
285
- task(:default).clear.enhance [:print_rails_env]
286
- CODE
287
-
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"
291
- end
292
-
293
- test "changing the Gemfile works" do
294
- assert_success %(bin/rails runner 'require "sqlite3"')
295
-
296
- File.write(app.gemfile, app.gemfile.read.sub(%{gem 'sqlite3'}, %{# gem 'sqlite3'}))
297
- app.await_reload
298
-
299
- assert_failure %(bin/rails runner 'require "sqlite3"'), stderr: "sqlite3"
300
- end
301
-
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]
314
- end
315
-
316
- test "changing the environment between runs" do
317
- File.write(app.application_config, "#{app.application_config.read}\nENV['BAR'] = 'bar'")
318
-
319
- app.env["OMG"] = "1"
320
- app.env["FOO"] = "1"
321
- app.env["RUBYOPT"] = "-rubygems"
322
-
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"
327
-
328
- app.env["OMG"] = "2"
329
- app.env.delete "FOO"
330
-
331
- assert_success %(bin/rails runner 'p ENV["OMG"]'), stdout: "2"
332
- assert_success %(bin/rails runner 'p ENV.key?("FOO")'), stdout: "false"
333
- end
334
- end