Skip to content

Commit 0bb2d3a

Browse files
devversionandrewseguin
authored andcommitted
build: add global types to api golden tests of youtube-player and google-maps
We refactored how type resolution works in the API golden test. The Bazel NodeJS linker no longer runs for API golden tests, making the API golden tests more reliable and less reliant on unexpected behavior/side-effects. A consequence is that Microsofts API extractor is no longer able to automatically resolve type definitions like `@types/google.maps`. These now need to be explicity referenced/loaded similar to how it would happen if we'd test the API goldens through the NPM package (using `api_golden_test_npm_package` -- so this is more consistent now).
1 parent bcebb25 commit 0bb2d3a

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

tools/public_api_guard/BUILD.bazel

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@ load(":generate-guard-tests.bzl", "generate_test_targets")
44

55
package(default_visibility = ["//visibility:public"])
66

7-
golden_files = [
8-
# Primary entry-points.
9-
"cdk/cdk.md",
10-
"material/material.md",
11-
"youtube-player/youtube-player.md",
12-
"google-maps/google-maps.md",
13-
] + ["cdk/%s.md" % e for e in CDK_ENTRYPOINTS] + \
14-
["material/%s.md" % e for e in MATERIAL_ENTRYPOINTS + MATERIAL_TESTING_ENTRYPOINTS]
7+
generate_test_targets(
8+
targets =
9+
[
10+
"//src/cdk",
11+
"//src/material",
12+
] + ["//src/cdk/%s" % e for e in CDK_ENTRYPOINTS] +
13+
["//src/material/%s" % e for e in MATERIAL_ENTRYPOINTS + MATERIAL_TESTING_ENTRYPOINTS],
14+
)
1515

16-
# Generate the API guard test targets for each golden file in the current package.
17-
generate_test_targets(targets = golden_files)
16+
generate_test_targets(
17+
targets = [
18+
"//src/youtube-player",
19+
],
20+
types = ["@npm//@types/youtube"],
21+
)
22+
23+
generate_test_targets(
24+
targets = [
25+
"//src/google-maps",
26+
],
27+
types = ["@npm//@types/google.maps"],
28+
)
Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
load("@npm//@angular/dev-infra-private/bazel/api-golden:index.bzl", "api_golden_test")
22

3-
"""
4-
Macro for generating `api_golden_test` Bazel test targets. Since there are multiple golden files
5-
in this package, we don't want to manually set up all golden files. In order to make this
6-
more maintainable, we allow passing a list of golden files that will be automatically verified
7-
against the associated source entry point.
8-
"""
3+
def generate_test_targets(targets, types = []):
4+
"""Macro for generating `api_golden_test` Bazel test targets. Since there are multiple
5+
golden files in this package, we don't want to manually set up all golden files.
6+
7+
In order to make this more maintainable, we allow passing a list of targets that will
8+
be automatically verified against the corresponding golden report file.
9+
"""
10+
11+
for target in targets:
12+
label = Label(target)
913

10-
def generate_test_targets(targets):
11-
for target_name in targets:
1214
# Splits the path that is relative to the current directory into the package name and
1315
# entry point tail path. The package name is always the first path segment (e.g. "cdk/")
14-
[package_name, entry_point_tail] = target_name.split("/", 1)
15-
16-
# Name of the entry-point (e.g. "a11y", "drag-drop", "platform")
17-
entry_point = entry_point_tail[:-len(".md")]
16+
segments = label.package[len("src/"):].split("/", 1)
17+
package_name = segments[0]
1818

19-
# Name of the .d.ts file that will be produced. We replace the slashes in the entry
20-
# point name with underscores so that we can get a flat directory of golden files.
21-
golden_file = "%s/%s" % (package_name, entry_point_tail.replace("/", "-"))
19+
# Name of the entry-point if not the primary-one (e.g. "a11y", "drag-drop", "platform")
20+
entry_point = segments[1] if len(segments) > 1 else None
2221

23-
# Construct the path to the given entry-point. Note that we also need to find a way to
24-
# allow guards for the primary entry-point of a package. e.g. "//src/cdk:cdk" should be also
25-
# validated. We achieve this by checking if the package_name is equal to the entry_point name.
26-
# For example: "public_api_guard/cdk/cdk.d.ts" will be the golden for the primary entry-point.
27-
entry_point_path = "%s" % (package_name if entry_point == package_name else "%s/%s" % (package_name, entry_point))
22+
golden_basename = (entry_point if entry_point else package_name).replace("/", "-")
23+
golden_file = "%s/%s.md" % (package_name, golden_basename)
2824

2925
# Create the test rule that compares the build output with the golden file.
3026
api_golden_test(
3127
name = "%s_api" % golden_file,
32-
entry_point = "angular_material/src/%s/index.d.ts" % entry_point_path,
33-
data = [golden_file] + [
34-
"//src/%s" % (entry_point_path),
35-
],
28+
entry_point = "angular_material/%s/index.d.ts" % label.package,
29+
data = [golden_file] + [target],
3630
golden = "angular_material/tools/public_api_guard/%s" % golden_file,
31+
types = types,
3732
)

0 commit comments

Comments
 (0)