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');
});
beforeEach(() => {
stream = new WritableStreamBuffer();
handler = new StreamHandler({ stream });
logger = new Logger({ handlers: new Set([handler]) });
});
beforeEach(() => {
stream = new WritableStreamBuffer();
handler = new StreamHandler({ stream });
logger = new Logger({ handlers: new Set([handler]) });
});
async combinedOutput(): Promise {
this._options.stdio = 'pipe';
const promise = this.run();
const buf = new WritableStreamBuffer();
promise.p.stdout.pipe(buf);
promise.p.stderr.pipe(buf);
try {
await promise;
} catch (e) {
e.output = buf.consume().toString();
throw e;
}
return buf.consume().toString();
}
async output(): Promise {
this._options.stdio = 'pipe';
const promise = this.run();
const stdoutBuf = new WritableStreamBuffer();
const stderrBuf = new WritableStreamBuffer();
const combinedBuf = new WritableStreamBuffer();
promise.p.stdout.pipe(stdoutBuf);
promise.p.stdout.pipe(combinedBuf);
promise.p.stderr.pipe(stderrBuf);
promise.p.stderr.pipe(combinedBuf);
try {
await promise;
} catch (e) {
stdoutBuf.end();
stderrBuf.end();
e.output = combinedBuf.consume().toString();
throw e;
}
async output(): Promise {
this._options.stdio = 'pipe';
const promise = this.run();
const stdoutBuf = new WritableStreamBuffer();
const stderrBuf = new WritableStreamBuffer();
const combinedBuf = new WritableStreamBuffer();
promise.p.stdout.pipe(stdoutBuf);
promise.p.stdout.pipe(combinedBuf);
promise.p.stderr.pipe(stderrBuf);
promise.p.stderr.pipe(combinedBuf);
try {
await promise;
} catch (e) {
stdoutBuf.end();
stderrBuf.end();
e.output = combinedBuf.consume().toString();
throw e;
}
stderrBuf.end();
.on('response', res => {
debug('response %d received for %s: (id: %s)', res.status, resource.name, resource.imageId);
if (res.status !== 200) {
const generalErrorMsg = !res.status || res.status >= 500
? 'the server may be experiencing difficulties right now--please try again later.'
: 'the server marked a source image as invalid for this resource.';
errorMarker = true;
const buf = new WritableStreamBuffer();
res.pipe(buf);
buf.on('finish', () => {
let serverMsg = buf.consume().toString();
try {
serverMsg = JSON.parse(serverMsg).Error;
} catch (e) {
serverMsg = formatResponseError(req, res.status, serverMsg);
}
result.error = new Error(
`Error while generating ${strong(resource.name)}: ` +
`${generalErrorMsg}\n\n` +
`Server: "${serverMsg}"`
async output(): Promise {
this._options.stdio = 'pipe';
const promise = this.run();
const stdoutBuf = new WritableStreamBuffer();
const stderrBuf = new WritableStreamBuffer();
const combinedBuf = new WritableStreamBuffer();
promise.p.stdout.pipe(stdoutBuf);
promise.p.stdout.pipe(combinedBuf);
promise.p.stderr.pipe(stderrBuf);
promise.p.stderr.pipe(combinedBuf);
try {
await promise;
} catch (e) {
stdoutBuf.end();
stderrBuf.end();
e.output = combinedBuf.consume().toString();
throw e;
}