blob: 902b182771b1672eca3a38029d574803fb71420f [file]
load("@bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@npm//js/private/test/node-patches:@babel/cli/package_json.bzl", babel_bin = "bin")
load("//js:defs.bzl", "js_library", "js_test")
TESTS = [
"escape.js",
]
MJS_TESTS = [
"lstat.mjs",
"opendir.mjs",
"readdir.mjs",
"readlink.mjs",
"realpath.mjs",
]
CJS_TESTS = [
t.replace(".mjs", ".cjs")
for t in MJS_TESTS
]
# Multiple node toolchains for testing across versions
TOOLCHAINS_NAMES = [
# default name+version
"nodejs",
# custom versions for tests
"node20",
"node22",
"node24",
]
TOOLCHAINS_VERSIONS = [
select({
"@bazel_tools//src/conditions:linux_x86_64": "@%s_linux_amd64//:node_toolchain" % n,
"@bazel_tools//src/conditions:darwin": "@%s_darwin_amd64//:node_toolchain" % n,
"@bazel_tools//src/conditions:windows": "@%s_windows_amd64//:node_toolchain" % n,
})
for n in TOOLCHAINS_NAMES
]
npm_link_all_packages()
# We need to copy the entry points to the bin so that the tests below don't follow the execroot
# symlink back to the source tree since the fs patches are not on for the tests as they are the code
# under test
[
copy_to_bin(
name = "copy_entry_{}".format(t),
srcs = [t],
)
for t in TESTS + MJS_TESTS
]
js_library(
name = "babel_config",
srcs = ["babel.config.js"],
deps = [
":node_modules/@babel/plugin-transform-modules-commonjs",
],
)
babel_bin.babel(
name = "babel_mjs2js",
srcs = MJS_TESTS,
outs = CJS_TESTS,
chdir = package_name(),
data = [":babel_config"],
fixed_args = [
"--config-file",
"$$RUNFILES_DIR/$(rlocationpath :babel_config)",
"--extensions",
".mjs",
"--out-file-extension",
".cjs",
"--out-dir",
".",
] + MJS_TESTS,
)
# Basic tests
[
[
# The primary tests, both .js and .mjs
[
js_test(
name = "{}_{}_test".format(
t.replace(".mjs", "").replace(".js", ""),
toolchain_name,
),
data = [
":node_modules/inline-fixtures",
"//js/private/node-patches/src:compile",
],
entry_point = "copy_entry_{}".format(t),
node_toolchain = toolchain,
patch_node_fs = False,
# Without node patches on for these tests, the program is going to escape the sandbox if it
# is on since the fs patches are not on for the tests as they are the code under test
tags = ["no-sandbox"],
)
for t in TESTS + MJS_TESTS
],
# The .cjs tests converted from .mjs
[
js_test(
name = "{}_{}_cjs_test".format(
t.replace(".cjs", ""),
toolchain_name,
),
data = [
":node_modules/inline-fixtures",
"//js/private/node-patches/src:compile",
],
entry_point = t,
node_toolchain = toolchain,
patch_node_fs = False,
# Without node patches on for these tests, the program is going to escape the sandbox if it
# is on since the fs patches are not on for the tests as they are the code under test
tags = ["no-sandbox"],
)
for t in CJS_TESTS
],
]
for toolchain_name, toolchain in zip(
TOOLCHAINS_NAMES,
TOOLCHAINS_VERSIONS,
)
]
# Node process spawning tests
[
js_test(
name = "spawn_test_%s" % toolchain_name,
args = [
"$(rootpath //js/private/test/node-patches:spawn_node_path.js)",
"$(rootpath //js/private/test/node-patches:spawn_node_path.sh)",
"$(rootpath //js/private/test/node-patches:spawn_patch_depth.js)",
"$(rootpath //js/private/test/node-patches:spawn_patch_depth.sh)",
],
data = [
"//js/private/test/node-patches:spawn_node_path.js",
"//js/private/test/node-patches:spawn_node_path.sh",
"//js/private/test/node-patches:spawn_patch_depth.js",
"//js/private/test/node-patches:spawn_patch_depth.sh",
],
entry_point = "spawn.js",
node_toolchain = toolchain,
patch_node_fs = True,
)
for toolchain_name, toolchain in zip(
TOOLCHAINS_NAMES,
TOOLCHAINS_VERSIONS,
)
]