Skip to content

Commit

Permalink
Adds _prerender_for_test to a number of rules_prerender macros.
Browse files Browse the repository at this point in the history
Fixes #10.

This is a public API which provides a reference to the `ts_library()` target which compiles the user's `srcs`. It allows users to easily invoke their prerender code for testing purposes. This is specifically marked `testonly` because it is focused on the single testing use case.

This target's existence technically breaks an invariant of `prerender_component()`, in that it only depends on a single piece of a complex component (it does not include client side scripts, CSS, or othe resources). As a result, I'm trying to restrict and discourage usage like this as much as possible. Unit testing is just the one exception where this really gets in the way, so hopefully this `testonly` target will satisfy that use case sufficiently without providing an abusable hook for users to exploit and workaround the guarantees provided by `prerender_component()`.
  • Loading branch information
dgp1130 committed Apr 8, 2021
1 parent 830bf8f commit d32c0e8
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/site/blog/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ts_library(
srcs = ["blog_test.ts"],
testonly = True,
deps = [
":blog_page_component_prerender",
":blog_prerender_for_test",
"@npm//@types/jasmine",
"@npm//node-html-parser",
],
Expand Down
2 changes: 1 addition & 1 deletion examples/site/components/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ts_library(
srcs = ["base_test.ts"],
testonly = True,
deps = [
":base_prerender",
":base_prerender_for_test",
"@npm//@types/jasmine",
"@npm//node-html-parser",
],
Expand Down
2 changes: 1 addition & 1 deletion examples/site/components/counter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ts_library(
srcs = ["counter_prerender_test.ts"],
testonly = True,
deps = [
":counter_prerender",
":counter_prerender_for_test",
"@npm//@types/jasmine",
"@npm//node-html-parser",
],
Expand Down
2 changes: 1 addition & 1 deletion examples/site/components/footer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ts_library(
srcs = ["footer_test.ts"],
testonly = True,
deps = [
":footer_prerender",
":footer_prerender_for_test",
"@npm//@types/jasmine",
"@npm//node-html-parser",
],
Expand Down
2 changes: 1 addition & 1 deletion examples/site/components/header/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ts_library(
srcs = ["header_test.ts"],
testonly = True,
deps = [
":header_prerender",
":header_prerender_for_test",
"@npm//@types/jasmine",
"@npm//node-html-parser",
],
Expand Down
27 changes: 26 additions & 1 deletion packages/rules_prerender/prerender_component.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,29 @@ def prerender_component(
deps: `prerender_component()` dependencies for this component.
testonly: See https://docs.bazel.build/versions/master/be/common-definitions.html.
visibility: See https://docs.bazel.build/versions/master/be/common-definitions.html.
Outputs:
`%{name}`: A compiled prerender component for use by other
`prerender_*()` rules/macros. This is not a real target as this
actually macro outputs several targets at `%{name}_*`. These are
**not** intended to be directly depended upon because it would be
very easy to accidentally depend on one part of a component (such as
the prerendered HTML) without also depending all the other parts
(such as the CSS) for it to work as expected. Instead of using the
outputs of this macro directly, use another `prerender_*()`
macro/rule to depend on and use this correctly.
`%{name}_prerender_for_test`: There is one exception which can be
directly depended upon. This is an alias to the `ts_library()`
target which compiles the `srcs` of this macro. It does **not**
include client side scripts, CSS, or other resources used by the
component. It is intended purely for testing purposes to assert that
prerender logic works as intended and is marked `testonly`
accordingly.
"""

prerender_lib = "%s_prerender" % name
ts_library(
name = "%s_prerender" % name,
name = prerender_lib,
srcs = srcs,
tsconfig = tsconfig,
data = data,
Expand All @@ -57,6 +76,12 @@ def prerender_component(
visibility = visibility,
)

native.alias(
name = "%s_prerender_for_test" % name,
actual = ":%s" % prerender_lib,
testonly = True,
)

ts_library(
name = "%s_scripts" % name,
srcs = [],
Expand Down
9 changes: 9 additions & 0 deletions packages/rules_prerender/prerender_pages.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def prerender_pages(
included. CSS is inlined directly in the HTML documents in a
`<style />` tag. Non-HTML files are included as well, but not
modified.
%{name}_prerender_for_test: An alias to the `ts_library()` target which
compiles the `src` of this macro marked as `testonly`. This provides
a simple hook for unit testing prerender logic.
Args:
name: The name of this rule.
Expand Down Expand Up @@ -113,6 +116,12 @@ def prerender_pages(
visibility = visibility,
)

native.alias(
name = "%s_prerender_for_test" % name,
actual = ":%s_prerender_for_test" % prerender_name,
testonly = True,
)

bundle = "%s_bundle" % name
if bundle_js:
# Bundle all client-side scripts at `%{name}_bundle.js`.
Expand Down
10 changes: 10 additions & 0 deletions packages/rules_prerender/prerender_pages_unbundled.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def prerender_pages_unbundled(
the page.
%{name}_resources: A `web_resources()` target containing all the
transitively used resources.
%{name}_prerender_for_test: An alias to the `ts_library()` target which
compiles the `src` of this macro marked as `testonly`. This provides
a simple hook for unit testing prerender logic.
Args:
name: The name of this rule.
Expand Down Expand Up @@ -99,10 +102,17 @@ def prerender_pages_unbundled(
testonly = testonly,
)
component_prerender = "%s_prerender" % component
component_prerender_for_test = "%s_prerender_for_test" % component
component_scripts = "%s_scripts" % component
component_styles = "%s_styles" % component
component_resources = "%s_resources" % component

native.alias(
name = "%s_prerender_for_test" % name,
actual = ":%s" % component_prerender_for_test,
testonly = True,
)

# Get the generated JS file path for the user provided TypeScript source.
prerender_js = "%s_prerender_js" % name
native.filegroup(
Expand Down

0 comments on commit d32c0e8

Please sign in to comment.