|
1 | 1 | load("@npm//@angular/dev-infra-private/bazel/api-golden:index.bzl", "api_golden_test")
|
2 | 2 |
|
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) |
9 | 13 |
|
10 |
| -def generate_test_targets(targets): |
11 |
| - for target_name in targets: |
12 | 14 | # Splits the path that is relative to the current directory into the package name and
|
13 | 15 | # 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] |
18 | 18 |
|
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 |
22 | 21 |
|
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) |
28 | 24 |
|
29 | 25 | # Create the test rule that compares the build output with the golden file.
|
30 | 26 | api_golden_test(
|
31 | 27 | 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], |
36 | 30 | golden = "angular_material/tools/public_api_guard/%s" % golden_file,
|
| 31 | + types = types, |
37 | 32 | )
|
0 commit comments