forked from rails/sprockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_erb_processor.rb
164 lines (136 loc) · 4.59 KB
/
test_erb_processor.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
# frozen_string_literal: true
require 'minitest/autorun'
require 'sprockets'
require 'sprockets/cache'
require 'sprockets/erb_processor'
require 'sass'
class TestERBProcessor < MiniTest::Test
def uri_path(path)
path = '/' + path if path[1] == ':' # Windows path / drive letter
path
end
def test_compile_js_erb_template
environment = Sprockets::Environment.new
input = {
environment: environment,
filename: "foo.js.erb",
content_type: 'application/javascript',
data: "var data = <%= JSON.generate({foo: true}) %>;",
metadata: {},
cache: Sprockets::Cache.new
}
output = "var data = {\"foo\":true};"
assert_equal output, Sprockets::ERBProcessor.call(input)[:data]
end
def test_compile_erb_template_that_depends_on_env
old_env_value = ::ENV['ERB_ENV_TEST_VALUE']
::ENV['ERB_ENV_TEST_VALUE'] = 'success'
root = File.expand_path("../fixtures", __FILE__)
environment = Sprockets::Environment.new(root)
environment.append_path 'default'
input = {
environment: environment,
filename: "foo.js.erb",
content_type: 'application/javascript',
data: "<%= ENV['ERB_ENV_TEST_VALUE'] %>;",
metadata: {},
cache: Sprockets::Cache.new
}
output = "success;"
result = Sprockets::ERBProcessor.call(input)
assert_equal output, result[:data]
assert_equal "env:ERB_ENV_TEST_VALUE", result[:dependencies].first
ensure
::ENV['ERB_ENV_TEST_VALUE'] = old_env_value
end
def test_compile_erb_template_that_depends_on_empty_env
old_env_value = ::ENV.delete('ERB_ENV_TEST_VALUE')
root = File.expand_path("../fixtures", __FILE__)
environment = Sprockets::Environment.new(root)
environment.append_path 'default'
input = {
environment: environment,
filename: "foo.js.erb",
content_type: 'application/javascript',
data: "<%= ENV['ERB_ENV_TEST_VALUE'] %>;",
metadata: {},
cache: Sprockets::Cache.new
}
output = ";"
result = Sprockets::ERBProcessor.call(input)
assert_equal output, result[:data]
assert_equal "env:ERB_ENV_TEST_VALUE", result[:dependencies].first
ensure
::ENV['ERB_ENV_TEST_VALUE'] = old_env_value
end
def test_compile_erb_template_with_depend_on_call
root = File.expand_path("../fixtures", __FILE__)
environment = Sprockets::Environment.new(root)
environment.append_path 'default'
path = "#{root}/default/gallery.js"
input = {
environment: environment,
filename: "foo.js.erb",
content_type: 'application/javascript',
data: "<%= depend_on('#{path}') %>\nvar data = 'DATA';",
metadata: {},
cache: Sprockets::Cache.new
}
output = "var data = 'DATA';"
result = Sprockets::ERBProcessor.call(input)
assert_equal output, result[:data]
assert_equal "file-digest://#{uri_path(path)}", result[:dependencies].first
end
def test_compile_erb_template_with_depend_on_call_outside_load_paths
root = File.expand_path("../fixtures", __FILE__)
environment = Sprockets::Environment.new(root)
environment.append_path 'default'
path = "#{root}/asset/application.js"
assert File.exist?(path)
input = {
environment: environment,
filename: "foo.js.erb",
content_type: 'application/javascript',
data: "<%= depend_on('#{path}') %>\nvar data = 'DATA';",
metadata: {},
cache: Sprockets::Cache.new
}
output = "var data = 'DATA';"
result = Sprockets::ERBProcessor.call(input)
assert_equal output, result[:data]
assert_equal "file-digest://#{uri_path(path)}", result[:dependencies].first
end
def test_pass_custom_erb_helpers_to_template
environment = Sprockets::Environment.new
template = Sprockets::ERBProcessor.new do
def foo
:bar
end
end
input = {
environment: environment,
filename: "foo.js.erb",
content_type: 'application/javascript',
data: "var foo = <%= foo %>;",
metadata: {},
cache: Sprockets::Cache.new
}
output = "var foo = bar;"
assert_equal output, template.call(input)[:data]
end
def test_compile_js_erb_template_with_top_level_constant_access
environment = Sprockets::Environment.new
Sprockets.const_set(:Sass, Class.new)
input = {
environment: environment,
filename: "foo.js.erb",
content_type: 'application/javascript',
data: "var sass_version = '<%= Sass::VERSION %>';",
metadata: {},
cache: Sprockets::Cache.new
}
assert_match(/sass_version/, Sprockets::ERBProcessor.call(input)[:data])
ensure
Sprockets.send(:remove_const, :Sass)
end
end