Skip to content
Myr

[Tests] `nvm_ls handles hash in pattern`: fix cleanup, isolate NVM_DIR, cover the sed path

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- `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.

authored by Jordan Harband · committed by Jordan Harband · parent 82315e2 · Browse files · 1 file+26 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+26 added−10 removed

  • tests:+26 added−10 removed

1 file+26 added−10 removed

modified:test/fast/Unit tests/nvm_ls handles hash in pattern+26 added-10 removed
1 unmodified line
23die () { echo "$@" ; cleanup ; exit 1; }45cleanup() {6 unset NVMRC_CONTENT OUTPUT EXIT_CODE TEST_DIR6 rm -rf "${TEST_DIR-}"7 unset NVMRC_CONTENT OUTPUT EXIT_CODE TEST_DIR NVM_DIR8}910: nvm.sh11\. ../../../nvm.sh1213\. ../../common.sh1415# an isolated NVM_DIR with a single known version, so that ambient versions16# and aliases can not affect any assertions17TEST_DIR="${PWD}/nvm_ls_hash_pattern_tmp"18mkdir -p "${TEST_DIR}/versions/node/v24.13.0/bin" || die 'failed to create test version dir'19make_echo "${TEST_DIR}/versions/node/v24.13.0/bin/node" 'v24.13.0' || die 'failed to create test node binary'20NVM_DIR="${TEST_DIR}"2122# Test: nvm_ls with pattern containing # should not cause sed error23# This is a regression test for https://github.com/nvm-sh/nvm/issues/37612425# The pattern with # should not cause sed "unterminated regular expression" error
10 unmodified lines
36# Should return N/A with exit code 3 (not found)37[ "$EXIT_CODE" = "3" ] || die "nvm_ls 'foo#bar' should exit with code 3, got $EXIT_CODE"38echo "$OUTPUT" | grep -q "N/A" || die "nvm_ls 'foo#bar' should output N/A, got: $OUTPUT"3931TEST_DIR="${PWD}/nvm_ls_hash_pattern_tmp"32mkdir -p "${TEST_DIR}/versions/node/v24.13.0/bin" || die 'failed to create test version dir'33printf '#!/bin/sh\nprintf v24.13.0\\\\n\n' > "${TEST_DIR}/versions/node/v24.13.0/bin/node" || die 'failed to create test node binary'34chmod +x "${TEST_DIR}/versions/node/v24.13.0/bin/node" || die 'failed to chmod test node binary'3536NVM_DIR="${TEST_DIR}"40NVMRC_CONTENT='v24.13.041# krypton is the codename for Node.js v24.x'4243OUTPUT="$(nvm_ls "${NVMRC_CONTENT}" 2>&1)"44EXIT_CODE=$?4543echo "$OUTPUT" | grep -q "unterminated regular expression" && \44 die "nvm_ls with .nvmrc comments caused sed 'unterminated regular expression' error: $OUTPUT"4546[ "$EXIT_CODE" = "0" ] || die "nvm_ls with .nvmrc comments should exit with code 0, got $EXIT_CODE"47[ "$OUTPUT" = "v24.13.0" ] || die "nvm_ls with .nvmrc comments should output v24.13.0, got: $OUTPUT"4849OUTPUT="$(nvm_version "${NVMRC_CONTENT}" 2>&1)"50[ "$OUTPUT" = "v24.13.0" ] || die "nvm_version with .nvmrc comments should output v24.13.0, got: $OUTPUT"5152# a partial version reaches nvm_ls's find/sed pipeline, which is where multiline53# patterns used to break sed ("unterminated regular expression" on BSD sed,54# "unterminated address regex" on GNU sed); a full x.y.z version takes the55# explicit-version fast path and never reaches it56NVMRC_CONTENT='2457# krypton is the codename for Node.js v24.x'5859OUTPUT="$(nvm_ls "${NVMRC_CONTENT}" 2>&1)"60EXIT_CODE=$?6162echo "$OUTPUT" | grep -q "unterminated" && \63 die "nvm_ls with partial version + comments caused a sed error: $OUTPUT"6465[ "$EXIT_CODE" = "0" ] || die "nvm_ls with partial version + comments should exit with code 0, got $EXIT_CODE"66[ "$OUTPUT" = "v24.13.0" ] || die "nvm_ls with partial version + comments should output v24.13.0, got: $OUTPUT"6768cleanup