Skip to content

Commit

Permalink
test: fix toolchain test now that nodejs_binary chooses the correct r…
Browse files Browse the repository at this point in the history
…unfiles for the host platform

Runfiles are no longer tested as what should be tested is that ctx.toolchains["@build_bazel_rules_nodejs//toolchains/node:toolchain_type"].nodeinfo.tool_files are for the correct `--platform`.
  • Loading branch information
gregmagolan committed Nov 7, 2019
1 parent 7551535 commit 3efce87
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 107 deletions.
64 changes: 59 additions & 5 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ tasks:
- "--test_tag_filters=-e2e,-examples,-no-bazelci,-no-bazelci-ubuntu,-manual"
test_targets:
- "//..."
# //internal/node/test:nodejs_toolchain_linux_test is a "manual" test that must be run
# explicitly; it should pass when running on Linux with no --platform set.
- "//internal/node/test:nodejs_toolchain_linux_test"
ubuntu1604_e2e:
name: ubuntu1604_e2e
platform: ubuntu1604
Expand Down Expand Up @@ -96,6 +99,9 @@ tasks:
- "--test_tag_filters=-e2e,-examples,-no-bazelci,-no-bazelci-ubuntu,-manual"
test_targets:
- "//..."
# //internal/node/test:nodejs_toolchain_linux_test is a "manual" test that must be run
# explicitly; it should pass when running on Linux with no --platform set.
- "//internal/node/test:nodejs_toolchain_linux_test"
ubuntu1804_debug:
name: ubuntu1804_debug
platform: ubuntu1804
Expand Down Expand Up @@ -130,7 +136,7 @@ tasks:
- "//..."
ubuntu1804_examples:
name: ubuntu1804_examples
platform: ubuntu1604
platform: ubuntu1804
# We need to reduce the memory & CPU usage of the top-level
# bazel process for bazel-in-bazel tests to not
# deplete the system memory completely.
Expand All @@ -156,6 +162,34 @@ tasks:
# TODO(gregmagolan): make node_repositories acccept different archives for different platforms
- "//examples:examples_vendored_node"
- "//examples:examples_vendored_node_and_yarn"
ubuntu1804_cross_compile_darwin:
name: ubuntu1804_cross_compile_darwin
platform: ubuntu1804
# Build on linux with the node --platform set to darwin
build_flags:
- "--platforms=@build_bazel_rules_nodejs//toolchains/node:darwin_amd64"
build_targets:
- "//..."
test_flags:
- "--platforms=@build_bazel_rules_nodejs//toolchains/node:darwin_amd64"
test_targets:
# //internal/node/test:nodejs_toolchain_darwin_test is a "manual" test that must be run
# explicitly with --platforms=@build_bazel_rules_nodejs//toolchains/node:darwin_amd64 set
- "//internal/node/test:nodejs_toolchain_darwin_test"
ubuntu1804_cross_compile_windows:
name: ubuntu1804_cross_compile_windows
platform: ubuntu1804
# Build on linux with the node --platform set to Windows
build_flags:
- "--platforms=@build_bazel_rules_nodejs//toolchains/node:windows_amd64"
build_targets:
- "//..."
test_flags:
- "--platforms=@build_bazel_rules_nodejs//toolchains/node:windows_amd64"
test_targets:
# //internal/node/test:nodejs_toolchain_windows_test is a "manual" test that must be run
# explicitly with --platforms=@build_bazel_rules_nodejs//toolchains/node:windows_amd64 set
- "//internal/node/test:nodejs_toolchain_windows_test"
macos:
name: macos
platform: macos
Expand All @@ -177,6 +211,9 @@ tasks:
- "--test_tag_filters=-e2e,-examples,-no-bazelci,-no-bazelci-mac,-manual"
test_targets:
- "//..."
# //internal/node/test:nodejs_toolchain_darwin_test is a "manual" test that must be run
# explicitly; it should pass when running on OSX with no --platform set.
- "//internal/node/test:nodejs_toolchain_darwin_test"
macos_e2e:
name: macos_e2e
platform: macos
Expand Down Expand Up @@ -225,13 +262,20 @@ tasks:
- "--test_arg=--test_tag_filters=-no-bazelci,-no-bazelci-mac,-manual"
test_targets:
- "//..."
macos_cross_compile:
name: macos_cross_compile
macos_cross_compile_linux:
name: macos_cross_compile_linux
platform: macos
# Build on mac with the node --platform set to linux
build_flags:
- "--platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64"
build_targets:
- "//..."
test_flags:
- "--platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64"
test_targets:
# //internal/node/test:nodejs_toolchain_linux_test is a "manual" test that must be run
# explicitly with --platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64 set
- "//internal/node/test:nodejs_toolchain_linux_test"
macos_fake_rbe:
name: macos_fake_rbe
platform: macos
Expand Down Expand Up @@ -264,6 +308,9 @@ tasks:
- "--test_tag_filters=-e2e,-examples,-no-bazelci,-no-bazelci-windows,-fix-windows,-manual"
test_targets:
- "//..."
# //internal/node/test:nodejs_toolchain_windows_test is a "manual" test that must be run
# explicitly; it should pass when running on Windows with no --platform set.
- "//internal/node/test:nodejs_toolchain_windows_test"
windows_e2e:
name: windows_e2e
platform: windows
Expand Down Expand Up @@ -294,13 +341,20 @@ tasks:
- "--test_arg=--local_resources=13288,1.0,1.0"
test_targets:
- "//..."
windows_cross_compile:
name: windows_cross_compile
windows_cross_compile_linux:
name: windows_cross_compile_linux
platform: windows
# Build on windows with the node --platform set to linux
build_flags:
- "--platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64"
build_targets:
- "//..."
test_flags:
- "--platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64"
test_targets:
# //internal/node/test:nodejs_toolchain_linux_test is a "manual" test that must be run
# explicitly with --platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64 set
- "//internal/node/test:nodejs_toolchain_linux_test"
rbe_ubuntu1604:
name: rbe_ubuntu1604
platform: rbe_ubuntu1604
Expand Down
25 changes: 22 additions & 3 deletions internal/node/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary", "nodejs_test", "n
load("//internal/js_library:js_library.bzl", "js_library")
load("//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_file.bzl", "copy_file")
load("//third_party/github.com/bazelbuild/bazel-skylib:rules/write_file.bzl", "write_file")
load(":nodejs_toolchain_test.bzl", "nodejs_binary_test_suite")

