1- load ("//bazel:extract_types.bzl" , "extract_types" )
2- load ("@build_bazel_rules_nodejs//:index.bzl" , "nodejs_binary" , "nodejs_test" )
1+ load ("@bazel_skylib//rules:write_file.bzl" , "write_file" )
2+ load ("@build_bazel_rules_nodejs//:index.bzl" , "pkg_npm" )
3+ load ("//bazel/api-golden:index_rjs.bzl" , _rjs_api_golden_test_npm_package = "api_golden_test_npm_package" )
34
45nodejs_test_args = [
56 # Needed so that node doesn't walk back to the source directory.
@@ -10,18 +11,8 @@ nodejs_test_args = [
1011
1112default_strip_export_pattern = "^ɵ(?!ɵdefineInjectable|ɵinject|ɵInjectableDef)"
1213
13- def _escape_regex_for_arg (value ):
14- """Escapes a Regular expression so that it can be passed as process argument."""
15- return "\" %s\" " % value
16-
1714def extract_module_names_from_npm_targets (type_targets ):
18- """Extracts the module names from a list of NPM targets.
19-
20- For example: Consider the `@npm//@types/node` target. This function extracts
21- `@types/node` from the label. This is needed so that the Node types can be
22- resolved from within the test runner through runfile resolution.
23- """
24- module_names = []
15+ types = {}
2516
2617 for type_target in type_targets :
2718 type_label = Label (type_target )
@@ -31,9 +22,9 @@ def extract_module_names_from_npm_targets(type_targets):
3122 fail ("Expected type targets to be part of the `@npm` workspace." +
3223 "e.g. `@npm//@types/nodes`." )
3324
34- module_names . append ( type_package )
25+ types [ type_target ] = type_package
3526
36- return module_names
27+ return types
3728
3829def api_golden_test (
3930 name ,
@@ -43,52 +34,33 @@ def api_golden_test(
4334 strip_export_pattern = default_strip_export_pattern ,
4435 types = [],
4536 ** kwargs ):
46- """Builds an API report for the specified entry-point and compares it against the
47- specified golden
48-
49- Args;
50- name: Name of the test target
51- golden: Manifest path to the golden file
52- entry_point: Manifest path to the type definition entry-point.
53- data: Runtime dependenices needed for the rule (e.g. transitive type definitions)
54- strip_export_pattern: An optional regular expression to filter out exports from the golden.
55- types: Optional list of type targets to make available in the API report generation.
56- """
57-
58- quoted_export_pattern = _escape_regex_for_arg (strip_export_pattern )
59-
60- kwargs ["tags" ] = kwargs .get ("tags" , []) + ["api_guard" ]
37+ write_file (
38+ name = "%s_synthetic_package_json" % name ,
39+ out = "package.json" ,
40+ content = [json .encode ({
41+ "name" : name ,
42+ "exports" : {
43+ "." : {
44+ "types" : entry_point ,
45+ },
46+ },
47+ })],
48+ )
6149
62- # For API golden tests not running against a NPM package, we extract all transitive
63- # declarations of the specified `data` targets. This is necessary because API extractor
64- # needs to resolve other targets that have been linked by the Bazel NodeJS rules. The
65- # linker by default only provides access to JavaScript sources, but the API extractor is
66- # specifically concerned with type definitions that we can extract manually here.
67- extract_types (
68- name = "%s_data_typings" % name ,
50+ pkg_npm (
51+ name = "%s_synthetic_package" % name ,
52+ deps = data + ["%s_synthetic_package_json" % name ],
6953 testonly = True ,
70- deps = data ,
7154 )
7255
73- test_data = ["//bazel/api-golden" , "//:package.json" , ":%s_data_typings" % name ] + \
74- data + types
75-
76- nodejs_test (
56+ _rjs_api_golden_test_npm_package (
57+ no_copy_to_bin = types ,
7758 name = name ,
78- data = test_data ,
79- entry_point = "//bazel/api-golden:index.ts" ,
80- templated_args = nodejs_test_args + [golden , entry_point , "false" , quoted_export_pattern ] +
81- extract_module_names_from_npm_targets (types ),
82- ** kwargs
83- )
84-
85- nodejs_binary (
86- name = name + ".accept" ,
87- testonly = True ,
88- data = test_data ,
89- entry_point = "//bazel/api-golden:index.ts" ,
90- templated_args = nodejs_test_args + [golden , entry_point , "true" , quoted_export_pattern ] +
91- extract_module_names_from_npm_targets (types ),
59+ golden_dir = fixup_path_for_rules_js (golden ),
60+ data = [":%s_synthetic_package" % name ] + data ,
61+ npm_package = "%s/%s_synthetic_package" % (native .package_name (), name ),
62+ strip_export_pattern = strip_export_pattern ,
63+ types = extract_module_names_from_npm_targets (types ),
9264 ** kwargs
9365 )
9466
@@ -100,37 +72,17 @@ def api_golden_test_npm_package(
10072 strip_export_pattern = default_strip_export_pattern ,
10173 types = [],
10274 ** kwargs ):
103- """Builds an API report for all entry-points within the given NPM package and compares it
104- against goldens within the specified directory.
105-
106- Args;
107- name: Name of the test target
108- golden_dir: Manifest path to the golden directory
109- npm_package: Manifest path to the NPM package.
110- data: Runtime dependenices needed for the rule (e.g. the tree artifact of the NPM package)
111- strip_export_pattern: An optional regular expression to filter out exports from the golden.
112- types: Optional list of type targets to make available in the API report generation.
113- """
114-
115- quoted_export_pattern = _escape_regex_for_arg (strip_export_pattern )
116-
117- kwargs ["tags" ] = kwargs .get ("tags" , []) + ["api_guard" ]
118-
119- nodejs_test (
75+ _rjs_api_golden_test_npm_package (
12076 name = name ,
121- data = ["//bazel/api-golden" ] + data + types ,
122- entry_point = "//bazel/api-golden:index_npm_packages.ts" ,
123- templated_args = nodejs_test_args + [golden_dir , npm_package , "false" , quoted_export_pattern ] +
124- extract_module_names_from_npm_targets (types ),
77+ no_copy_to_bin = types ,
78+ golden_dir = fixup_path_for_rules_js (golden_dir ),
79+ npm_package = fixup_path_for_rules_js (npm_package ),
80+ data = data ,
81+ strip_export_pattern = strip_export_pattern ,
82+ types = extract_module_names_from_npm_targets (types ),
12583 ** kwargs
12684 )
12785
128- nodejs_binary (
129- name = name + ".accept" ,
130- testonly = True ,
131- data = ["//bazel/api-golden" ] + data + types ,
132- entry_point = "//bazel/api-golden:index_npm_packages.ts" ,
133- templated_args = nodejs_test_args + [golden_dir , npm_package , "true" , quoted_export_pattern ] +
134- extract_module_names_from_npm_targets (types ),
135- ** kwargs
136- )
86+ def fixup_path_for_rules_js (p ):
87+ segs = p .split ("/" )
88+ return "/" .join (segs [1 :])
0 commit comments