Skip to content

Commit 4fd4653

Browse files
Dan Mulleralexeagle
authored andcommitted
fix(cypress): allow for async cypress plugins
cypress_repository now fails if cypress verify fails set the HOME env variable since cypress writes files to it remove unused includeScreenshots / includeVideos variables browserify files are no longer included in test output files screenshots/videos are stored directory in TEST_UNDECLARED_OUTPUTS_DIR
1 parent 88c19f1 commit 4fd4653

7 files changed

Lines changed: 58 additions & 62 deletions

File tree

WORKSPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ load("//packages/cypress:index.bzl", "cypress_repository")
120120
cypress_repository(
121121
name = "cypress",
122122
cypress_bin = "@cypress_deps//:node_modules/cypress/bin/cypress",
123+
# Currently cypress cannot be installed on our Linux/Windows CI machines
124+
fail_on_error = False,
123125
)
124126

125127
#

packages/cypress/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ pkg_npm(
7676
srcs = [
7777
"index.bzl",
7878
"package.json",
79-
"//packages/cypress:internal/cypress-install.js",
8079
"//packages/cypress:internal/cypress_repository.bzl",
80+
"//packages/cypress:internal/install-cypress.js",
8181
"//packages/cypress:internal/plugins/base.js",
8282
"//packages/cypress:internal/plugins/index.template.js",
8383
"//packages/cypress:internal/run-cypress.js",
@@ -90,8 +90,8 @@ pkg_npm(
9090
"plugins/base.js",
9191
])""",
9292
substitutions = {
93-
"@build_bazel_rules_nodejs//packages/cypress:internal/cypress-install.js": "TEMPLATED_node_modules_workspace_name//@bazel/cypress:internal/cypress-install.js",
9493
"@build_bazel_rules_nodejs//packages/cypress:internal/cypress_repository.bzl": "//packages/cypress:internal/cypress_repository.bzl",
94+
"@build_bazel_rules_nodejs//packages/cypress:internal/install-cypress.js": "TEMPLATED_node_modules_workspace_name//@bazel/cypress:internal/install-cypress.js",
9595
"@build_bazel_rules_nodejs//packages/cypress:internal/plugins/base.js": "TEMPLATED_node_modules_workspace_name//@bazel/cypress:internal/plugins/base.js",
9696
"@build_bazel_rules_nodejs//packages/cypress:internal/plugins/index.template.js": "TEMPLATED_node_modules_workspace_name//@bazel/cypress:internal/plugins/index.template.js",
9797
"@build_bazel_rules_nodejs//packages/cypress:internal/run-cypress.js": "TEMPLATED_node_modules_workspace_name//@bazel/cypress:internal/run-cypress.js",

packages/cypress/internal/cypress_repository.bzl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ load("@build_bazel_rules_nodejs//internal/node:node_labels.bzl", "get_node_label
2020
def _cypress_repository_impl(repository_ctx):
2121
node = repository_ctx.path(get_node_label(repository_ctx))
2222

23-
cypress_install = "packages/cypress/internal/cypress-install.js"
23+
install_cypress = "packages/cypress/internal/install-cypress.js"
2424
repository_ctx.template(
25-
cypress_install,
26-
repository_ctx.path(repository_ctx.attr._cypress_install),
25+
install_cypress,
26+
repository_ctx.path(repository_ctx.attr._install_cypress),
2727
{},
2828
)
2929

@@ -36,10 +36,13 @@ def _cypress_repository_impl(repository_ctx):
3636
)
3737

3838
exec_result = repository_ctx.execute(
39-
[node, cypress_install, repository_ctx.path(repository_ctx.attr.cypress_bin)],
39+
[node, install_cypress, repository_ctx.path(repository_ctx.attr.cypress_bin)],
4040
quiet = repository_ctx.attr.quiet,
4141
)
4242

43+
if exec_result.return_code != 0 and repository_ctx.attr.fail_on_error:
44+
fail("\ncypress_repository exited with code: {}\n\nstdout:\n{}\n\nstderr:\n{}\n\n".format(exec_result.return_code, exec_result.stdout, exec_result.stderr))
45+
4346
cypress_repository = repository_rule(
4447
implementation = _cypress_repository_impl,
4548
attrs = {
@@ -48,17 +51,21 @@ cypress_repository = repository_rule(
4851
allow_single_file = True,
4952
default = "@npm//:node_modules/cypress/bin/cypress",
5053
),
54+
"fail_on_error": attr.bool(
55+
default = True,
56+
doc = "If the repository rule should allow errors",
57+
),
5158
"quiet": attr.bool(
5259
default = True,
5360
doc = "If stdout and stderr should be printed to the terminal",
5461
),
55-
"_cypress_install": attr.label(
56-
allow_single_file = True,
57-
default = "//packages/cypress:internal/cypress-install.js",
58-
),
5962
"_cypress_web_test": attr.label(
6063
allow_single_file = True,
6164
default = "//packages/cypress:internal/template.cypress_web_test.bzl",
6265
),
66+
"_install_cypress": attr.label(
67+
allow_single_file = True,
68+
default = "//packages/cypress:internal/install-cypress.js",
69+
),
6370
},
6471
)

packages/cypress/internal/cypress-install.js renamed to packages/cypress/internal/install-cypress.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* cypress-install is responsible for creating an external repository from which a cypress_web_test
2+
* install-cypress is responsible for creating an external repository from which a cypress_web_test
33
* can be loaded. The script invokes `cypress install` to download and install the cypress binary
44
* and subsequently calls cypress verify to ensure the binary is runnable.
55
*
@@ -47,7 +47,12 @@ exports_files([
4747
_cypress_web_test = "cypress_web_test_global_cache",
4848
)
4949
cypress_web_test = _cypress_web_test`)
50+
51+
const env = {
52+
PATH: `${dirname(nodePath)}:${process.env.PATH}`,
53+
};
5054
const spawnOptions = {
55+
env,
5156
stdio: [process.stdin, process.stdout, process.stderr],
5257
shell: process.env.SHELL
5358
};
@@ -71,6 +76,7 @@ function installSandboxedCypressCache() {
7176
const env = {
7277
CYPRESS_CACHE_FOLDER: join(cwd, 'cypress-cache'),
7378
PATH: `${dirname(nodePath)}:${process.env.PATH}`,
79+
DEBUG: 'cypress:*'
7480
}
7581

7682
const spawnOptions =
@@ -91,9 +97,7 @@ function installSandboxedCypressCache() {
9197
};
9298

9399
let CYPRESS_RUN_BINARY;
94-
const cacheFiles = [];
95100
walkDir(env.CYPRESS_CACHE_FOLDER, (filePath) => {
96-
cacheFiles.push(filePath);
97101
if (basename(filePath) === 'Cypress') {
98102
if (CYPRESS_RUN_BINARY) {
99103
throw new Error(`More than one cypress executable found: ${CYPRESS_RUN_BINARY} ${filePath}`)
@@ -107,12 +111,19 @@ function installSandboxedCypressCache() {
107111
throw new Error(`No cypress executable found.`);
108112
}
109113

110-
const verify = spawnSync(`${cypressBin}`, ['verify'], spawnOptions);
114+
spawnOptions.env.CYPRESS_RUN_BINARY = CYPRESS_RUN_BINARY;
115+
116+
const verify = spawnSync(cypressBin, ['verify'], spawnOptions);
111117

112118
if (verify.status !== 0) {
113119
throw new Error(`cypress verify failed`);
114120
}
115121

122+
const cacheFiles = [];
123+
walkDir(env.CYPRESS_CACHE_FOLDER, (filePath) => {
124+
cacheFiles.push(filePath);
125+
});
126+
116127
writeFileSync('index.bzl', `load(
117128
"//:packages/cypress/internal/cypress_web_test.bzl",
118129
_cypress_web_test = "cypress_web_test",
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const fs = require('fs');
2-
const {join, basename, normalize} = require('path');
1+
const {join, normalize} = require('path');
32

43
const basePluginShortPath = 'TEMPLATED_pluginsFile';
54
const integrationFileShortPaths = TEMPLATED_integrationFileShortPaths;
@@ -12,31 +11,27 @@ const browserifyFactory = require(normalize(`TEMPLATED_@cypress/browserify-prepr
1211
const browserify = browserifyFactory(browserifyFactory.defaultOptions);
1312

1413
module.exports = (on, config) => {
15-
// Load in the user's cypress plugin
16-
config = basePlugin(on, config);
14+
// Set env variables needed usually set by for `bazel test` invocations
15+
// (they are not set automatically for `bazel run`).
16+
process.env.RUNFILES_DIR = process.env.RUNFILES_DIR || join(cwd, '../');
17+
process.env.RUNFILES_MANIFEST_FILE =
18+
process.env.RUNFILES_MANIFEST_FILE || join(cwd, '../', 'MANIFEST');
1719

1820
// Set test files to tests passed as `srcs`
1921
config.integrationFolder = cwd;
2022
config.testFiles = integrationFileShortPaths;
2123

22-
// Set screenshots folder to a writable directory
23-
const screenshotsFolder = join(process.env['TEST_UNDECLARED_OUTPUTS_DIR'], 'screenshots');
24-
fs.mkdirSync(screenshotsFolder);
25-
config.screenshotsFolder = screenshotsFolder;
26-
27-
// Set videos folder to a writable directory
28-
const videosFolder = join(process.env['TEST_UNDECLARED_OUTPUTS_DIR'], 'videos');
29-
fs.mkdirSync(videosFolder);
30-
config.videosFolder = videosFolder;
31-
32-
// Chrome sandboxing must be disabled for execution during bazel test.
33-
config.chromeWebSecurity = false;
24+
// Set screenshots/videos folder to a writable directory
25+
config.screenshotsFolder = process.env['TEST_UNDECLARED_OUTPUTS_DIR'] || process.env.RUNFILES_DIR;
26+
config.videosFolder = process.env['TEST_UNDECLARED_OUTPUTS_DIR'] || process.env.RUNFILES_DIR;
3427

3528
// Set file preprocessing output path to writable directory.
3629
on('file:preprocessor', (file) => {
37-
file.outputPath = join(process.env['TEST_UNDECLARED_OUTPUTS_DIR'], basename(file.outputPath));
30+
file.outputPath =
31+
join(process.env['TEST_TMPDIR'], file.outputPath.split('/bundles/').slice(1).join());
3832
return browserify(file);
3933
});
4034

41-
return config;
35+
// Load in the user's cypress plugin
36+
return basePlugin(on, config);
4237
};

packages/cypress/internal/run-cypress.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ const [node, entry, configFilePath, pluginsFilePath, cypressExecutable, ...args]
77
if (cypressExecutable) {
88
process.env.CYPRESS_RUN_BINARY =
99
join(process.cwd(), cypressExecutable.replace('external/', '../'));
10+
process.env.CYPRESS_CACHE_FOLDER =
11+
join(process.env.CYPRESS_RUN_BINARY.split('/cypress-cache/')[0], '/cypress-cache');
12+
process.env.HOME = process.env['TEST_TMPDIR'];
1013
}
1114

12-
console.log(process.argv);
13-
14-
1515
const pluginsFile = runfiles.resolveWorkspaceRelative(pluginsFilePath).replace(process.cwd(), '.');
1616
const configFile = runfiles.resolveWorkspaceRelative(configFilePath).replace(process.cwd(), '.');
1717

packages/cypress/internal/template.cypress_web_test.bzl

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ def _cypress_plugin_impl(ctx):
4141
template = ctx.file._plugin_template,
4242
substitutions = {
4343
"TEMPLATED_@cypress/browserify-preprocessor": "${cwd}/../cypress_deps/node_modules/@cypress/browserify-preprocessor/index",
44-
"TEMPLATED_includeScreenshots": "true" if ctx.attr.include_screenshots else "false",
45-
"TEMPLATED_includeVideos": "true" if ctx.attr.include_video else "false",
4644
"TEMPLATED_integrationFileShortPaths": "[\n {files}\n]".format(files = ",\n ".join(integration_files_short_paths)),
4745
"TEMPLATED_pluginsFile": plugins_file.short_path,
4846
},
@@ -52,13 +50,6 @@ def _cypress_plugin_impl(ctx):
5250
files = depset([ctx.outputs.plugin]),
5351
)]
5452

55-
# Avoid using non-normalized paths (workspace/../other_workspace/path)
56-
def _to_manifest_path(ctx, file):
57-
if file.short_path.startswith("../"):
58-
return file.short_path[3:]
59-
else:
60-
return ctx.workspace_name + "/" + file.short_path
61-
6253
_cypress_plugin = rule(
6354
implementation = _cypress_plugin_impl,
6455
outputs = {"plugin": "%{name}_cypress_plugin.js"},
@@ -67,8 +58,6 @@ _cypress_plugin = rule(
6758
allow_single_file = [".json"],
6859
mandatory = True,
6960
),
70-
"include_screenshots": attr.bool(default = False),
71-
"include_video": attr.bool(default = False),
7261
"plugins_file": attr.label(
7362
default = Label("@build_bazel_rules_nodejs//packages/cypress:internal/plugins/base.js"),
7463
allow_single_file = True,
@@ -89,8 +78,6 @@ def cypress_web_test(
8978
name,
9079
config_file,
9180
srcs = [],
92-
include_screenshots = False,
93-
include_video = False,
9481
plugins_file = Label("@build_bazel_rules_nodejs//packages/cypress:internal/plugins/base.js"),
9582
cypress = Label("TEMPLATED_node_modules_workspace_name//cypress:cypress"),
9683
cypress_browserify_preprocessor = Label("TEMPLATED_node_modules_workspace_name//@cypress/browserify-preprocessor"),
@@ -106,8 +93,6 @@ def cypress_web_test(
10693
name = cypress_plugin,
10794
srcs = srcs,
10895
tags = tags,
109-
include_screenshots = include_screenshots,
110-
include_video = include_video,
11196
plugins_file = plugins_file,
11297
config_file = config_file,
11398
testonly = True,
@@ -123,14 +108,14 @@ def cypress_web_test(
123108
cypress_browserify_preprocessor,
124109
cypress_cache,
125110
cypress_executable,
126-
":{cypress_plugin}".format(cypress_plugin = cypress_plugin),
127-
":{config_file}".format(config_file = config_file),
111+
"{cypress_plugin}".format(cypress_plugin = cypress_plugin),
112+
"{config_file}".format(config_file = config_file),
128113
] + srcs,
129114
entry_point = "@build_bazel_rules_nodejs//packages/cypress:internal/run-cypress.js",
130115
templated_args = [
131116
"--nobazel_patch_module_resolver",
132-
"$(rootpath :{config_file})".format(config_file = config_file),
133-
"$(rootpath :{cypress_plugin})".format(cypress_plugin = cypress_plugin),
117+
"$(rootpath {config_file})".format(config_file = config_file),
118+
"$(rootpath {cypress_plugin})".format(cypress_plugin = cypress_plugin),
134119
"$(rootpath {cypress_executable})".format(cypress_executable = cypress_executable),
135120
] + templated_args,
136121
**kwargs
@@ -140,8 +125,6 @@ def cypress_web_test_global_cache(
140125
name,
141126
config_file,
142127
srcs = [],
143-
include_screenshots = False,
144-
include_video = False,
145128
plugins_file = Label("@build_bazel_rules_nodejs//packages/cypress:plugins/base.js"),
146129
cypress = Label("TEMPLATED_node_modules_workspace_name//cypress:cypress"),
147130
cypress_browserify_preprocessor = Label("TEMPLATED_node_modules_workspace_name//@cypress/browserify-preprocessor"),
@@ -155,8 +138,6 @@ def cypress_web_test_global_cache(
155138
name = cypress_plugin,
156139
srcs = srcs,
157140
tags = tags,
158-
include_screenshots = include_screenshots,
159-
include_video = include_video,
160141
plugins_file = plugins_file,
161142
config_file = config_file,
162143
testonly = True,
@@ -170,14 +151,14 @@ def cypress_web_test_global_cache(
170151
plugins_file,
171152
cypress,
172153
cypress_browserify_preprocessor,
173-
":{cypress_plugin}".format(cypress_plugin = cypress_plugin),
174-
":{config_file}".format(config_file = config_file),
154+
"{cypress_plugin}".format(cypress_plugin = cypress_plugin),
155+
"{config_file}".format(config_file = config_file),
175156
] + srcs,
176157
entry_point = "@build_bazel_rules_nodejs//packages/cypress:internal/run-cypress.js",
177158
templated_args = [
178159
"--nobazel_patch_module_resolver",
179-
"$(rootpath :{config_file})".format(config_file = config_file),
180-
"$(rootpath :{cypress_plugin})".format(cypress_plugin = cypress_plugin),
160+
"$(rootpath {config_file})".format(config_file = config_file),
161+
"$(rootpath {cypress_plugin})".format(cypress_plugin = cypress_plugin),
181162
] + templated_args,
182163
**kwargs
183164
)

0 commit comments

Comments
 (0)