Skip to content
Merged
Changes from all commits
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
199 changes: 123 additions & 76 deletions test/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava'

import fs from 'node:fs/promises'
import path from 'path'
import { exec, spawn } from 'child_process'
import { spawn } from 'child_process'
import chokidar from 'chokidar'

import ENV from './helpers/env.js'
Expand Down Expand Up @@ -49,9 +49,17 @@ testCb('--watch works', (t) => {

// Start postcss-cli:
watcher.on('ready', () => {
// Using exec() and quoting "*.css" to test watch's glob handling:
cp = exec(
`node ${path.resolve('index.js')} "*.css" -o output.css --no-map -w`,
cp = spawn(
'node',
[
path.resolve('index.js'),
// '*.css' arrives as a single literal arg to test watch's glob handling
'*.css',
'-o',
'output.css',
'--no-map',
'-w',
],
{ cwd: dir },
)
cp.on('error', t.end)
Expand Down Expand Up @@ -114,10 +122,18 @@ testCb('--watch dependencies', (t) => {

// Start postcss-cli:
watcher.on('ready', () => {
cp = exec(
`node ${path.resolve(
'index.js',
)} import.css -o output.css -u postcss-import -w --no-map`,
cp = spawn(
'node',
[
path.resolve('index.js'),
'import.css',
'-o',
'output.css',
'-u',
'postcss-import',
'-w',
'--no-map',
],
{ cwd: dir },
)

Expand All @@ -144,72 +160,85 @@ testCb('--watch dependencies', (t) => {
.catch(t.end)
})

// Doesn't work on CI for some reason
;(process.env.CI ? test.cb.skip : test.cb)(
"--watch doesn't exit on CssSyntaxError",
(t) => {
t.plan(0)

ENV('', ['a.css'])
.then((dir) => {
// Init watcher:
const watcher = chokidar.watch('.', {
cwd: dir,
ignoreInitial: true,
awaitWriteFinish: true,
})
watcher.on('add', (p) => {
if (p === 'output.css') {
// Change to invalid CSS
fs.writeFile(path.join(dir, 'a.css'), '.a { color: red').catch(done)
}
})
testCb("--watch doesn't exit on CssSyntaxError", (t) => {
t.plan(0)

let killed = false
const cp = exec(
`node ${path.resolve(
'index.js',
)} a.css -o output.css -u postcss-import -w --no-map`,
{ cwd: dir },
)
cp.on('error', t.end)
cp.stderr.on('data', (chunk) => {
// When error message is printed, kill the process after a timeout
if (~chunk.indexOf('Unclosed block')) {
setTimeout(() => {
killed = true
cp.kill()
}, 1000)
}
})
cp.on('exit', (code) => {
if (!killed)
return t.end(`Should not exit (exited with code ${code})`)
done()
})
ENV('', ['a.css'])
.then((dir) => {
// Init watcher:
const watcher = chokidar.watch('.', {
cwd: dir,
ignoreInitial: true,
awaitWriteFinish: true,
})
watcher.on('add', (p) => {
if (p === 'output.css') {
// Change to invalid CSS
fs.writeFile(path.join(dir, 'a.css'), '.a { color: red').catch(done)
}
})

function done(err) {
try {
let killed = false
const cp = spawn(
'node',
[
path.resolve('index.js'),
'a.css',
'-o',
'output.css',
'-u',
'postcss-import',
'-w',
'--no-map',
],
{ cwd: dir },
)
cp.on('error', t.end)
cp.stderr.on('data', (chunk) => {
// When error message is printed, kill the process after a timeout
if (~chunk.indexOf('Unclosed block')) {
setTimeout(() => {
killed = true
cp.kill()
} catch {}

t.end(err)
}, 1000)
}
})
.catch(t.end)
},
)
cp.on('exit', (code) => {
if (!killed) return t.end(`Should not exit (exited with code ${code})`)
done()
})

function done(err) {
try {
cp.kill()
} catch {}

t.end(err)
}
})
.catch(t.end)
})

testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => {
t.plan(1)

const cp = spawn(`./index.js test/fixtures/a.css -o ${tmp()} -w --no-map`, {
shell: true,
env: {
...process.env,
FORCE_IS_TTY: true,
const cp = spawn(
'node',
[
path.resolve('index.js'),
'test/fixtures/a.css',
'-o',
tmp(),
'-w',
'--no-map',
],
{
env: {
...process.env,
FORCE_IS_TTY: true,
},
},
})
)

cp.on('error', t.end)
cp.on('exit', (code) => {
Expand Down Expand Up @@ -278,10 +307,16 @@ testCb('--watch watches dependencies', (t) => {

// Start postcss-cli:
watcher.on('ready', () => {
cp = exec(
`node ${path.resolve(
'index.js',
)} "s.css" -o output.css --no-map -w`,
cp = spawn(
'node',
[
path.resolve('index.js'),
's.css',
'-o',
'output.css',
'--no-map',
'-w',
],
{ cwd: dir },
)
cp.on('error', t.end)
Expand Down Expand Up @@ -375,10 +410,16 @@ testCb('--watch watches directory dependencies', (t) => {

// Start postcss-cli:
watcher.on('ready', () => {
cp = exec(
`node ${path.resolve(
'index.js',
)} "s.css" -o output.css --no-map -w`,
cp = spawn(
'node',
[
path.resolve('index.js'),
's.css',
'-o',
'output.css',
'--no-map',
'-w',
],
{ cwd: dir },
)
cp.on('error', t.end)
Expand Down Expand Up @@ -480,10 +521,16 @@ testCb(

// Start postcss-cli:
watcher.on('ready', () => {
cp = exec(
`node ${path.resolve(
'index.js',
)} "s.css" -o output.css --no-map -w`,
cp = spawn(
'node',
[
path.resolve('index.js'),
's.css',
'-o',
'output.css',
'--no-map',
'-w',
],
{ cwd: dir },
)
cp.on('error', t.end)
Expand Down