Skip to content

Commit 0eadeeb

Browse files
committed
build: speed up golden testing by parallelizing
Speeds up golden testing by parallelizing.
1 parent 492cc1f commit 0eadeeb

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

bazel/api-golden/index_npm_packages.ts

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,43 +42,54 @@ async function main(
4242
const entryPoints = findEntryPointsWithinNpmPackage(npmPackageDir, packageJson);
4343
const outdatedGoldens: string[] = [];
4444

45-
let allTestsSucceeding = true;
45+
const tasks: Promise<boolean>[] = [];
4646

4747
for (const {subpath, typesEntryPointPath} of entryPoints) {
48-
// API extractor generates API reports as markdown files. For each types
49-
// entry-point we maintain a separate golden file. These golden files are
50-
// based on the name of the defining NodeJS exports subpath in the NPM package,
51-
// See: https://api-extractor.com/pages/overview/demo_api_report/.
52-
const goldenName = path.join(subpath, 'index.api.md');
53-
const goldenFilePath = path.join(goldenDir, goldenName);
54-
const moduleName = normalizePathToPosix(path.join(packageJson.name, subpath));
55-
56-
const actual = await testApiGolden(
57-
typesEntryPointPath,
58-
stripExportPattern,
59-
typeNames,
60-
packageJsonPath,
61-
moduleName,
62-
);
48+
tasks.push(
49+
(async () => {
50+
// API extractor generates API reports as markdown files. For each types
51+
// entry-point we maintain a separate golden file. These golden files are
52+
// based on the name of the defining NodeJS exports subpath in the NPM package,
53+
// See: https://api-extractor.com/pages/overview/demo_api_report/.
54+
const goldenName = path.join(subpath, 'index.api.md');
55+
const goldenFilePath = path.join(goldenDir, goldenName);
56+
const moduleName = normalizePathToPosix(path.join(packageJson.name, subpath));
57+
58+
const actual = await testApiGolden(
59+
typesEntryPointPath,
60+
stripExportPattern,
61+
typeNames,
62+
packageJsonPath,
63+
moduleName,
64+
);
65+
66+
if (actual === null) {
67+
console.error(
68+
`Could not generate API golden for subpath: "${subpath}". See errors above.`,
69+
);
70+
process.exit(1);
71+
}
6372

64-
if (actual === null) {
65-
console.error(`Could not generate API golden for subpath: "${subpath}". See errors above.`);
66-
process.exit(1);
67-
}
68-
69-
if (approveGolden) {
70-
fs.mkdirSync(path.dirname(goldenFilePath), {recursive: true});
71-
fs.writeFileSync(goldenFilePath, actual, 'utf8');
72-
} else {
73-
const expected = fs.readFileSync(goldenFilePath, 'utf8');
74-
if (actual !== expected) {
75-
// Keep track of outdated goldens for error message.
76-
outdatedGoldens.push(goldenName);
77-
allTestsSucceeding = false;
78-
}
79-
}
73+
if (approveGolden) {
74+
await fs.promises.mkdir(path.dirname(goldenFilePath), {recursive: true});
75+
await fs.promises.writeFile(goldenFilePath, actual, 'utf8');
76+
} else {
77+
const expected = await fs.promises.readFile(goldenFilePath, 'utf8');
78+
if (actual !== expected) {
79+
// Keep track of outdated goldens for error message.
80+
outdatedGoldens.push(goldenName);
81+
return false;
82+
}
83+
}
84+
85+
return true;
86+
})(),
87+
);
8088
}
8189

90+
const results = await Promise.all(tasks);
91+
const allTestsSucceeding = results.every((r) => r === true);
92+
8293
if (outdatedGoldens.length) {
8394
console.error(chalk.red(`The following goldens are outdated:`));
8495
outdatedGoldens.forEach((name) => console.info(`- ${name}`));

bazel/api-golden/test_api_report.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export async function testApiGolden(
5555
customPackageName: string,
5656
): Promise<string | null> {
5757
const tempDir =
58-
process.env.TEST_TMPDIR ?? fs.mkdtempSync(path.join(os.tmpdir(), 'api-golden-rule'));
58+
process.env.TEST_TMPDIR ??
59+
(await fs.promises.mkdtemp(path.join(os.tmpdir(), 'api-golden-rule')));
5960
const rjsMode = process.env['RJS_MODE'] === 'true';
6061

6162
let resolvedTypePackages: Awaited<ReturnType<typeof resolveTypePackages>> | null = null;
@@ -144,8 +145,8 @@ export async function testApiGolden(
144145
if (!result.succeeded) {
145146
return null;
146147
}
147-
const reportOut = fs.readFileSync(reportTmpOutPath, 'utf8');
148-
fs.rmSync(reportTmpOutPath);
148+
const reportOut = await fs.promises.readFile(reportTmpOutPath, 'utf8');
149+
await fs.promises.rm(reportTmpOutPath);
149150
return reportOut;
150151
}
151152

0 commit comments

Comments
 (0)