Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create js_run_devserver target in macro #92

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cypress/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ bzl_library(
visibility = ["//visibility:public"],
deps = [
"@aspect_rules_cypress//cypress/private:cypress_test",
"@aspect_rules_js//js:defs",
"@aspect_rules_js//js:libs",
],
)
Expand Down
18 changes: 18 additions & 0 deletions cypress/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load("@aspect_bazel_lib//lib:directory_path.bzl", "directory_path")
load("@aspect_rules_cypress//cypress/private:cypress_test.bzl", "cypress_test_lib")
load("@aspect_rules_js//js:defs.bzl", "js_run_devserver")
load("@aspect_rules_js//js:libs.bzl", "js_binary_lib")

_cypress_test = rule(
Expand Down Expand Up @@ -43,6 +44,9 @@ def cypress_test(
See documentation on what arguments the cypress CLI supports:
https://docs.cypress.io/guides/guides/command-line#What-you-ll-learn
Macro produces two targets:
- `[name]`: Test target which will invoke `cypress run`
- `[name].open`: Runnable target which will invoke `cypress open`. Meant to be used in conjunction with ibazel
Args:
name: The name used for this rule and output files
Expand Down Expand Up @@ -83,15 +87,29 @@ def cypress_test(
tags = ["manual"],
)

args = kwargs.pop("args", [])
data = kwargs.pop("data", [])
_cypress_test_macro(
name = name,
entry_point = entry_point,
cypress = cypress,
disable_sandbox = disable_sandbox,
browsers = browsers,
args = ["run"] + args,
data = data,
**kwargs
)

js_run_devserver(
name = name + ".open",
tool = name,
args = ["open"] + args,
# Allow using _cypress_test_macro as the tool. It is testonly so this target also needs to be testonly.
testonly = True,
data = data,
grant_sandbox_write_permissions = True,
)

def cypress_module_test(
name,
runner,
Expand Down
18 changes: 5 additions & 13 deletions cypress/private/cypress_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,23 @@ _attrs = dicts.add(js_binary_lib.attrs, {
),
})

# Do the opposite of _to_manifest_path in
# https://github.com/bazelbuild/rules_nodejs/blob/8b5d27400db51e7027fe95ae413eeabea4856f8e/nodejs/toolchain.bzl#L50
# to get back to the short_path.
# TODO: fix toolchain so we don't have to do this
def _target_tool_short_path(path):
return ("../" + path[len("external/"):]) if path.startswith("external/") else path

def _impl(ctx):
cypress_bin = _target_tool_short_path(ctx.toolchains["@aspect_rules_cypress//cypress:toolchain_type"].cypressinfo.target_tool_path)
cypress_bin = ctx.toolchains["@aspect_rules_cypress//cypress:toolchain_type"].cypressinfo.target_tool_path
cypress_files = ctx.toolchains["@aspect_rules_cypress//cypress:toolchain_type"].cypressinfo.tool_files

files = ctx.files.data[:] + cypress_files + ctx.files.browsers

if ctx.attr.chdir:
cypress_bin = "/".join([".." for _ in ctx.attr.chdir.split("/")] + [cypress_bin])
# TODO: Augment rules_js to expose JS__EXECROOT before setting fixed_env. Then use that variable.
execroot = "$$( realpath \"$${BASH_SOURCE[0]}\" | sed -E 's/^(.*\\/execroot\\/[^/]+).*/\\1/')"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Dep on realpath not great and it will also escape the sandbox this way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably augment rules_js to support what you want here instead of this line

cypress_run_binary = "/".join([execroot, cypress_bin])

launcher = js_binary_lib.create_launcher(
ctx,
log_prefix_rule_set = "aspect_rules_cypess",
log_prefix_rule = "cypress_node_test",
fixed_args = ctx.attr.fixed_args,
fixed_env = {
"CYPRESS_RUN_BINARY": cypress_bin,
"HOME": "$$TEST_TMPDIR",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TEST_TMPDIR isn't defined in bazel run so I removed these. I'd like to keep them and use something like or if they aren't defined. Any suggestions @gregmagolan ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we set HOME in the first place? Guessing cypress was trying to write to the home directory when it the sandbox and failing?

"XDG_CONFIG_HOME": "$$TEST_TMPDIR",
"CYPRESS_RUN_BINARY": cypress_run_binary,
},
)

Expand Down
3 changes: 3 additions & 0 deletions docs/defs.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion e2e/workspace/cli_test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ cypress_test(
name = "cli_test",
timeout = "short",
args = [
"run",
"--e2e",
"--browser=electron",
"--config-file=cli_test/cypress.config.ts",
],
data = [
Expand Down
3 changes: 2 additions & 1 deletion e2e/workspace/server_example/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ cypress_test(
name = "server_example",
timeout = "short",
args = [
"run",
"--e2e",
"--browser=electron",
"--config-file=server_example/cypress.config.js",
],
data = [
Expand Down
6 changes: 3 additions & 3 deletions e2e/workspace/server_example/cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ module.exports = defineConfig({
launchOptions.args.push("--disable-gpu-shader-disk-cache");
});

const port = "3000";
const port = "4000";
return new Promise((resolve, reject) => {
// Launch the server
const workspaceRoot = join(
process.env.RUNFILES_DIR,
process.env.TEST_WORKSPACE,
process.env.JS_BINARY__RUNFILES,
gregmagolan marked this conversation as resolved.
Show resolved Hide resolved
process.env.TEST_WORKSPACE || '_main',
);
const serverProcess = spawn(
join(workspaceRoot, "server_example/server_/server"),
Expand Down
1 change: 1 addition & 0 deletions e2e/workspace/server_example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if (isNaN(port)) {
}

app.get("/", (req, res) => {
res.type('html');
res.send(`
<html>
<body>
Expand Down