Skip to content

Commit

Permalink
feat: Create js_run_devserver target in macro
Browse files Browse the repository at this point in the history
For every `cypress_test` the macro now generates
a supplementary target `[name].open` which runs
cypress using js_run_devserver. This allows users
to use ibazel to interactively update their tests.
  • Loading branch information
mrmeku committed Aug 17, 2024
1 parent 12c8fc3 commit 98c52d1
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
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
20 changes: 7 additions & 13 deletions cypress/private/cypress_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,25 @@ _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 EXECROOT before setting fixed_env. Then use that variable.
entry_point_dir = '"$$( dirname "$$( realpath "$${BASH_SOURCE[0]}" )" )"'
entry_point_offset = "../../../.."
execroot = "/".join([entry_point_dir, entry_point_offset] + [".." for _ in ctx.attr.name.split("/")])
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",
"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,
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

0 comments on commit 98c52d1

Please sign in to comment.