forked from rails/sprockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_asset.rb
1315 lines (1041 loc) · 41.8 KB
/
test_asset.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require "sprockets_test"
module AssetTests
def self.test(name, &block)
define_method("test_#{name.inspect}", &block)
end
test "id is a SHA256 String" do
assert_kind_of String, @asset.id
assert_match(/^[0-9a-f]{64}$/, @asset.id)
end
test "uri can find itself" do
# assert_kind_of URI, @asset.uri
assert_equal @asset, @env.load(@asset.uri)
end
test "length is source length" do
assert_equal @asset.to_s.length, @asset.length
end
test "bytesize is source bytesize" do
assert_equal @asset.to_s.bytesize, @asset.bytesize
end
test "links are a Set" do
assert_kind_of Set, @asset.links
end
test "write to file" do
target = fixture_path('asset/tmp.js')
begin
@asset.write_to(target)
assert File.exist?(target)
ensure
FileUtils.rm(target) if File.exist?(target)
assert !File.exist?(target)
end
end
test "write to gzipped file" do
target = fixture_path('asset/tmp.js.gz')
begin
@asset.write_to(target)
assert File.exist?(target)
ensure
FileUtils.rm(target) if File.exist?(target)
assert !File.exist?(target)
end
end
end
module FreshnessTests
def self.test(name, &block)
define_method("test_#{name.inspect}", &block)
end
test "modify asset contents" do
filename = fixture_path('asset/test.js')
sandbox filename do
write(filename, "a;")
asset = asset('test.js')
old_digest = asset.hexdigest
old_uri = asset.uri
assert_equal "a;\n", asset.to_s
write(filename, "b;")
asset = asset('test.js')
refute_equal old_digest, asset.hexdigest
refute_equal old_uri, asset.uri
assert_equal "b;\n", asset.to_s
end
end
test "remove asset" do
filename = fixture_path('asset/test.js')
sandbox filename do
write(filename, "a;")
asset('test.js')
File.unlink(filename)
refute asset('test.js')
end
end
test "modify asset's dependency file" do
main = fixture_path('asset/test-main.js.erb')
dep = fixture_path('asset/test-dep.js')
sandbox main, dep do
write(main, "//= depend_on test-dep\n<%= File.read('#{dep}') %>")
write(dep, "a;")
asset = asset('test-main.js')
old_digest = asset.hexdigest
old_uri = asset.uri
assert_equal "a;\n", asset.to_s
write(dep, "b;")
asset = asset('test-main.js')
refute_equal old_digest, asset.hexdigest
refute_equal old_uri, asset.uri
assert_equal "b;\n", asset.to_s
end
end
test "remove asset's dependency file" do
main = fixture_path('asset/test-main.js')
dep = fixture_path('asset/test-dep.js')
sandbox main, dep do
write(main, "//= depend_on test-dep\n")
write(dep, "a;")
asset('test-main.js')
File.unlink(dep)
assert_raises(Sprockets::FileNotFound) do
asset('test-main.js')
end
end
end
end
class TextStaticAssetTest < Sprockets::TestCase
def setup
@env = Sprockets::Environment.new
@env.append_path(fixture_path('asset'))
@env.cache = {}
@asset = @env['log.txt']
end
include AssetTests
test "uri" do
assert_equal "file://#{fixture_path_for_uri('asset/log.txt')}?type=text/plain&id=xxx",
normalize_uri(@asset.uri)
end
test "logical path" do
assert_equal "log.txt", @asset.logical_path
end
test "digest path" do
assert_equal "log-66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18.txt",
@asset.digest_path
end
test "content type" do
assert_equal "text/plain", @asset.content_type
end
test "charset is UTF-8" do
assert_equal 'utf-8', @asset.charset
end
test "length" do
assert_equal 6, @asset.length
end
test "bytesize" do
assert_equal 6, @asset.bytesize
end
end
class BinaryStaticAssetTest < Sprockets::TestCase
def setup
@env = Sprockets::Environment.new
@env.append_path(fixture_path('asset'))
@env.cache = {}
@asset = @env['POW.png']
end
include AssetTests
test "uri" do
assert_equal "file://#{fixture_path_for_uri('asset/POW.png')}?type=image/png&id=xxx",
normalize_uri(@asset.uri)
end
test "logical path" do
assert_equal "POW.png", @asset.logical_path
end
test "digest path" do
assert_equal "POW-1da2e59df75d33d8b74c3d71feede698f203f136512cbaab20c68a5bdebd5800.png",
@asset.digest_path
end
test "content type" do
assert_equal "image/png", @asset.content_type
end
test "charset is nil" do
assert_nil @asset.charset
end
test "length" do
assert_equal 42917, @asset.length
end
test "bytesize" do
assert_equal 42917, @asset.bytesize
end
test "source digest" do
assert_equal [29, 162, 229, 157, 247, 93, 51, 216, 183, 76, 61, 113, 254, 237, 230, 152, 242, 3, 241, 54, 81, 44, 186, 171, 32, 198, 138, 91, 222, 189, 88, 0], @asset.digest.bytes.to_a
end
test "source hexdigest" do
assert_equal "1da2e59df75d33d8b74c3d71feede698f203f136512cbaab20c68a5bdebd5800", @asset.hexdigest
end
test "source base64digest" do
assert_equal "HaLlnfddM9i3TD1x/u3mmPID8TZRLLqrIMaKW969WAA=", @asset.base64digest
end
test "integrity" do
assert_equal "sha256-HaLlnfddM9i3TD1x/u3mmPID8TZRLLqrIMaKW969WAA=", @asset.integrity
end
test "asset is fresh if its mtime is changed but its contents is the same" do
filename = fixture_path('asset/test-POW.png')
sandbox filename do
File.open(filename, 'w') { |f| f.write "a" }
asset = @env['test-POW.png']
assert asset
old_digest = asset.hexdigest
old_uri = asset.uri
File.open(filename, 'w') { |f| f.write "a" }
mtime = Time.now + 1
File.utime(mtime, mtime, filename)
assert_equal old_digest, @env['test-POW.png'].hexdigest
assert_equal old_uri, @env['test-POW.png'].uri
end
end
test "asset is stale when its contents has changed" do
filename = fixture_path('asset/POW.png')
sandbox filename do
File.open(filename, 'w') { |f| f.write "a" }
asset = @env['POW.png']
assert asset
old_digest = asset.hexdigest
old_uri = asset.uri
File.open(filename, 'w') { |f| f.write "b" }
mtime = Time.now + 1
File.utime(mtime, mtime, filename)
refute_equal old_digest, @env['POW.png'].hexdigest
refute_equal old_uri, @env['POW.png'].uri
end
end
test "asset is fresh if the file is removed" do
filename = fixture_path('asset/POW.png')
sandbox filename do
File.open(filename, 'w') { |f| f.write "a" }
asset = @env['POW.png']
assert asset
File.unlink(filename)
refute @env['POW.png']
end
end
end
class SourceAssetTest < Sprockets::TestCase
def setup
@env = Sprockets::Environment.new
@env.append_path(fixture_path('asset'))
@env.cache = {}
@pipeline = :source
@asset = @env.find_asset('application.js', pipeline: :source)
end
include AssetTests
test "uri" do
assert_equal "file://#{fixture_path_for_uri('asset/application.js')}?type=application/javascript&pipeline=source&id=xxx",
normalize_uri(@asset.uri)
end
test "logical path" do
assert_equal "application.source.js", @asset.logical_path
end
test "digest path" do
assert_equal "application.source-6ae801e02813bf209a84a89b8c5b5edf5eb770ca9e4253c56834c08a2fc5dbea.js",
@asset.digest_path
end
test "content type" do
assert_equal "application/javascript", @asset.content_type
end
test "length" do
assert_equal 109, @asset.length
end
test "source digest" do
assert_equal [106, 232, 1, 224, 40, 19, 191, 32, 154, 132, 168, 155, 140, 91, 94, 223, 94, 183, 112, 202, 158, 66, 83, 197, 104, 52, 192, 138, 47, 197, 219, 234], @asset.digest.bytes.to_a
end
test "source hexdigest" do
assert_equal "6ae801e02813bf209a84a89b8c5b5edf5eb770ca9e4253c56834c08a2fc5dbea", @asset.hexdigest
end
test "source base64digest" do
assert_equal "augB4CgTvyCahKibjFte3163cMqeQlPFaDTAii/F2+o=", @asset.base64digest
end
test "integrity" do
assert_equal "sha256-augB4CgTvyCahKibjFte3163cMqeQlPFaDTAii/F2+o=", @asset.integrity
end
test "to_s" do
assert_equal "// =require \"project\"\n// =require \"users\"\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", @asset.to_s
end
def asset(logical_path, options = {})
@env.find_asset(logical_path, **{pipeline: @pipeline}.merge(options))
end
end
class ProcessedAssetTest < Sprockets::TestCase
include FreshnessTests
def setup
@env = Sprockets::Environment.new
@env.append_path(fixture_path('asset'))
@env.cache = {}
@pipeline = :self
@asset = @env.find_asset('application.js', pipeline: :self)
end
include AssetTests
test "uri" do
assert_equal "file://#{fixture_path_for_uri('asset/application.js')}?type=application/javascript&pipeline=self&id=xxx",
normalize_uri(@asset.uri)
end
test "logical path" do
assert_equal "application.self.js", @asset.logical_path
end
test "digest path" do
assert_equal "application.self-6a5fff89e8328f158e77642b53e325c24ed844a6bcd5a96ec0f9004384e9c9a5.js",
@asset.digest_path
end
test "content type" do
assert_equal "application/javascript", @asset.content_type
end
test "length" do
assert_equal 69, @asset.length
end
test "source digest" do
assert_equal [106, 95, 255, 137, 232, 50, 143, 21, 142, 119, 100, 43, 83, 227, 37, 194, 78, 216, 68, 166, 188, 213, 169, 110, 192, 249, 0, 67, 132, 233, 201, 165], @asset.digest.bytes.to_a
end
test "source hexdigest" do
assert_equal "6a5fff89e8328f158e77642b53e325c24ed844a6bcd5a96ec0f9004384e9c9a5", @asset.hexdigest
end
test "source base64digest" do
assert_equal "al//iegyjxWOd2QrU+Mlwk7YRKa81aluwPkAQ4TpyaU=", @asset.base64digest
end
test "integrity" do
assert_equal "sha256-al//iegyjxWOd2QrU+Mlwk7YRKa81aluwPkAQ4TpyaU=", @asset.integrity
end
test "charset is UTF-8" do
assert_equal 'utf-8', @asset.charset
end
test "to_s" do
assert_equal "\n\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", @asset.to_s
end
test "each" do
body = +""
@asset.each { |part| body << part }
assert_equal "\n\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", body
end
def asset(logical_path, options = {})
@env.find_asset(logical_path, **{pipeline: @pipeline}.merge(options))
end
end
class BundledAssetTest < Sprockets::TestCase
include FreshnessTests
def setup
@env = Sprockets::Environment.new
@env.append_path(fixture_path('asset'))
@env.cache = {}
@pipeline = nil
@asset = @env['application.js']
end
include AssetTests
test "uri" do
assert_equal "file://#{fixture_path_for_uri('asset/application.js')}?type=application/javascript&id=xxx",
normalize_uri(@asset.uri)
end
test "logical path" do
assert_equal "application.js", @asset.logical_path
end
test "digest path" do
assert_equal "application-955b2dddd0d1449b1c617124b83b46300edadec06d561104f7f6165241b31a94.js",
@asset.digest_path
end
test "content type" do
assert_equal "application/javascript", @asset.content_type
end
test "length" do
assert_equal 159, @asset.length
end
test "source digest" do
assert_equal [149, 91, 45, 221, 208, 209, 68, 155, 28, 97, 113, 36, 184, 59, 70, 48, 14, 218, 222, 192, 109, 86, 17, 4, 247, 246, 22, 82, 65, 179, 26, 148], @asset.digest.bytes.to_a
end
test "source hexdigest" do
assert_equal "955b2dddd0d1449b1c617124b83b46300edadec06d561104f7f6165241b31a94", @asset.hexdigest
end
test "source base64digest" do
assert_equal "lVst3dDRRJscYXEkuDtGMA7a3sBtVhEE9/YWUkGzGpQ=", @asset.base64digest
end
test "integrity" do
assert_equal "sha256-lVst3dDRRJscYXEkuDtGMA7a3sBtVhEE9/YWUkGzGpQ=", @asset.integrity
end
test "charset is UTF-8" do
assert_equal 'utf-8', @asset.charset
end
test "to_s" do
assert_equal "var Project = {\n find: function(id) {\n }\n};\nvar Users = {\n find: function(id) {\n }\n};\n\n\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", @asset.to_s
end
test "each" do
body = +""
@asset.each { |part| body << part }
assert_equal "var Project = {\n find: function(id) {\n }\n};\nvar Users = {\n find: function(id) {\n }\n};\n\n\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", body
end
test "asset is stale when one of its source files is modified" do
main = fixture_path('asset/test-main.js')
dep = fixture_path('asset/test-dep.js')
sandbox main, dep do
File.open(main, 'w') { |f| f.write "//= require test-dep\n" }
File.open(dep, 'w') { |f| f.write "a;" }
asset = asset('test-main.js')
old_digest = asset.hexdigest
old_uri = asset.uri
File.open(dep, 'w') { |f| f.write "b;" }
mtime = Time.now + 1
File.utime(mtime, mtime, dep)
refute_equal old_digest, asset('test-main.js').hexdigest
refute_equal old_uri, asset('test-main.js').uri
end
end
test "asset is stale when one of its asset dependencies is modified" do
main = fixture_path('asset/test-main.js')
dep = fixture_path('asset/test-dep.js')
sandbox main, dep do
File.open(main, 'w') { |f| f.write "//= depend_on_asset test-dep\n" }
File.open(dep, 'w') { |f| f.write "a;" }
asset = asset('test-main.js')
old_digest = asset.hexdigest
old_uri = asset.uri
File.open(dep, 'w') { |f| f.write "b;" }
mtime = Time.now + 1
File.utime(mtime, mtime, dep)
asset = asset('test-main.js')
assert_equal old_digest, asset.hexdigest
refute_equal old_uri, asset.uri
end
end
test "asset is stale when one of its source files dependencies is modified" do
a = fixture_path('asset/test-a.js')
b = fixture_path('asset/test-b.js')
c = fixture_path('asset/test-c.js')
sandbox a, b, c do
File.open(a, 'w') { |f| f.write "//= require test-b\n" }
File.open(b, 'w') { |f| f.write "//= require test-c\n" }
File.open(c, 'w') { |f| f.write "c;" }
asset_a = asset('test-a.js')
asset_b = asset('test-b.js')
asset_c = asset('test-c.js')
old_asset_a_digest = asset_a.hexdigest
old_asset_b_digest = asset_b.hexdigest
old_asset_c_digest = asset_c.hexdigest
old_asset_a_uri = asset_a.uri
old_asset_b_uri = asset_b.uri
old_asset_c_uri = asset_c.uri
File.open(c, 'w') { |f| f.write "x;" }
mtime = Time.now + 1
File.utime(mtime, mtime, c)
refute_equal old_asset_a_digest, asset('test-a.js').hexdigest
refute_equal old_asset_b_digest, asset('test-b.js').hexdigest
refute_equal old_asset_c_digest, asset('test-c.js').hexdigest
refute_equal old_asset_a_uri, asset('test-a.js').uri
refute_equal old_asset_b_uri, asset('test-b.js').uri
refute_equal old_asset_c_uri, asset('test-c.js').uri
end
end
test "asset is stale when one of its dependency dependencies is modified" do
a = fixture_path('asset/test-a.js')
b = fixture_path('asset/test-b.js')
c = fixture_path('asset/test-c.js')
sandbox a, b, c do
File.open(a, 'w') { |f| f.write "//= require test-b\n" }
File.open(b, 'w') { |f| f.write "//= depend_on test-c\n" }
File.open(c, 'w') { |f| f.write "c;" }
asset_a = asset('test-a.js')
asset_b = asset('test-b.js')
asset_c = asset('test-c.js')
old_asset_a_uri = asset_a.uri
old_asset_b_uri = asset_b.uri
old_asset_c_uri = asset_c.uri
File.open(c, 'w') { |f| f.write "x;" }
mtime = Time.now + 1
File.utime(mtime, mtime, c)
refute_equal old_asset_a_uri, asset('test-a.js').uri
refute_equal old_asset_b_uri, asset('test-b.js').uri
refute_equal old_asset_c_uri, asset('test-c.js').uri
end
end
test "asset is stale when one of its asset dependency dependencies is modified" do
a = fixture_path('asset/test-a.js')
b = fixture_path('asset/test-b.js')
c = fixture_path('asset/test-c.js')
sandbox a, b, c do
File.open(a, 'w') { |f| f.write "//= depend_on_asset test-b\n" }
File.open(b, 'w') { |f| f.write "//= depend_on_asset test-c\n" }
File.open(c, 'w') { |f| f.write "c;" }
asset_a = asset('test-a.js')
asset_b = asset('test-b.js')
asset_c = asset('test-c.js')
old_asset_a_uri = asset_a.uri
old_asset_b_uri = asset_b.uri
old_asset_c_uri = asset_c.uri
File.open(c, 'w') { |f| f.write "x;" }
mtime = Time.now + 1
File.utime(mtime, mtime, c)
refute_equal old_asset_a_uri, asset('test-a.js').uri
refute_equal old_asset_b_uri, asset('test-b.js').uri
refute_equal old_asset_c_uri, asset('test-c.js').uri
end
end
test "asset is stale when one of its linked assets is modified" do
a = fixture_path('asset/test-a.js')
b = fixture_path('asset/test-b.js')
sandbox a, b do
File.open(a, 'w') { |f| f.write "//= link test-b\n" }
File.open(b, 'w') { |f| f.write "b;" }
asset_a = asset('test-a.js')
asset_b = asset('test-b.js')
old_asset_a_uri = asset_a.uri
old_asset_b_uri = asset_b.uri
File.open(b, 'w') { |f| f.write "x;" }
mtime = Time.now + 1
File.utime(mtime, mtime, b)
refute_equal old_asset_a_uri, asset('test-a.js').uri
refute_equal old_asset_b_uri, asset('test-b.js').uri
end
end
test "erb asset is stale when one of its linked assets is modified" do
a = fixture_path('asset/test-a.js.erb')
b = fixture_path('asset/test-b.js.erb')
sandbox a, b do
File.open(a, 'w') { |f| f.write "<% link_asset 'test-b' %>\n" }
File.open(b, 'w') { |f| f.write "b;" }
asset_a = asset('test-a.js')
asset_b = asset('test-b.js')
old_asset_a_uri = asset_a.uri
old_asset_b_uri = asset_b.uri
File.open(b, 'w') { |f| f.write "x;" }
mtime = Time.now + 1
File.utime(mtime, mtime, b)
refute_equal old_asset_a_uri, asset('test-a.js').uri
refute_equal old_asset_b_uri, asset('test-b.js').uri
end
end
test "asset is stale if a file is added to its require directory" do
asset = asset("tree/all_with_require_directory.js")
assert asset
old_uri = asset.uri
dirname = File.join(fixture_path("asset"), "tree/all")
filename = File.join(dirname, "z.js")
sandbox filename do
File.open(filename, 'w') { |f| f.write "z" }
mtime = Time.now + 1
File.utime(mtime, mtime, dirname)
refute_equal old_uri, asset("tree/all_with_require_directory.js").uri
end
end
test "asset is stale if a file is added to its require tree" do
asset = asset("tree/all_with_require_tree.js")
assert asset
old_uri = asset.uri
dirname = File.join(fixture_path("asset"), "tree/all/b/c")
filename = File.join(dirname, "z.js")
sandbox filename do
File.open(filename, 'w') { |f| f.write "z" }
mtime = Time.now + 1
File.utime(mtime, mtime, dirname)
refute_equal old_uri, asset("tree/all_with_require_tree.js").uri
end
end
test "asset is stale if its declared dependency changes" do
sprite = fixture_path('asset/sprite.css.erb')
image = fixture_path('asset/POW.png')
sandbox sprite, image do
asset = asset('sprite.css')
assert asset
old_uri = asset.uri
File.open(image, 'w') { |f| f.write "(change)" }
mtime = Time.now + 1
File.utime(mtime, mtime, image)
refute_equal old_uri, asset('sprite.css').uri
end
end
test "asset if stale if once of its source files is removed" do
main = fixture_path('asset/test-main.js')
dep = fixture_path('asset/test-dep.js')
sandbox main, dep do
File.open(main, 'w') { |f| f.write "//= require test-dep\n" }
File.open(dep, 'w') { |f| f.write "a;" }
assert asset('test-main.js')
File.unlink(dep)
assert_raises(Sprockets::FileNotFound) do
asset('test-main.js')
end
end
end
test "asset is stale when one of its stubbed targets dependencies are modified" do
frameworks = fixture_path('asset/stub-frameworks.js')
app = fixture_path('asset/stub-app.js')
jquery = fixture_path('asset/stub-jquery.js')
sandbox frameworks, app, jquery do
write(frameworks, "frameworks = {};")
write(app, "//= stub stub-frameworks\n//= require stub-jquery\napp = {};")
write(jquery, "jquery = {};")
asset_jquery = asset('stub-jquery.js', pipeline: :self)
old_asset_frameworks_uri = asset('stub-frameworks.js').uri
old_asset_app_uri = asset('stub-app.js').uri
write(frameworks, "//= require stub-jquery\nframeworks = {};")
# jquery never changed
assert_equal asset_jquery.uri, asset('stub-jquery.js', pipeline: :self).uri
refute_equal old_asset_frameworks_uri, asset('stub-frameworks.js').uri
refute_equal old_asset_app_uri, asset('stub-app.js').uri
end
end
test "requiring the same file multiple times has no effect" do
assert_equal read("asset/project.js.erb")+"\n\n\n", asset("multiple.js").to_s
end
test "requiring index file directly and by alias includes it only once" do
assert_equal "alert(1);\n\n\n", asset("index_alias/require.js").to_s
end
test "requiring index file by tree and by alias includes it only once" do
assert_equal "alert(1);\n", asset("index_alias/require_tree.js").to_s
end
test "requiring a file of a different format raises an exception" do
assert_raises Sprockets::FileNotFound do
asset("mismatch.js")
end
end
test "bundling joins files with blank line" do
assert_equal "var Project = {\n find: function(id) {\n }\n};\nvar Users = {\n find: function(id) {\n }\n};\n\n\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n",
asset("application.js").to_s
end
test "dependencies appear in the source before files that required them" do
assert_match(/Project.+Users.+focus/m, asset("application.js").to_s)
end
test "processing a source file with no engine extensions" do
assert_equal read("asset/users.js.erb"), asset("noengine.js").to_s
end
test "processing a source file with different content type extensions" do
assert_equal read("asset/users.js.erb"), asset("es6_asset.js").to_s
end
test "processing a source file with different content type extensions 1" do
assert_equal read("asset/users.js.erb") + "(function() {\n\n\n}).call(this);\n", asset("coffee_asset.js").to_s
end
test "processing a source file with unknown extensions" do
assert_equal read("asset/users.js.erb") + "var jQuery;\n\n\n", asset("unknownexts.min.js").to_s
end
test "requiring a file with a relative path" do
assert_equal read("asset/project.js.erb") + "\n",
asset("relative/require.js").to_s
end
test "can't require files outside the load path" do
assert [email protected]?(fixture_path("default")), @env.paths.inspect
assert_raises Sprockets::FileNotFound do
asset("relative/require_outside_path.js")
end
end
test "can't require files in another load path" do
@env.append_path(fixture_path("default"))
assert @env.paths.include?(fixture_path("default")), @env.paths.inspect
assert_raises Sprockets::FileNotFound do
asset("relative/require_other_load_path.js")
end
end
test "can't require absolute files outside the load path" do
assert_raises Sprockets::FileOutsidePaths do
asset("absolute/require_outside_path.js").to_s
end
end
test "require_directory requires all child files in alphabetical order" do
assert_equal(
"ok(\"b.js.erb\");\n",
asset("tree/all_with_require_directory.js").to_s
)
end
test "require_directory current directory includes self last" do
assert_equal(
"var Bar;\nvar Foo;\nvar App;\n",
asset("tree/directory/application.js").to_s
)
end
test "require_tree requires all descendant files in alphabetical order" do
assert_equal(
asset("tree/all_with_require.js").to_s,
asset("tree/all_with_require_tree.js").to_s + "\n\n\n\n\n\n"
)
end
test "require_tree without an argument defaults to the current directory" do
assert_equal(
"a();\nb();\n",
asset("tree/without_argument/require_tree_without_argument.js").to_s
)
end
test "require_tree with current directory includes self last" do
assert_equal(
"var Bar;\nvar Foo;\nvar App;\n",
asset("tree/tree/application.js").to_s
)
end
test "require_tree with a logical path argument raises an exception" do
assert_raises(Sprockets::ArgumentError) do
asset("tree/with_logical_path/require_tree_with_logical_path.js").to_s
end
end
test "require_tree with a nonexistent path raises an exception" do
assert_raises(Sprockets::ArgumentError) do
asset("tree/with_logical_path/require_tree_with_nonexistent_path.js").to_s
end
end
test "require_tree with a nonexistent absolute path raises an exception" do
assert_raises(Sprockets::ArgumentError) do
asset("absolute/require_nonexistent_path.js").to_s
end
end
test "require_tree respects order of child dependencies" do
assert_equal(
"var c;\nvar b;\nvar a;\n\n",
asset("tree/require_tree_alpha.js").to_s
)
end
test "require_self inserts the current file's body at the specified point" do
assert_equal "/* b.css */\nb { display: none }\n/*\n\n\n\n */\n\nbody {}\n.project {}\n", asset("require_self.css").to_s
end
test "multiple require_self directives raises and error" do
assert_raises(Sprockets::ArgumentError) do
asset("require_self_twice.css")
end
end
test "linked asset depends on target asset" do
assert asset = asset("require_manifest.js")
assert_equal <<-EOS, asset.to_s
define("application.js", "application-955b2dddd0d1449b1c617124b83b46300edadec06d561104f7f6165241b31a94.js")
define("application.css", "application-46d50149c56fc370805f53c29f79b89a52d4cc479eeebcdc8db84ab116d7ab1a.css")
define("POW.png", "POW-1da2e59df75d33d8b74c3d71feede698f203f136512cbaab20c68a5bdebd5800.png");
EOS
assert_equal [
"file://#{fixture_path_for_uri("asset/POW.png")}?type=image/png&id=xxx",
"file://#{fixture_path_for_uri("asset/application.css")}?type=text/css&id=xxx",
"file://#{fixture_path_for_uri("asset/application.js")}?type=application/javascript&id=xxx"
], normalize_uris(asset.links)
end
test "directive linked asset depends on target asset" do
assert asset = asset("require_manifest2.js")
assert_equal <<-EOS, asset.to_s
define("application.js", "application-955b2dddd0d1449b1c617124b83b46300edadec06d561104f7f6165241b31a94.js")
define("application.css", "application-46d50149c56fc370805f53c29f79b89a52d4cc479eeebcdc8db84ab116d7ab1a.css")
define("POW.png", "POW-1da2e59df75d33d8b74c3d71feede698f203f136512cbaab20c68a5bdebd5800.png");
EOS
assert_equal [
"file://#{fixture_path_for_uri("asset/POW.png")}?type=image/png&id=xxx",
"file://#{fixture_path_for_uri("asset/application.css")}?type=text/css&id=xxx",
"file://#{fixture_path_for_uri("asset/application.js")}?type=application/javascript&id=xxx"
], normalize_uris(asset.links)
end
test "link_directory current directory includes self last" do
assert_equal [
"file://#{fixture_path_for_uri("asset/link/directory/bar.js")}?type=application/javascript&id=xxx",
"file://#{fixture_path_for_uri("asset/link/directory/foo.js")}?type=application/javascript&id=xxx"
], normalize_uris(asset("link/directory/application.js").links)
end
test "link_tree requires all descendant files in alphabetical order" do
assert_equal normalize_uris(asset("link/all_with_require.js").links),
normalize_uris(asset("link/all_with_require_tree.js").links)
end
test "link_tree without an argument defaults to the current directory" do
assert_equal [
"file://#{fixture_path_for_uri("asset/link/without_argument/a.js")}?type=application/javascript&id=xxx",
"file://#{fixture_path_for_uri("asset/link/without_argument/b.js")}?type=application/javascript&id=xxx"
], normalize_uris(asset("link/without_argument/require_tree_without_argument.js").links)
end
test "link_tree with current directory includes self last" do
assert_equal [
"file://#{fixture_path_for_uri("asset/link/tree/bar.js")}?type=application/javascript&id=xxx",
"file://#{fixture_path_for_uri("asset/link/tree/foo.js")}?type=application/javascript&id=xxx"
], normalize_uris(asset("link/tree/application.js").links)
end
test "link_tree with a logical path argument raises an exception" do
assert_raises(Sprockets::ArgumentError) do
asset("link/with_logical_path/require_tree_with_logical_path.js")
end
end
test "link_tree with a nonexistent path raises an exception" do
assert_raises(Sprockets::ArgumentError) do
asset("link/with_logical_path/require_tree_with_nonexistent_path.js")
end
end
test "link_directory requires all child files in alphabetical order" do
assert_equal [
"file://#{fixture_path_for_uri("asset/link/all/README.md")}?id=xxx",
"file://#{fixture_path_for_uri("asset/link/all/b.css")}?type=text/css&id=xxx",
"file://#{fixture_path_for_uri("asset/link/all/b.js.erb")}?type=application/javascript+ruby&id=xxx"
], normalize_uris(asset("link/all_with_require_directory.js").links)
end
test "link_directory as app/js requires all child files in alphabetical order" do
assert_equal [
"file://#{fixture_path_for_uri("asset/link/all/b.js.erb")}?type=application/javascript&id=xxx"
], normalize_uris(asset("link/all_with_require_directory_as_js.js").links)
end
test "link_tree respects order of child dependencies" do
assert_equal [
"file://#{fixture_path_for_uri("asset/link/alpha/a.js")}?type=application/javascript&id=xxx",
"file://#{fixture_path_for_uri("asset/link/alpha/b.js")}?type=application/javascript&id=xxx",
"file://#{fixture_path_for_uri("asset/link/alpha/c.js")}?type=application/javascript&id=xxx"
], normalize_uris(asset("link/require_tree_alpha.js").links)
end
test "link_tree as app/js respects order of child dependencies" do
assert_equal [
"file://#{fixture_path_for_uri("asset/link/alpha/a.js")}?type=application/javascript&id=xxx",
"file://#{fixture_path_for_uri("asset/link/alpha/b.js")}?type=application/javascript&id=xxx",
"file://#{fixture_path_for_uri("asset/link/alpha/c.js")}?type=application/javascript&id=xxx"
], normalize_uris(asset("link/require_tree_alpha_as_js.js").links)
end
test "link_asset with uri" do
assert asset = asset("link/asset_uri.css")
assert_equal <<-EOS, asset.to_s
.logo {
background: url(POW-1da2e59df75d33d8b74c3d71feede698f203f136512cbaab20c68a5bdebd5800.png);
}
EOS
assert_equal [
"file://#{fixture_path_for_uri("asset/POW.png")}?type=image/png&id=xxx"
], normalize_uris(asset.links)
end
test "stub single dependency" do
assert_equal "var jQuery.UI = {};\n\n\n", asset("stub/skip_jquery").to_s
end
test "stub dependency tree" do