From 606b7355bd74c881ad26631188ea152c70d15f82 Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Tue, 1 Sep 2026 10:24:21 -0700 Subject: [PATCH] test: add test of npm_import(patch_tool) --- MODULE.bazel | 14 ++++++++++++++ npm/private/test/BUILD.bazel | 10 ++++++++++ npm/private/test/meaning-of-life@1.0.0.patch | 7 +++++++ npm/private/test/patch_tool.sh | 8 ++++++++ npm/private/test/patch_tool_test.js | 7 +++++++ 5 files changed, 46 insertions(+) create mode 100644 npm/private/test/meaning-of-life@1.0.0.patch create mode 100755 npm/private/test/patch_tool.sh create mode 100644 npm/private/test/patch_tool_test.js diff --git a/MODULE.bazel b/MODULE.bazel index ba2c6aafa7..f559f74a14 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -270,6 +270,18 @@ npm.npm_translate_lock( npmrc = "//:.npmrc", pnpm_lock = "//:pnpm-lock.yaml", ) +npm.npm_import( + name = "npm__meaning-of-life__1.0.0", + integrity = "sha512-fVA4xSydqtK9owabGcYw1r4EKEsMOVVeYQLeCXPu77Z+8Y2j2B2I16UqZlKIOHnYkJ4RSvpJ00ywy9IWjmuxYw==", + package = "meaning-of-life", + patch_args = ["-p1"], + # A custom patch tool must be a pre-built binary or source file since repository + # rules cannot depend on the output of a build target. + patch_tool = "//npm/private/test:patch_tool.sh", + patches = ["//npm/private/test:meaning-of-life@1.0.0.patch"], + root_package = "npm/private/test", + version = "1.0.0", +) use_repo( npm, "npm", @@ -278,6 +290,8 @@ use_repo( "npm__es5-ext__0.10.64__links", "npm__fsevents__2.3.3__links", "npm__google-closure-compiler__20251111.0.0__links", + "npm__meaning-of-life__1.0.0", + "npm__meaning-of-life__1.0.0__links", "npm__rollup__4.55.2", "npm__rollup__4.55.2__links", "npm__unused__0.2.2__links", diff --git a/npm/private/test/BUILD.bazel b/npm/private/test/BUILD.bazel index 4cb96cf82f..36857491ed 100644 --- a/npm/private/test/BUILD.bazel +++ b/npm/private/test/BUILD.bazel @@ -2,6 +2,7 @@ load("@aspect_rules_js//js:defs.bzl", "js_test") load("@bazel_lib//lib:write_source_files.bzl", "write_source_files") load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@npm//:defs.bzl", "npm_link_all_packages") +load("@npm__meaning-of-life__1.0.0__links//:defs.bzl", "npm_link_imported_package") load("@rules_shell//shell:sh_test.bzl", "sh_test") load(":generated_pkg_json_test.bzl", "generated_pkg_json_test") load(":npm_auth_test.bzl", "npm_auth_failure_test_suite", "npm_auth_test_suite") @@ -15,6 +16,15 @@ load(":utils_tests.bzl", "utils_tests") npm_link_all_packages() +# Links the /MODULE npm_import() of meaning-of-life, patched with a custom `patch_tool` +npm_link_imported_package() + +js_test( + name = "patch_tool_test", + data = [":node_modules/meaning-of-life"], + entry_point = "patch_tool_test.js", +) + # Unit tests utils_tests(name = "test_utils") diff --git a/npm/private/test/meaning-of-life@1.0.0.patch b/npm/private/test/meaning-of-life@1.0.0.patch new file mode 100644 index 0000000000..b0a2e55850 --- /dev/null +++ b/npm/private/test/meaning-of-life@1.0.0.patch @@ -0,0 +1,7 @@ +diff --git a/index.js b/index.js +index a8653a9c9264ca1ac9fd2acb6c523a321912ab33..ae967de93c7c12074a686e1e8437b7b2344108be 100644 +--- a/index.js ++++ b/index.js +@@ -1 +1 @@ +-module.exports = 42 ++module.exports = "forty two" diff --git a/npm/private/test/patch_tool.sh b/npm/private/test/patch_tool.sh new file mode 100755 index 0000000000..270ff1dbf8 --- /dev/null +++ b/npm/private/test/patch_tool.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -o errexit -o nounset -o pipefail + +patch "$@" + +# Leave a trace only this custom tool would produce so the test can +# distinguish it from the default `patch` from PATH. +echo 'module.exports += " (custom patch_tool)"' >>index.js diff --git a/npm/private/test/patch_tool_test.js b/npm/private/test/patch_tool_test.js new file mode 100644 index 0000000000..a8210c1b86 --- /dev/null +++ b/npm/private/test/patch_tool_test.js @@ -0,0 +1,7 @@ +const assert = require('node:assert') + +const meaningOfLife = require('meaning-of-life') + +// `meaning-of-life@1.0.0.patch` changes 42 to "forty two" and the custom +// `patch_tool.sh` appends the " (custom patch_tool)" suffix. +assert.strictEqual(meaningOfLife, 'forty two (custom patch_tool)')