From 25943ab18b56af18f5941779500b8d01605b1352 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 8 Oct 2024 00:14:53 +0200 Subject: [PATCH] cli: move repot test result processing to runner, and by path Signed-off-by: Patrik Oldsberg --- .../cli/config/jestCacheResultProcessor.cjs | 21 +-------- packages/cli/src/commands/repo/test.ts | 44 ++++++++++++++++--- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/packages/cli/config/jestCacheResultProcessor.cjs b/packages/cli/config/jestCacheResultProcessor.cjs index ae7de0ba3c..4af401d8b5 100644 --- a/packages/cli/config/jestCacheResultProcessor.cjs +++ b/packages/cli/config/jestCacheResultProcessor.cjs @@ -16,25 +16,8 @@ module.exports = async results => { const cache = global.__backstageCli_jestSuccessCache; - if (!cache) { - return results; + if (cache) { + await cache.reportResults(results); } - - const successful = new Set(); - const failed = new Set(); - for (const testResult of results.testResults) { - const projectName = testResult.displayName.name; - if (testResult.numFailingTests > 0) { - failed.add(projectName); - successful.delete(projectName); - } else if (!failed.has(projectName)) { - successful.add(projectName); - } - } - - await cache.reportResults({ - successful: successful, - }); - return results; }; diff --git a/packages/cli/src/commands/repo/test.ts b/packages/cli/src/commands/repo/test.ts index 64ea23a99e..4e11e07515 100644 --- a/packages/cli/src/commands/repo/test.ts +++ b/packages/cli/src/commands/repo/test.ts @@ -23,6 +23,7 @@ import { Command, OptionValues } from 'commander'; import { Lockfile, PackageGraph } from '@backstage/cli-node'; import { paths } from '../../lib/paths'; import { runCheck, runPlain } from '../../lib/run'; +import { isChildPath } from '@backstage/cli-common'; type JestProject = { displayName: string; @@ -34,7 +35,18 @@ interface GlobalWithCache extends Global { projectConfigs: JestProject[], globalConfig: unknown, ): Promise; - reportResults(options: { successful: Set }): Promise; + reportResults(results: { + testResults: Array<{ + displayName?: { name: string }; + numFailingTests: number; + testFilePath: string; + testExecError: { + message: string; + stack: string; + }; + failureMessage: string; + }>; + }): Promise; }; } @@ -267,6 +279,8 @@ export async function command(opts: OptionValues, cmd: Command): Promise { ); } + const graph = await getPackageGraph(); + // Shared state for the bridge const projectHashes = new Map(); const outputSuccessCache = new Array(); @@ -276,7 +290,6 @@ export async function command(opts: OptionValues, cmd: Command): Promise { const globalWithCache = global as GlobalWithCache; globalWithCache.__backstageCli_jestSuccessCache = { async filterConfigs(projectConfigs, globalRootConfig) { - const graph = await getPackageGraph(); const cache = await readCache(cacheDir); const lockfile = await Lockfile.load( paths.resolveTargetRoot('yarn.lock'), @@ -327,14 +340,33 @@ export async function command(opts: OptionValues, cmd: Command): Promise { return project; }); }, - async reportResults(options) { - for (const packageName of options.successful) { - const sha = projectHashes.get(packageName); + async reportResults(results) { + const successful = new Set(); + const failed = new Set(); + for (const testResult of results.testResults) { + for (const [pkgName, pkg] of graph) { + if (isChildPath(pkg.dir, testResult.testFilePath)) { + if ( + testResult.testExecError || + testResult.failureMessage || + testResult.numFailingTests > 0 + ) { + failed.add(pkgName); + successful.delete(pkgName); + } else if (!failed.has(pkgName)) { + successful.add(pkgName); + } + break; + } + } + } + for (const pkgName of successful) { + const sha = projectHashes.get(pkgName); if (sha) { outputSuccessCache.push(sha); } } - writeCache(cacheDir, outputSuccessCache); + await writeCache(cacheDir, outputSuccessCache); }, }; }