1+ # frozen_string_literal: true
2+
3+ require "helper"
4+ require "jekyll"
5+ require "fileutils"
6+ require "time"
7+ require_relative "../_plugins/tailwind_incremental_fix"
8+
9+ describe Jekyll ::PostcssTrigger do
10+ before do
11+ # Reset module state before each test
12+ Jekyll ::PostcssTrigger . last_check_time = nil
13+ Jekyll ::PostcssTrigger . css_touched = false
14+ end
15+
16+ after do
17+ # Clean up module state
18+ Jekyll ::PostcssTrigger . last_check_time = nil
19+ Jekyll ::PostcssTrigger . css_touched = false
20+ end
21+
22+ describe "module initialization" do
23+ it "has last_check_time accessor" do
24+ _ ( Jekyll ::PostcssTrigger ) . must_respond_to :last_check_time
25+ _ ( Jekyll ::PostcssTrigger ) . must_respond_to :last_check_time=
26+ end
27+
28+ it "has css_touched accessor" do
29+ _ ( Jekyll ::PostcssTrigger ) . must_respond_to :css_touched
30+ _ ( Jekyll ::PostcssTrigger ) . must_respond_to :css_touched=
31+ end
32+
33+ it "initializes with nil last_check_time" do
34+ _ ( Jekyll ::PostcssTrigger . last_check_time ) . must_be_nil
35+ end
36+
37+ it "initializes with false css_touched" do
38+ _ ( Jekyll ::PostcssTrigger . css_touched ) . must_equal false
39+ end
40+ end
41+
42+ describe "state management" do
43+ it "can set and get last_check_time" do
44+ time = Time . now
45+ Jekyll ::PostcssTrigger . last_check_time = time
46+ _ ( Jekyll ::PostcssTrigger . last_check_time ) . must_equal time
47+ end
48+
49+ it "can set and get css_touched" do
50+ Jekyll ::PostcssTrigger . css_touched = true
51+ _ ( Jekyll ::PostcssTrigger . css_touched ) . must_equal true
52+ end
53+
54+ it "can reset state" do
55+ Jekyll ::PostcssTrigger . last_check_time = Time . now
56+ Jekyll ::PostcssTrigger . css_touched = true
57+
58+ Jekyll ::PostcssTrigger . last_check_time = nil
59+ Jekyll ::PostcssTrigger . css_touched = false
60+
61+ _ ( Jekyll ::PostcssTrigger . last_check_time ) . must_be_nil
62+ _ ( Jekyll ::PostcssTrigger . css_touched ) . must_equal false
63+ end
64+ end
65+
66+ describe "file modification detection logic" do
67+ before do
68+ chdir_tempdir
69+ end
70+
71+ after do
72+ teardown_tempdir
73+ end
74+
75+ it "detects files modified after a timestamp" do
76+ # Create a file
77+ create_file ( "test.html" , "<html>content</html>" )
78+
79+ # Record current time
80+ check_time = Time . now
81+
82+ # Wait to ensure time difference
83+ sleep 0.1
84+
85+ # Modify the file
86+ create_file ( "test.html" , "<html>modified</html>" )
87+
88+ # Check mtime
89+ _ ( File . mtime ( "test.html" ) > check_time ) . must_equal true
90+ end
91+
92+ it "does not detect unmodified files" do
93+ # Create a file
94+ create_file ( "test.html" , "<html>content</html>" )
95+
96+ # Record time after creation
97+ sleep 0.1
98+ check_time = Time . now
99+
100+ # Wait
101+ sleep 0.1
102+
103+ # Don't modify the file
104+
105+ # Check mtime
106+ _ ( File . mtime ( "test.html" ) > check_time ) . must_equal false
107+ end
108+
109+ it "can touch a file to update mtime" do
110+ create_file ( "test.css" , "/* css */" )
111+ original_mtime = File . mtime ( "test.css" )
112+
113+ sleep 0.1
114+
115+ FileUtils . touch ( "test.css" )
116+ new_mtime = File . mtime ( "test.css" )
117+
118+ _ ( new_mtime > original_mtime ) . must_equal true
119+ end
120+
121+ it "matches HTML files with glob pattern" do
122+ create_file ( "index.html" , "<html></html>" )
123+ create_file ( "about.html" , "<html></html>" )
124+ create_file ( "style.css" , "/* css */" )
125+
126+ html_files = Dir . glob ( "*.html" )
127+
128+ _ ( html_files . length ) . must_equal 2
129+ _ ( html_files ) . must_include "index.html"
130+ _ ( html_files ) . must_include "about.html"
131+ _ ( html_files ) . wont_include "style.css"
132+ end
133+
134+ it "matches nested HTML files with glob pattern" do
135+ create_file ( "_includes/header.html" , "<header></header>" )
136+ create_file ( "_layouts/default.html" , "<html></html>" )
137+
138+ includes_files = Dir . glob ( "_includes/**/*.html" )
139+ layouts_files = Dir . glob ( "_layouts/**/*.html" )
140+
141+ _ ( includes_files . length ) . must_equal 1
142+ _ ( layouts_files . length ) . must_equal 1
143+ end
144+
145+ it "matches markdown files with glob pattern" do
146+ create_file ( "about.md" , "# About" )
147+ create_file ( "contact.md" , "# Contact" )
148+
149+ md_files = Dir . glob ( "*.md" )
150+
151+ _ ( md_files . length ) . must_equal 2
152+ end
153+ end
154+
155+ describe "integration with incremental config" do
156+ before do
157+ chdir_tempdir
158+ end
159+
160+ after do
161+ teardown_tempdir
162+ end
163+
164+ it "recognizes incremental mode in config" do
165+ config = Jekyll . configuration (
166+ source : "." ,
167+ incremental : true ,
168+ quiet : true
169+ )
170+
171+ _ ( config [ 'incremental' ] ) . must_equal true
172+ end
173+
174+ it "recognizes non-incremental mode in config" do
175+ config = Jekyll . configuration (
176+ source : "." ,
177+ incremental : false ,
178+ quiet : true
179+ )
180+
181+ _ ( config [ 'incremental' ] ) . must_equal false
182+ end
183+ end
184+
185+ describe "CSS file operations" do
186+ before do
187+ chdir_tempdir
188+ end
189+
190+ after do
191+ teardown_tempdir
192+ end
193+
194+ it "can check if CSS file exists" do
195+ create_file ( "stylesheets/main.css" , "/* css */" )
196+
197+ _ ( File . exist? ( "stylesheets/main.css" ) ) . must_equal true
198+ end
199+
200+ it "can touch CSS file" do
201+ create_file ( "stylesheets/main.css" , "/* css */" )
202+ original_mtime = File . mtime ( "stylesheets/main.css" )
203+
204+ sleep 0.1
205+
206+ FileUtils . touch ( "stylesheets/main.css" )
207+ new_mtime = File . mtime ( "stylesheets/main.css" )
208+
209+ _ ( new_mtime > original_mtime ) . must_equal true
210+ end
211+
212+ it "preserves CSS file content when touched" do
213+ content = "/* original css */"
214+ create_file ( "stylesheets/main.css" , content )
215+
216+ FileUtils . touch ( "stylesheets/main.css" )
217+
218+ _ ( File . read ( "stylesheets/main.css" ) ) . must_equal content
219+ end
220+ end
221+ end
0 commit comments