Skip to content
Myr

[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

8 files+138 added−3 removed

added:test/fast/Aliases/nvm_alias handles alias with spaces in name+17 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# Create an alias file whose name contains spaces11mkdir -p ../../../alias12printf 'v22.1.0\n' > "../../../alias/test edge spaces"13ACTUAL="$(nvm_alias "test edge spaces")"14EXPECTED='v22.1.0'15[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"1617rm -f "../../../alias/test edge spaces"
added:test/fast/Aliases/nvm_alias handles binary data after version+20 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# Create an alias file where the first line has a valid version11# followed by a second line of binary-like data12printf 'v22.1.0\n' > ../../../alias/test-edge-binary13printf '\001\002\003\377\n' >> ../../../alias/test-edge-binary14ACTUAL="$(nvm_alias test-edge-binary)"15# nvm_alias emits every non-blank, non-comment line — first line should be the version16FIRST_LINE="$(nvm_echo "${ACTUAL}" | command head -n 1)"17EXPECTED='v22.1.0'18[ "${FIRST_LINE}" = "${EXPECTED}" ] || die "expected first line >${EXPECTED}<, got >${FIRST_LINE}<"1920rm -f ../../../alias/test-edge-binary
added:test/fast/Aliases/nvm_alias handles carriage return+22 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# Create an alias file with Windows-style line endings (CRLF)11printf 'v22.1.0\r\n' > ../../../alias/test-edge-cr12ACTUAL="$(nvm_alias test-edge-cr)"13EXPECTED='v22.1.0'14[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"1516# Create an alias file with bare carriage return (no newline)17printf 'v22.2.0\r' > ../../../alias/test-edge-cr-bare18ACTUAL="$(nvm_alias test-edge-cr-bare)"19EXPECTED='v22.2.0'20[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for bare CR, got >${ACTUAL}<"2122rm -f ../../../alias/test-edge-cr ../../../alias/test-edge-cr-bare
added:test/fast/Aliases/nvm_alias handles embedded NUL+22 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# 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.13printf 'v22.1.0\000garbage\n' > ../../../alias/test-edge-nul14ACTUAL="$(nvm_alias test-edge-nul)"15EXIT_CODE=$?16[ "${EXIT_CODE}" = '0' ] || die "expected exit code 0, got ${EXIT_CODE}"17case "${ACTUAL}" in18 v22.1.0*) ;; # OK — starts with the version regardless of NUL handling19 *) die "expected output starting with >v22.1.0<, got >${ACTUAL}<" ;;20esac2122rm -f ../../../alias/test-edge-nul
added:test/fast/Aliases/nvm_alias handles form feed and vertical tab+22 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# Create an alias file with trailing form feed11printf 'v22.1.0\f\n' > ../../../alias/test-edge-ff12ACTUAL="$(nvm_alias test-edge-ff)"13EXPECTED='v22.1.0'14[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for form feed, got >${ACTUAL}<"1516# Create an alias file with trailing vertical tab17printf 'v22.2.0\v\n' > ../../../alias/test-edge-vt18ACTUAL="$(nvm_alias test-edge-vt)"19EXPECTED='v22.2.0'20[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}< for vertical tab, got >${ACTUAL}<"2122rm -f ../../../alias/test-edge-ff ../../../alias/test-edge-vt
added:test/fast/Aliases/nvm_alias handles no trailing newline+16 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# Create an alias file with no trailing newline11printf 'v22.1.0' > ../../../alias/test-edge-no-newline12ACTUAL="$(nvm_alias test-edge-no-newline)"13EXPECTED='v22.1.0'14[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"1516rm -f ../../../alias/test-edge-no-newline
added:test/fast/Aliases/nvm_alias handles very long line+17 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../../.. && pwd)"67: nvm.sh8\. "${NVM_DIR}/nvm.sh"910# Create an alias file with a very long line (version followed by a long comment)11LONG_COMMENT="$(printf '%0*d' 10000 0 | command tr '0' 'x')"12printf 'v22.1.0 #%s\n' "${LONG_COMMENT}" > ../../../alias/test-edge-long13ACTUAL="$(nvm_alias test-edge-long)"14EXPECTED='v22.1.0'15[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"1617rm -f ../../../alias/test-edge-long
modified:nvm.sh+2 added-3 removed
1372 unmodified lines
1373 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