From 9d74e68f79cfae59507d1464194cb3af8002eabd Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 5 Apr 2024 17:28:12 +0200 Subject: [PATCH] e2e-test: added --keep option + error message improvements Signed-off-by: Patrik Oldsberg --- packages/e2e-test/cli-report.md | 3 ++- packages/e2e-test/src/commands/index.ts | 6 +++++- packages/e2e-test/src/commands/run.ts | 28 +++++++++++++++++++------ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/packages/e2e-test/cli-report.md b/packages/e2e-test/cli-report.md index 3a3912b8fc..f72344f859 100644 --- a/packages/e2e-test/cli-report.md +++ b/packages/e2e-test/cli-report.md @@ -12,7 +12,7 @@ Options: -h, --help Commands: - run + run [options] help [command] ``` @@ -22,5 +22,6 @@ Commands: Usage: e2e-test run [options] Options: + --keep -h, --help ``` diff --git a/packages/e2e-test/src/commands/index.ts b/packages/e2e-test/src/commands/index.ts index 95d311427c..fd0ba59c30 100644 --- a/packages/e2e-test/src/commands/index.ts +++ b/packages/e2e-test/src/commands/index.ts @@ -18,5 +18,9 @@ import { Command } from 'commander'; import { run } from './run'; export function registerCommands(program: Command) { - program.command('run').description('Run e2e tests').action(run); + program + .command('run') + .option('--keep', 'Do not remove the temporary dir after tests complete') + .description('Run e2e tests') + .action(run); } diff --git a/packages/e2e-test/src/commands/run.ts b/packages/e2e-test/src/commands/run.ts index a1b35049ab..161109267b 100644 --- a/packages/e2e-test/src/commands/run.ts +++ b/packages/e2e-test/src/commands/run.ts @@ -34,6 +34,7 @@ import mysql from 'mysql2/promise'; import pgtools from 'pgtools'; import { findPaths } from '@backstage/cli-common'; +import { OptionValues } from 'commander'; // eslint-disable-next-line no-restricted-syntax const paths = findPaths(__dirname); @@ -45,7 +46,7 @@ const templatePackagePaths = [ 'packages/create-app/templates/default-app/packages/backend/package.json.hbs', ]; -export async function run() { +export async function run(opts: OptionValues) { const rootDir = await fs.mkdtemp(resolvePath(os.tmpdir(), 'backstage-e2e-')); print(`CLI E2E test root: ${rootDir}\n`); @@ -109,8 +110,12 @@ export async function run() { // runner will be destroyed anyway print('All tests successful'); } else { - print('All tests successful, removing test dir'); - await fs.remove(rootDir); + if (opts.keep) { + print(`All tests successful, app dir available at ${appDir}`); + } else { + print('All tests successful, removing test dir'); + await fs.remove(rootDir); + } } // Just in case some child process was left hanging @@ -546,9 +551,20 @@ async function testBackendStart(appDir: string, ...args: string[]) { print('Try to fetch entities from the backend'); // Try fetch entities, should be ok - await fetch('http://localhost:7007/api/catalog/entities').then(res => - res.json(), - ); + const res = await fetch('http://localhost:7007/api/catalog/entities'); + if (!res.ok) { + throw new Error( + `Failed to fetch entities: ${res.status} ${res.statusText}`, + ); + } + const content = await res.text(); + try { + JSON.parse(content); + } catch (error) { + throw new Error( + `Failed to parse entities JSON response: ${error}\n${content}`, + ); + } print('Entities fetched successfully'); successful = true; } catch (error) {