Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bazel/api-golden/api_golden_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def api_golden_test(
entry_point,
data = [],
strip_export_pattern = default_strip_export_pattern,
types = [],
types = {},
**kwargs):
# We can't directly write `package.json` as this could cause conflicts
# if there are multiple individual file tests in the same Bazel package.
Expand Down
17 changes: 13 additions & 4 deletions bazel/api-golden/api_golden_test_npm_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def api_golden_test_npm_package(
npm_package,
data = [],
strip_export_pattern = default_strip_export_pattern,
types = [],
types = {},
**kwargs):
"""Builds an API report for all entry-points within the given NPM package and compares it.

Expand All @@ -31,13 +31,22 @@ def api_golden_test_npm_package(
kwargs["tags"] = kwargs.get("tags", []) + ["api_guard"]

data.append("@devinfra//bazel/api-golden")
data.extend(types)

types_name_and_path = []
for label, n in types.items():
data.append(label)
type_label = Label(label)

# The path for the package is determined by the package location and name and found relative
# to the exec root during our js_test and js_binary runs.
path = "{package}/{name}".format(package = type_label.package, name = type_label.name)
types_name_and_path.append("{name}|{path}".format(name = n, path = path))

js_test(
name = name,
data = data,
entry_point = "@devinfra//bazel/api-golden:index_npm_packages.cjs",
args = [golden_dir, npm_package, "false", quoted_export_pattern],
args = [golden_dir, npm_package, "false", quoted_export_pattern] + types_name_and_path,
**kwargs
)

Expand All @@ -46,6 +55,6 @@ def api_golden_test_npm_package(
testonly = True,
data = data,
entry_point = "@devinfra//bazel/api-golden:index_npm_packages.cjs",
args = [golden_dir, npm_package, "true", quoted_export_pattern],
args = [golden_dir, npm_package, "true", quoted_export_pattern] + types_name_and_path,
**kwargs
)
4 changes: 2 additions & 2 deletions bazel/api-golden/index_npm_packages.cts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function main(
npmPackageDir: string,
approveGolden: boolean,
stripExportPattern: RegExp,
typeNames: string[],
typeNamesAndLocations: string[],
) {
/** Whether the goldenDir provided is actually pointing to a single file. */
const singleFileMode = fs.existsSync(goldenDir) && fs.statSync(goldenDir).isFile();
Expand Down Expand Up @@ -72,7 +72,7 @@ async function main(
const actual = await worker.run([
typesEntryPointPath,
stripExportPattern,
typeNames,
typeNamesAndLocations,
packageJsonPath,
moduleName,
]);
Expand Down
4 changes: 3 additions & 1 deletion bazel/api-golden/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ api_golden_test_npm_package(
npm_package = "bazel/api-golden/test/fixtures/test_package",
# API extractor needs to be able to resolve `@babel/core` due to an aliased namespace
# we expose as part of the `nested.d.ts` fake entry-point.
types = ["//bazel:node_modules/@types/babel__core"],
types = {
"//bazel:node_modules/@types/babel__core": "babel",
},
)

api_golden_test_npm_package(
Expand Down
19 changes: 15 additions & 4 deletions bazel/api-golden/test_api_report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ const _origFetchAstModuleExportInfo = ExportAnalyzer.prototype.fetchAstModuleExp
* @param indexFilePath Entry point file that is analyzed to build the API report.
* @param stripExportPattern Regular Expression that can be used to filter out exports
* from the API report.
* @param typePackageNames Package names for which types should be included in the analysis of the
* API-report entry-point. Packages are expected to exist within the external `npm` workspace.
* @param typeNamesAndLocations Package names and their relative filepath location with a |
* character as a separator in the string. Describes which types should be included in the
* analysis of the API-report entry-point. Packages are expected to exist within the
* external `npm` workspace.
* @param packageJsonPath Optional path to a `package.json` file that contains the entry
* point. Note that the `package.json` is currently only used by `api-extractor` to determine
* the package name displayed within the API golden.
Expand All @@ -49,17 +51,26 @@ const _origFetchAstModuleExportInfo = ExportAnalyzer.prototype.fetchAstModuleExp
export async function testApiGolden(
indexFilePath: string,
stripExportPattern: RegExp,
typeNames: string[],
typeNamesAndLocations: string[],
packageJsonPath: string,
customPackageName: string,
): Promise<string | null> {
const tempDir =
process.env.TEST_TMPDIR ??
(await fs.promises.mkdtemp(path.join(os.tmpdir(), 'api-golden-rule')));
const typeNames: string[] = [];
const typeLocations: string[] = [];

typeNamesAndLocations.forEach((nameAndLocation) => {
const [name, location] = nameAndLocation.split('|');
typeNames.push(name);
typeLocations.push(`./${location}`);
});

const configObject: IConfigFile = {
compiler: {
overrideTsconfig: {
files: [indexFilePath],
files: [indexFilePath, ...typeLocations],
compilerOptions: {
paths: {},
types: typeNames,
Expand Down
Loading