From 4e20482b83186623330d460c673034154e3a00e6 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Tue, 11 Jul 2023 16:00:20 +0200 Subject: [PATCH] test(e2e): Fix timeout & types --- packages/e2e-tests/lib/types.ts | 2 +- packages/e2e-tests/lib/utils.ts | 58 ++++++++++++------- packages/e2e-tests/package.json | 3 +- .../create-remix-app/test-recipe.json | 2 +- packages/e2e-tests/tsconfig.json | 1 + 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/packages/e2e-tests/lib/types.ts b/packages/e2e-tests/lib/types.ts index a86c748ef6d0..f06094ce0426 100644 --- a/packages/e2e-tests/lib/types.ts +++ b/packages/e2e-tests/lib/types.ts @@ -47,4 +47,4 @@ export interface RecipeTestResult extends RecipeInstance { tests: TestResult[]; } -export type Env = Record; +export type Env = Record; diff --git a/packages/e2e-tests/lib/utils.ts b/packages/e2e-tests/lib/utils.ts index 0c786a34f520..ef6d65dc84dc 100644 --- a/packages/e2e-tests/lib/utils.ts +++ b/packages/e2e-tests/lib/utils.ts @@ -17,22 +17,50 @@ interface SpawnAsync { status: number | null; } -export function spawnAsync( - cmd: string, - options?: - | childProcess.SpawnOptionsWithoutStdio - | childProcess.SpawnOptionsWithStdioTuple, - input?: string, -): Promise { - const start = Date.now(); +interface SpawnOptions { + timeout?: number; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + env?: any; + cwd?: string; +} + +export function spawnAsync(cmd: string, options?: SpawnOptions, input?: string): Promise { + const timeoutMs = options?.timeout || 60_000 * 5; return new Promise(resolve => { const cp = childProcess.spawn(cmd, { shell: true, ...options }); + // Ensure we properly time out after max. 5 min per command + let timeout: ReturnType | undefined = setTimeout(() => { + console.log(`Command "${cmd}" timed out after 5 minutes.`); + cp.kill(); + end(null, `ETDIMEDOUT: Process timed out after ${timeoutMs} ms.`); + }, timeoutMs); + const stderr: unknown[] = []; const stdout: string[] = []; let error: Error | undefined; + function end(status: number | null, errorMessage?: string): void { + // This means we already ended + if (!timeout) { + return; + } + + if (!error && errorMessage) { + error = new Error(errorMessage); + } + + clearTimeout(timeout); + timeout = undefined; + resolve({ + stdout: stdout.join(''), + stderr: stderr.join(''), + error: error || (status !== 0 ? new Error(`Process exited with status ${status}`) : undefined), + status, + }); + } + cp.stdout.on('data', data => { stdout.push(data ? (data as object).toString() : ''); }); @@ -46,19 +74,7 @@ export function spawnAsync( }); cp.on('close', status => { - const end = Date.now(); - - // We manually mark this as timed out if the process takes too long - if (!error && status === 1 && options?.timeout && end >= start + options.timeout) { - error = new Error(`ETDIMEDOUT: Process timed out after ${options.timeout} ms.`); - } - - resolve({ - stdout: stdout.join(''), - stderr: stderr.join(''), - error: error || (status !== 0 ? new Error(`Process exited with status ${status}`) : undefined), - status, - }); + end(status); }); if (input) { diff --git a/packages/e2e-tests/package.json b/packages/e2e-tests/package.json index 21a8f0e42552..c8b4473d24e4 100644 --- a/packages/e2e-tests/package.json +++ b/packages/e2e-tests/package.json @@ -10,9 +10,10 @@ "fix": "run-s fix:eslint fix:prettier", "fix:eslint": "eslint . --format stylish --fix", "fix:prettier": "prettier --config ../../.prettierrc.json --write . ", - "lint": "run-s lint:prettier lint:eslint", + "lint": "run-s lint:prettier lint:eslint lint:ts", "lint:eslint": "eslint . --format stylish", "lint:prettier": "prettier --config ../../.prettierrc.json --check .", + "lint:ts": "tsc --noEmit", "test:e2e": "run-s test:validate-configuration test:validate-test-app-setups test:run", "test:run": "ts-node run.ts", "test:validate-configuration": "ts-node validate-verdaccio-configuration.ts", diff --git a/packages/e2e-tests/test-applications/create-remix-app/test-recipe.json b/packages/e2e-tests/test-applications/create-remix-app/test-recipe.json index fb81b49c1195..dc16b21af54c 100644 --- a/packages/e2e-tests/test-applications/create-remix-app/test-recipe.json +++ b/packages/e2e-tests/test-applications/create-remix-app/test-recipe.json @@ -1,6 +1,6 @@ { "$schema": "../../test-recipe-schema.json", "testApplicationName": "create-remix-app", - "buildCommand": "# disabling build test because it is causing timeouts: https://github.com/getsentry/sentry-javascript/pull/8486 pnpm install && pnpm build && pnpm start", + "buildCommand": "pnpm install && pnpm build", "tests": [] } diff --git a/packages/e2e-tests/tsconfig.json b/packages/e2e-tests/tsconfig.json index 2e051315981a..026669f596c0 100644 --- a/packages/e2e-tests/tsconfig.json +++ b/packages/e2e-tests/tsconfig.json @@ -1,5 +1,6 @@ { "extends": "../../tsconfig.json", + "exclude": ["node_modules", "test-applications"], "compilerOptions": { "types": ["node"] }