Skip to content

Commit 6fa655c

Browse files
committed
build: move web test defaults into dedicated file
This is cleaner and follows the new `tools/bazel/x.bzl` folder pattern.
1 parent d79f75b commit 6fa655c

File tree

7 files changed

+106
-114
lines changed

7 files changed

+106
-114
lines changed

src/cdk/coercion/BUILD.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
load("//tools:defaults.bzl", "karma_web_test_suite", "markdown_to_html")
2-
load("//tools:defaults2.bzl", "ts_project")
1+
load("//tools:defaults.bzl", "markdown_to_html")
2+
load("//tools:defaults2.bzl", "ng_web_test_suite", "ts_project")
33

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

@@ -27,7 +27,7 @@ ts_project(
2727
],
2828
)
2929

30-
karma_web_test_suite(
30+
ng_web_test_suite(
3131
name = "unit_tests",
3232
deps = [":unit_test_sources"],
3333
)

src/cdk/keycodes/BUILD.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
load("//tools:defaults.bzl", "karma_web_test_suite", "markdown_to_html")
2-
load("//tools:defaults2.bzl", "ng_project", "ts_project")
1+
load("//tools:defaults.bzl", "markdown_to_html")
2+
load("//tools:defaults2.bzl", "ng_project", "ng_web_test_suite", "ts_project")
33

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

@@ -31,7 +31,7 @@ ts_project(
3131
],
3232
)
3333

