From 66b214651f66b34ecaf8378bd0ed04046e206061 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 30 Sep 2014 00:23:37 -0700 Subject: [PATCH] Add `nvm_alias` and unit tests. --- nvm.sh | 18 ++++++++++++++++++ test/fast/Unit tests/nvm_alias | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100755 test/fast/Unit tests/nvm_alias diff --git a/nvm.sh b/nvm.sh index 69de1be..b0ed78a 100644 --- a/nvm.sh +++ b/nvm.sh @@ -234,6 +234,24 @@ nvm_binary_available() { nvm_version_greater_than_or_equal_to "$1" "$FIRST_VERSION_WITH_BINARY" } +nvm_alias() { + local ALIAS + ALIAS="$1" + if [ -z "$ALIAS" ]; then + echo >&2 'An alias is required.' + return 1 + fi + + local NVM_ALIAS_PATH + NVM_ALIAS_PATH="$NVM_DIR/alias/$ALIAS" + if [ ! -e "$NVM_ALIAS_PATH" ]; then + echo >&2 'Alias does not exist.' + return 2 + fi + + cat "$NVM_ALIAS_PATH" +} + nvm_ls_current() { local NODE_PATH NODE_PATH="$(which node 2> /dev/null)" diff --git a/test/fast/Unit tests/nvm_alias b/test/fast/Unit tests/nvm_alias new file mode 100755 index 0000000..f51aac8 --- /dev/null +++ b/test/fast/Unit tests/nvm_alias @@ -0,0 +1,33 @@ +#!/bin/sh + +die () { echo $@ ; cleanup ; exit 1; } + +cleanup () { + rm -rf ../../../alias/test +} + +. ../../../nvm.sh + +OUTPUT="$(nvm_alias 2>&1)" +EXPECTED_OUTPUT='An alias is required.' +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_alias' produced wrong output; got $OUTPUT" + +EXIT_CODE="$(nvm_alias >/dev/null 2>&1 ; echo $?)" +[ "_$EXIT_CODE" = "_1" ] || die "'nvm_alias' exited with $EXIT_CODE, expected 1" + +rm -rf ../../../alias/nonexistent + +OUTPUT="$(nvm_alias nonexistent 2>&1)" +EXPECTED_OUTPUT='Alias does not exist.' +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_alias nonexistent' produced wrong output; got $OUTPUT" + +EXIT_CODE="$(nvm_alias nonexistent >/dev/null 2>&1 ; echo $?)" +[ "_$EXIT_CODE" = "_2" ] || die "'nvm_alias nonexistent' exited with $EXIT_CODE, expected 2" + +EXPECTED_OUTPUT="0.10" +nvm alias test "$EXPECTED_OUTPUT" || die "'nvm alias test $EXPECTED_OUTPUT' failed" +OUTPUT="$(nvm_alias test)" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_alias test' produced wrong output; got $OUTPUT" + +cleanup +