Skip to content

Commit

Permalink
run test
Browse files Browse the repository at this point in the history
  • Loading branch information
kb-kerem committed Apr 17, 2024
1 parent c03d204 commit 68fe0e2
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions tests/utils/process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { exec } from 'child_process';
import { FileHandle } from 'fs/promises';
import { ChildProcess } from 'node:child_process';

export type CommandResult = {
statusCode: number;
stdout: string;
stderr: string;
};

export function execute(
command: string,
options: {
cwd?: string;
env?: { [key: string]: string };
pipeOutput?: boolean;
fileOutput?: FileHandle;
displayOutputOnFailure?: boolean;
} = { env: {} },
): Promise<CommandResult> {
const execOpts = {
cwd: options.cwd ?? process.cwd(),
env: {
...process.env,
...options.env,
},
};

return new Promise((resolve, reject) => {
let stderr = '';
let stdout = '';
let output = '';

const proc = exec(command, execOpts, async (err) => {
if (err) {
reject(err);
}
});

proc.on('close', (statusCode: number) => {
if (options.displayOutputOnFailure && statusCode !== 0) {
process.stdout.write(output);
}
resolve({
statusCode,
stderr,
stdout,
});
});

proc.on('exit', (statusCode: number) => {
if (options.displayOutputOnFailure && statusCode !== 0) {
process.stdout.write(output);
}
resolve({
statusCode,
stderr,
stdout,
});
});

proc.stdout?.on('data', (chunk: any) => {
stdout += chunk;
output += chunk;
options.fileOutput?.write(chunk);
if (options.pipeOutput) {
process.stdout.write(chunk);
}
});

proc.stderr?.on('data', (chunk: any) => {
stderr += chunk;
output += chunk;
options.fileOutput?.write(chunk);
if (options.pipeOutput) {
process.stderr.write(chunk);
}
});
});
}

export type ExecuteResult = {
childProcess: ChildProcess;
shutdown: () => void;
};

export function executeInBackground(
command: string,
options: {
cwd?: string;
env?: { [key: string]: string };
fileOutput?: FileHandle;
pipeOutput?: boolean;
},
): ExecuteResult {
const execOpts = {
cwd: options.cwd ?? process.cwd(),
env: {
...process.env,
...(options.env ?? {}),
},
};

const childProcess = exec(command, execOpts);

childProcess.stdout?.on('data', (chunk: any) => {
options.fileOutput?.write(chunk);
if (options.pipeOutput) {
process.stdout.write(chunk);
}
});

childProcess.stderr?.on('data', (chunk: any) => {
options.fileOutput?.write(chunk);
if (options.pipeOutput) {
process.stderr.write(chunk);
}
});

const shutdown = () => {
childProcess.stdout?.destroy();
childProcess.stderr?.destroy();
childProcess.stdin?.destroy();

childProcess.kill();
};

return {
childProcess,
shutdown,
};
}

0 comments on commit 68fe0e2

Please sign in to comment.