mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-12-06 22:52:23 +03:00
[Tests] nvm_is_version_or_alias: add unit tests
This commit is contained in:
1 file changed
+92
Executable
+92
@@ -0,0 +1,92 @@
|
||||
#!/bin/sh
|
||||
|
||||
cleanup () {
|
||||
case "${TEST_DIR-}" in
|
||||
/*) rm -rf "${TEST_DIR}" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
TEST_DIR="$(mktemp -d)" || die 'unable to create temp dir'
|
||||
NVM_DIR="${TEST_DIR}"
|
||||
mkdir -p "${NVM_DIR}/alias" || die 'unable to create alias dir'
|
||||
echo 'v1.2.3' > "${NVM_DIR}/alias/myalias" || die 'unable to write myalias'
|
||||
echo 'v1.2.3' > "${NVM_DIR}/alias/my alias" || die 'unable to write "my alias"'
|
||||
echo 'v1.2.3' > "${NVM_DIR}/alias/vfoo" || die 'unable to write vfoo'
|
||||
|
||||
expect () {
|
||||
EXPECTED_CODE="$1"
|
||||
shift
|
||||
ACTUAL_OUTPUT="$(nvm_is_version_or_alias "$@" 2>&1)" && ACTUAL_CODE=0 || ACTUAL_CODE=$?
|
||||
[ "_${ACTUAL_CODE}" = "_${EXPECTED_CODE}" ] \
|
||||
|| die "nvm_is_version_or_alias '$*': got exit code >${ACTUAL_CODE}<, expected >${EXPECTED_CODE}<"
|
||||
[ "_${ACTUAL_OUTPUT}" = "_" ] \
|
||||
|| die "nvm_is_version_or_alias '$*': got output >${ACTUAL_OUTPUT}<, expected >< (nothing)"
|
||||
}
|
||||
|
||||
# special names
|
||||
expect 0 system
|
||||
expect 2 systemx
|
||||
expect 0 'lts/*'
|
||||
|
||||
# lts/-N: N must be one or more digits, and no lts alias needs to exist
|
||||
expect 0 lts/-1
|
||||
expect 0 lts/-12
|
||||
expect 0 lts/-10
|
||||
expect 0 lts/-100
|
||||
expect 1 lts/-
|
||||
expect 1 lts/-a
|
||||
expect 1 lts/-1a
|
||||
expect 1 lts/-1-2
|
||||
expect 1 lts/--1
|
||||
expect 1 'lts/-1 '
|
||||
|
||||
# these files exist, so only the name checks can reject them
|
||||
mkdir -p "${NVM_DIR}/alias/lts" || die 'unable to create alias/lts dir'
|
||||
echo 'v1.2.3' > "${NVM_DIR}/alias/lts/Argon" || die 'unable to write lts/Argon'
|
||||
echo 'v1.2.3' > "${NVM_DIR}/alias/lts/ir0n" || die 'unable to write lts/ir0n'
|
||||
|
||||
# lts/name: name must be lowercase letters only, and need not exist
|
||||
expect 0 lts/iron
|
||||
expect 0 lts/hydrogen
|
||||
expect 0 lts/a
|
||||
expect 1 lts/
|
||||
expect 1 lts/Argon
|
||||
expect 1 lts/ir0n
|
||||
expect 1 lts/iron-x
|
||||
expect 1 lts/iron/x
|
||||
expect 1 'lts/ir on'
|
||||
|
||||
# versions
|
||||
expect 0 v1.2.3
|
||||
expect 0 1.2.3
|
||||
expect 0 v1.2
|
||||
expect 0 node
|
||||
expect 0 iojs
|
||||
expect 0 stable
|
||||
expect 0 unstable
|
||||
expect 0 iojs-v1.0.0
|
||||
|
||||
# aliases: the status of `nvm_alias`, whose output is discarded
|
||||
expect 0 myalias
|
||||
expect 0 'my alias'
|
||||
expect 0 vfoo
|
||||
expect 2 nope
|
||||
expect 3 ../alias/myalias
|
||||
expect 2 'foo$(echo bar)'
|
||||
|
||||
# missing or empty argument
|
||||
expect 1
|
||||
expect 1 ''
|
||||
|
||||
ACTUAL_OUTPUT="$(set -u; nvm_is_version_or_alias 2>&1)" && ACTUAL_CODE=0 || ACTUAL_CODE=$?
|
||||
[ "_${ACTUAL_CODE}" = "_1" ] \
|
||||
|| die "nvm_is_version_or_alias with nounset and no argument: got exit code >${ACTUAL_CODE}<, expected >1<"
|
||||
[ "_${ACTUAL_OUTPUT}" = "_" ] \
|
||||
|| die "nvm_is_version_or_alias with nounset and no argument: got output >${ACTUAL_OUTPUT}<, expected >< (nothing)"
|
||||
|
||||
cleanup
|
||||
Reference in new issue
Block a user