Skip to content

Commit 64e129b

Browse files
committed
fix(bazel/api-golden): allow api golden tooling to handle single golden files (#2900)
Allow for the golden tooling to handle when a specific golden file is provided rather than a directory PR Close #2900
1 parent 872ab8f commit 64e129b

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

bazel/api-golden/index_npm_packages.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,22 @@ async function main(
3434
stripExportPattern: RegExp,
3535
typeNames: string[],
3636
) {
37+
/** Whether the goldenDir provided is actually pointing to a single file. */
38+
const singleFileMode = fs.existsSync(goldenDir) && fs.statSync(goldenDir).isFile();
3739
// TODO(ESM) This can be replaced with an actual ESM import when `ts_library` is
3840
// guaranteed to be ESM-only and supports the `mts` extension.
3941
const chalk = {red: (v: string) => v, yellow: (v: string) => v};
4042

4143
const packageJsonPath = path.join(npmPackageDir, 'package.json');
4244
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')) as PackageJson;
4345
const entryPoints = findEntryPointsWithinNpmPackage(npmPackageDir, packageJson);
46+
if (entryPoints.length === 0) {
47+
console.error(
48+
'No entry points were found in the provided package for determining the API surface.',
49+
);
50+
process.exitCode = 3;
51+
return;
52+
}
4453
const worker = new Piscina<Parameters<typeof testApiGolden>, string>({
4554
filename: path.resolve(__dirname, './test_api_report.js'),
4655
});
@@ -50,7 +59,11 @@ async function main(
5059
// entry-point we maintain a separate golden file. These golden files are
5160
// based on the name of the defining NodeJS exports subpath in the NPM package,
5261
// See: https://api-extractor.com/pages/overview/demo_api_report/.
53-
const goldenName = path.join(subpath, 'index.api.md');
62+
let goldenName = path.join(subpath, 'index.api.md');
63+
// In single file mode, the subpath is the golden file.
64+
if (singleFileMode) {
65+
goldenName = subpath;
66+
}
5467
const goldenFilePath = path.join(goldenDir, goldenName);
5568
const moduleName = normalizePathToPosix(path.join(packageJson.name, subpath));
5669

@@ -105,14 +118,28 @@ async function main(
105118
const allTestsSucceeding = results.every((r) => r === true);
106119

107120
if (outdatedGoldens.length) {
108-
console.error(chalk.red(`The following goldens are outdated:`));
109-
outdatedGoldens.forEach((name) => console.info(`- ${name}`));
110-
console.info();
111-
console.info(
112-
chalk.yellow(
113-
`The goldens can be updated by running: yarn bazel run ${process.env.TEST_TARGET}.accept`,
114-
),
115-
);
121+
console.error();
122+
console.error(Array(80).fill('=').join(''));
123+
console.error(`${Array(35).fill('=').join('')} RESULTS ${Array(36).fill('=').join('')}`);
124+
console.error(Array(80).fill('=').join(''));
125+
if (singleFileMode) {
126+
console.error(
127+
chalk.red(
128+
`The golden is out of date and can be updated by running:\n - yarn bazel run ${process.env.TEST_TARGET}.accept`,
129+
),
130+
);
131+
} else {
132+
console.error(chalk.red(`The following goldens are outdated:`));
133+
outdatedGoldens.forEach((name) => console.info(`- ${name}`));
134+
console.info();
135+
console.info(
136+
chalk.yellow(
137+
`The goldens can be updated by running:\n - yarn bazel run ${process.env.TEST_TARGET}.accept`,
138+
),
139+
);
140+
}
141+
console.error(Array(80).fill('=').join(''));
142+
console.error();
116143
}
117144

118145
// Bazel expects `3` as exit code for failing tests.

0 commit comments

Comments
 (0)