Merge pull request #567 from xcambar/nvm_source
[install script] Refactors NVM_SOURCE
diff --git a/install.sh b/install.sh
index 8974593..15de6bf 100755
--- a/install.sh
+++ b/install.sh
@@ -11,6 +11,33 @@
NVM_DIR="$HOME/.nvm"
fi
+#
+# Outputs the location to NVM depending on:
+# * The availability of $NVM_SOURCE
+# * The method used ("script" or "git" in the script, defaults to "git")
+# NVM_SOURCE always takes precedence
+#
+nvm_source() {
+ local NVM_METHOD
+ NVM_METHOD="$1"
+ if [ -z "$NVM_SOURCE" ]; then
+ local NVM_SOURCE
+ else
+ echo "$NVM_SOURCE"
+ return 0
+ fi
+ if [ "_$NVM_METHOD" = "_script" ]; then
+ NVM_SOURCE="https://raw.githubusercontent.com/creationix/nvm/v0.18.0/nvm.sh"
+ elif [ "_$NVM_METHOD" = "_git" ] || [ -z "$NVM_METHOD" ]; then
+ NVM_SOURCE="https://github.com/creationix/nvm.git"
+ else
+ echo >&2 "Unexpected value \"$NVM_METHOD\" for \$NVM_METHOD"
+ return 1
+ fi
+ echo "$NVM_SOURCE"
+ return 0
+}
+
nvm_download() {
if nvm_has "curl"; then
curl $*
@@ -27,10 +54,6 @@
}
install_nvm_from_git() {
- if [ -z "$NVM_SOURCE" ]; then
- NVM_SOURCE="https://github.com/creationix/nvm.git"
- fi
-
if [ -d "$NVM_DIR/.git" ]; then
echo "=> nvm is already installed in $NVM_DIR, trying to update"
printf "\r=> "
@@ -42,16 +65,15 @@
echo "=> Downloading nvm from git to '$NVM_DIR'"
printf "\r=> "
mkdir -p "$NVM_DIR"
- git clone "$NVM_SOURCE" "$NVM_DIR"
+ git clone "$(nvm_source "git")" "$NVM_DIR"
fi
cd "$NVM_DIR" && git checkout v0.18.0 && git branch -D master >/dev/null 2>&1
return
}
install_nvm_as_script() {
- if [ -z "$NVM_SOURCE" ]; then
- NVM_SOURCE="https://raw.githubusercontent.com/creationix/nvm/v0.18.0/nvm.sh"
- fi
+ local NVM_SOURCE
+ NVM_SOURCE=$(nvm_source "script")
# Downloading to $NVM_DIR
mkdir -p "$NVM_DIR"
@@ -60,8 +82,8 @@
else
echo "=> Downloading nvm as script to '$NVM_DIR'"
fi
- nvm_download -s "$NVM_SOURCE" -o "$NVM_DIR/nvm.sh" || {
- echo >&2 "Failed to download '$NVM_SOURCE'.."
+ nvm_download -s "$_source" -o "$NVM_DIR/nvm.sh" || {
+ echo >&2 "Failed to download '$_source'.."
return 1
}
}
diff --git a/test/install_script/nvm_source b/test/install_script/nvm_source
new file mode 100755
index 0000000..94c773f
--- /dev/null
+++ b/test/install_script/nvm_source
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+cleanup () {
+ unset -f die cleanup
+ unset NVM_SOURCE out
+}
+die () { echo $@ ; cleanup ; exit 1; }
+
+NVM_ENV=testing . ../../install.sh
+
+# nvm_source with no parameter returns the git endpoint
+echo $(nvm_source) | grep "nvm.git$" > /dev/null || die "nvm_source without arguments should return the location of the git repo"
+
+# nvm_source with git parameter returns the location of the nvm repo
+echo $(nvm_source "git") | grep "nvm.git$" > /dev/null || die "nvm_source without arguments should return the location of the git repo"
+
+# nvm_source with script parameter returns the location of nvm.sh
+echo $(nvm_source "script") | grep "nvm.sh$" > /dev/null || die "nvm_source \"script\" should return the location of nvm.sh"
+
+# nvm_source with any other parameter errors out and exits
+nvm_source "anything" 2> /dev/null && die "nvm_source with invalid parameter should exit"
+out=$(nvm_source "anything" 2>&1 >/dev/null) || : #Saving the process here
+[ -z "$out" ] && die "nvm_source with invalid parameter should error out"
+
+#nvm_source should always return NVM_SOURCE no matter the parameters
+NVM_SOURCE="my_location"
+out=$(nvm_source)
+[ "_$out" = "_my_location" ] || die "nvm_source without arguments should have returned \$NVM_SOURCE. Got \"$out\""
+out=$(nvm_source "git")
+[ "_$out" = "_my_location" ] || die "nvm_source git should have returned \$NVM_SOURCE. Got \"$out\""
+out=$(nvm_source "script")
+[ "_$out" = "_my_location" ] || die "nvm_source script should have returned \$NVM_SOURCE. Got \"$out\""
+out=$(nvm_source "anything")
+[ "_$out" = "_my_location" ] || die "nvm_source script should have returned \$NVM_SOURCE. Got \"$out\""
+
+cleanup
+