Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should error for signal exit', async () => {
const cmd = new Subprocess('cmd', []);
const mockSpawnStdout = new ReadableStreamBuffer();
const mockSpawnStderr = new ReadableStreamBuffer();
const cp = new class extends EventEmitter { stdout = mockSpawnStdout; stderr = mockSpawnStderr; };
mockCrossSpawn.mockImplementation(() => cp);
const buf = new WritableStreamBuffer();
const promise = cmd.run();
promise.p.stdout.pipe(buf);
promise.p.stderr.pipe(buf);
mockSpawnStdout.stop();
mockSpawnStderr.stop();
await promisifyEvent(buf, 'finish');
cp.emit('close', null, 'SIGINT');
await expect(promise).rejects.toThrow('Signal exit from subprocess.');
});
it('should pipe stdout and stderr in run()', async () => {
const cmd = new Subprocess('cmd', []);
const mockSpawnStdout = new ReadableStreamBuffer();
const mockSpawnStderr = new ReadableStreamBuffer();
const cp = new class extends EventEmitter { stdout = mockSpawnStdout; stderr = mockSpawnStderr; };
mockCrossSpawn.mockImplementation(() => cp);
const stdoutMock = new WritableStreamBuffer();
const stderrMock = new WritableStreamBuffer();
const promise = cmd.run();
promise.p.stdout.pipe(stdoutMock);
promise.p.stderr.pipe(stderrMock);
mockSpawnStdout.feed('hello world!');
mockSpawnStdout.stop();
mockSpawnStderr.feed('oh no!');
mockSpawnStderr.stop();
await Promise.all([promisifyEvent(stdoutMock, 'finish'), promisifyEvent(stderrMock, 'finish')]);
cp.emit('close', 0);
await promise;
expect(mockCrossSpawn).toHaveBeenCalledTimes(1);
expect(stdoutMock.consume().toString()).toEqual('hello world!');
expect(stderrMock.consume().toString()).toEqual('oh no!');
});
it('should create a writable stream', () => {
const stream = new WritableStreamBuffer();
const handler = new StreamHandler({ stream });
const logger = new Logger({ handlers: new Set([handler]) });
const ws = logger.createWriteStream();
ws.write('hi');
expect(stream.consume().toString()).toEqual('hi\n');
});
it('should pipe stdout and stderr in run()', async () => {
const cmd = new Subprocess('cmd', []);
const mockSpawnStdout = new ReadableStreamBuffer();
const mockSpawnStderr = new ReadableStreamBuffer();
const cp = new class extends EventEmitter { stdout = mockSpawnStdout; stderr = mockSpawnStderr; };
mockCrossSpawn.mockImplementation(() => cp);
const stdoutMock = new WritableStreamBuffer();
const stderrMock = new WritableStreamBuffer();
const promise = cmd.run();
promise.p.stdout.pipe(stdoutMock);
promise.p.stderr.pipe(stderrMock);
mockSpawnStdout.feed('hello world!');
mockSpawnStdout.stop();
mockSpawnStderr.feed('oh no!');
mockSpawnStderr.stop();
await Promise.all([promisifyEvent(stdoutMock, 'finish'), promisifyEvent(stderrMock, 'finish')]);
cp.emit('close', 0);
await promise;
expect(mockCrossSpawn).toHaveBeenCalledTimes(1);
expect(stdoutMock.consume().toString()).toEqual('hello world!');
it('should have child process in run() return value', async () => {
const cmd = new Subprocess('cmd', []);
const mockSpawnStdout = new ReadableStreamBuffer();
const mockSpawnStderr = new ReadableStreamBuffer();
const cp = new class extends EventEmitter { stdout = mockSpawnStdout; stderr = mockSpawnStderr; };
mockCrossSpawn.mockImplementation(() => cp);
const promise = cmd.run();
expect(promise.p).toBe(cp);
});
it('should error with combined output for output()', async done => {
const cmd = new Subprocess('cmd', []);
const mockSpawnStdout = new ReadableStreamBuffer();
const mockSpawnStderr = new ReadableStreamBuffer();
const cp = new class extends EventEmitter { stdout = mockSpawnStdout; stderr = mockSpawnStderr; };
mockCrossSpawn.mockImplementation(() => cp);
const p = cmd.output();
const outletter = '1';
const errletter = '2';
const outinput = outletter.repeat(26);
const errinput = errletter.repeat(26);
mockSpawnStdout.feed(outinput);
mockSpawnStdout.stop();
mockSpawnStderr.feed(errinput);
mockSpawnStderr.stop();
await Promise.all([promisifyEvent(mockSpawnStdout, 'end'), promisifyEvent(mockSpawnStderr, 'end')]);
cp.emit('close', 1);
try {
await p;
} catch (e) {
it('should error for signal exit', async () => {
const cmd = new Subprocess('cmd', []);
const mockSpawnStdout = new ReadableStreamBuffer();
const mockSpawnStderr = new ReadableStreamBuffer();
const cp = new class extends EventEmitter { stdout = mockSpawnStdout; stderr = mockSpawnStderr; };
mockCrossSpawn.mockImplementation(() => cp);
const buf = new WritableStreamBuffer();
const promise = cmd.run();
promise.p.stdout.pipe(buf);
promise.p.stderr.pipe(buf);
mockSpawnStdout.stop();
mockSpawnStderr.stop();
await promisifyEvent(buf, 'finish');
cp.emit('close', null, 'SIGINT');
await expect(promise).rejects.toThrow('Signal exit from subprocess.');
});
it('should have child process in run() return value', async () => {
const cmd = new Subprocess('cmd', []);
const mockSpawnStdout = new ReadableStreamBuffer();
const mockSpawnStderr = new ReadableStreamBuffer();
const cp = new class extends EventEmitter { stdout = mockSpawnStdout; stderr = mockSpawnStderr; };
mockCrossSpawn.mockImplementation(() => cp);
const promise = cmd.run();
expect(promise.p).toBe(cp);
});
it('should pipe stdout and stderr in run()', async () => {
const cmd = new Subprocess('cmd', []);
const mockSpawnStdout = new ReadableStreamBuffer();
const mockSpawnStderr = new ReadableStreamBuffer();
const cp = new class extends EventEmitter { stdout = mockSpawnStdout; stderr = mockSpawnStderr; };
mockCrossSpawn.mockImplementation(() => cp);
const stdoutMock = new WritableStreamBuffer();
const stderrMock = new WritableStreamBuffer();
const promise = cmd.run();
promise.p.stdout.pipe(stdoutMock);
promise.p.stderr.pipe(stderrMock);
mockSpawnStdout.feed('hello world!');
mockSpawnStdout.stop();
mockSpawnStderr.feed('oh no!');
mockSpawnStderr.stop();
await Promise.all([promisifyEvent(stdoutMock, 'finish'), promisifyEvent(stderrMock, 'finish')]);
cp.emit('close', 0);
await promise;
expect(mockCrossSpawn).toHaveBeenCalledTimes(1);
it('should error with combined output for output()', async done => {
const cmd = new Subprocess('cmd', []);
const mockSpawnStdout = new ReadableStreamBuffer();
const mockSpawnStderr = new ReadableStreamBuffer();
const cp = new class extends EventEmitter { stdout = mockSpawnStdout; stderr = mockSpawnStderr; };
mockCrossSpawn.mockImplementation(() => cp);
const p = cmd.output();
const outletter = '1';
const errletter = '2';
const outinput = outletter.repeat(26);
const errinput = errletter.repeat(26);
mockSpawnStdout.feed(outinput);
mockSpawnStdout.stop();
mockSpawnStderr.feed(errinput);
mockSpawnStderr.stop();
await Promise.all([promisifyEvent(mockSpawnStdout, 'end'), promisifyEvent(mockSpawnStderr, 'end')]);
cp.emit('close', 1);
try {
await p;