Skip to content

Commit

Permalink
feat(cypress): use a toolchain instead of a select statements (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Muller committed May 25, 2021
1 parent ec05f29 commit a049446
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 48 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ load("//packages/cypress:index.bzl", "cypress_repositories")

cypress_repositories(
name = "cypress",
amd64_sha256 = "d8ea8d16fed33fdae8f17178bcae076aaf532fa7ccb48f377df1f143e60abd59",
darwin_sha256 = "101a0ced77fb74b356800cb3a3919f5288d23cc63fdd39a0c500673159e954fc",
linux_sha256 = "d8ea8d16fed33fdae8f17178bcae076aaf532fa7ccb48f377df1f143e60abd59",
version = "7.3.0",
windows_sha256 = "8a8809e4fd22fe7bfc3103c39df3f4fce9db0964450ce927558e9a09558cb26c",
)
Expand Down
8 changes: 4 additions & 4 deletions packages/cypress/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package(default_visibility = ["//visibility:public"])

exports_files(
[
"internal/plugins/index.template.js",
"internal/plugins/index.js.tpl",
"internal/run-cypress.js",
],
visibility = ["//packages/cypress/test:__subpackages__"],
Expand Down Expand Up @@ -67,20 +67,20 @@ pkg_npm(
"//packages/cypress:internal/cypress_repositories.bzl",
"//packages/cypress:internal/cypress_web_test.bzl",
"//packages/cypress:internal/plugins/base.js",
"//packages/cypress:internal/plugins/index.template.js",
"//packages/cypress:internal/plugins/index.js.tpl",
"//packages/cypress:internal/run-cypress.js",
],
build_file_content = """exports_files([
"internal/plugins/base.js",
"internal/plugins/index.template.js",
"internal/plugins/index.js.tpl",
"internal/run-cypress.js",
"plugins/base.js",
])""",
substitutions = {
"@build_bazel_rules_nodejs//packages/cypress:internal/cypress_repositories.bzl": "//@bazel/cypress:internal/cypress_repositories.bzl",
"@build_bazel_rules_nodejs//packages/cypress:internal/cypress_web_test.bzl": "//@bazel/cypress:internal/cypress_web_test.bzl",
"@build_bazel_rules_nodejs//packages/cypress:internal/plugins/base.js": "//@bazel/cypress:internal/plugins/base.js",
"@build_bazel_rules_nodejs//packages/cypress:internal/plugins/index.template.js": "//@bazel/cypress:internal/plugins/index.template.js",
"@build_bazel_rules_nodejs//packages/cypress:internal/plugins/index.js.tpl": "//@bazel/cypress:internal/plugins/index.js.tpl",
"@build_bazel_rules_nodejs//packages/cypress:internal/run-cypress.js": "//@bazel/cypress:internal/run-cypress.js",
},
deps = [
Expand Down
21 changes: 0 additions & 21 deletions packages/cypress/internal/_cypress_repository_build_TEMPLATE

This file was deleted.

38 changes: 24 additions & 14 deletions packages/cypress/internal/cypress_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//packages/cypress/internal/toolchain:cypress_toolchain_configure.bzl", "cypress_toolchain_configure")

def _cypress_repository_impl(repository_ctx):
repository_ctx.template(
Expand All @@ -39,8 +40,8 @@ _cypress_repository = repository_rule(
def cypress_repositories(
name,
version,
amd64_urls = [],
amd64_sha256 = "",
linux_urls = [],
linux_sha256 = "",
darwin_urls = [],
darwin_sha256 = "",
windows_urls = [],
Expand All @@ -51,8 +52,8 @@ def cypress_repositories(
Args:
name: Name of the external workspace where the cypress binary lives
version: Version of cypress binary to use. Should match package.json
amd64_urls: (Optional) URLs at which the cypress binary for amd64 distros of linux can be downloaded. If omitted, https://cdn.cypress.io/desktop will be used.
amd64_sha256: (Optional) SHA-256 of the amd64 cypress binary
linux_urls: (Optional) URLs at which the cypress binary for linux distros of linux can be downloaded. If omitted, https://cdn.cypress.io/desktop will be used.
linux_sha256: (Optional) SHA-256 of the linux cypress binary
darwin_urls: (Optional) URLs at which the cypress binary for darwin distros of linux can be downloaded. If omitted, https://cdn.cypress.io/desktop will be used.
darwin_sha256: (Optional) SHA-256 of the darwin cypress binary
windows_urls: (Optional) URLs at which the cypress binary for windows distros of linux can be downloaded. If omitted, https://cdn.cypress.io/desktop will be used.
Expand All @@ -67,13 +68,13 @@ def cypress_repositories(
strip_prefix = "Cypress",
build_file_content = """
filegroup(
name = "data",
name = "files",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
filegroup(
name = "binary",
name = "bin",
srcs = ["Cypress.exe"],
visibility = ["//visibility:public"],
)
Expand All @@ -90,13 +91,13 @@ filegroup(
],
build_file_content = """
filegroup(
name = "data",
name = "files",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
filegroup(
name = "binary",
name = "bin",
# Cypress checks that the binary path matches **/Contents/MacOS/Cypress
srcs = ["Contents/MacOS/Cypress"],
visibility = ["//visibility:public"],
Expand All @@ -105,25 +106,34 @@ filegroup(
)

http_archive(
name = "{}_amd64".format(name),
sha256 = amd64_sha256,
name = "{}_linux".format(name),
sha256 = linux_sha256,
strip_prefix = "Cypress",
urls = amd64_urls + [
urls = linux_urls + [
"https://cdn.cypress.io/desktop/{}/linux-x64/cypress.zip".format(version),
],
build_file_content = """
filegroup(
name = "data",
name = "files",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
filegroup(
name = "binary",
name = "bin",
srcs = ["Cypress"],
visibility = ["//visibility:public"],
)
""",
)

_cypress_repository(name = name)
# This needs to be setup so toolchains can access nodejs for all different versions
for os_name in ["windows", "darwin", "linux"]:
cypress_repository_name = "{}_{}".format(name, os_name)

native.register_toolchains("//packages/cypress/internal/toolchain:cypress_{}_toolchain".format(os_name))
cypress_toolchain_configure(
name = "%s_config" % cypress_repository_name,
cypress_bin = "@%s//:cypress_bin" % cypress_repository_name,
cypress_files = "@%s//:cypress_files" % cypress_repository_name,
)
9 changes: 1 addition & 8 deletions packages/cypress/internal/cypress_web_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ _cypress_plugin = rule(
),
# Unused by this rule, but here to require that a user supplies one for the downstream nodejs_test
"_plugin_template": attr.label(
default = Label("@build_bazel_rules_nodejs//packages/cypress:internal/plugins/index.template.js"),
default = Label("@build_bazel_rules_nodejs//packages/cypress:internal/plugins/index.js.tpl"),
allow_single_file = True,
),
},
Expand All @@ -79,8 +79,6 @@ def cypress_web_test(
name,
config_file,
srcs = [],
cypress_binary = Label("@cypress//:binary"),
cypress_data = Label("@cypress//:data"),
cypress_npm_package = Label("//cypress"),
data = [],
plugins_file = Label("//plugins/base.js"),
Expand All @@ -92,8 +90,6 @@ def cypress_web_test(
name: Name of the bazel test target
config_file: cypress.json configuration file. See https://docs.cypress.io/guides/references/configuration
srcs: A list of test files. See https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Test-files
cypress_binary: The cypress binary target to execute. If your cypress_repositories rule was named cypress this should be set to "@cypress//:binary"
cypress_data: The cypress data file group to include in the sandbox. If your cypress_repositories rule was named cypress this should be set to "@cypress//:data"
cypress_npm_package: The cypress npm package to exectue. If you installed cypress as a peer dependency, this should not need to be set.
data: Runtime files to include in the sandbox during test exectution.
plugins_file: Your cypress plugin file. See https://docs.cypress.io/guides/tooling/plugins-guide
Expand All @@ -117,8 +113,6 @@ def cypress_web_test(
tags = tags,
data = data + [
plugins_file,
cypress_data,
cypress_binary,
cypress_npm_package,
cypress_plugin,
config_file,
Expand All @@ -127,7 +121,6 @@ def cypress_web_test(
templated_args = [
"$(rootpath {config_file})".format(config_file = config_file),
"$(rootpath {cypress_plugin})".format(cypress_plugin = cypress_plugin),
"$(rootpath {cypress_binary})".format(cypress_binary = cypress_binary),
] + kwargs.pop("templated_args", []),
**kwargs
)
83 changes: 83 additions & 0 deletions packages/cypress/internal/toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package(default_visibility = ["//visibility:private"])

platform(
name = "darwin",
constraint_values = [
"@platforms//os:osx",
"@platforms//cpu:x86_64",
],
)

platform(
name = "linux",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
)

platform(
name = "windows",
constraint_values = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
)

# cypress toolchain type
toolchain_type(name = "cypress_toolchain")

# Allow targets to use a toolchains attribute, such as sh_binary and genrule
# This way they can reference the cypress_PATH make variable.
alias(
name = "toolchain",
actual = select({
"@bazel_tools//src/conditions:darwin": "@cypress_darwin_config//:toolchain",
"@bazel_tools//src/conditions:linux": "@cypress_linux_config//:toolchain",
"@bazel_tools//src/conditions:windows": "@cypress_windows_config//:toolchain",
"//conditions:default": "@cypress_linux_config//:toolchain",
}),
visibility = ["//visibility:public"],
)

# Allow targets to declare a dependency on the cypress binary for the current host platform
alias(
name = "cypress_bin",
actual = select({
"@bazel_tools//src/conditions:darwin": "@cypress_darwin//:cypress_bin",
"@bazel_tools//src/conditions:linux": "@cypress_linux//:cypress_bin",
"@bazel_tools//src/conditions:windows": "@cypress_windows//:cypress_bin",
"//conditions:default": "@cypress_linux//:cypress_bin",
}),
visibility = ["//visibility:public"],
)

toolchain(
name = "cypress_linux_toolchain",
target_compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
toolchain = "@cypress_linux_config//:toolchain",
toolchain_type = ":cypress_toolchain",
)

toolchain(
name = "cypress_darwin_toolchain",
target_compatible_with = [
"@platforms//os:osx",
"@platforms//cpu:x86_64",
],
toolchain = "@cypress_darwin_config//:toolchain",
toolchain_type = ":cypress_toolchain",
)

toolchain(
name = "cypress_windows_toolchain",
target_compatible_with = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
toolchain = "@cypress_windows_config//:toolchain",
toolchain_type = ":cypress_toolchain",
)
25 changes: 25 additions & 0 deletions packages/cypress/internal/toolchain/BUILD.bazel.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2021 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This BUILD file is auto-generated from @TEMPLATED_cypress_workspace_name//internal:BUILD.bazel.tpl
"""

package(default_visibility = ["//visibility:public"])

load("@TEMPLATED_cypress_workspace_name//internal/toolchain:cypress_toolchain.bzl", "cypress_toolchain")

cypress_toolchain(
name = "toolchain",
%{TOOL_ATTRS}
)
Loading

0 comments on commit a049446

Please sign in to comment.