vesln / nixt

Simple and elegant end-to-end testing for command-line apps
MIT License
310 stars 19 forks source link

Wait for standard IO streams to close #15

Closed eush77 closed 8 years ago

eush77 commented 8 years ago

Currently test results are evaluated in the listener for exit event of the child process. However, exit event is not guaranteed to be emitted after standard output streams are closed, as demostrated by the program below:

var spawn = require('child_process').spawn;

var cp = spawn('echo', ['foo']);
cp.stdout.on('data', console.log);
cp.on('exit', () => console.log('exit'));
$ node --version
v5.0.0
$ node example.js
exit
<Buffer 66 6f 6f 0a>

See also this warning from Node.js docs:

Note that when the 'exit' event is triggered, child process stdio streams might still be open.

I can't come up with a failing test for this, but here's a failing example on the command line:

$ node -e 'require("./")().run("echo foo").stdout("foo").end(err => { if (err) console.log(err.toString()) })'
AssertionError: `echo foo`: Expected stdout to match "foo". Actual: ""

This patch listens for close event instead:

The 'close' event is emitted when the stdio streams of a child process have been closed.

vesln commented 8 years ago

@eush77 thanks a ton!

would you be maybe interested in maintain the project? I'm happy to give you push access to both github and npm

eush77 commented 8 years ago

@vesln Yeah, I would be happy to help! :hammer:

vesln commented 8 years ago

just granted u push access, lmk what ur npm username is

eush77 commented 8 years ago

The same as here: eush77.

vesln commented 8 years ago

done!