Files
nvm/test/fast/Unit tests/nvm_process_nvmrc_content rejects duplicate keys
T
Jordan Harband 944232943b [Fix] nvm_process_nvmrc_content: a key containing a space is not a duplicate of each word in it
The duplicate-key check kept keys in a space-separated list,
so a key such as `a b` followed by a key `a` or `b` was reported as a duplicate,
unlike in the `nvmrc` package.
Keep the keys newline-separated, since a key cannot contain a newline.
2026-11-18 16:20:53 -08:00

44 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
die () { echo "$@" ; exit 1; }
: nvm.sh
\. ../../../nvm.sh
\. ../../common.sh
for KEY in a a.b 'a*' '(' '[' 'a\' 'a b'; do
CONTENT="20
${KEY}=1
other=2
${KEY} = 3"
try_err nvm_process_nvmrc_content "${CONTENT}"
[ "${CAPTURED_EXIT_CODE}" = 1 ] || die "expected a duplicate key \`${KEY}\` to be invalid; got exit code ${CAPTURED_EXIT_CODE}"
case "${CAPTURED_STDERR}" in
*'invalid .nvmrc!'*) ;;
*) die "expected the invalid .nvmrc message for a duplicate key \`${KEY}\`; got >${CAPTURED_STDERR}<" ;;
esac
try nvm_process_nvmrc_content "${CONTENT}"
[ -z "${CAPTURED_STDOUT}" ] || die "expected no version for a duplicate key \`${KEY}\`; got \`${CAPTURED_STDOUT}\`"
done
# keys are compared as they are written, not as patterns
for KEY in b ab A a. '*' '.*' '?' 'a|b' '[a]' 'a b' 'b a' 'b a'; do
for CONTENT in "20
a=1
${KEY}=2" "20
${KEY}=1
a=2"; do
try_err nvm_process_nvmrc_content "${CONTENT}"
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected the keys \`a\` and \`${KEY}\` to be distinct; got exit code ${CAPTURED_EXIT_CODE}"
case "${CAPTURED_STDERR}" in
*'invalid .nvmrc!'*) die "expected no error for the keys \`a\` and \`${KEY}\`; got >${CAPTURED_STDERR}<" ;;
esac
try nvm_process_nvmrc_content "${CONTENT}"
[ "${CAPTURED_STDOUT}" = '20' ] || die "expected \`20\` with the keys \`a\` and \`${KEY}\`; got \`${CAPTURED_STDOUT}\`"
done
done