nodejs_binary_test_suite()
load(":nodejs_toolchain_test.bzl", "nodejs_toolchain_test")

# You can have a nodejs_binary with no node_modules attribute
# and no fine grained deps
Expand Down Expand Up @@ -183,3 +181,24 @@ nodejs_test(
],
entry_point = "npm_package_bin.spec.js",
)

# must be run with --platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64
nodejs_toolchain_test(
name = "nodejs_toolchain_linux_test",
expected_node_path = "external/nodejs_linux_amd64/bin/nodejs/bin/node",
tags = ["manual"],
)

# must be run with --platforms=@build_bazel_rules_nodejs//toolchains/node:darwin_amd64
nodejs_toolchain_test(
name = "nodejs_toolchain_darwin_test",
expected_node_path = "external/nodejs_darwin_amd64/bin/nodejs/bin/node",
tags = ["manual"],
)

# must be run with --platforms=@build_bazel_rules_nodejs//toolchains/node:windows_amd64
nodejs_toolchain_test(
name = "nodejs_toolchain_windows_test",
expected_node_path = "external/nodejs_windows_amd64/bin/nodejs/node.exe",
tags = ["manual"],
)
152 changes: 53 additions & 99 deletions internal/node/test/nodejs_toolchain_test.bzl
Original file line number Diff line number Diff line change
@@ -1,101 +1,55 @@
"Unit tests for node.bzl toolchain support"

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")

def _runfiles_contents_test_impl(ctx):
env = analysistest.begin(ctx)
target_under_test = analysistest.target_under_test(env)

# check target's runfiles
runfiles = sorted(target_under_test[DefaultInfo].default_runfiles.files.to_list())
asserts.true(env, ctx.file.node_selected in runfiles)
asserts.false(env, ctx.files.node_other[0] in runfiles)
asserts.false(env, ctx.files.node_other[1] in runfiles)

# This is a bit of a hack, but because "@nodejs//:node_bin" is just an alias to one of the other nodejs repositories
# bazel automatically filters it from the list if the aliased label already exists.
# So we have to check that it has not been filtered out and then we do expect it to be in runfiles, as it just points
# to the same file as "node_selected"
if len(ctx.files.node_other) == 3:
asserts.true(env, ctx.files.node_other[2] in runfiles)

