From 21d33ef04a748563e546925e0df021f283fa6469 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 8 Jul 2026 23:14:23 -0700 Subject: [PATCH] [Tests] `nvm_ls handles hash in pattern`: fix cleanup, isolate NVM_DIR, cover the sed path - `cleanup` unset TEST_DIR before `rm -rf "${TEST_DIR-}"`, so the temp dir (with an executable fake `node`) was never removed, and urchin executes any executable file it finds under the test dir on the next local run. - the preexisting `foo#bar` assertions ran against the ambient NVM_DIR; now that `#` patterns comment-strip to `foo`, a real local alias named `foo` would resolve and break them, so all assertions now run against an isolated NVM_DIR. - the multiline content used a full x.y.z version, which takes nvm_ls's explicit-version fast path and never reaches the find/sed pipeline where the newline actually broke sed; a partial version exercises that path, and matching on "unterminated" covers both the BSD and GNU sed error wordings. --- .../Unit tests/nvm_ls handles hash in pattern | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/test/fast/Unit tests/nvm_ls handles hash in pattern b/test/fast/Unit tests/nvm_ls handles hash in pattern index afa2ff7e..1342f016 100755 --- a/test/fast/Unit tests/nvm_ls handles hash in pattern +++ b/test/fast/Unit tests/nvm_ls handles hash in pattern @@ -3,13 +3,22 @@ die () { echo "$@" ; cleanup ; exit 1; } cleanup() { - unset NVMRC_CONTENT OUTPUT EXIT_CODE TEST_DIR rm -rf "${TEST_DIR-}" + unset NVMRC_CONTENT OUTPUT EXIT_CODE TEST_DIR NVM_DIR } : nvm.sh \. ../../../nvm.sh +\. ../../common.sh + +# an isolated NVM_DIR with a single known version, so that ambient versions +# and aliases can not affect any assertions +TEST_DIR="${PWD}/nvm_ls_hash_pattern_tmp" +mkdir -p "${TEST_DIR}/versions/node/v24.13.0/bin" || die 'failed to create test version dir' +make_echo "${TEST_DIR}/versions/node/v24.13.0/bin/node" 'v24.13.0' || die 'failed to create test node binary' +NVM_DIR="${TEST_DIR}" + # Test: nvm_ls with pattern containing # should not cause sed error # This is a regression test for https://github.com/nvm-sh/nvm/issues/3761 @@ -28,25 +37,32 @@ echo "$OUTPUT" | grep -q "invalid command code" && \ [ "$EXIT_CODE" = "3" ] || die "nvm_ls 'foo#bar' should exit with code 3, got $EXIT_CODE" echo "$OUTPUT" | grep -q "N/A" || die "nvm_ls 'foo#bar' should output N/A, got: $OUTPUT" -TEST_DIR="${PWD}/nvm_ls_hash_pattern_tmp" -mkdir -p "${TEST_DIR}/versions/node/v24.13.0/bin" || die 'failed to create test version dir' -printf '#!/bin/sh\nprintf v24.13.0\\\\n\n' > "${TEST_DIR}/versions/node/v24.13.0/bin/node" || die 'failed to create test node binary' -chmod +x "${TEST_DIR}/versions/node/v24.13.0/bin/node" || die 'failed to chmod test node binary' - -NVM_DIR="${TEST_DIR}" NVMRC_CONTENT='v24.13.0 # krypton is the codename for Node.js v24.x' OUTPUT="$(nvm_ls "${NVMRC_CONTENT}" 2>&1)" EXIT_CODE=$? -echo "$OUTPUT" | grep -q "unterminated regular expression" && \ - die "nvm_ls with .nvmrc comments caused sed 'unterminated regular expression' error: $OUTPUT" - [ "$EXIT_CODE" = "0" ] || die "nvm_ls with .nvmrc comments should exit with code 0, got $EXIT_CODE" [ "$OUTPUT" = "v24.13.0" ] || die "nvm_ls with .nvmrc comments should output v24.13.0, got: $OUTPUT" OUTPUT="$(nvm_version "${NVMRC_CONTENT}" 2>&1)" [ "$OUTPUT" = "v24.13.0" ] || die "nvm_version with .nvmrc comments should output v24.13.0, got: $OUTPUT" +# a partial version reaches nvm_ls's find/sed pipeline, which is where multiline +# patterns used to break sed ("unterminated regular expression" on BSD sed, +# "unterminated address regex" on GNU sed); a full x.y.z version takes the +# explicit-version fast path and never reaches it +NVMRC_CONTENT='24 +# krypton is the codename for Node.js v24.x' + +OUTPUT="$(nvm_ls "${NVMRC_CONTENT}" 2>&1)" +EXIT_CODE=$? + +echo "$OUTPUT" | grep -q "unterminated" && \ + die "nvm_ls with partial version + comments caused a sed error: $OUTPUT" + +[ "$EXIT_CODE" = "0" ] || die "nvm_ls with partial version + comments should exit with code 0, got $EXIT_CODE" +[ "$OUTPUT" = "v24.13.0" ] || die "nvm_ls with partial version + comments should output v24.13.0, got: $OUTPUT" + cleanup