Skip to content

Commit

Permalink
feat: support on event (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Apr 22, 2020
1 parent 7cc2798 commit 462c583
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 2 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/nodejs.yml
@@ -0,0 +1,31 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ${{ matrix.os }}

strategy:
matrix:
node-version: [10.x, 12.x]
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run ci
env:
CI: true
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -281,6 +281,21 @@ const { stdout, stderr, code } = await coffee.fork('path/to/cli').end();
assert(stdout.includes(abcdefg));
```

#### coffee.on(event, callback)

Emit `stdout/stderr` event.

```js
coffee.fork('path/to/cli')
.on('stdout', function (buf) {
if (buf.toString().includes('egg-ready')) {
this.proc.kill();
}
})
.expect('stdout', 'egg-ready')
.end(done);
```

#### coffee.debug(level)

Write data to process.stdout and process.stderr for debug
Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
@@ -1,5 +1,5 @@
import { EventEmitter } from 'events';
import { ForkOptions, SpawnOptions } from 'child_process';
import { ForkOptions, SpawnOptions, ChildProcess } from 'child_process';
import { Readable } from 'stream';

export interface RuleOpt {
Expand All @@ -23,6 +23,7 @@ export interface Result {
stderr: string;
code: number;
error: Error | null;
proc: ChildProcess | null;
}

export interface CoffeeOpt<T> {
Expand Down
5 changes: 5 additions & 0 deletions lib/coffee.js
Expand Up @@ -58,11 +58,13 @@ class Coffee extends EventEmitter {
debug('output stdout `%s`', show(buf));
this._debug_stdout && process.stdout.write(buf);
this.stdout += buf;
this.emit('stdout', buf.toString());
});
this.on('stderr_data', buf => {
debug('output stderr `%s`', show(buf));
this._debug_stderr && process.stderr.write(buf);
this.stderr += buf;
this.emit('stderr', buf.toString());
});
this.on('error', err => {
this.error = err;
Expand All @@ -81,10 +83,12 @@ class Coffee extends EventEmitter {
stderr: this.stderr,
code: this.code,
error: this.error,
proc: this.proc,
};
this.emit('complete_success', result);
this.cb && this.cb(undefined, result);
} catch (err) {
err.proc = this.proc;
this.emit('complete_error', err);
return this.cb && this.cb(err);
}
Expand Down Expand Up @@ -266,6 +270,7 @@ class Coffee extends EventEmitter {

process.once('exit', code => {
debug(`coffee exit with ${code}`);
cmd.exitCode = code;
cmd.kill();
});

Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/long-run.js
@@ -0,0 +1,9 @@
'use strict';

let i = 0;

setInterval(() => {
i++;
if (i === 10) console.log('egg-ready');
console.log('hi', i);
}, 100);
10 changes: 10 additions & 0 deletions test/fixtures/long-run.sh
@@ -0,0 +1,10 @@
#!/bin/bash

i=0
while [ : ]
do
i=$(($i+1))
if [ $i -eq 10 ]; then echo "egg-ready"; fi
echo "$i"
sleep 0.1
done
43 changes: 42 additions & 1 deletion test/index.test.js
Expand Up @@ -539,7 +539,10 @@ function run(type) {
.expect('stderr', 'stderr\n')
.expect('code', 0)
.end()
.then(() => done())
.then(result => {
assert(result.proc);
done();
})
.catch(done);
});

Expand All @@ -551,10 +554,48 @@ function run(type) {
.end()
.catch(function(err) {
assert(err);
assert(err.proc);
done();
});
});

it('should support on()', done => {
let hasStdout;
let hasStderr;
call('stdout-stderr')
.on('stdout', function(buf) {
hasStdout = !!buf && !!this.proc;
})
.on('stderr', function(buf) {
hasStderr = !!buf && !!this.proc;
})
.expect('stdout', 'write to stdout\n')
.expect('stderr', 'stderr\n')
.expect('code', 0)
.end()
.then(result => {
assert(result.proc);
assert(hasStdout);
assert(hasStderr);
done();
})
.catch(done);
});

it('use event to kill', done => {
call('long-run')
// .debug()
.on('stdout', function(buf) {
if (buf.toString().includes('egg-ready')) {
this.proc.exitCode = 0;
this.proc.kill();
}
})
.expect('stdout', /egg-ready/)
.expect('code', 0)
.end(done);
});

it('should return this when call coverage', () => {
const c = coffee.spawn('cat');
assert(c.coverage() === c);
Expand Down

0 comments on commit 462c583

Please sign in to comment.