Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
bopm committed Apr 7, 2025
commit b34e6323406d2e7aef636398085bb7c2b1ae59a4
24 changes: 11 additions & 13 deletions lib/tailwindcss/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ def rails_root
end

def remove_tempfile!
if @@tempfile
@@tempfile.unlink
@@tempfile = nil
if class_variable_defined?(:@@tempfile) && @@tempfile
@@tempfile.unlink if File.exist?(@@tempfile.path)
remove_class_variable(:@@tempfile)
end
end

Expand All @@ -19,7 +19,7 @@ def compile_command(input: application_css, debug: false, **kwargs)

command = [
Tailwindcss::Ruby.executable(**kwargs),
"-i", application_css.to_s,
"-i", input.to_s,
"-o", rails_root.join("app/assets/builds/tailwind.css").to_s,
]

Expand All @@ -35,10 +35,13 @@ def application_css
return rails_root.join("app/assets/tailwind/application.css").to_s if engines_roots.empty?

@@tempfile = Tempfile.new("tailwind.application.css")

# Write content to tempfile
engines_roots.each do |root|
@@tempfile.puts "@import \"#{root}\";"
@@tempfile.write("@import \"#{root}\";\n")
end
@@tempfile.puts "\n@import \"#{rails_root.join('app/assets/tailwind/application.css')}\";"
@@tempfile.write("\n@import \"#{rails_root.join('app/assets/tailwind/application.css')}\";\n")
@@tempfile.flush
@@tempfile.close

@@tempfile.path
Expand All @@ -65,13 +68,8 @@ def rails_css_compressor?
def engines_roots
return [] unless defined?(Rails)

