-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathcommands_test.rb
276 lines (233 loc) · 9.51 KB
/
commands_test.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
require "test_helper"
require "minitest/mock"
class Tailwindcss::CommandsTest < ActiveSupport::TestCase
attr_accessor :executable
def setup
super
@executable = Tailwindcss::Ruby.executable
end
test ".compile_command" do
Rails.stub(:root, File) do # Rails.root won't work in this test suite
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
assert_includes(actual, "-i")
assert_includes(actual, "-o")
end
end
test ".compile_command debug flag" do
Rails.stub(:root, File) do # Rails.root won't work in this test suite
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
assert_includes(actual, "--minify")
actual = Tailwindcss::Commands.compile_command(debug: true)
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
refute_includes(actual, "--minify")
end
end
test ".compile_command debug environment variable" do
begin
Rails.stub(:root, File) do # Rails.root won't work in this test suite
ENV["TAILWINDCSS_DEBUG"] = ""
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
assert_includes(actual, "--minify")
actual = Tailwindcss::Commands.compile_command(debug: true)
assert_kind_of(Array, actual)
assert_includes(actual, "--minify")
ENV["TAILWINDCSS_DEBUG"] = "any non-blank value"
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
refute_includes(actual, "--minify")
actual = Tailwindcss::Commands.compile_command(debug: true)
assert_kind_of(Array, actual)
refute_includes(actual, "--minify")
end
ensure
ENV.delete('TAILWINDCSS_DEBUG')
end
end
test ".compile_command when Rails compression is on" do
Rails.stub(:root, File) do # Rails.root won't work in this test suite
Tailwindcss::Commands.stub(:rails_css_compressor?, true) do
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
refute_includes(actual, "--minify")
end
Tailwindcss::Commands.stub(:rails_css_compressor?, false) do
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
assert_includes(actual, "--minify")
end
end
end
test ".compile_command when postcss.config.js exists" do
Dir.mktmpdir do |tmpdir|
Rails.stub(:root, Pathname.new(tmpdir)) do # Rails.root won't work in this test suite
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
refute_includes(actual, "--postcss")
config_file = Rails.root.join("postcss.config.js")
FileUtils.touch(config_file)
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
assert_includes(actual, "--postcss")
postcss_index = actual.index("--postcss")
assert_equal(actual[postcss_index + 1], config_file.to_s)
end
end
end
test ".watch_command" do
Rails.stub(:root, File) do # Rails.root won't work in this test suite
actual = Tailwindcss::Commands.watch_command
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
assert_includes(actual, "-w")
refute_includes(actual, "-p")
assert_includes(actual, "--minify")
actual = Tailwindcss::Commands.watch_command(debug: true)
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
assert_includes(actual, "-w")
refute_includes(actual, "-p")
refute_includes(actual, "--minify")
actual = Tailwindcss::Commands.watch_command(poll: true)
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
assert_includes(actual, "-w")
refute_includes(actual, "always")
assert_includes(actual, "-p")
assert_includes(actual, "--minify")
actual = Tailwindcss::Commands.watch_command(always: true)
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
assert_includes(actual, "-w")
assert_includes(actual, "always")
end
end
test ".engines_tailwindcss_roots when there are no engines" do
Rails.stub(:root, Pathname.new("/dummy")) do
Rails::Engine.stub(:subclasses, []) do
assert_empty Tailwindcss::Commands.engines_tailwindcss_roots
end
end
end
test ".engines_tailwindcss_roots when there are engines" do
Dir.mktmpdir do |tmpdir|
root = Pathname.new(tmpdir)
# Create multiple engines
engine_root1 = root.join('engine1')
engine_root2 = root.join('engine2')
engine_root3 = root.join('engine3')
FileUtils.mkdir_p(engine_root1)
FileUtils.mkdir_p(engine_root2)
FileUtils.mkdir_p(engine_root3)
engine1 = Class.new(Rails::Engine) do
define_singleton_method(:engine_name) { "test_engine1" }
define_singleton_method(:root) { engine_root1 }
end
engine2 = Class.new(Rails::Engine) do
define_singleton_method(:engine_name) { "test_engine2" }
define_singleton_method(:root) { engine_root2 }
end
engine3 = Class.new(Rails::Engine) do
define_singleton_method(:engine_name) { "test_engine3" }
define_singleton_method(:root) { engine_root3 }
end
# Create mock specs for engines
spec1 = Minitest::Mock.new
spec1.expect(:dependencies, [Gem::Dependency.new("tailwindcss-rails")])
spec2 = Minitest::Mock.new
spec2.expect(:dependencies, [Gem::Dependency.new("tailwindcss-rails")])
spec3 = Minitest::Mock.new
spec3.expect(:dependencies, [])
# Set up file structure
# Engine 1: CSS in engine root
engine1_css = engine_root1.join("app/assets/tailwind/test_engine1/application.css")
FileUtils.mkdir_p(File.dirname(engine1_css))
FileUtils.touch(engine1_css)
# Engine 2: CSS in Rails root
engine2_css = root.join("app/assets/tailwind/test_engine2/application.css")
FileUtils.mkdir_p(File.dirname(engine2_css))
FileUtils.touch(engine2_css)
# Engine 3: CsS in engine root, but no tailwindcss-rails dependency
engine3_css = engine_root2.join("app/assets/tailwind/test_engine3/application.css")
FileUtils.mkdir_p(File.dirname(engine3_css))
FileUtils.touch(engine3_css)
find_by_name_results = {
"test_engine1" => spec1,
"test_engine2" => spec2,
"test_engine3" => spec3,
}
Gem::Specification.stub(:find_by_name, ->(name) { find_by_name_results[name] }) do
Rails.stub(:root, root) do
Rails::Engine.stub(:subclasses, [engine1, engine2]) do
roots = Tailwindcss::Commands.engines_tailwindcss_roots
assert_equal 2, roots.size
assert_includes roots, engine1_css.to_s
assert_includes roots, engine2_css.to_s
assert_not_includes roots, engine3_css.to_s
end
end
end
spec1.verify
spec2.verify
end
end
test ".enhance_command when there are no engines" do
Dir.mktmpdir do |tmpdir|
root = Pathname.new(tmpdir)
input_path = root.join("app/assets/tailwind/application.css")
output_path = root.join("app/assets/builds/tailwind.css")
command = ["tailwindcss", "-i", input_path.to_s, "-o", output_path.to_s]
Rails.stub(:root, root) do
Tailwindcss::Commands.stub(:engines_tailwindcss_roots, []) do
Tailwindcss::Commands.enhance_command(command) do |actual|
assert_equal command, actual
end
end
end
end
end
test ".enhance_command when there are engines" do
Dir.mktmpdir do |tmpdir|
root = Pathname.new(tmpdir)
input_path = root.join("app/assets/tailwind/application.css")
output_path = root.join("app/assets/builds/tailwind.css")
# Create necessary files
FileUtils.mkdir_p(File.dirname(input_path))
FileUtils.touch(input_path)
# Create engine CSS file
engine_css_path = root.join("app/assets/tailwind/test_engine/application.css")
FileUtils.mkdir_p(File.dirname(engine_css_path))
FileUtils.touch(engine_css_path)
command = ["tailwindcss", "-i", input_path.to_s, "-o", output_path.to_s]
Rails.stub(:root, root) do
Tailwindcss::Commands.stub(:engines_tailwindcss_roots, [engine_css_path.to_s]) do
Tailwindcss::Commands.enhance_command(command) do |actual|
# Command should be modified to use a temporary file
assert_equal command[0], actual[0] # executable
assert_equal command[1], actual[1] # -i flag
assert_equal command[3], actual[3] # -o flag
assert_equal command[4], actual[4] # output path
temp_path = Pathname.new(actual[2])
refute_equal command[2], temp_path.to_s # input path should be different
assert_match(/tailwind\.css/, temp_path.basename.to_s) # should use temp file
assert_includes [Dir.tmpdir, '/tmp'], temp_path.dirname.to_s # should be in temp directory
# Check temp file contents
temp_content = File.read(temp_path)
expected_content = <<~CSS
@import "#{engine_css_path}";
@import "#{input_path}";
CSS
assert_equal expected_content.strip, temp_content.strip
end
end
end
end
end
end