[Refactor] nvm_alias: use [[:space:]] instead of literal tab

[Tests] `nvm_alias`: add edge-case tests for hostile file content
This commit is contained in:
Jake Lodwick
2026-02-18 14:29:52 -07:00
committed by Jordan Harband
parent d22bf7f538
commit 304a22ad59
8 changed files with 138 additions and 3 deletions
+2 -3
View File
@@ -1374,13 +1374,12 @@ nvm_alias() {
while IFS= read -r NVM_ALIAS_LINE || [ -n "${NVM_ALIAS_LINE}" ]; do while IFS= read -r NVM_ALIAS_LINE || [ -n "${NVM_ALIAS_LINE}" ]; do
NVM_ALIAS_LINE="${NVM_ALIAS_LINE%%#*}" NVM_ALIAS_LINE="${NVM_ALIAS_LINE%%#*}"
case "${NVM_ALIAS_LINE}" in case "${NVM_ALIAS_LINE}" in
*[!\ \ ]*) ;; *[![:space:]]*) ;;
*) continue ;; *) continue ;;
esac esac
while : ; do while : ; do
case "${NVM_ALIAS_LINE}" in case "${NVM_ALIAS_LINE}" in
*' ') NVM_ALIAS_LINE="${NVM_ALIAS_LINE% }" ;; *[[:space:]]) NVM_ALIAS_LINE="${NVM_ALIAS_LINE%[[:space:]]}" ;;
*' ') NVM_ALIAS_LINE="${NVM_ALIAS_LINE% }" ;;
*) break ;; *) break ;;
esac esac
done done
@@ -0,0 +1,17 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
export NVM_DIR="$(cd ../../.. && pwd)"
: nvm.sh
\. "${NVM_DIR}/nvm.sh"
# Create an alias file whose name contains spaces
mkdir -p ../../../alias
printf 'v22.1.0\n' > "../../../alias/test edge spaces"
ACTUAL="$(nvm_alias "test edge spaces")"
EXPECTED='v22.1.0'
[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"
rm -f "../../../alias/test edge spaces"
@@ -0,0 +1,20 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
export NVM_DIR="$(cd ../../.. && pwd)"
: nvm.sh
\. "${NVM_DIR}/nvm.sh"
# Create an alias file where the first line has a valid version
# followed by a second line of binary-like data
printf 'v22.1.0\n' > ../../../alias/test-edge-binary
printf '\001\002\003\377\n' >> ../../../alias/test-edge-binary
ACTUAL="$(nvm_alias test-edge-binary)"
# nvm_alias emits every non-blank, non-comment line — first line should be the version
FIRST_LINE="$(nvm_echo "${ACTUAL}" | command head -n 1)"
EXPECTED='v22.1.0'
[ "${FIRST_LINE}" = "${EXPECTED}" ] || die "expected first line >${EXPECTED}<, got >${FIRST_LINE}<"
rm -f ../../../alias/test-edge-binary
+22
View File
@@ -0,0 +1,22 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
export NVM_DIR="$(cd ../../.. && pwd)"
: nvm.sh
\. "${NVM_DIR}/nvm.sh"
# Create an alias file with Windows-style line endings (CRLF)
printf 'v22.1.0\r\n' > ../../../alias/test-edge-cr
ACTUAL="$(nvm_alias test-edge-cr)"
EXPECTED='v22.1.0'
[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"
# Create an alias file with bare carriage return (no newline)
printf 'v22.2.0\r' > ../../../alias/test-edge-cr-bare
ACTUAL="$(nvm_alias test-edge-cr-bare)"
EXPECTED='v22.2.0'
[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for bare CR, got >${ACTUAL}<"
rm -f ../../../alias/test-edge-cr ../../../alias/test-edge-cr-bare
+22
View File
@@ -0,0 +1,22 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
export NVM_DIR="$(cd ../../.. && pwd)"
: nvm.sh
\. "${NVM_DIR}/nvm.sh"
# Create an alias file with an embedded NUL byte after the version.
# NUL handling varies by shell: some stop at NUL, others read through it.
# The function must not crash, and must emit output starting with the version.
printf 'v22.1.0\000garbage\n' > ../../../alias/test-edge-nul
ACTUAL="$(nvm_alias test-edge-nul)"
EXIT_CODE=$?
[ "${EXIT_CODE}" = '0' ] || die "expected exit code 0, got ${EXIT_CODE}"
case "${ACTUAL}" in
v22.1.0*) ;; # OK — starts with the version regardless of NUL handling
*) die "expected output starting with >v22.1.0<, got >${ACTUAL}<" ;;
esac
rm -f ../../../alias/test-edge-nul
@@ -0,0 +1,22 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
export NVM_DIR="$(cd ../../.. && pwd)"
: nvm.sh
\. "${NVM_DIR}/nvm.sh"
# Create an alias file with trailing form feed
printf 'v22.1.0\f\n' > ../../../alias/test-edge-ff
ACTUAL="$(nvm_alias test-edge-ff)"
EXPECTED='v22.1.0'
[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for form feed, got >${ACTUAL}<"
# Create an alias file with trailing vertical tab
printf 'v22.2.0\v\n' > ../../../alias/test-edge-vt
ACTUAL="$(nvm_alias test-edge-vt)"
EXPECTED='v22.2.0'
[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for vertical tab, got >${ACTUAL}<"
rm -f ../../../alias/test-edge-ff ../../../alias/test-edge-vt
+16
View File
@@ -0,0 +1,16 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
export NVM_DIR="$(cd ../../.. && pwd)"
: nvm.sh
\. "${NVM_DIR}/nvm.sh"
# Create an alias file with no trailing newline
printf 'v22.1.0' > ../../../alias/test-edge-no-newline
ACTUAL="$(nvm_alias test-edge-no-newline)"
EXPECTED='v22.1.0'
[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"
rm -f ../../../alias/test-edge-no-newline
+17
View File
@@ -0,0 +1,17 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
export NVM_DIR="$(cd ../../.. && pwd)"
: nvm.sh
\. "${NVM_DIR}/nvm.sh"
# Create an alias file with a very long line (version followed by a long comment)
LONG_COMMENT="$(printf '%0*d' 10000 0 | command tr '0' 'x')"
printf 'v22.1.0 #%s\n' "${LONG_COMMENT}" > ../../../alias/test-edge-long
ACTUAL="$(nvm_alias test-edge-long)"
EXPECTED='v22.1.0'
[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"
rm -f ../../../alias/test-edge-long