From 3d764e6833bb495288e322cf33a6def7e283f920 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 5 Apr 2015 16:49:29 -0700 Subject: [PATCH] Add `nvm_ensure_default_set` --- nvm.sh | 19 +++++++++++++++++++ test/fast/Aliases/nvm_ensure_default_set | 18 ++++++++++++++++++ test/fast/Aliases/teardown | 1 + test/fast/Unit tests/nvm_ensure_default_set | 13 +++++++++++++ 4 files changed, 51 insertions(+) create mode 100755 test/fast/Aliases/nvm_ensure_default_set create mode 100755 test/fast/Unit tests/nvm_ensure_default_set diff --git a/nvm.sh b/nvm.sh index 931bfaf..5de1858 100755 --- a/nvm.sh +++ b/nvm.sh @@ -873,6 +873,25 @@ nvm_get_arch() { echo "$NVM_ARCH" } +nvm_ensure_default_set() { + local VERSION + VERSION="$1" + if [ -z "$VERSION" ]; then + echo 'nvm_ensure_default_set: a version is required' >&2 + return 1 + fi + if nvm_alias default >/dev/null 2>&1; then + # default already set + return 0 + fi + local OUTPUT + OUTPUT="$(nvm alias default "$VERSION")" + local EXIT_CODE + EXIT_CODE="$?" + echo "Creating default alias: $OUTPUT" + return $EXIT_CODE +} + nvm_install_iojs_binary() { local PREFIXED_VERSION PREFIXED_VERSION="$1" diff --git a/test/fast/Aliases/nvm_ensure_default_set b/test/fast/Aliases/nvm_ensure_default_set new file mode 100755 index 0000000..c719e4b --- /dev/null +++ b/test/fast/Aliases/nvm_ensure_default_set @@ -0,0 +1,18 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +nvm alias default 0.1 >/dev/null || die "'nvm alias default 0.1' failed" + +nvm_ensure_default_set 0.3 || die "'nvm_ensure_default_set' with an existing default alias exits 0" + +nvm unalias default || die "'nvm unalias default' failed" + +OUTPUT="$(nvm_ensure_default_set 0.2)" +EXPECTED_OUTPUT="Creating default alias: default -> 0.2 (-> iojs-v0.2.10)" +EXIT_CODE="$?" + +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_ensure_default_set 0.2' did not output '$EXPECTED_OUTPUT', got '$OUTPUT'" +[ "_$EXIT_CODE" = "_0" ] || die "'nvm_ensure_default_set 0.2' did not exit with 0, got $EXIT_CODE" diff --git a/test/fast/Aliases/teardown b/test/fast/Aliases/teardown index 845567d..005c729 100755 --- a/test/fast/Aliases/teardown +++ b/test/fast/Aliases/teardown @@ -14,3 +14,4 @@ rm -f "../../../alias/stable" rm -f "../../../alias/unstable" rm -f "../../../alias/node" rm -f "../../../alias/iojs" +rm -f "../../../alias/default" diff --git a/test/fast/Unit tests/nvm_ensure_default_set b/test/fast/Unit tests/nvm_ensure_default_set new file mode 100755 index 0000000..edd926c --- /dev/null +++ b/test/fast/Unit tests/nvm_ensure_default_set @@ -0,0 +1,13 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +EXPECTED_OUTPUT="nvm_ensure_default_set: a version is required" +OUTPUT="$(nvm_ensure_default_set 2>&1 >/dev/null)" +EXIT_CODE="$?" +[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_ensure_default_set' did not output "$EXPECTED_OUTPUT", got "$OUTPUT"" +[ "_$EXIT_CODE" = "_1" ] || die "'nvm_ensure_default_set' did not exit with 1, got "$EXIT_CODE"" + +# see test/fast/Aliases for remaining nvm_ensure_default_set tests