@@ -2,7 +2,7 @@ import test from 'ava'
22
33import fs from 'node:fs/promises'
44import path from 'path'
5- import { exec , spawn } from 'child_process'
5+ import { spawn } from 'child_process'
66import chokidar from 'chokidar'
77
88import ENV from './helpers/env.js'
@@ -49,9 +49,17 @@ testCb('--watch works', (t) => {
4949
5050 // Start postcss-cli:
5151 watcher . on ( 'ready' , ( ) => {
52- // Using exec() and quoting "*.css" to test watch's glob handling:
53- cp = exec (
54- `node ${ path . resolve ( 'index.js' ) } "*.css" -o output.css --no-map -w` ,
52+ cp = spawn (
53+ 'node' ,
54+ [
55+ path . resolve ( 'index.js' ) ,
56+ // '*.css' arrives as a single literal arg to test watch's glob handling
57+ '*.css' ,
58+ '-o' ,
59+ 'output.css' ,
60+ '--no-map' ,
61+ '-w' ,
62+ ] ,
5563 { cwd : dir } ,
5664 )
5765 cp . on ( 'error' , t . end )
@@ -114,10 +122,18 @@ testCb('--watch dependencies', (t) => {
114122
115123 // Start postcss-cli:
116124 watcher . on ( 'ready' , ( ) => {
117- cp = exec (
118- `node ${ path . resolve (
119- 'index.js' ,
120- ) } import.css -o output.css -u postcss-import -w --no-map`,
125+ cp = spawn (
126+ 'node' ,
127+ [
128+ path . resolve ( 'index.js' ) ,
129+ 'import.css' ,
130+ '-o' ,
131+ 'output.css' ,
132+ '-u' ,
133+ 'postcss-import' ,
134+ '-w' ,
135+ '--no-map' ,
136+ ] ,
121137 { cwd : dir } ,
122138 )
123139
@@ -144,72 +160,85 @@ testCb('--watch dependencies', (t) => {
144160 . catch ( t . end )
145161} )
146162
147- // Doesn't work on CI for some reason
148- ; ( process . env . CI ? test . cb . skip : test . cb ) (
149- "--watch doesn't exit on CssSyntaxError" ,
150- ( t ) => {
151- t . plan ( 0 )
152-
153- ENV ( '' , [ 'a.css' ] )
154- . then ( ( dir ) => {
155- // Init watcher:
156- const watcher = chokidar . watch ( '.' , {
157- cwd : dir ,
158- ignoreInitial : true ,
159- awaitWriteFinish : true ,
160- } )
161- watcher . on ( 'add' , ( p ) => {
162- if ( p === 'output.css' ) {
163- // Change to invalid CSS
164- fs . writeFile ( path . join ( dir , 'a.css' ) , '.a { color: red' ) . catch ( done )
165- }
166- } )
163+ testCb ( "--watch doesn't exit on CssSyntaxError" , ( t ) => {
164+ t . plan ( 0 )
167165
168- let killed = false
169- const cp = exec (
170- `node ${ path . resolve (
171- 'index.js' ,
172- ) } a.css -o output.css -u postcss-import -w --no-map`,
173- { cwd : dir } ,
174- )
175- cp . on ( 'error' , t . end )
176- cp . stderr . on ( 'data' , ( chunk ) => {
177- // When error message is printed, kill the process after a timeout
178- if ( ~ chunk . indexOf ( 'Unclosed block' ) ) {
179- setTimeout ( ( ) => {
180- killed = true
181- cp . kill ( )
182- } , 1000 )
183- }
184- } )
185- cp . on ( 'exit' , ( code ) => {
186- if ( ! killed )
187- return t . end ( `Should not exit (exited with code ${ code } )` )
188- done ( )
189- } )
166+ ENV ( '' , [ 'a.css' ] )
167+ . then ( ( dir ) => {
168+ // Init watcher:
169+ const watcher = chokidar . watch ( '.' , {
170+ cwd : dir ,
171+ ignoreInitial : true ,
172+ awaitWriteFinish : true ,
173+ } )
174+ watcher . on ( 'add' , ( p ) => {
175+ if ( p === 'output.css' ) {
176+ // Change to invalid CSS
177+ fs . writeFile ( path . join ( dir , 'a.css' ) , '.a { color: red' ) . catch ( done )
178+ }
179+ } )
190180
191- function done ( err ) {
192- try {
181+ let killed = false
182+ const cp = spawn (
183+ 'node' ,
184+ [
185+ path . resolve ( 'index.js' ) ,
186+ 'a.css' ,
187+ '-o' ,
188+ 'output.css' ,
189+ '-u' ,
190+ 'postcss-import' ,
191+ '-w' ,
192+ '--no-map' ,
193+ ] ,
194+ { cwd : dir } ,
195+ )
196+ cp . on ( 'error' , t . end )
197+ cp . stderr . on ( 'data' , ( chunk ) => {
198+ // When error message is printed, kill the process after a timeout
199+ if ( ~ chunk . indexOf ( 'Unclosed block' ) ) {
200+ setTimeout ( ( ) => {
201+ killed = true
193202 cp . kill ( )
194- } catch { }
195-
196- t . end ( err )
203+ } , 1000 )
197204 }
198205 } )
199- . catch ( t . end )
200- } ,
201- )
206+ cp . on ( 'exit' , ( code ) => {
207+ if ( ! killed ) return t . end ( `Should not exit (exited with code ${ code } )` )
208+ done ( )
209+ } )
210+
211+ function done ( err ) {
212+ try {
213+ cp . kill ( )
214+ } catch { }
215+
216+ t . end ( err )
217+ }
218+ } )
219+ . catch ( t . end )
220+ } )
202221
203222testCb ( '--watch does exit on closing stdin (Ctrl-D/EOF)' , ( t ) => {
204223 t . plan ( 1 )
205224
206- const cp = spawn ( `./index.js test/fixtures/a.css -o ${ tmp ( ) } -w --no-map` , {
207- shell : true ,
208- env : {
209- ...process . env ,
210- FORCE_IS_TTY : true ,
225+ const cp = spawn (
226+ 'node' ,
227+ [
228+ path . resolve ( 'index.js' ) ,
229+ 'test/fixtures/a.css' ,
230+ '-o' ,
231+ tmp ( ) ,
232+ '-w' ,
233+ '--no-map' ,
234+ ] ,
235+ {
236+ env : {
237+ ...process . env ,
238+ FORCE_IS_TTY : true ,
239+ } ,
211240 } ,
212- } )
241+ )
213242
214243 cp . on ( 'error' , t . end )
215244 cp . on ( 'exit' , ( code ) => {
@@ -278,10 +307,16 @@ testCb('--watch watches dependencies', (t) => {
278307
279308 // Start postcss-cli:
280309 watcher . on ( 'ready' , ( ) => {
281- cp = exec (
282- `node ${ path . resolve (
283- 'index.js' ,
284- ) } "s.css" -o output.css --no-map -w`,
310+ cp = spawn (
311+ 'node' ,
312+ [
313+ path . resolve ( 'index.js' ) ,
314+ 's.css' ,
315+ '-o' ,
316+ 'output.css' ,
317+ '--no-map' ,
318+ '-w' ,
319+ ] ,
285320 { cwd : dir } ,
286321 )
287322 cp . on ( 'error' , t . end )
@@ -375,10 +410,16 @@ testCb('--watch watches directory dependencies', (t) => {
375410
376411 // Start postcss-cli:
377412 watcher . on ( 'ready' , ( ) => {
378- cp = exec (
379- `node ${ path . resolve (
380- 'index.js' ,
381- ) } "s.css" -o output.css --no-map -w`,
413+ cp = spawn (
414+ 'node' ,
415+ [
416+ path . resolve ( 'index.js' ) ,
417+ 's.css' ,
418+ '-o' ,
419+ 'output.css' ,
420+ '--no-map' ,
421+ '-w' ,
422+ ] ,
382423 { cwd : dir } ,
383424 )
384425 cp . on ( 'error' , t . end )
@@ -480,10 +521,16 @@ testCb(
480521
481522 // Start postcss-cli:
482523 watcher . on ( 'ready' , ( ) => {
483- cp = exec (
484- `node ${ path . resolve (
485- 'index.js' ,
486- ) } "s.css" -o output.css --no-map -w`,
524+ cp = spawn (
525+ 'node' ,
526+ [
527+ path . resolve ( 'index.js' ) ,
528+ 's.css' ,
529+ '-o' ,
530+ 'output.css' ,
531+ '--no-map' ,
532+ '-w' ,
533+ ] ,
487534 { cwd : dir } ,
488535 )
489536 cp . on ( 'error' , t . end )
0 commit comments