Files
nvm/test/fast/Unit tests/nvm_ls handles hash in pattern
T
2026-06-09 00:21:55 +09:00

53 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
die () { echo "$@" ; cleanup ; exit 1; }
cleanup() {
unset NVMRC_CONTENT OUTPUT EXIT_CODE TEST_DIR
rm -rf "${TEST_DIR-}"
}
: nvm.sh
\. ../../../nvm.sh
# 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
# The pattern with # should not cause sed "unterminated regular expression" error
# It's OK if it returns N/A (no match), but it should not produce sed errors
OUTPUT="$(nvm_ls 'foo#bar' 2>&1)"
EXIT_CODE=$?
# Check that output doesn't contain sed error message
echo "$OUTPUT" | grep -q "unterminated regular expression" && \
die "nvm_ls with # in pattern caused sed 'unterminated regular expression' error: $OUTPUT"
echo "$OUTPUT" | grep -q "invalid command code" && \
die "nvm_ls with # in pattern caused sed 'invalid command code' error: $OUTPUT"
# Should return N/A with exit code 3 (not found)
[ "$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"
cleanup