-
Notifications
You must be signed in to change notification settings - Fork 247
/
test_railtie.rb
419 lines (334 loc) · 11 KB
/
test_railtie.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
require 'active_support'
require 'active_support/testing/isolation'
require 'minitest/autorun'
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
def silence_stderr
orig_stderr = $stderr.clone
$stderr.reopen File.new('/dev/null', 'w')
yield
ensure
$stderr.reopen orig_stderr
end
class TestBoot < Minitest::Test
include ActiveSupport::Testing::Isolation
ROOT = File.expand_path("../../tmp/app", __FILE__)
FIXTURES_PATH = File.expand_path("../fixtures", __FILE__)
attr_reader :app
def setup
require 'rails'
# Can't seem to get initialize to run w/o this
require 'action_controller/railtie'
require 'active_support/dependencies'
require 'tzinfo'
ENV['RAILS_ENV'] = 'test'
FileUtils.mkdir_p ROOT
Dir.chdir ROOT
@app = Class.new(Rails::Application)
@app.config.eager_load = false
@app.config.time_zone = 'UTC'
@app.config.middleware ||= Rails::Configuration::MiddlewareStackProxy.new
@app.config.active_support.deprecation = :notify
ActionView::Base # load ActionView
Dir.chdir(app.root) do
dir = "app/assets/config"
FileUtils.mkdir_p(dir)
File.open("#{ dir }/manifest.js", "w") do |f|
f << ""
end
end
end
def test_initialize
app.initialize!
end
end
class TestRailtie < TestBoot
def setup
require 'sprockets/railtie'
super
# sprockets-4.0.0.beta8 does not like 'rake assets:clobber' when this directory does not exist
Dir.chdir(app.root) do
dir = "tmp/cache/assets/sprockets"
FileUtils.mkdir_p(dir)
end
end
def test_defaults_to_compile_assets_with_env_and_manifest_available
assert_equal true, app.config.assets.compile
app.initialize!
# Env is available
refute_nil env = app.assets
assert_kind_of Sprockets::Environment, env
# Manifest is always available
assert manifest = app.assets_manifest
assert_equal app.assets, manifest.environment
assert_equal File.join(ROOT, "public/assets"), manifest.dir
# Resolves against manifest then environment by default
assert_equal [ :manifest, :environment ], app.config.assets.resolve_with
# Sprockets config
assert_equal ROOT, env.root
assert_equal "", env.version
assert env.cache
assert_includes(env.paths, "#{ROOT}/app/assets/config")
assert_nil env.js_compressor
assert_nil env.css_compressor
end
def test_disabling_compile_has_manifest_but_no_env
app.configure do
config.assets.compile = false
end
assert_equal false, app.config.assets.compile
app.initialize!
# No env when compile is disabled
assert_nil app.assets
# Manifest is always available
refute_nil manifest = app.assets_manifest
assert_nil manifest.environment
assert_equal File.join(ROOT, "public/assets"), manifest.dir
# Resolves against manifest only
assert_equal [ :manifest ], app.config.assets.resolve_with
end
def test_enabling_debug_resolves_with_env_only
app.configure do
config.assets.debug = true
end
assert_equal true, app.config.assets.debug
assert_equal true, app.config.assets.compile
app.initialize!
# Resolves against environment only
assert_equal [ :environment ], app.config.assets.resolve_with
end
def test_copies_paths
app.configure do
config.assets.paths << "javascripts"
config.assets.paths << "stylesheets"
end
app.initialize!
assert env = app.assets
assert_includes(env.paths, "#{ROOT}/javascripts")
assert_includes(env.paths, "#{ROOT}/stylesheets")
assert_includes(env.paths, "#{ROOT}/app/assets/config")
end
def test_compressors
app.configure do
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
end
app.initialize!
assert env = app.assets
assert_equal Sprockets::UglifierCompressor.name, env.js_compressor.name
silence_warnings do
require 'sprockets/sass_compressor'
end
assert_equal Sprockets::SassCompressor.name, env.css_compressor.name
end
def test_custom_compressors
compressor = Class.new
app.configure do
config.assets.configure do |env|
env.register_compressor "application/javascript", :test_js, compressor
env.register_compressor "text/css", :test_css, compressor
end
config.assets.js_compressor = :test_js
config.assets.css_compressor = :test_css
end
app.initialize!
assert env = app.assets
assert_equal compressor, env.js_compressor
assert_equal compressor, env.css_compressor
end
def test_default_gzip_config
app.initialize!
assert env = app.assets
assert_equal true, env.gzip?
end
def test_gzip_config
app.configure do
config.assets.gzip = false
end
app.initialize!
assert env = app.assets
assert_equal false, env.gzip?
end
def test_default_check_precompiled_assets
assert app.config.assets.check_precompiled_asset
app.initialize!
@view = ActionView::Base.new
assert @view.check_precompiled_asset
end
def test_configure_check_precompiled_assets
app.configure do
config.assets.check_precompiled_asset = false
end
app.initialize!
@view = ActionView::Base.new
refute @view.check_precompiled_asset
end
def test_version
app.configure do
config.assets.version = 'v2'
end
app.initialize!
assert env = app.assets
assert_equal "v2", env.version
end
def test_configure
app.configure do
config.assets.configure do |env|
env.append_path "javascripts"
end
config.assets.configure do |env|
env.append_path "stylesheets"
end
end
app.initialize!
assert env = app.assets
assert_includes(env.paths, "#{ROOT}/javascripts")
assert_includes(env.paths, "#{ROOT}/stylesheets")
assert_includes(env.paths, "#{ROOT}/app/assets/config")
end
def test_environment_is_frozen_if_caching_classes
app.configure do
config.cache_classes = true
end
app.initialize!
assert env = app.assets
assert_kind_of Sprockets::CachedEnvironment, env
end
def test_action_view_helper
app.configure do
config.assets.paths << FIXTURES_PATH
config.assets.precompile += ["foo.js"]
end
app.initialize!
assert app.assets.paths.include?(FIXTURES_PATH)
assert_equal false, ActionView::Base.debug_assets
assert_equal true, ActionView::Base.digest_assets
assert_equal "/assets", ActionView::Base.assets_prefix
assert_equal app.assets, ActionView::Base.assets_environment
assert_equal app.assets_manifest, ActionView::Base.assets_manifest
assert_kind_of Sprockets::Environment, ActionView::Base.assets_environment
@view = ActionView::Base.new
assert_equal "/javascripts/xmlhr.js", @view.javascript_path("xmlhr")
assert_equal "/assets/foo-4ef5541f349f7ed5a0d6b71f2fa4c82745ca106ae02f212aea5129726ac6f6ab.js", @view.javascript_path("foo")
env = @view.assets_environment
assert_kind_of Sprockets::CachedEnvironment, env
assert @view.assets_environment.equal?(env), "view didn't return the same cached instance"
end
def test_action_view_helper_when_no_compile
app.configure do
config.assets.compile = false
end
assert_equal false, app.config.assets.compile
app.initialize!
refute ActionView::Base.assets_environment
assert_equal app.assets_manifest, ActionView::Base.assets_manifest
@view = ActionView::Base.new
refute @view.assets_environment
assert_equal app.assets_manifest, @view.assets_manifest
end
def test_sprockets_context_helper
app.initialize!
assert env = app.assets
assert_equal "/assets", env.context_class.assets_prefix
assert_equal true, env.context_class.digest_assets
assert_nil env.context_class.config.asset_host
end
def test_manifest_path
app.configure do
config.assets.manifest = Rails.root.join('config','foo','bar.json')
end
app.initialize!
assert manifest = app.assets_manifest
assert_match %r{config/foo/bar\.json$}, manifest.path
assert_match %r{public/assets$}, manifest.dir
end
def test_manifest_path_respects_rails_public_path
app.configure do
config.paths['public'] = 'test_public'
end
app.initialize!
assert manifest = app.assets_manifest
assert_match %r{test_public/assets/\.sprockets-manifest-.*\.json$}, manifest.path
assert_match %r{test_public/assets$}, manifest.dir
end
def test_load_tasks
app.initialize!
app.load_tasks
assert Rake.application['assets:environment']
assert Rake.application['assets:precompile']
assert Rake.application['assets:clean']
assert Rake.application['assets:clobber']
end
def test_task_precompile
app.configure do
config.assets.paths << FIXTURES_PATH
config.assets.precompile += ["foo.js"]
end
app.initialize!
app.load_tasks
path = "#{app.assets_manifest.dir}/foo-4ef5541f349f7ed5a0d6b71f2fa4c82745ca106ae02f212aea5129726ac6f6ab.js"
silence_stderr do
Rake.application['assets:clobber'].execute
end
refute File.exist?(path)
silence_stderr do
Rake.application['assets:precompile'].execute
end
assert File.exist?(path)
silence_stderr do
Rake.application['assets:clobber'].execute
end
refute File.exist?(path)
end
def test_task_precompile_compile_false
app.configure do
config.assets.compile = false
config.assets.paths << FIXTURES_PATH
config.assets.precompile += ["foo.js"]
end
app.initialize!
app.load_tasks
path = "#{app.assets_manifest.dir}/foo-4ef5541f349f7ed5a0d6b71f2fa4c82745ca106ae02f212aea5129726ac6f6ab.js"
silence_stderr do
Rake.application['assets:clobber'].execute
end
refute File.exist?(path)
silence_stderr do
Rake.application['assets:precompile'].execute
end
assert File.exist?(path)
silence_stderr do
Rake.application['assets:clobber'].execute
end
refute File.exist?(path)
end
def test_direct_build_environment_call
app.configure do
config.assets.paths << "javascripts"
config.assets.paths << "stylesheets"
end
app.initialize!
assert env = Sprockets::Railtie.build_environment(app)
assert_kind_of Sprockets::Environment, env
assert_equal ROOT, env.root
assert_includes(env.paths, "#{ROOT}/javascripts")
assert_includes(env.paths, "#{ROOT}/stylesheets")
assert_includes(env.paths, "#{ROOT}/app/assets/config")
end
def test_quiet_assets_defaults_to_off
app.initialize!
app.load_tasks
assert_equal false, app.config.assets.quiet
refute app.config.middleware.include?(Sprockets::Rails::QuietAssets)
end
def test_quiet_assets_inserts_middleware
app.configure do
config.assets.quiet = true
end
app.initialize!
app.load_tasks
middleware = app.config.middleware
assert_equal true, app.config.assets.quiet
assert middleware.include?(Sprockets::Rails::QuietAssets)
assert middleware.each_cons(2).include?([Sprockets::Rails::QuietAssets, Rails::Rack::Logger])
end
end