Skip to content

Commit

Permalink
fix: Disable sandboxing for cypress_test rules
Browse files Browse the repository at this point in the history
Cypress has moved away from supporting the env
variable RUN_ELECTRON_AS_NODE which makes it
hard to keep up with their electron server's
sandboxing violations. We now disable the sandbox
so that user's can don't run into these issues.
  • Loading branch information
mrmeku committed Apr 16, 2024
1 parent 3c1ccc0 commit e455847
Show file tree
Hide file tree
Showing 12 changed files with 504 additions and 52 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module(
)

cypress = use_extension("//cypress:extensions.bzl", "cypress")
cypress.toolchain(cypress_version = "12.3.0")
cypress.toolchain(cypress_version = "13.6.6")
use_repo(cypress, "cypress_toolchains")

register_toolchains("@cypress_toolchains//:all")
Expand Down
55 changes: 28 additions & 27 deletions cypress/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ _cypress_test = rule(
toolchains = js_binary_lib.toolchains + ["@aspect_rules_cypress//cypress:toolchain_type"],
)

def _cypress_test_macro(name, entry_point, cypress, **kwargs):
_cypress_test(
name = name,
entry_point = entry_point,
data = kwargs.pop("data", []) + [
cypress,
],
chdir = native.package_name(),
enable_runfiles = select({
"@aspect_rules_js//js/private:enable_runfiles": True,
"//conditions:default": False,
}),
unresolved_symlinks_enabled = select({
"@aspect_rules_js//js/private:experimental_allow_unresolved_symlinks": True,
"//conditions:default": False,
}),
# Turn off sandboxing by default to allow electron to perform write
# operations. Cypress does not expose the underlying electron apis so we
# cannot alter the user app data directory to be within the bazel
# sandbox.
tags = kwargs.pop("tags", []) + ["no-sandbox"],
**kwargs
)

def cypress_test(name, cypress = "//:node_modules/cypress", **kwargs):
"""cypress_test runs the cypress CLI with the cypress toolchain.
Expand All @@ -34,21 +58,10 @@ def cypress_test(name, cypress = "//:node_modules/cypress", **kwargs):
tags = ["manual"],
)

_cypress_test(
_cypress_test_macro(
name = name,
entry_point = entry_point,
data = kwargs.pop("data", []) + [
cypress,
],
chdir = native.package_name(),
enable_runfiles = select({
"@aspect_rules_js//js/private:enable_runfiles": True,
"//conditions:default": False,
}),
unresolved_symlinks_enabled = select({
"@aspect_rules_js//js/private:experimental_allow_unresolved_symlinks": True,
"//conditions:default": False,
}),
cypress = cypress,
**kwargs
)

Expand Down Expand Up @@ -84,21 +97,9 @@ def cypress_module_test(name, runner, cypress = "//:node_modules/cypress", **kwa
cypress: The cypress npm package which was already linked using an API like npm_link_all_packages.
**kwargs: All other args from `js_test`. See https://github.com/aspect-build/rules_js/blob/main/docs/js_binary.md#js_test
"""
_cypress_test(
_cypress_test_macro(
name = name,
enable_runfiles = select({
"@aspect_rules_js//js/private:enable_runfiles": True,
"//conditions:default": False,
}),
unresolved_symlinks_enabled = select({
"@aspect_rules_js//js/private:experimental_allow_unresolved_symlinks": True,
"//conditions:default": False,
}),
entry_point = runner,
chdir = native.package_name(),
data = kwargs.pop("data", []) + [
cypress,
],
patch_node_fs = kwargs.pop("patch_node_fs", False),
cypress = cypress,
**kwargs
)
434 changes: 434 additions & 0 deletions cypress/private/versions.bzl

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions e2e/workspace/cli_test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ load("@aspect_rules_cypress//cypress:defs.bzl", "cypress_test")

cypress_test(
name = "cli_test",
timeout = "short",
args = [
"run",
"--config-file=cypress.config.ts",
Expand Down
14 changes: 8 additions & 6 deletions e2e/workspace/cli_test/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { defineConfig } from 'cypress'
import { defineConfig } from "cypress";

process.env.DISPLAY = ":0";

export default defineConfig({
e2e: {
specPattern: ["cli_test.cy.ts"],
supportFile: false,
setupNodeEvents(on, config) {
on('before:browser:launch', (browser, launchOptions) => {
launchOptions.args.push("--disable-gpu-shader-disk-cache")
})
}
on("before:browser:launch", (browser, launchOptions) => {
launchOptions.args.push("--disable-gpu-shader-disk-cache");
});
},
},
})
});
1 change: 1 addition & 0 deletions e2e/workspace/module_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ load("@aspect_rules_cypress//cypress:defs.bzl", "cypress_module_test")

cypress_module_test(
name = "module_test",
timeout = "short",
data = [
"cypress.config.js",
"module_test.cy.js",
Expand Down
14 changes: 8 additions & 6 deletions e2e/workspace/module_test/cypress.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const { defineConfig } = require('cypress')
const { defineConfig } = require("cypress");

process.env.DISPLAY = ":0";

module.exports = defineConfig({
e2e: {
specPattern: ["*.cy.js"],
supportFile: false,
setupNodeEvents(on, config) {
on('before:browser:launch', (browser = {}, launchOptions) => {
launchOptions.args.push("--disable-gpu-shader-disk-cache")
})
}
on("before:browser:launch", (browser = {}, launchOptions) => {
launchOptions.args.push("--disable-gpu-shader-disk-cache");
});
},
},
})
});
3 changes: 2 additions & 1 deletion e2e/workspace/module_test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ async function main() {
const result = await cypress.run({
headless: true,
});
result.browserName;

// If any tests have failed, results.failures is non-zero, some tests have failed
if (result.failures) {
Expand All @@ -12,7 +13,7 @@ async function main() {
return 1;
}

if (result.status !== "finished") {
if (result.status) {
console.error("Cypress tests failed with status", result.status);
return 2;
}
Expand Down
4 changes: 2 additions & 2 deletions e2e/workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "1.0.0",
"description": "Tests for cypress bazel rules",
"dependencies": {
"cypress": "12.12.0",
"cypress": "13.6.6",
"express": "^4.19.2",
"typescript": "4.9.5"
}
}
}
25 changes: 16 additions & 9 deletions e2e/workspace/pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions e2e/workspace/server_example/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ js_binary(

cypress_test(
name = "server_example",
timeout = "short",
args = [
"run",
"--config-file=cypress.config.js",
Expand Down
2 changes: 2 additions & 0 deletions e2e/workspace/server_example/cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const { defineConfig } = require("cypress");
const { spawn } = require("node:child_process");
const { join } = require("path");

process.env.DISPLAY = ":0";

module.exports = defineConfig({
e2e: {
specPattern: ["server_example_test.cy.js"],
Expand Down

0 comments on commit e455847

Please sign in to comment.