Skip to content

Commit 20aa562

Browse files
committed
Add tests for command enhancement and Tailwind roots
1 parent bc0af61 commit 20aa562

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

test/lib/tailwindcss/commands_test.rb

+139
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,143 @@ def setup
126126
assert_includes(actual, "always")
127127
end
128128
end
129+
130+
test ".engines_tailwindcss_roots when there are no engines" do
131+
Rails.stub(:root, Pathname.new("/dummy")) do
132+
Rails::Engine.stub(:subclasses, []) do
133+
assert_empty Tailwindcss::Commands.engines_tailwindcss_roots
134+
end
135+
end
136+
end
137+
138+
test ".engines_tailwindcss_roots when there are engines" do
139+
Dir.mktmpdir do |tmpdir|
140+
root = Pathname.new(tmpdir)
141+
142+
# Create two engines
143+
engine_root1 = root.join('engine1')
144+
engine_root2 = root.join('engine2')
145+
FileUtils.mkdir_p(engine_root1)
146+
FileUtils.mkdir_p(engine_root2)
147+
148+
engine1 = Class.new(Rails::Engine) do
149+
define_singleton_method(:engine_name) { "test_engine1" }
150+
define_singleton_method(:root) { engine_root1 }
151+
end
152+
153+
engine2 = Class.new(Rails::Engine) do
154+
define_singleton_method(:engine_name) { "test_engine2" }
155+
define_singleton_method(:root) { engine_root2 }
156+
end
157+
158+
# Create mock specs for both engines
159+
spec1 = Minitest::Mock.new
160+
spec1.expect(:dependencies, [Gem::Dependency.new("tailwindcss-rails")])
161+
162+
spec2 = Minitest::Mock.new
163+
spec2.expect(:dependencies, [Gem::Dependency.new("tailwindcss-rails")])
164+
165+
spec3 = Minitest::Mock.new
166+
spec3.expect(:dependencies, [])
167+
168+
# Set up file structure
169+
# Engine 1: CSS in engine root
170+
engine1_css = engine_root1.join("app/assets/tailwind/test_engine1/application.css")
171+
FileUtils.mkdir_p(File.dirname(engine1_css))
172+
FileUtils.touch(engine1_css)
173+
174+
# Engine 2: CSS in Rails root
175+
engine2_css = root.join("app/assets/tailwind/test_engine2/application.css")
176+
FileUtils.mkdir_p(File.dirname(engine2_css))
177+
FileUtils.touch(engine2_css)
178+
179+
# Engine 3: CsS in engine root, but no tailwindcss-rails dependency
180+
engine3_css = engine_root2.join("app/assets/tailwind/test_engine3/application.css")
181+
FileUtils.mkdir_p(File.dirname(engine3_css))
182+
FileUtils.touch(engine3_css)
183+
184+
find_by_name_results = {
185+
"test_engine1" => spec1,
186+
"test_engine2" => spec2
187+
}
188+
189+
Gem::Specification.stub(:find_by_name, ->(name) { find_by_name_results[name] }) do
190+
Rails.stub(:root, root) do
191+
Rails::Engine.stub(:subclasses, [engine1, engine2]) do
192+
roots = Tailwindcss::Commands.engines_tailwindcss_roots
193+
194+
assert_equal 2, roots.size
195+
assert_includes roots, engine1_css.to_s
196+
assert_includes roots, engine2_css.to_s
197+
assert_not_includes roots, engine3_css.to_s
198+
end
199+
end
200+
end
201+
202+
spec1.verify
203+
spec2.verify
204+
end
205+
end
206+
207+
test ".enhance_command when there are no engines" do
208+
Dir.mktmpdir do |tmpdir|
209+
root = Pathname.new(tmpdir)
210+
input_path = root.join("app/assets/tailwind/application.css")
211+
output_path = root.join("app/assets/builds/tailwind.css")
212+
213+
command = ["tailwindcss", "-i", input_path.to_s, "-o", output_path.to_s]
214+
215+
Rails.stub(:root, root) do
216+
Tailwindcss::Commands.stub(:engines_tailwindcss_roots, []) do
217+
Tailwindcss::Commands.enhance_command(command) do |actual|
218+
assert_equal command, actual
219+
end
220+
end
221+
end
222+
end
223+
end
224+
225+
test ".enhance_command when there are engines" do
226+
Dir.mktmpdir do |tmpdir|
227+
root = Pathname.new(tmpdir)
228+
input_path = root.join("app/assets/tailwind/application.css")
229+
output_path = root.join("app/assets/builds/tailwind.css")
230+
231+
# Create necessary files
232+
FileUtils.mkdir_p(File.dirname(input_path))
233+
FileUtils.touch(input_path)
234+
235+
# Create engine CSS file
236+
engine_css_path = root.join("app/assets/tailwind/test_engine/application.css")
237+
FileUtils.mkdir_p(File.dirname(engine_css_path))
238+
FileUtils.touch(engine_css_path)
239+
240+
command = ["tailwindcss", "-i", input_path.to_s, "-o", output_path.to_s]
241+
242+
Rails.stub(:root, root) do
243+
Tailwindcss::Commands.stub(:engines_tailwindcss_roots, [engine_css_path.to_s]) do
244+
Tailwindcss::Commands.enhance_command(command) do |actual|
245+
# Command should be modified to use a temporary file
246+
assert_equal command[0], actual[0] # executable
247+
assert_equal command[1], actual[1] # -i flag
248+
assert_equal command[3], actual[3] # -o flag
249+
assert_equal command[4], actual[4] # output path
250+
251+
temp_path = Pathname.new(actual[2])
252+
refute_equal command[2], temp_path.to_s # input path should be different
253+
assert_match(/tailwind\.css/, temp_path.basename.to_s) # should use temp file
254+
assert_includes [Dir.tmpdir, '/tmp'], temp_path.dirname.to_s # should be in temp directory
255+
256+
# Check temp file contents
257+
temp_content = File.read(temp_path)
258+
expected_content = <<~CSS
259+
@import "#{engine_css_path}";
260+
@import "#{input_path}";
261+
CSS
262+
assert_equal expected_content.strip, temp_content.strip
263+
end
264+
end
265+
end
266+
end
267+
end
129268
end

0 commit comments

Comments
 (0)