[Refactor] `nvm_alias`: use [[:space:]] instead of literal tab
Before adoption: this commit came in with the history the project adopted, so no landing records it.
[Tests] `nvm_alias`: add edge-case tests for hostile file content
authored by Jake Lodwick · committed by Jordan Harband · parent d22bf7f · Browse files · 8 files·+138 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+138 added−3 removed
- tests:+136 added−0 removed
- other:+2 added−3 removed
Provenance
- Commit
304a22a- Tree
6eb6148- Author
Jake Lodwick <25925+jakelodwick@users.noreply.github.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
d22bf7f
8 files changed+138 added−3 removed
added:test/fast/Aliases/nvm_alias handles alias with spaces in name+17 added
1+#!/bin/sh2+3+die () { echo "$@" ; exit 1; }4+5+export NVM_DIR="$(cd ../../.. && pwd)"6+7+: nvm.sh8+\. "${NVM_DIR}/nvm.sh"9+10+# Create an alias file whose name contains spaces11+mkdir -p ../../../alias12+printf 'v22.1.0\n' > "../../../alias/test edge spaces"13+ACTUAL="$(nvm_alias "test edge spaces")"14+EXPECTED='v22.1.0'15+[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"16+17+rm -f "../../../alias/test edge spaces"added:test/fast/Aliases/nvm_alias handles binary data after version+20 added
1+#!/bin/sh2+3+die () { echo "$@" ; exit 1; }4+5+export NVM_DIR="$(cd ../../.. && pwd)"6+7+: nvm.sh8+\. "${NVM_DIR}/nvm.sh"9+10+# Create an alias file where the first line has a valid version11+# followed by a second line of binary-like data12+printf 'v22.1.0\n' > ../../../alias/test-edge-binary13+printf '\001\002\003\377\n' >> ../../../alias/test-edge-binary14+ACTUAL="$(nvm_alias test-edge-binary)"15+# nvm_alias emits every non-blank, non-comment line — first line should be the version16+FIRST_LINE="$(nvm_echo "${ACTUAL}" | command head -n 1)"17+EXPECTED='v22.1.0'18+[ "${FIRST_LINE}" = "${EXPECTED}" ] || die "expected first line >${EXPECTED}<, got >${FIRST_LINE}<"19+20+rm -f ../../../alias/test-edge-binaryadded:test/fast/Aliases/nvm_alias handles carriage return+22 added
1+#!/bin/sh2+3+die () { echo "$@" ; exit 1; }4+5+export NVM_DIR="$(cd ../../.. && pwd)"6+7+: nvm.sh8+\. "${NVM_DIR}/nvm.sh"9+10+# Create an alias file with Windows-style line endings (CRLF)11+printf 'v22.1.0\r\n' > ../../../alias/test-edge-cr12+ACTUAL="$(nvm_alias test-edge-cr)"13+EXPECTED='v22.1.0'14+[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"15+16+# Create an alias file with bare carriage return (no newline)17+printf 'v22.2.0\r' > ../../../alias/test-edge-cr-bare18+ACTUAL="$(nvm_alias test-edge-cr-bare)"19+EXPECTED='v22.2.0'20+[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for bare CR, got >${ACTUAL}<"21+22+rm -f ../../../alias/test-edge-cr ../../../alias/test-edge-cr-bareadded:test/fast/Aliases/nvm_alias handles embedded NUL+22 added
1+#!/bin/sh2+3+die () { echo "$@" ; exit 1; }4+5+export NVM_DIR="$(cd ../../.. && pwd)"6+7+: nvm.sh8+\. "${NVM_DIR}/nvm.sh"9+10+# Create an alias file with an embedded NUL byte after the version.11+# NUL handling varies by shell: some stop at NUL, others read through it.12+# The function must not crash, and must emit output starting with the version.13+printf 'v22.1.0\000garbage\n' > ../../../alias/test-edge-nul14+ACTUAL="$(nvm_alias test-edge-nul)"15+EXIT_CODE=$?16+[ "${EXIT_CODE}" = '0' ] || die "expected exit code 0, got ${EXIT_CODE}"17+case "${ACTUAL}" in18+ v22.1.0*) ;; # OK — starts with the version regardless of NUL handling19+ *) die "expected output starting with >v22.1.0<, got >${ACTUAL}<" ;;20+esac21+22+rm -f ../../../alias/test-edge-nuladded:test/fast/Aliases/nvm_alias handles form feed and vertical tab+22 added
1+#!/bin/sh2+3+die () { echo "$@" ; exit 1; }4+5+export NVM_DIR="$(cd ../../.. && pwd)"6+7+: nvm.sh8+\. "${NVM_DIR}/nvm.sh"9+10+# Create an alias file with trailing form feed11+printf 'v22.1.0\f\n' > ../../../alias/test-edge-ff12+ACTUAL="$(nvm_alias test-edge-ff)"13+EXPECTED='v22.1.0'14+[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for form feed, got >${ACTUAL}<"15+16+# Create an alias file with trailing vertical tab17+printf 'v22.2.0\v\n' > ../../../alias/test-edge-vt18+ACTUAL="$(nvm_alias test-edge-vt)"19+EXPECTED='v22.2.0'20+[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for vertical tab, got >${ACTUAL}<"21+22+rm -f ../../../alias/test-edge-ff ../../../alias/test-edge-vtadded:test/fast/Aliases/nvm_alias handles no trailing newline+16 added
1+#!/bin/sh2+3+die () { echo "$@" ; exit 1; }4+5+export NVM_DIR="$(cd ../../.. && pwd)"6+7+: nvm.sh8+\. "${NVM_DIR}/nvm.sh"9+10+# Create an alias file with no trailing newline11+printf 'v22.1.0' > ../../../alias/test-edge-no-newline12+ACTUAL="$(nvm_alias test-edge-no-newline)"13+EXPECTED='v22.1.0'14+[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"15+16+rm -f ../../../alias/test-edge-no-newlineadded:test/fast/Aliases/nvm_alias handles very long line+17 added
1+#!/bin/sh2+3+die () { echo "$@" ; exit 1; }4+5+export NVM_DIR="$(cd ../../.. && pwd)"6+7+: nvm.sh8+\. "${NVM_DIR}/nvm.sh"9+10+# Create an alias file with a very long line (version followed by a long comment)11+LONG_COMMENT="$(printf '%0*d' 10000 0 | command tr '0' 'x')"12+printf 'v22.1.0 #%s\n' "${LONG_COMMENT}" > ../../../alias/test-edge-long13+ACTUAL="$(nvm_alias test-edge-long)"14+EXPECTED='v22.1.0'15+[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"16+17+rm -f ../../../alias/test-edge-longmodified:nvm.sh+2 added-3 removed
1372 unmodified lines1373 local NVM_ALIAS_LINE1374 while IFS= read -r NVM_ALIAS_LINE || [ -n "${NVM_ALIAS_LINE}" ]; do1375 NVM_ALIAS_LINE="${NVM_ALIAS_LINE%%#*}"1376 case "${NVM_ALIAS_LINE}" in1377- *[!\ \ ]*) ;;1377+ *[![:space:]]*) ;;1378 *) continue ;;1379 esac1380 while : ; do1381 case "${NVM_ALIAS_LINE}" in1382- *' ') NVM_ALIAS_LINE="${NVM_ALIAS_LINE% }" ;;1383- *' ') NVM_ALIAS_LINE="${NVM_ALIAS_LINE% }" ;;1382+ *[[:space:]]) NVM_ALIAS_LINE="${NVM_ALIAS_LINE%[[:space:]]}" ;;1383 *) break ;;1384 esac1385 done1386 nvm_echo "${NVM_ALIAS_LINE}"3557 unmodified lines