turbo-sprockets-rails3 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ /* Different from other style.css */
File without changes
File without changes
Binary file
@@ -0,0 +1 @@
1
+ //= require xmlhr
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1 @@
1
+ /*= require style */
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,364 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/sprockets_helpers_abstract_unit")
2
+ require 'sprockets'
3
+ require 'sprockets/helpers/rails_helper'
4
+ require 'turbo-sprockets/sprockets_overrides/helpers/rails_helper'
5
+ require 'mocha'
6
+
7
+ class SprocketsHelperTest < ActionView::TestCase
8
+ include Sprockets::Helpers::RailsHelper
9
+
10
+ attr_accessor :assets
11
+
12
+ class MockRequest
13
+ def protocol() 'http://' end
14
+ def ssl?() false end
15
+ def host_with_port() 'localhost' end
16
+ end
17
+
18
+ def setup
19
+ super
20
+
21
+ @controller = BasicController.new
22
+ @controller.request = MockRequest.new
23
+
24
+ @assets = Sprockets::Environment.new
25
+ @assets.append_path(FIXTURES.join("app/javascripts"))
26
+ @assets.append_path(FIXTURES.join("app/stylesheets"))
27
+ @assets.append_path(FIXTURES.join("app/images"))
28
+ @assets.append_path(FIXTURES.join("app/fonts"))
29
+
30
+ application = Struct.new(:config, :assets).new(config, @assets)
31
+ Rails.stubs(:application).returns(application)
32
+ @config = config
33
+ @config.perform_caching = true
34
+ @config.assets.digest = true
35
+ @config.assets.compile = true
36
+ end
37
+
38
+ def url_for(*args)
39
+ "http://www.example.com"
40
+ end
41
+
42
+ def config
43
+ @controller ? @controller.config : @config
44
+ end
45
+
46
+ def compute_host(source, request, options = {})
47
+ raise "Should never get here"
48
+ end
49
+
50
+ test "asset_path" do
51
+ assert_match %r{/assets/logo-[0-9a-f]+.png},
52
+ asset_path("logo.png")
53
+ assert_match %r{/assets/logo-[0-9a-f]+.png},
54
+ asset_path("logo.png", :digest => true)
55
+ assert_match %r{/assets/logo.png},
56
+ asset_path("logo.png", :digest => false)
57
+ end
58
+
59
+ test "custom_asset_path" do
60
+ @config.assets.prefix = '/s'
61
+ assert_match %r{/s/logo-[0-9a-f]+.png},
62
+ asset_path("logo.png")
63
+ assert_match %r{/s/logo-[0-9a-f]+.png},
64
+ asset_path("logo.png", :digest => true)
65
+ assert_match %r{/s/logo.png},
66
+ asset_path("logo.png", :digest => false)
67
+ end
68
+
69
+ test "asset_path with root relative assets" do
70
+ assert_equal "/images/logo",
71
+ asset_path("/images/logo")
72
+ assert_equal "/images/logo.gif",
73
+ asset_path("/images/logo.gif")
74
+
75
+ assert_equal "/dir/audio",
76
+ asset_path("/dir/audio")
77
+ end
78
+
79
+ test "asset_path with absolute urls" do
80
+ assert_equal "http://www.example.com/video/play",
81
+ asset_path("http://www.example.com/video/play")
82
+ assert_equal "http://www.example.com/video/play.mp4",
83
+ asset_path("http://www.example.com/video/play.mp4")
84
+ end
85
+
86
+ test "with a simple asset host the url should default to protocol relative" do
87
+ @controller.config.default_asset_host_protocol = :relative
88
+ @controller.config.asset_host = "assets-%d.example.com"
89
+ assert_match %r{^//assets-\d.example.com/assets/logo-[0-9a-f]+.png},
90
+ asset_path("logo.png")
91
+ end
92
+
93
+ test "with a simple asset host the url can be changed to use the request protocol" do
94
+ @controller.config.asset_host = "assets-%d.example.com"
95
+ @controller.config.default_asset_host_protocol = :request
96
+ assert_match %r{http://assets-\d.example.com/assets/logo-[0-9a-f]+.png},
97
+ asset_path("logo.png")
98
+ end
99
+
100
+ test "With a proc asset host that returns no protocol the url should be protocol relative" do
101
+ @controller.config.default_asset_host_protocol = :relative
102
+ @controller.config.asset_host = Proc.new do |asset|
103
+ "assets-999.example.com"
104
+ end
105
+ assert_match %r{^//assets-999.example.com/assets/logo-[0-9a-f]+.png},
106
+ asset_path("logo.png")
107
+ end
108
+
109
+ test "with a proc asset host that returns a protocol the url use it" do
110
+ @controller.config.asset_host = Proc.new do |asset|
111
+ "http://assets-999.example.com"
112
+ end
113
+ assert_match %r{http://assets-999.example.com/assets/logo-[0-9a-f]+.png},
114
+ asset_path("logo.png")
115
+ end
116
+
117
+ test "stylesheets served with a controller in scope can access the request" do
118
+ config.asset_host = Proc.new do |asset, request|
119
+ assert_not_nil request
120
+ "http://assets-666.example.com"
121
+ end
122
+ assert_match %r{http://assets-666.example.com/assets/logo-[0-9a-f]+.png},
123
+ asset_path("logo.png")
124
+ end
125
+
126
+ test "stylesheets served without a controller in scope cannot access the request" do
127
+ @controller = nil
128
+ @config.asset_host = Proc.new do |asset, request|
129
+ fail "This should not have been called."
130
+ end
131
+ assert_raises ActionController::RoutingError do
132
+ asset_path("logo.png")
133
+ end
134
+ @config.asset_host = method :compute_host
135
+ assert_raises ActionController::RoutingError do
136
+ asset_path("logo.png")
137
+ end
138
+ end
139
+
140
+ test "image_tag" do
141
+ assert_dom_equal '<img alt="Xml" src="/assets/xml.png" />', image_tag("xml.png")
142
+ end
143
+
144
+ test "image_path" do
145
+ assert_match %r{/assets/logo-[0-9a-f]+.png},
146
+ image_path("logo.png")
147
+
148
+ assert_match %r{/assets/logo-[0-9a-f]+.png},
149
+ path_to_image("logo.png")
150
+ end
151
+
152
+ test "font_path" do
153
+ assert_match %r{/assets/font-[0-9a-f]+.ttf},
154
+ font_path("font.ttf")
155
+
156
+ assert_match %r{/assets/font-[0-9a-f]+.ttf},
157
+ path_to_font("font.ttf")
158
+ end
159
+
160
+ test "javascript_path" do
161
+ assert_match %r{/assets/application-[0-9a-f]+.js},
162
+ javascript_path("application")
163
+
164
+ assert_match %r{/assets/application-[0-9a-f]+.js},
165
+ javascript_path("application.js")
166
+
167
+ assert_match %r{/assets/application-[0-9a-f]+.js},
168
+ path_to_javascript("application.js")
169
+ end
170
+
171
+ test "stylesheet_path" do
172
+ assert_match %r{/assets/application-[0-9a-f]+.css},
173
+ stylesheet_path("application")
174
+
175
+ assert_match %r{/assets/application-[0-9a-f]+.css},
176
+ stylesheet_path("application.css")
177
+
178
+ assert_match %r{/assets/application-[0-9a-f]+.css},
179
+ path_to_stylesheet("application.css")
180
+ end
181
+
182
+ test "stylesheets served without a controller in do not use asset hosts when the default protocol is :request" do
183
+ @controller = nil
184
+ @config.asset_host = "assets-%d.example.com"
185
+ @config.default_asset_host_protocol = :request
186
+ @config.perform_caching = true
187
+
188
+ assert_match %r{/assets/logo-[0-9a-f]+.png},
189
+ asset_path("logo.png")
190
+ end
191
+
192
+ test "asset path with relative url root" do
193
+ @controller.config.relative_url_root = "/collaboration/hieraki"
194
+ assert_equal "/collaboration/hieraki/images/logo.gif",
195
+ asset_path("/images/logo.gif")
196
+ end
197
+
198
+ test "asset path with relative url root when controller isn't present but relative_url_root is" do
199
+ @controller = nil
200
+ @config.relative_url_root = "/collaboration/hieraki"
201
+ assert_equal "/collaboration/hieraki/images/logo.gif",
202
+ asset_path("/images/logo.gif")
203
+ end
204
+
205
+ test "font path through asset_path" do
206
+ assert_match %r{/assets/font-[0-9a-f]+.ttf},
207
+ asset_path('font.ttf')
208
+
209
+ assert_match %r{/assets/dir/font-[0-9a-f]+.ttf},
210
+ asset_path("dir/font.ttf")
211
+
212
+ assert_equal "http://www.example.com/fonts/font.ttf",
213
+ asset_path("http://www.example.com/fonts/font.ttf")
214
+ end
215
+
216
+ test "javascript path through asset_path" do
217
+ assert_match %r{/assets/application-[0-9a-f]+.js},
218
+ asset_path(:application, :ext => "js")
219
+
220
+ assert_match %r{/assets/xmlhr-[0-9a-f]+.js},
221
+ asset_path("xmlhr", :ext => "js")
222
+ assert_match %r{/assets/dir/xmlhr-[0-9a-f]+.js},
223
+ asset_path("dir/xmlhr.js", :ext => "js")
224
+
225
+ assert_equal "/dir/xmlhr.js",
226
+ asset_path("/dir/xmlhr", :ext => "js")
227
+
228
+ assert_equal "http://www.example.com/js/xmlhr",
229
+ asset_path("http://www.example.com/js/xmlhr", :ext => "js")
230
+ assert_equal "http://www.example.com/js/xmlhr.js",
231
+ asset_path("http://www.example.com/js/xmlhr.js", :ext => "js")
232
+ end
233
+
234
+ test "javascript include tag" do
235
+ assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>},
236
+ javascript_include_tag(:application)
237
+ assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>},
238
+ javascript_include_tag(:application, :digest => true)
239
+ assert_match %r{<script src="/assets/application.js" type="text/javascript"></script>},
240
+ javascript_include_tag(:application, :digest => false)
241
+
242
+ assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js" type="text/javascript"></script>},
243
+ javascript_include_tag("xmlhr")
244
+ assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js" type="text/javascript"></script>},
245
+ javascript_include_tag("xmlhr.js")
246
+ assert_equal '<script src="http://www.example.com/xmlhr" type="text/javascript"></script>',
247
+ javascript_include_tag("http://www.example.com/xmlhr")
248
+
249
+ assert_match %r{<script src=\"/assets/xmlhr-[0-9a-f]+.js" type=\"text/javascript\"></script>\n<script src=\"/assets/extra-[0-9a-f]+.js" type=\"text/javascript\"></script>},
250
+ javascript_include_tag("xmlhr", "extra")
251
+
252
+ assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js\?body=1" type="text/javascript"></script>\n<script src="/assets/application-[0-9a-f]+.js\?body=1" type="text/javascript"></script>},
253
+ javascript_include_tag(:application, :debug => true)
254
+
255
+ assert_match %r{<script src="/assets/jquery.plugin.js" type="text/javascript"></script>},
256
+ javascript_include_tag('jquery.plugin', :digest => false)
257
+
258
+ assert_match %r{\A<script src="/assets/xmlhr-[0-9a-f]+.js" type="text/javascript"></script>\Z},
259
+ javascript_include_tag("xmlhr", "xmlhr")
260
+
261
+ assert_match %r{\A<script src="/assets/foo.min-[0-9a-f]+.js" type="text/javascript"></script>\Z},
262
+ javascript_include_tag("foo.min")
263
+
264
+ @config.assets.compile = true
265
+ @config.assets.debug = true
266
+ assert_match %r{<script src="/javascripts/application.js" type="text/javascript"></script>},
267
+ javascript_include_tag('/javascripts/application')
268
+ assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js\?body=1" type="text/javascript"></script>\n<script src="/assets/application-[0-9a-f]+.js\?body=1" type="text/javascript"></script>},
269
+ javascript_include_tag(:application)
270
+ end
271
+
272
+ test "stylesheet path through asset_path" do
273
+ assert_match %r{/assets/application-[0-9a-f]+.css}, asset_path(:application, :ext => "css")
274
+
275
+ assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
276
+ assert_match %r{/assets/dir/style-[0-9a-f]+.css}, asset_path("dir/style.css", :ext => "css")
277
+ assert_equal "/dir/style.css", asset_path("/dir/style.css", :ext => "css")
278
+
279
+ assert_equal "http://www.example.com/css/style",
280
+ asset_path("http://www.example.com/css/style", :ext => "css")
281
+ assert_equal "http://www.example.com/css/style.css",
282
+ asset_path("http://www.example.com/css/style.css", :ext => "css")
283
+ end
284
+
285
+ test "stylesheet link tag" do
286
+ assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
287
+ stylesheet_link_tag(:application)
288
+ assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
289
+ stylesheet_link_tag(:application, :digest => true)
290
+ assert_match %r{<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />},
291
+ stylesheet_link_tag(:application, :digest => false)
292
+
293
+ assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
294
+ stylesheet_link_tag("style")
295
+ assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
296
+ stylesheet_link_tag("style.css")
297
+
298
+ assert_equal '<link href="http://www.example.com/style.css" media="screen" rel="stylesheet" type="text/css" />',
299
+ stylesheet_link_tag("http://www.example.com/style.css")
300
+ assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="all" rel="stylesheet" type="text/css" />},
301
+ stylesheet_link_tag("style", :media => "all")
302
+ assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="print" rel="stylesheet" type="text/css" />},
303
+ stylesheet_link_tag("style", :media => "print")
304
+
305
+ assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/extra-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
306
+ stylesheet_link_tag("style", "extra")
307
+
308
+ assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />},
309
+ stylesheet_link_tag(:application, :debug => true)
310
+
311
+ assert_match %r{\A<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />\Z},
312
+ stylesheet_link_tag("style", "style")
313
+
314
+ assert_match %r{\A<link href="/assets/style-[0-9a-f]+.ext" media="screen" rel="stylesheet" type="text/css" />\Z},
315
+ stylesheet_link_tag("style.ext")
316
+
317
+ assert_match %r{\A<link href="/assets/style.min-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />\Z},
318
+ stylesheet_link_tag("style.min")
319
+
320
+ @config.assets.compile = true
321
+ @config.assets.debug = true
322
+ assert_match %r{<link href="/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />},
323
+ stylesheet_link_tag('/stylesheets/application')
324
+
325
+ assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />},
326
+ stylesheet_link_tag(:application)
327
+
328
+ assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="print" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="print" rel="stylesheet" type="text/css" />},
329
+ stylesheet_link_tag(:application, :media => "print")
330
+ end
331
+
332
+ test "alternate asset prefix" do
333
+ stubs(:asset_prefix).returns("/themes/test")
334
+ assert_match %r{/themes/test/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
335
+ end
336
+
337
+ test "alternate asset environment" do
338
+ assets = Sprockets::Environment.new
339
+ assets.append_path(FIXTURES.join("alternate/stylesheets"))
340
+ stubs(:asset_environment).returns(assets)
341
+ assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
342
+ end
343
+
344
+ test "alternate hash based on environment" do
345
+ assets = Sprockets::Environment.new
346
+ assets.version = 'development'
347
+ assets.append_path(FIXTURES.join("alternate/stylesheets"))
348
+ stubs(:asset_environment).returns(assets)
349
+ dev_path = asset_path("style", :ext => "css")
350
+
351
+ assets.version = 'production'
352
+ prod_path = asset_path("style", :ext => "css")
353
+
354
+ assert_not_equal prod_path, dev_path
355
+ end
356
+
357
+ test "precedence of `config.digest = false` over manifest.yml asset digests" do
358
+ Rails.application.config.assets.digest_files = {'logo.png' => 'logo-d1g3st.png'}
359
+ @config.assets.digest = false
360
+
361
+ assert_equal '/assets/logo.png',
362
+ asset_path("logo.png")
363
+ end
364
+ end
@@ -0,0 +1,58 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/sprockets_helpers_abstract_unit")
2
+ require 'sprockets'
3
+ require 'sprockets/helpers/rails_helper'
4
+ require 'turbo-sprockets/sprockets_overrides/helpers/rails_helper'
5
+ require 'mocha'
6
+
7
+ class SprocketsHelperWithRoutesTest < ActionView::TestCase
8
+ include Sprockets::Helpers::RailsHelper
9
+
10
+ # Let's bring in some named routes to test namespace conflicts with potential *_paths.
11
+ # We have to do this after we bring in the Sprockets RailsHelper so if there are conflicts,
12
+ # they'll fail in the way we expect in a real live Rails app.
13
+ routes = ActionDispatch::Routing::RouteSet.new
14
+ routes.draw do
15
+ resources :assets
16
+ end
17
+ include routes.url_helpers
18
+
19
+ def setup
20
+ super
21
+ @controller = BasicController.new
22
+
23
+ @assets = Sprockets::Environment.new
24
+ @assets.append_path(FIXTURES.join("app/javascripts"))
25
+ @assets.append_path(FIXTURES.join("app/stylesheets"))
26
+ @assets.append_path(FIXTURES.join("app/images"))
27
+
28
+ application = Struct.new(:config, :assets).new(config, @assets)
29
+ Rails.stubs(:application).returns(application)
30
+ @config = config
31
+ @config.perform_caching = true
32
+ @config.assets.digest = true
33
+ @config.assets.compile = true
34
+ end
35
+
36
+ test "namespace conflicts on a named route called asset_path" do
37
+ # Testing this for sanity - asset_path is now a named route!
38
+ assert_match asset_path('test_asset'), '/assets/test_asset'
39
+
40
+ assert_match %r{/assets/logo-[0-9a-f]+.png},
41
+ path_to_asset("logo.png")
42
+ assert_match %r{/assets/logo-[0-9a-f]+.png},
43
+ path_to_asset("logo.png", :digest => true)
44
+ assert_match %r{/assets/logo.png},
45
+ path_to_asset("logo.png", :digest => false)
46
+ end
47
+
48
+ test "javascript_include_tag with a named_route named asset_path" do
49
+ assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>},
50
+ javascript_include_tag(:application)
51
+ end
52
+
53
+ test "stylesheet_link_tag with a named_route named asset_path" do
54
+ assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
55
+ stylesheet_link_tag(:application)
56
+ end
57
+
58
+ end