diff --git a/bazel/api-golden/api_golden_test.bzl b/bazel/api-golden/api_golden_test.bzl index d8f6eb624..54419d32c 100644 --- a/bazel/api-golden/api_golden_test.bzl +++ b/bazel/api-golden/api_golden_test.bzl @@ -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. diff --git a/bazel/api-golden/api_golden_test_npm_package.bzl b/bazel/api-golden/api_golden_test_npm_package.bzl index 062bd66dc..74744c69a 100644 --- a/bazel/api-golden/api_golden_test_npm_package.bzl +++ b/bazel/api-golden/api_golden_test_npm_package.bzl @@ -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. @@ -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 ) @@ -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 ) diff --git a/bazel/api-golden/index_npm_packages.cts b/bazel/api-golden/index_npm_packages.cts index 3899738ea..f3c399ddc 100644 --- a/bazel/api-golden/index_npm_packages.cts +++ b/bazel/api-golden/index_npm_packages.cts @@ -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(); @@ -72,7 +72,7 @@ async function main( const actual = await worker.run([ typesEntryPointPath, stripExportPattern, - typeNames, + typeNamesAndLocations, packageJsonPath, moduleName, ]); diff --git a/bazel/api-golden/test/BUILD.bazel b/bazel/api-golden/test/BUILD.bazel index 697df45f4..ef4616aa1 100644 --- a/bazel/api-golden/test/BUILD.bazel +++ b/bazel/api-golden/test/BUILD.bazel @@ -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( diff --git a/bazel/api-golden/test_api_report.ts b/bazel/api-golden/test_api_report.ts index 88708435b..b4d5ba796 100644 --- a/bazel/api-golden/test_api_report.ts +++ b/bazel/api-golden/test_api_report.ts @@ -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. @@ -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 { 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,