Rails::Engine.subclasses.select do |engine|
begin
spec = Gem::Specification.find_by_name(engine.engine_name)
spec.dependencies.any? { |d| d.name == 'tailwindcss-rails' }
rescue Gem::MissingSpecError
false
end
Rails::Engine.descendants.select do |engine|
engine.engine_name.in?(Rails.application.config.tailwindcss_rails.engines)
end.map do |engine|
[
rails_root.join("app/assets/tailwind/#{engine.engine_name}/application.css"),
Expand Down
4 changes: 4 additions & 0 deletions lib/tailwindcss/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

module Tailwindcss
class Engine < ::Rails::Engine
initializer 'tailwindcss.add_engines_roots_config' do
Rails.application.config.tailwindcss_rails.engines = []
end

initializer "tailwindcss.disable_generator_stylesheets" do
Rails.application.config.generators.stylesheets = false
end
Expand Down
135 changes: 77 additions & 58 deletions test/lib/tailwindcss/commands_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require "test_helper"
require "minitest/mock"
require "tmpdir"
require "ostruct"
require "active_support/core_ext/class/subclasses"

class Tailwindcss::CommandsTest < ActiveSupport::TestCase
attr_accessor :executable
Expand All @@ -16,8 +19,31 @@ def teardown
end
end

def with_rails_mocks(root: Pathname.new(Dir.mktmpdir), engines: [])
mock_config = Object.new
def mock_config.tailwindcss_rails
@tailwindcss_rails ||= OpenStruct.new(engines: @engines)
end
def mock_config.assets
@assets ||= OpenStruct.new(css_compressor: nil)
end
mock_config.instance_variable_set(:@engines, engines)

mock_application = Object.new
def mock_application.config
@config
end
mock_application.instance_variable_set(:@config, mock_config)

Rails.stub(:root, root) do
Rails.stub(:application, mock_application) do
yield
end
end
end

test ".compile_command" do
Rails.stub(:root, File) do # Rails.root won't work in this test suite
with_rails_mocks do
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
Expand All @@ -27,7 +53,7 @@ def teardown
end

test ".compile_command debug flag" do
Rails.stub(:root, File) do # Rails.root won't work in this test suite
with_rails_mocks do
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
Expand All @@ -42,7 +68,7 @@ def teardown

test ".compile_command debug environment variable" do
begin
Rails.stub(:root, File) do # Rails.root won't work in this test suite
with_rails_mocks do
ENV["TAILWINDCSS_DEBUG"] = ""
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
Expand All @@ -67,7 +93,7 @@ def teardown
end

test ".compile_command when Rails compression is on" do
Rails.stub(:root, File) do # Rails.root won't work in this test suite
with_rails_mocks do
Tailwindcss::Commands.stub(:rails_css_compressor?, true) do
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
Expand All @@ -84,13 +110,14 @@ def teardown

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
root = Pathname.new(tmpdir)
with_rails_mocks(root: root) do
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")
config_file = root.join("postcss.config.js")
FileUtils.touch(config_file)
actual = Tailwindcss::Commands.compile_command
assert_kind_of(Array, actual)
Expand All @@ -103,7 +130,7 @@ def teardown
end

test ".watch_command" do
Rails.stub(:root, File) do # Rails.root won't work in this test suite
with_rails_mocks do
actual = Tailwindcss::Commands.watch_command
assert_kind_of(Array, actual)
assert_equal(executable, actual.first)
Expand Down Expand Up @@ -135,7 +162,7 @@ def teardown
end

test ".engines_roots when there are no engines" do
Rails.stub(:root, Pathname.new("/dummy")) do
with_rails_mocks do
Rails::Engine.stub(:subclasses, []) do
assert_empty Tailwindcss::Commands.engines_roots
end
Expand Down Expand Up @@ -169,16 +196,6 @@ def teardown
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
engine1_css = engine_root1.join("app/assets/tailwind/test_engine1/application.css")
engine2_css = root.join("app/assets/tailwind/test_engine2/application.css")
Expand All @@ -189,51 +206,53 @@ def teardown
FileUtils.touch(css_path)
end

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_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
with_rails_mocks(root: root, engines: %w[test_engine1 test_engine2]) do
Rails::Engine.stub(:descendants, [engine1, engine2, engine3]) do
roots = Tailwindcss::Commands.engines_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

spec1.verify
spec2.verify
end
end

test ".application_css creates tempfile when engines exist" do
Dir.mktmpdir do |tmpdir|
root = Pathname.new(tmpdir)

# Create necessary files
# Create engine files
engine_root = root.join('engine1')
FileUtils.mkdir_p(engine_root)

engine = Class.new(Rails::Engine) do
define_singleton_method(:engine_name) { "test_engine" }
define_singleton_method(:root) { engine_root }
end

app_css = root.join("app/assets/tailwind/application.css")
FileUtils.mkdir_p(File.dirname(app_css))
FileUtils.touch(app_css)
engine_css = engine_root.join("app/assets/tailwind/test_engine/application.css")

engine_css = root.join("app/assets/tailwind/test_engine/application.css")
FileUtils.mkdir_p(File.dirname(app_css))
FileUtils.mkdir_p(File.dirname(engine_css))
FileUtils.touch(app_css)
FileUtils.touch(engine_css)

Rails.stub(:root, root) do
Tailwindcss::Commands.stub(:engines_roots, [engine_css.to_s]) do
with_rails_mocks(root: root, engines: ["test_engine"]) do
Rails::Engine.stub(:descendants, [engine]) do
Tailwindcss::Commands.remove_tempfile!
css_path = Tailwindcss::Commands.application_css
assert_equal css_path, Tailwindcss::Commands.class_variable_get(:@@tempfile).path

assert File.exist?(css_path), "Tempfile should exist"
content = File.read(css_path)
assert_match "@import \"#{engine_css}\";", content
assert_match "@import \"#{app_css}\";", content
expected_content = <<~CSS
@import "#{engine_css}";

@import "#{app_css}";
CSS
assert_equal expected_content, content
end
end
end
Expand All @@ -247,27 +266,27 @@ def teardown
FileUtils.mkdir_p(File.dirname(app_css))
FileUtils.touch(app_css)

Rails.stub(:root, root) do
Tailwindcss::Commands.stub(:engines_roots, []) do
css_path = Tailwindcss::Commands.application_css
assert_equal app_css.to_s, css_path
end
with_rails_mocks(root: root) do
css_path = Tailwindcss::Commands.application_css
assert_equal app_css.to_s, css_path
end
end
end

test ".remove_tempfile! cleans up temporary file" do
Dir.mktmpdir do |tmpdir|
root = Pathname.new(tmpdir)
app_css = root.join("app/assets/tailwind/application.css")
FileUtils.mkdir_p(File.dirname(app_css))
FileUtils.touch(app_css)

Rails.stub(:root, root) do
Tailwindcss::Commands.stub(:engines_roots, ["dummy_engine"]) do
css_path = Tailwindcss::Commands.application_css
assert File.exist?(css_path)
with_rails_mocks(root: root, engines: ["test_engine"]) do
Tailwindcss::Commands.remove_tempfile!
css_path = Tailwindcss::Commands.application_css
assert File.exist?(css_path), "Tempfile should exist before removal"

Tailwindcss::Commands.remove_tempfile!
refute File.exist?(css_path)
end
Tailwindcss::Commands.remove_tempfile!
refute File.exist?(css_path), "Tempfile should not exist after removal"
end
end
end
Expand Down