From 1c3f8da6c38bdfecf3dbf01c6753a6fd27032b9d Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 27 Apr 2016 14:23:34 -0700 Subject: [PATCH] `nvm alias`: factor out alias creation to `nvm_make_alias` --- nvm.sh | 24 ++++++++++++++++++++---- test/fast/Unit tests/nvm_make_alias | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100755 test/fast/Unit tests/nvm_make_alias diff --git a/nvm.sh b/nvm.sh index a42334e..6135a1f 100755 --- a/nvm.sh +++ b/nvm.sh @@ -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" ) diff --git a/test/fast/Unit tests/nvm_make_alias b/test/fast/Unit tests/nvm_make_alias new file mode 100755 index 0000000..c57906d --- /dev/null +++ b/test/fast/Unit tests/nvm_make_alias @@ -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'"