From f317a618b82e96fa1704c7cce1ce19f93c61db09 Mon Sep 17 00:00:00 2001 From: eric Date: Sun, 15 Mar 2026 11:50:25 +0100 Subject: [PATCH] fix: context leak --- tests/BUILD.bazel | 2 ++ tests/bundle_test/BUILD.bazel | 2 ++ tests/bundle_test/verify_flag_aquery.sh | 26 +++++++++++-------- tests/bundle_test/verify_sourcemap_shape.sh | 26 +++++++++++-------- tests/install_test/BUILD.bazel | 3 +++ tests/install_test/lifecycle_scripts.sh | 24 +++++++++-------- tests/install_test/repeatability.sh | 24 +++++++++-------- tests/install_test/workspace_parity.sh | 24 +++++++++-------- tests/nested_bazel_test.sh | 24 +++++++++++++++++ tests/npm_compat_test/BUILD.bazel | 1 + .../npm_translate_lock_workspace_test.sh | 26 ++++++++++--------- 11 files changed, 115 insertions(+), 67 deletions(-) create mode 100644 tests/nested_bazel_test.sh diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index ffd0fb0..34ed902 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -1 +1,3 @@ package(default_visibility = ["//visibility:public"]) + +exports_files(["nested_bazel_test.sh"]) diff --git a/tests/bundle_test/BUILD.bazel b/tests/bundle_test/BUILD.bazel index 487bdc3..6e90390 100644 --- a/tests/bundle_test/BUILD.bazel +++ b/tests/bundle_test/BUILD.bazel @@ -202,6 +202,7 @@ sh_test( "//:repo_runtime_files", "//bun:repo_runtime_files", "//internal:repo_runtime_files", + "//tests:nested_bazel_test.sh", "//tests/bundle_test/sourcemap_case:BUILD.bazel", "//tests/bundle_test/sourcemap_case:entry.ts", ], @@ -246,6 +247,7 @@ sh_test( "//:repo_runtime_files", "//bun:repo_runtime_files", "//internal:repo_runtime_files", + "//tests:nested_bazel_test.sh", ], env_inherit = ["PATH"], ) diff --git a/tests/bundle_test/verify_flag_aquery.sh b/tests/bundle_test/verify_flag_aquery.sh index b45da5f..bf9658f 100755 --- a/tests/bundle_test/verify_flag_aquery.sh +++ b/tests/bundle_test/verify_flag_aquery.sh @@ -1,19 +1,15 @@ #!/usr/bin/env bash set -euo pipefail -if command -v bazel >/dev/null 2>&1; then - bazel_cmd=(bazel) -elif command -v bazelisk >/dev/null 2>&1; then - bazel_cmd=(bazelisk) -else - echo "bazel or bazelisk is required on PATH" >&2 - exit 1 -fi +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +# shellcheck source=../nested_bazel_test.sh +source "${script_dir}/../nested_bazel_test.sh" +setup_nested_bazel_cmd find_workspace_root() { local candidate local module_path - local script_dir + local search_dir for candidate in \ "${TEST_SRCDIR:-}/${TEST_WORKSPACE:-}" \ @@ -32,8 +28,8 @@ find_workspace_root() { fi fi - script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" - candidate="$(cd "${script_dir}/../.." && pwd -P)" + search_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" + candidate="$(cd "${search_dir}/../.." && pwd -P)" if [[ -f "${candidate}/MODULE.bazel" ]]; then printf '%s\n' "${candidate}" return 0 @@ -45,6 +41,14 @@ find_workspace_root() { rules_bun_root="$(find_workspace_root)" +cleanup() { + local status="$1" + trap - EXIT + shutdown_nested_bazel_workspace "${rules_bun_root}" + exit "${status}" +} +trap 'cleanup $?' EXIT + run_aquery() { local mnemonic="$1" local target="$2" diff --git a/tests/bundle_test/verify_sourcemap_shape.sh b/tests/bundle_test/verify_sourcemap_shape.sh index 0d9f8d8..60865ac 100755 --- a/tests/bundle_test/verify_sourcemap_shape.sh +++ b/tests/bundle_test/verify_sourcemap_shape.sh @@ -1,19 +1,15 @@ #!/usr/bin/env bash set -euo pipefail -if command -v bazel >/dev/null 2>&1; then - bazel_cmd=(bazel) -elif command -v bazelisk >/dev/null 2>&1; then - bazel_cmd=(bazelisk) -else - echo "bazel or bazelisk is required on PATH" >&2 - exit 1 -fi +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +# shellcheck source=../nested_bazel_test.sh +source "${script_dir}/../nested_bazel_test.sh" +setup_nested_bazel_cmd find_workspace_root() { local candidate local module_path - local script_dir + local search_dir for candidate in \ "${TEST_SRCDIR:-}/${TEST_WORKSPACE:-}" \ @@ -32,8 +28,8 @@ find_workspace_root() { fi fi - script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" - candidate="$(cd "${script_dir}/../.." && pwd -P)" + search_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" + candidate="$(cd "${search_dir}/../.." && pwd -P)" if [[ -f "${candidate}/MODULE.bazel" ]]; then printf '%s\n' "${candidate}" return 0 @@ -45,6 +41,14 @@ find_workspace_root() { rules_bun_root="$(find_workspace_root)" +cleanup() { + local status="$1" + trap - EXIT + shutdown_nested_bazel_workspace "${rules_bun_root}" + exit "${status}" +} +trap 'cleanup $?' EXIT + bundle_output="$( cd "${rules_bun_root}" && "${bazel_cmd[@]}" aquery 'mnemonic("BunBundle", //tests/bundle_test/sourcemap_case:sourcemap_bundle)' --output=textproto diff --git a/tests/install_test/BUILD.bazel b/tests/install_test/BUILD.bazel index fbc82fc..1b62639 100644 --- a/tests/install_test/BUILD.bazel +++ b/tests/install_test/BUILD.bazel @@ -166,6 +166,7 @@ sh_test( "//:repo_runtime_files", "//bun:repo_runtime_files", "//internal:repo_runtime_files", + "//tests:nested_bazel_test.sh", ], env_inherit = ["PATH"], ) @@ -201,6 +202,7 @@ sh_test( "//:repo_runtime_files", "//bun:repo_runtime_files", "//internal:repo_runtime_files", + "//tests:nested_bazel_test.sh", ], env_inherit = ["PATH"], ) @@ -228,6 +230,7 @@ sh_test( "//:repo_runtime_files", "//bun:repo_runtime_files", "//internal:repo_runtime_files", + "//tests:nested_bazel_test.sh", ], env_inherit = ["PATH"], ) diff --git a/tests/install_test/lifecycle_scripts.sh b/tests/install_test/lifecycle_scripts.sh index c11c52d..bd85d90 100755 --- a/tests/install_test/lifecycle_scripts.sh +++ b/tests/install_test/lifecycle_scripts.sh @@ -1,22 +1,24 @@ #!/usr/bin/env bash set -euo pipefail +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +# shellcheck source=../nested_bazel_test.sh +source "${script_dir}/../nested_bazel_test.sh" +setup_nested_bazel_cmd + bun_path="$1" -if command -v bazel >/dev/null 2>&1; then - bazel_cmd=(bazel) -elif command -v bazelisk >/dev/null 2>&1; then - bazel_cmd=(bazelisk) -else - echo "bazel or bazelisk is required on PATH" >&2 - exit 1 -fi - -script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" rules_bun_root="$(cd "${script_dir}/../.." && pwd -P)" workdir="$(mktemp -d)" -trap 'rm -rf "${workdir}"' EXIT +cleanup() { + local status="$1" + trap - EXIT + shutdown_nested_bazel_workspace "${fixture_dir:-}" + rm -rf "${workdir}" + exit "${status}" +} +trap 'cleanup $?' EXIT fixture_dir="${workdir}/fixture" mkdir -p "${fixture_dir}" diff --git a/tests/install_test/repeatability.sh b/tests/install_test/repeatability.sh index f16fb4c..64865f5 100755 --- a/tests/install_test/repeatability.sh +++ b/tests/install_test/repeatability.sh @@ -1,22 +1,24 @@ #!/usr/bin/env bash set -euo pipefail +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +# shellcheck source=../nested_bazel_test.sh +source "${script_dir}/../nested_bazel_test.sh" +setup_nested_bazel_cmd + bun_path="$1" -if command -v bazel >/dev/null 2>&1; then - bazel_cmd=(bazel) -elif command -v bazelisk >/dev/null 2>&1; then - bazel_cmd=(bazelisk) -else - echo "bazel or bazelisk is required on PATH" >&2 - exit 1 -fi - -script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" rules_bun_root="$(cd "${script_dir}/../.." && pwd -P)" workdir="$(mktemp -d)" -trap 'rm -rf "${workdir}"' EXIT +cleanup() { + local status="$1" + trap - EXIT + shutdown_nested_bazel_workspace "${fixture_dir:-}" + rm -rf "${workdir}" + exit "${status}" +} +trap 'cleanup $?' EXIT fixture_dir="${workdir}/fixture" mkdir -p "${fixture_dir}" diff --git a/tests/install_test/workspace_parity.sh b/tests/install_test/workspace_parity.sh index 108fef4..99f6f60 100755 --- a/tests/install_test/workspace_parity.sh +++ b/tests/install_test/workspace_parity.sh @@ -1,22 +1,24 @@ #!/usr/bin/env bash set -euo pipefail +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +# shellcheck source=../nested_bazel_test.sh +source "${script_dir}/../nested_bazel_test.sh" +setup_nested_bazel_cmd + bun_path="${1:-bun}" -if command -v bazel >/dev/null 2>&1; then - bazel_cmd=(bazel) -elif command -v bazelisk >/dev/null 2>&1; then - bazel_cmd=(bazelisk) -else - echo "bazel or bazelisk is required on PATH" >&2 - exit 1 -fi - -script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" rules_bun_root="$(cd "${script_dir}/../.." && pwd -P)" workdir="$(mktemp -d)" -trap 'rm -rf "${workdir}"' EXIT +cleanup() { + local status="$1" + trap - EXIT + shutdown_nested_bazel_workspace "${bazel_dir:-}" + rm -rf "${workdir}" + exit "${status}" +} +trap 'cleanup $?' EXIT fixture_dir="${workdir}/fixture" plain_dir="${workdir}/plain" diff --git a/tests/nested_bazel_test.sh b/tests/nested_bazel_test.sh new file mode 100644 index 0000000..a9af815 --- /dev/null +++ b/tests/nested_bazel_test.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +setup_nested_bazel_cmd() { + if command -v bazel >/dev/null 2>&1; then + bazel_cmd=(bazel) + elif command -v bazelisk >/dev/null 2>&1; then + bazel_cmd=(bazelisk) + else + echo "bazel or bazelisk is required on PATH" >&2 + exit 1 + fi +} + +shutdown_nested_bazel_workspace() { + local workspace_dir="${1:-}" + if [[ -z ${workspace_dir} || ! -d ${workspace_dir} ]]; then + return 0 + fi + + ( + cd "${workspace_dir}" + "${bazel_cmd[@]}" shutdown >/dev/null 2>&1 + ) || true +} diff --git a/tests/npm_compat_test/BUILD.bazel b/tests/npm_compat_test/BUILD.bazel index 1e07a70..ddc7154 100644 --- a/tests/npm_compat_test/BUILD.bazel +++ b/tests/npm_compat_test/BUILD.bazel @@ -65,6 +65,7 @@ sh_test( "//internal:repo_runtime_files", "//js:repo_runtime_files", "//npm:repo_runtime_files", + "//tests:nested_bazel_test.sh", ], env_inherit = ["PATH"], ) diff --git a/tests/npm_compat_test/npm_translate_lock_workspace_test.sh b/tests/npm_compat_test/npm_translate_lock_workspace_test.sh index 41ad8ca..fb1ccff 100755 --- a/tests/npm_compat_test/npm_translate_lock_workspace_test.sh +++ b/tests/npm_compat_test/npm_translate_lock_workspace_test.sh @@ -1,22 +1,24 @@ #!/usr/bin/env bash set -euo pipefail -if command -v bazel >/dev/null 2>&1; then - bazel_bin="$(command -v bazel)" -elif command -v bazelisk >/dev/null 2>&1; then - bazel_bin="$(command -v bazelisk)" -else - echo "bazel or bazelisk is required on PATH" >&2 - exit 1 -fi - bun_path="${1:-bun}" script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +# shellcheck source=../nested_bazel_test.sh +source "${script_dir}/../nested_bazel_test.sh" +setup_nested_bazel_cmd + rules_bun_root="$(cd "${script_dir}/../.." && pwd -P)" workdir="$(mktemp -d)" -trap 'rm -rf "${workdir}"' EXIT +cleanup() { + local status="$1" + trap - EXIT + shutdown_nested_bazel_workspace "${fixture_dir:-}" + rm -rf "${workdir}" + exit "${status}" +} +trap 'cleanup $?' EXIT fixture_dir="${workdir}/fixture" mkdir -p "${fixture_dir}" @@ -100,7 +102,7 @@ EOF output="$( cd "${fixture_dir}" && - "${bazel_bin}" run //:app + "${bazel_cmd[@]}" run //:app )" if [[ ${output} != *"compat:true"* ]]; then @@ -110,7 +112,7 @@ fi query_output="$( cd "${fixture_dir}" && - "${bazel_bin}" query //:npm__is_number + "${bazel_cmd[@]}" query //:npm__is_number )" if ! grep -Fxq "//:npm__is_number" <<<"${query_output}"; then echo "expected npm_link_all_packages to create //:npm__is_number" >&2