-
Notifications
You must be signed in to change notification settings - Fork 791
/
Copy pathtest_exporting.rb
109 lines (84 loc) · 3.55 KB
/
test_exporting.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
require 'sprockets_test'
class TestExporting < Sprockets::TestCase
def setup
@dir = Dir.mktmpdir
FileUtils.mkdir_p(@dir)
@env = Sprockets::Environment.new(".") do |env|
env.append_path(fixture_path('default'))
end
@manifest = Sprockets::Manifest.new(@env, @dir)
end
def teardown
FileUtils.remove_entry_secure(@dir) if File.exist?(@dir)
assert Dir["#{@dir}/*"].empty?
end
test 'extension exporters' do
@env.register_exporter 'application/javascript', ReverseExtensionExporter
@env.register_exporter '*/*', DoubleExtensionExporter
@manifest.compile('application.js')
@manifest.compile('blank.gif')
js_path = @env['application.js'].digest_path
gif_path = @env['blank.gif'].digest_path
reverse_js_path = "#{@dir}/#{js_path[0..-4]}.sj"
double_js_path = "#{@dir}/#{js_path}js"
reverse_gif_path = "#{@dir}/#{gif_path[0..-5]}.fig"
double_gif_path = "#{@dir}/#{gif_path}gif"
assert File.exist?(reverse_js_path), "Expected #{reverse_js_path} to exist, but it didn't"
assert !File.exist?(reverse_gif_path), "Expected #{reverse_gif_path} to not exist, but it didn't"
assert File.exist?(double_js_path), "Expected #{double_js_path} to exist, but it didn't"
assert File.exist?(double_gif_path), "Expected #{double_gif_path} to exist, but it didn't"
end
test 'unregistering exporter without mime type' do
@env.register_exporter '*/*', DoubleExtensionExporter
@env.unregister_exporter DoubleExtensionExporter
@manifest.compile('application.js')
js_path = @env['application.js'].digest_path
double_js_path = "#{@dir}/#{js_path}js"
assert !File.exist?(double_js_path)
end
test 'unregistering exporter with mime type' do
@env.register_exporter 'application/javascript', DoubleExtensionExporter
@env.unregister_exporter 'application/javascript', DoubleExtensionExporter
@manifest.compile('application.js')
js_path = @env['application.js'].digest_path
double_js_path = "#{@dir}/#{js_path}js"
assert !File.exist?(double_js_path)
end
test 'unregistering exporter with multiple mime types' do
@env.register_exporter %w(application/javascript image/gif), ReverseExtensionExporter
@env.unregister_exporter %w(application/javascript image/gif), ReverseExtensionExporter
@manifest.compile('application.js')
@manifest.compile('blank.gif')
js_path = @env['application.js'].digest_path
gif_path = @env['blank.gif'].digest_path
reverse_js_path = "#{@dir}/#{js_path[0..-4]}.sj"
double_js_path = "#{@dir}/#{js_path}js"
reverse_gif_path = "#{@dir}/#{gif_path[0..-5]}.fig"
double_gif_path = "#{@dir}/#{gif_path}gif"
assert !File.exist?(reverse_js_path), "Expected #{ reverse_js_path } to not exist, but it did"
assert !File.exist?(reverse_gif_path), "Expected #{ reverse_gif_path } to not exist, but it did"
assert !File.exist?(double_js_path), "Expected #{ double_js_path } to not exist, but it did"
assert !File.exist?(double_gif_path), "Expected #{ double_gif_path } to not exist, but it did"
end
end
class ReverseExtensionExporter < Sprockets::Exporters::Base
def call
split = target.split('.')
split[1] = split[1].reverse
new_target = split.join('.')
write(new_target) do |f|
f.write(asset.source)
end
return new_target
end
end
class DoubleExtensionExporter < Sprockets::Exporters::Base
def call
split = target.split('.')
new_target = target + split[1]
write(new_target) do |f|
f.write(asset.source)
end
return new_target
end
end