return analysistest.end(env)

linux_platform_toolchain_test = analysistest.make(
_runfiles_contents_test_impl,
config_settings = {
"//command_line_option:platforms": "@build_bazel_rules_nodejs//toolchains/node:linux_amd64",
},
attrs = {
"node_other": attr.label_list(
default = [Label("@nodejs_windows_amd64//:node_bin"), Label("@nodejs_darwin_amd64//:node_bin"), Label("@nodejs//:node_bin")],
allow_files = True,
),
"node_selected": attr.label(
default = Label("@nodejs_linux_amd64//:node_bin"),
allow_single_file = True,
# Copyright 2017 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.

"""Testing for node toolchains
This test verifies that if --platforms=@build_bazel_rules_nodejs//toolchains/node:<platform> is set then
the correct node path is available to rules via
ctx.toolchains["@build_bazel_rules_nodejs//toolchains/node:toolchain_type"].nodeinfo.tool_files[0].path
"""

_SCRIPT_TEMPLATE = """#!/bin/bash
EXPECTED_NODE_PATH="{expected_node_path}"
TOOLCHAIN_NODE_PATH="{toolchain_node_path}"
if [ "$EXPECTED_NODE_PATH" != "$TOOLCHAIN_NODE_PATH" ]; then
echo "Expected platform node path to be '$EXPECTED_NODE_PATH' but got '$TOOLCHAIN_NODE_PATH'"
exit 1
fi
"""

_ATTRS = {
"expected_node_path": attr.string(),
}

def _nodejs_toolchain_test(ctx):
script = ctx.actions.declare_file(ctx.label.name)
ctx.actions.write(
script,
_SCRIPT_TEMPLATE.format(
expected_node_path = ctx.attr.expected_node_path,
toolchain_node_path = ctx.toolchains["@build_bazel_rules_nodejs//toolchains/node:toolchain_type"].nodeinfo.tool_files[0].path,
),
},
)

windows_platform_toolchain_test = analysistest.make(
_runfiles_contents_test_impl,
config_settings = {
"//command_line_option:platforms": "@build_bazel_rules_nodejs//toolchains/node:windows_amd64",
},
attrs = {
"node_other": attr.label_list(
default = [Label("@nodejs_linux_amd64//:node_bin"), Label("@nodejs_darwin_amd64//:node_bin"), Label("@nodejs//:node_bin")],
allow_files = True,
),
"node_selected": attr.label(
default = Label("@nodejs_windows_amd64//:node_bin"),
allow_single_file = True,
),
},
)

darwin_platform_toolchain_test = analysistest.make(
_runfiles_contents_test_impl,
config_settings = {
"//command_line_option:platforms": "@build_bazel_rules_nodejs//toolchains/node:darwin_amd64",
},
attrs = {
"node_other": attr.label_list(
default = [Label("@nodejs_windows_amd64//:node_bin"), Label("@nodejs_linux_amd64//:node_bin"), Label("@nodejs//:node_bin")],
allow_files = True,
),
"node_selected": attr.label(
default = Label("@nodejs_darwin_amd64//:node_bin"),
allow_single_file = True,
),
},
)

def test_runfiles_contents():
linux_platform_toolchain_test(
name = "linux_platform_toolchain_test",
target_under_test = ":no_deps",
)

windows_platform_toolchain_test(
name = "windows_platform_toolchain_test",
target_under_test = ":no_deps",
)

darwin_platform_toolchain_test(
name = "darwin_platform_toolchain_test",
target_under_test = ":no_deps",
)

def nodejs_binary_test_suite():
test_runfiles_contents()

native.test_suite(
name = "nodejs_toolchain_test",
tests = [
":linux_platform_toolchain_test",
":windows_platform_toolchain_test",
":darwin_platform_toolchain_test",
],
is_executable = True,
)
return [DefaultInfo(executable = script)]

nodejs_toolchain_test = rule(
implementation = _nodejs_toolchain_test,
attrs = _ATTRS,
test = True,
toolchains = [
"@build_bazel_rules_nodejs//toolchains/node:toolchain_type",
"@bazel_tools//tools/sh:toolchain_type",
],
)

0 comments on commit 3efce87

Please sign in to comment.