Files
privacy.sexy/tests/checks/desktop-runtime-errors/check-desktop-runtime-errors/utils/run-command.ts
undergroundwires 19e42c9c52 Refactor and improve external URL checks
- Move external URL checks to its own module under `tests/`. This
  separates them from integration test, addressing long runs and
  frequent failures that led to ignoring test results.
- Move `check-desktop-runtime-errors` to `tests/checks` to keep all
  test-related checks into one directory.
- Replace `ts-node` with `vite` for running
  `check-desktop-runtime-errors` to maintain a consistent execution
  environment across checks.
- Implement a timeout for each fetch call.
- Be nice to external sources, wait 5 seconds before sending another
  request to an URL under same domain. This solves rate-limiting issues.
- Instead of running test on every push/pull request, run them only
  weekly.
- Do not run tests on each commit/PR but only scheduled (weekly) to
  minimize noise.
- Fix URLs are not captured correctly inside backticks or parenthesis.
2023-09-01 00:18:47 +02:00

59 lines
1.4 KiB
TypeScript

import { exec } from 'child_process';
import { indentText } from './text';
import type { ExecOptions, ExecException } from 'child_process';
const TIMEOUT_IN_SECONDS = 180;
const MAX_OUTPUT_BUFFER_SIZE = 1024 * 1024; // 1 MB
export function runCommand(
command: string,
options?: ExecOptions,
): Promise<CommandResult> {
return new Promise((resolve) => {
options = {
cwd: process.cwd(),
timeout: TIMEOUT_IN_SECONDS * 1000,
maxBuffer: MAX_OUTPUT_BUFFER_SIZE * 2,
...(options ?? {}),
};
exec(command, options, (error, stdout, stderr) => {
let errorText: string | undefined;
if (error || stderr?.length > 0) {
errorText = formatError(command, error, stdout, stderr);
}
resolve({
stdout,
error: errorText,
});
});
});
}
export interface CommandResult {
readonly stdout: string;
readonly error?: string;
}
function formatError(
command: string,
error: ExecException | undefined,
stdout: string | undefined,
stderr: string | undefined,
) {
const errorParts = [
'Error while running command.',
`Command:\n${indentText(command, 1)}`,
];
if (error?.toString().trim()) {
errorParts.push(`Error:\n${indentText(error.toString(), 1)}`);
}
if (stderr?.trim()) {
errorParts.push(`stderr:\n${indentText(stderr, 1)}`);
}
if (stdout?.trim()) {
errorParts.push(`stdout:\n${indentText(stdout, 1)}`);
}
return errorParts.join('\n---\n');
}