| load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin") |
| load("@npm//:@babel/cli/package_json.bzl", babel_bin = "bin") |
| load("//js:defs.bzl", "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 = [ |
| "node16", |
| "node18", |
| "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 |
| ] |
| |
| # 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 |
| ] |
| |
| babel_bin.babel( |
| name = "babel_mjs2js", |
| srcs = MJS_TESTS + [ |
| "babel.config.js", |
| "//:node_modules/@babel/plugin-transform-modules-commonjs", |
| ], |
| outs = [t.replace(".mjs", ".cjs") for t in MJS_TESTS], |
| args = [ |
| "--extensions", |
| ".mjs", |
| "--out-file-extension", |
| ".cjs", |
| "--out-dir", |
| ".", |
| ] + MJS_TESTS, |
| chdir = package_name(), # to automatically pickup babel.config.js |
| ) |
| |
| # 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, |
| ) |
| ] |