From d79bb8df7f20cfe3cfe94ddd63fdd2c9a0b2cfa3 Mon Sep 17 00:00:00 2001 From: Timmo Verlaan Date: Mon, 9 Nov 2020 15:36:51 +0100 Subject: [PATCH 1/4] exit watch process on EOF / Ctrl-D --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index edc7a3f..f6ce2d6 100644 --- a/index.js +++ b/index.js @@ -92,6 +92,9 @@ Promise.resolve() }) .then((results) => { if (argv.watch) { + process.stdin.on('end', () => process.exit(0)) + process.stdin.resume() + const printMessage = () => printVerbose(chalk.dim('\nWaiting for file changes...')) const watcher = chokidar.watch(input.concat(dependencies(results)), { From 8d6eddaa3d19358fa1522207882ff69ed70473c2 Mon Sep 17 00:00:00 2001 From: Timmo Verlaan Date: Thu, 12 Nov 2020 16:41:06 +0100 Subject: [PATCH 2/4] test for exiting watch process with EOF / Ctrl-D --- test/watch.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/watch.js b/test/watch.js index b5bce40..da56eef 100644 --- a/test/watch.js +++ b/test/watch.js @@ -3,7 +3,7 @@ const test = require('ava') const fs = require('fs-extra') const path = require('path') -const { exec } = require('child_process') +const { exec, spawn } = require('child_process') const chokidar = require('chokidar') const ENV = require('./helpers/env.js') @@ -285,3 +285,15 @@ testCb("--watch doesn't exit on CssSyntaxError", (t) => { // Timeout: setTimeout(() => t.end('test timeout'), 50000) }) + +testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => { + t.plan(0) + + const cp = spawn( + `node ${path.resolve('bin/postcss')} -o output.css -w --no-map`, + { shell: true } + ) + cp.on('error', t.end) + cp.on('exit', () => t.end()) + cp.stdin.end() +}) From 28185bc7e1b8cafd70eb08c6ea26c98a961684fd Mon Sep 17 00:00:00 2001 From: Timmo Verlaan Date: Thu, 12 Nov 2020 18:15:05 +0100 Subject: [PATCH 3/4] make test validate the correct exit code --- index.js | 8 +++++--- test/watch.js | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index f6ce2d6..47a6e5a 100644 --- a/index.js +++ b/index.js @@ -51,6 +51,11 @@ let configFile if (argv.env) process.env.NODE_ENV = argv.env if (argv.config) argv.config = path.resolve(argv.config) +if (argv.watch) { + process.stdin.on('end', () => process.exit(0)) + process.stdin.resume() +} + Promise.resolve() .then(() => { if (argv.watch && !(argv.output || argv.replace || argv.dir)) { @@ -92,9 +97,6 @@ Promise.resolve() }) .then((results) => { if (argv.watch) { - process.stdin.on('end', () => process.exit(0)) - process.stdin.resume() - const printMessage = () => printVerbose(chalk.dim('\nWaiting for file changes...')) const watcher = chokidar.watch(input.concat(dependencies(results)), { diff --git a/test/watch.js b/test/watch.js index da56eef..a6cf2e4 100644 --- a/test/watch.js +++ b/test/watch.js @@ -294,6 +294,6 @@ testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => { { shell: true } ) cp.on('error', t.end) - cp.on('exit', () => t.end()) + cp.on('exit', t.end) cp.stdin.end() }) From 3f380e2ae07fd1e75fa7a690b3924e47de5be1ca Mon Sep 17 00:00:00 2001 From: Timmo Verlaan Date: Fri, 13 Nov 2020 09:18:27 +0100 Subject: [PATCH 4/4] improve test with feedback on PR --- test/watch.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/watch.js b/test/watch.js index a6cf2e4..4a51a11 100644 --- a/test/watch.js +++ b/test/watch.js @@ -8,6 +8,7 @@ const chokidar = require('chokidar') const ENV = require('./helpers/env.js') const read = require('./helpers/read.js') +const tmp = require('./helpers/tmp.js') // XXX: All the tests in this file are skipped on the CI; too flacky there const testCb = process.env.CI ? test.cb.skip : test.cb @@ -287,13 +288,17 @@ testCb("--watch doesn't exit on CssSyntaxError", (t) => { }) testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => { - t.plan(0) + t.plan(1) const cp = spawn( - `node ${path.resolve('bin/postcss')} -o output.css -w --no-map`, + `./bin/postcss test/fixtures/a.css -o ${tmp()} -w --no-map`, { shell: true } ) + cp.on('error', t.end) - cp.on('exit', t.end) + cp.on('exit', (code) => { + t.is(code, 0) + t.end() + }) cp.stdin.end() })