9
9
10
10
load ("//:build/wd_test.bzl" , "wd_test" )
11
11
12
- def wpt_test (name , wpt_directory , test_js ):
13
- test_gen_rule = "{}@_wpt_test_gen " .format (name )
14
- _wpt_test_gen (
15
- name = test_gen_rule ,
12
+ def wpt_test (name , wpt_directory , test_config ):
13
+ js_test_gen_rule = "{}@_wpt_js_test_gen " .format (name )
14
+ _wpt_js_test_gen (
15
+ name = js_test_gen_rule ,
16
16
test_name = name ,
17
17
wpt_directory = wpt_directory ,
18
- test_js = test_js ,
18
+ test_config = test_config ,
19
+ )
20
+
21
+ wd_test_gen_rule = "{}@_wpt_wd_test_gen" .format (name )
22
+ _wpt_wd_test_gen (
23
+ name = wd_test_gen_rule ,
24
+ test_name = name ,
25
+ wpt_directory = wpt_directory ,
26
+ test_config = test_config ,
27
+ test_js_generated = js_test_gen_rule ,
19
28
)
20
29
21
30
wd_test (
22
31
name = "{}" .format (name ),
23
- src = test_gen_rule ,
32
+ src = wd_test_gen_rule ,
24
33
args = ["--experimental" ],
25
34
data = [
26
35
"//src/wpt:wpt-test-harness" ,
27
- test_js ,
36
+ test_config ,
37
+ js_test_gen_rule ,
28
38
wpt_directory ,
29
39
"//src/workerd/io:trimmed-supported-compatibility-date.txt" ,
30
40
],
31
41
)
32
42
33
- def _wpt_test_gen_impl (ctx ):
43
+ def _wpt_js_test_gen_impl (ctx ):
44
+ src = ctx .actions .declare_file ("{}-test.generated.js" .format (ctx .attr .test_name ))
45
+ ctx .actions .write (
46
+ output = src ,
47
+ content = WPT_JS_TEST_TEMPLATE .format (
48
+ test_config = ctx .file .test_config .basename ,
49
+ cases = generate_external_cases (ctx .attr .wpt_directory .files ),
50
+ ),
51
+ )
52
+
53
+ return DefaultInfo (
54
+ files = depset ([src ]),
55
+ )
56
+
57
+ def generate_external_cases (files ):
58
+ result = []
59
+
60
+ for file in files .to_list ():
61
+ if file .extension == "js" :
62
+ entry = """export const {} = run(config, '{}');""" .format (test_case_name (file .basename ), file .basename )
63
+ else :
64
+ continue
65
+ result .append (entry )
66
+
67
+ return "\n " .join (result )
68
+
69
+ def test_case_name (filename ):
70
+ words = (filename
71
+ .removesuffix (".js" )
72
+ .removesuffix (".any" )
73
+ .replace ("." , "-" )
74
+ .split ("-" ))
75
+
76
+ return words [0 ] + "" .join ([word .capitalize () for word in words [1 :]])
77
+
78
+ WPT_JS_TEST_TEMPLATE = """// This file is autogenerated by wpt_test.bzl
79
+ // DO NOT EDIT.
80
+ import {{ run }} from 'wpt:harness';
81
+ import config from '{test_config}';
82
+
83
+ {cases}
84
+ """
85
+
86
+ def _wpt_wd_test_gen_impl (ctx ):
34
87
src = ctx .actions .declare_file ("{}.wd-test" .format (ctx .attr .test_name ))
35
88
ctx .actions .write (
36
89
output = src ,
37
- content = WPT_TEST_TEMPLATE .format (
90
+ content = WPT_WD_TEST_TEMPLATE .format (
38
91
test_name = ctx .attr .test_name ,
39
- test_js = wd_relative_path (ctx .file .test_js ),
92
+ test_config = ctx .file .test_config .basename ,
93
+ test_js_generated = wd_relative_path (ctx .file .test_js_generated ),
40
94
modules = generate_external_modules (ctx .attr .wpt_directory .files ),
41
95
),
42
96
)
@@ -45,14 +99,15 @@ def _wpt_test_gen_impl(ctx):
45
99
files = depset ([src ]),
46
100
)
47
101
48
- WPT_TEST_TEMPLATE = """
102
+ WPT_WD_TEST_TEMPLATE = """
49
103
using Workerd = import "/workerd/workerd.capnp";
50
104
const unitTests :Workerd.Config = (
51
105
services = [
52
106
( name = "{test_name}",
53
107
worker = (
54
108
modules = [
55
- (name = "worker", esModule = embed "{test_js}"),
109
+ (name = "worker", esModule = embed "{test_js_generated}"),
110
+ (name = "{test_config}", esModule = embed "{test_config}"),
56
111
(name = "wpt:harness", esModule = embed "../../../../../workerd/src/wpt/harness.js"),
57
112
{modules}
58
113
],
@@ -102,14 +157,28 @@ def generate_external_modules(files):
102
157
103
158
return ",\n " .join (result )
104
159
105
- _wpt_test_gen = rule (
106
- implementation = _wpt_test_gen_impl ,
160
+ _wpt_wd_test_gen = rule (
161
+ implementation = _wpt_wd_test_gen_impl ,
162
+ attrs = {
163
+ # A string to use as the test name. Used in the wd-test filename and the worker's name
164
+ "test_name" : attr .string (),
165
+ # A file group representing a directory of wpt tests. All files in the group will be embedded.
166
+ "wpt_directory" : attr .label (),
167
+ # A JS file containing the test configuration.
168
+ "test_config" : attr .label (allow_single_file = True ),
169
+ # An auto-generated JS file containing the test logic.
170
+ "test_js_generated" : attr .label (allow_single_file = True ),
171
+ },
172
+ )
173
+
174
+ _wpt_js_test_gen = rule (
175
+ implementation = _wpt_js_test_gen_impl ,
107
176
attrs = {
108
177
# A string to use as the test name. Used in the wd-test filename and the worker's name
109
178
"test_name" : attr .string (),
110
179
# A file group representing a directory of wpt tests. All files in the group will be embedded.
111
180
"wpt_directory" : attr .label (),
112
- # A JS file containing the actual test logic .
113
- "test_js " : attr .label (allow_single_file = True ),
181
+ # A JS file containing the test configuration .
182
+ "test_config " : attr .label (allow_single_file = True ),
114
183
},
115
184
)
0 commit comments