34-
karma_web_test_suite(
34+
ng_web_test_suite(
3535
name = "unit_tests",
3636
deps = [":unit_test_sources"],
3737
)

src/e2e-app/test_suite.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("//tools:defaults.bzl", "protractor_web_test_suite")
1+
load("//tools:defaults2.bzl", "protractor_web_test_suite")
22

33
def e2e_test_suite(name, tags = ["e2e"], deps = []):
44
protractor_web_test_suite(

src/universal-app/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("@bazel_skylib//rules:build_test.bzl", "build_test")
22
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary")
3-
load("//tools:defaults.bzl", "protractor_web_test_suite", "sass_binary")
4-
load("//tools:defaults2.bzl", "http_server", "ng_project", "ts_project")
3+
load("//tools:defaults.bzl", "sass_binary")
4+
load("//tools:defaults2.bzl", "http_server", "ng_project", "protractor_web_test_suite", "ts_project")
55
load("@aspect_rules_ts//ts:defs.bzl", rules_js_tsconfig = "ts_config")
66
load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
77
load("//src/cdk:config.bzl", "CDK_TARGETS")

tools/bazel/web_test_suite.bzl

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
load("@rules_browsers//src/wtr:index.bzl", "wtr_test")
2+
load("@devinfra//bazel/spec-bundling:index_rjs.bzl", "spec_bundle")
3+
4+
def _web_test(name, tags = [], deps = [], bootstrap = [], **kwargs):
5+
spec_bundle(
6+
name = "%s_bundle" % name,
7+
srcs = ["//src:build-tsconfig"],
8+
bootstrap = bootstrap,
9+
deps = deps,
10+
config = {
11+
"resolveExtensions": [".js"],
12+
"tsconfig": "./src/bazel-tsconfig-build.json",
13+
},
14+
)
15+
16+
test_tags = ["partial-compilation-integration"] + tags
17+
18+
wtr_test(
19+
name = name,
20+
deps = [":%s_bundle" % name],
21+
tags = test_tags,
22+
**kwargs
23+
)
24+
25+
def ng_web_test_suite(deps = [], static_css = [], **kwargs):
26+
# Always include a prebuilt theme in the test suite because otherwise tests, which depend on CSS
27+
# that is needed for measuring, will unexpectedly fail. Also always adding a prebuilt theme
28+
# reduces the amount of setup that is needed to create a test suite Bazel target. Note that the
29+
# prebuilt theme will be also added to CDK test suites but shouldn't affect anything.
30+
static_css = static_css + [
31+
"//src/material/prebuilt-themes:azure-blue",
32+
]
33+
34+
bootstrap = []
35+
36+
# Workaround for https://github.com/bazelbuild/rules_typescript/issues/301
37+
# Since some of our tests depend on CSS files which are not part of the `ng_project` rule,
38+
# we need to somehow load static CSS files within Karma (e.g. overlay prebuilt). Those styles
39+
# are required for successful test runs. Since the `karma_web_test_suite` rule currently only
40+
# allows JS files to be included and served within Karma, we need to create a JS file that
41+
# loads the given CSS file.
42+
for css_label in static_css:
43+
css_id = "static-css-file-%s" % (css_label.replace("/", "_").replace(":", "-"))
44+
bootstrap.append(":%s" % css_id)
45+
46+
native.genrule(
47+
name = css_id,
48+
srcs = [css_label],
49+
outs = ["%s.css.init.js" % css_id],
50+
output_to_bindir = True,
51+
cmd = """
52+
files=($(execpaths %s))
53+
# Escape all double-quotes so that the content can be safely inlined into the
54+
# JS template. Note that it needs to be escaped a second time because the string
55+
# will be evaluated first in Bash and will then be stored in the JS output.
56+
css_content=$$(cat $${files[0]} | sed 's/"/\\\\"/g')
57+
js_template='var cssElement = document.createElement("style"); \
58+
cssElement.type = "text/css"; \
59+
cssElement.innerHTML = "'"$$css_content"'"; \
60+
document.head.appendChild(cssElement);'
61+
echo "$$js_template" > $@
62+
""" % css_label,
63+
)
64+
65+
_web_test(
66+
# Depend on our custom test initialization script. This needs to be the first dependency.
67+
deps = deps,
68+
bootstrap = ["//test:angular_test_init"] + bootstrap,
69+
**kwargs
70+
)

tools/defaults.bzl

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ load("//:pkg-externals.bzl", "PKG_EXTERNALS")
99
load("//tools/markdown-to-html:index.bzl", _markdown_to_html = "markdown_to_html")
1010
load("//tools/extract-tokens:index.bzl", _extract_tokens = "extract_tokens")
1111
load("//tools/bazel:ng_package_interop.bzl", "ng_package_interop")
12-
load("//tools:defaults2.bzl", "spec_bundle", _karma_web_test_suite = "karma_web_test_suite")
13-
load("@rules_browsers//src/protractor_test:index.bzl", "protractor_test")
12+
load("//tools:defaults2.bzl", _ng_web_test_suite = "ng_web_test_suite")
1413

1514
npmPackageSubstitutions = select({
1615
"//tools:stamp": NPM_PACKAGE_SUBSTITUTIONS,
@@ -20,7 +19,7 @@ npmPackageSubstitutions = select({
2019
# Re-exports to simplify build file load statements
2120
markdown_to_html = _markdown_to_html
2221
extract_tokens = _extract_tokens
23-
karma_web_test_suite = _karma_web_test_suite
22+
ng_web_test_suite = _ng_web_test_suite
2423

2524
def sass_binary(sourcemap = False, include_paths = [], **kwargs):
2625
_sass_binary(
@@ -122,89 +121,3 @@ def pkg_npm(name, visibility = None, **kwargs):
122121
visibility = visibility,
123122
**kwargs
124123
)
125-
126-
pkg_tar(
127-
name = name + "_archive",
128-
srcs = [":%s" % name],
129-
package_dir = "package/",
130-
extension = "tar.gz",
131-
strip_prefix = "./%s" % name,
132-
# Target should not build on CI unless it is explicitly requested.
133-
tags = ["manual"],
134-
visibility = visibility,
135-
)
136-
137-
def protractor_web_test_suite(name, deps, **kwargs):
138-
spec_bundle(
139-
name = "%s_bundle" % name,
140-
deps = deps,
141-
external = ["protractor", "selenium-webdriver"],
142-
)
143-
144-
protractor_test(
145-
name = name,
146-
deps = [":%s_bundle" % name],
147-
extra_config = {
148-
"useAllAngular2AppRoots": True,
149-
"allScriptsTimeout": 120000,
150-
"getPageTimeout": 120000,
151-
"jasmineNodeOpts": {
152-
"defaultTimeoutInterval": 120000,
153-
},
154-
# Since we want to use async/await we don't want to mix up with selenium's promise
155-
# manager. In order to enforce this, we disable the promise manager.
156-
"SELENIUM_PROMISE_MANAGER": False,
157-
},
158-
data = [
159-
"//:node_modules/protractor",
160-
"//:node_modules/selenium-webdriver",
161-
],
162-
**kwargs
163-
)
164-
165-
def ng_web_test_suite(deps = [], static_css = [], **kwargs):
166-
# Always include a prebuilt theme in the test suite because otherwise tests, which depend on CSS
167-
# that is needed for measuring, will unexpectedly fail. Also always adding a prebuilt theme
168-
# reduces the amount of setup that is needed to create a test suite Bazel target. Note that the
169-
# prebuilt theme will be also added to CDK test suites but shouldn't affect anything.
170-
static_css = static_css + [
171-
"//src/material/prebuilt-themes:azure-blue",
172-
]
173-
174-
bootstrap = []
175-
176-
# Workaround for https://github.com/bazelbuild/rules_typescript/issues/301
177-
# Since some of our tests depend on CSS files which are not part of the `ng_project` rule,
178-
# we need to somehow load static CSS files within Karma (e.g. overlay prebuilt). Those styles
179-
# are required for successful test runs. Since the `karma_web_test_suite` rule currently only
180-
# allows JS files to be included and served within Karma, we need to create a JS file that
181-
# loads the given CSS file.
182-
for css_label in static_css:
183-
css_id = "static-css-file-%s" % (css_label.replace("/", "_").replace(":", "-"))
184-
bootstrap.append(":%s" % css_id)
185-
186-
native.genrule(
187-
name = css_id,
188-
srcs = [css_label],
189-
outs = ["%s.css.init.js" % css_id],
190-
output_to_bindir = True,
191-
cmd = """
192-
files=($(execpaths %s))
193-
# Escape all double-quotes so that the content can be safely inlined into the
194-
# JS template. Note that it needs to be escaped a second time because the string
195-
# will be evaluated first in Bash and will then be stored in the JS output.
196-
css_content=$$(cat $${files[0]} | sed 's/"/\\\\"/g')
197-
js_template='var cssElement = document.createElement("style"); \
198-
cssElement.type = "text/css"; \
199-
cssElement.innerHTML = "'"$$css_content"'"; \
200-
document.head.appendChild(cssElement);'
201-
echo "$$js_template" > $@
202-
""" % css_label,
203-
)
204-
205-
karma_web_test_suite(
206-
# Depend on our custom test initialization script. This needs to be the first dependency.
207-
deps = deps,
208-
bootstrap = ["//test:angular_test_init"] + bootstrap,
209-
**kwargs
210-
)

tools/defaults2.bzl

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
load("@aspect_rules_jasmine//jasmine:defs.bzl", _jasmine_test = "jasmine_test")
2-
load("//tools/bazel:ts_project_interop.bzl", _ts_project = "ts_project")
3-
load("//tools/bazel:module_name.bzl", "compute_module_name")
42
load("@aspect_rules_js//npm:defs.bzl", _npm_package = "npm_package")
5-
load("@rules_angular//src/ng_project:index.bzl", _ng_project = "ng_project")
6-
load("@devinfra//bazel/spec-bundling:index_rjs.bzl", _spec_bundle = "spec_bundle")
7-
load("@rules_browsers//src/wtr:index.bzl", "wtr_test")
83
load("@devinfra//bazel/http-server:index.bzl", _http_server = "http_server")
4+
load("@devinfra//bazel/spec-bundling:index_rjs.bzl", _spec_bundle = "spec_bundle")
5+
load("@rules_angular//src/ng_project:index.bzl", _ng_project = "ng_project")
6+
load("@rules_browsers//src/protractor_test:index.bzl", "protractor_test")
7+
load("//tools/bazel:module_name.bzl", "compute_module_name")
8+
load("//tools/bazel:ts_project_interop.bzl", _ts_project = "ts_project")
9+
load("//tools/bazel:web_test_suite.bzl", _ng_web_test_suite = "ng_web_test_suite")
910

1011
spec_bundle = _spec_bundle
1112
http_server = _http_server
13+
ng_web_test_suite = _ng_web_test_suite
1214

1315
def npm_package(**kwargs):
1416
_npm_package(**kwargs)
@@ -79,23 +81,30 @@ def jasmine_test(name, data = [], args = [], **kwargs):
7981
**kwargs
8082
)
8183

82-
def karma_web_test_suite(name, tags = [], deps = [], bootstrap = [], **kwargs):
84+
def protractor_web_test_suite(name, deps, **kwargs):
8385
spec_bundle(
8486
name = "%s_bundle" % name,
85-
srcs = ["//src:build-tsconfig"],
86-
bootstrap = bootstrap,
8787
deps = deps,
88-
config = {
89-
"resolveExtensions": [".js"],
90-
"tsconfig": "./src/bazel-tsconfig-build.json",
91-
},
88+
external = ["protractor", "selenium-webdriver"],
9289
)
9390

94-
test_tags = ["partial-compilation-integration"] + tags
95-
96-
wtr_test(
91+
protractor_test(
9792
name = name,
9893
deps = [":%s_bundle" % name],
99-
tags = test_tags,
94+
extra_config = {
95+
"useAllAngular2AppRoots": True,
96+
"allScriptsTimeout": 120000,
97+
"getPageTimeout": 120000,
98+
"jasmineNodeOpts": {
99+
"defaultTimeoutInterval": 120000,
100+
},
101+
# Since we want to use async/await we don't want to mix up with selenium's promise
102+
# manager. In order to enforce this, we disable the promise manager.
103+
"SELENIUM_PROMISE_MANAGER": False,
104+
},
105+
data = [
106+
"//:node_modules/protractor",
107+
"//:node_modules/selenium-webdriver",
108+
],
100109
**kwargs
101110
)

0 commit comments

Comments
 (0)