2020-03-31 04:37:58 +00:00
|
|
|
import { expect } from 'chai';
|
2021-08-08 19:58:43 +00:00
|
|
|
import sinon from 'sinon';
|
2020-03-31 04:37:58 +00:00
|
|
|
|
2022-10-08 19:10:17 +00:00
|
|
|
import { ChildProcessError } from '../../src/error/ChildProcessError.js';
|
|
|
|
import { ChildStreams, childResult } from '../../src/Child.js';
|
|
|
|
import { Maybe, mustExist } from '../../src/Maybe.js';
|
2020-03-31 04:37:58 +00:00
|
|
|
|
2021-08-08 19:58:43 +00:00
|
|
|
const { match, stub } = sinon;
|
|
|
|
|
2020-03-31 04:37:58 +00:00
|
|
|
type Closer = (status: number) => Promise<void>;
|
|
|
|
|
2021-07-10 21:54:41 +00:00
|
|
|
function createChild(): ChildStreams & { closer: Maybe<Closer> } {
|
2020-03-31 04:37:58 +00:00
|
|
|
return {
|
2021-08-08 19:58:43 +00:00
|
|
|
closer /* Maybe<Closer> */: undefined,
|
2020-03-31 04:37:58 +00:00
|
|
|
on(event: string, fn: Closer) {
|
|
|
|
if (event === 'close') {
|
|
|
|
this.closer = fn;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
stderr: {
|
|
|
|
on() {
|
|
|
|
/* noop */
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stdout: {
|
|
|
|
on() {
|
|
|
|
/* noop */
|
|
|
|
},
|
|
|
|
},
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
} as any;
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:14:30 +00:00
|
|
|
describe('child process utils', async () => {
|
|
|
|
describe('wait for child helper', async () => {
|
2021-08-08 19:58:43 +00:00
|
|
|
it('should read exit status', async () => {
|
2020-03-31 04:37:58 +00:00
|
|
|
const child = createChild();
|
|
|
|
|
2021-08-08 00:34:19 +00:00
|
|
|
const resultPromise = childResult(child);
|
2020-03-31 04:37:58 +00:00
|
|
|
await mustExist(child.closer)(0);
|
|
|
|
|
|
|
|
const result = await resultPromise;
|
|
|
|
expect(result.status).to.equal(0);
|
|
|
|
});
|
|
|
|
|
2020-06-30 13:14:30 +00:00
|
|
|
it('should reject on failure status', async () => {
|
2020-03-31 04:37:58 +00:00
|
|
|
const child = createChild();
|
|
|
|
|
2021-08-08 00:34:19 +00:00
|
|
|
const resultPromise = childResult(child);
|
2020-03-31 04:37:58 +00:00
|
|
|
await mustExist(child.closer)(1);
|
|
|
|
|
|
|
|
return expect(resultPromise).to.eventually.be.rejectedWith(ChildProcessError);
|
|
|
|
});
|
2021-08-08 19:58:43 +00:00
|
|
|
|
|
|
|
it('should read output', async () => {
|
|
|
|
const child = createChild();
|
|
|
|
stub(child.stdout, 'on').withArgs('data', match.func).yields(Buffer.from('hello'));
|
|
|
|
|
|
|
|
const resultPromise = childResult(child);
|
|
|
|
await mustExist(child.closer)(0);
|
|
|
|
|
|
|
|
return expect(resultPromise).to.eventually.deep.equal({
|
|
|
|
stderr: '',
|
|
|
|
stdout: 'hello',
|
|
|
|
status: 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should reject on error output', async () => {
|
|
|
|
const child = createChild();
|
|
|
|
stub(child.stderr, 'on').withArgs('data', match.func).yields(Buffer.from('nope'));
|
|
|
|
|
|
|
|
const resultPromise = childResult(child);
|
|
|
|
await mustExist(child.closer)(0);
|
|
|
|
|
|
|
|
return expect(resultPromise).to.eventually.be.rejectedWith(ChildProcessError);
|
|
|
|
});
|
2020-03-31 04:37:58 +00:00
|
|
|
});
|
|
|
|
});
|