`nvm alias`: factor out alias creation to `nvm_make_alias`

Jordan Harband 2016-04-27 14:23:34 -07:00
parent 2d50606a78
commit 1c3f8da6c3
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
2 changed files with 39 additions and 4 deletions

24
nvm.sh
View File

@ -464,6 +464,22 @@ nvm_print_default_alias() {
fi
}
nvm_make_alias() {
local ALIAS
ALIAS="${1-}"
if [ -z "$ALIAS" ]; then
>&2 echo "an alias name is required"
return 1
fi
local VERSION
VERSION="${2-}"
if [ -z "$VERSION" ]; then
>&2 echo "an alias target version is required"
return 2
fi
echo "$VERSION" | tee "$(nvm_alias_path)/${ALIAS}" >/dev/null
}
nvm_alias() {
local ALIAS
ALIAS="${1-}"
@ -2356,11 +2372,11 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | command grep -v "N/A" | command sed '/^$/d'
if [ $? -ne 0 ]; then
echo "! WARNING: Version '${3-}' does not exist." >&2
fi
echo "$3" | tee "$NVM_ALIAS_DIR/${2-}" >/dev/null
if [ ! "_$3" = "_$VERSION" ]; then
echo "${2-} -> ${3-} (-> $VERSION)"
else
nvm_make_alias "${2-}" "${3-}"
if [ "_$3" = "_$VERSION" ]; then
echo "${2-} -> ${3-}"
else
echo "${2-} -> ${3-} (-> $VERSION)"
fi
;;
"unalias" )

View File

@ -0,0 +1,19 @@
#!/bin/sh
die () { echo $@ ; exit 1; }
. ../../../nvm.sh
OUTPUT="$(nvm_make_alias 2>&1)"
EXIT_CODE="$(nvm_make_alias >/dev/null 2>&1 ; echo $?)"
EXPECTED_OUTPUT='an alias name is required'
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "\`nvm_make_alias\` did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'"
[ "$EXIT_CODE" -eq 1 ] || die "\`nvm_make_alias\` did not exit with 1, got '$EXIT_CODE'"
OUTPUT="$(nvm_make_alias foo 2>&1)"
EXIT_CODE="$(nvm_make_alias foo >/dev/null 2>&1 ; echo $?)"
EXPECTED_OUTPUT='an alias target version is required'
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "\`nvm_make_alias foo\` did not error with '$EXPECTED_OUTPUT'; got '$OUTPUT'"
[ "$EXIT_CODE" -eq 2 ] || die "\`nvm_make_alias foo\` did not exit with 2, got '$EXIT_CODE'"