diff --git a/nvm.sh b/nvm.sh index 0e1c9dc..926ca35 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1828,6 +1828,27 @@ nvm() { NVM_PREFIX="$(nvm_node_prefix)" NVM_SUCCESS_MSG="Uninstalled node $VERSION" fi + + # Check version dir permissions + local PERMISSIONS_OK=true + check_file_permissions() { + for FILE in $1/* $1/.[!.]* $1/..?* ; do + if [ -d "$FILE" ]; then + check_file_permissions "$FILE" + elif [ -e "$FILE" ]; then + [ ! -w "$FILE" ] && PERMISSIONS_OK=false + fi + done + } + check_file_permissions "$VERSION_PATH" + if [ "$PERMISSIONS_OK" = false ]; then + >&2 echo 'Cannot uninstall, incorrect permissions on installation folder.' + >&2 echo 'This is usually caused by running `npm install -g` as root. Run the following command as root to fix the permissions and then try again.' + >&2 echo + >&2 echo " chown -R $(whoami) \"$VERSION_PATH\"" + return 1 + fi + # Delete all files related to target version. command rm -rf "$NVM_DIR/src/$NVM_PREFIX-$VERSION" \ "$NVM_DIR/src/$NVM_PREFIX-$VERSION.tar.*" \ diff --git "a/test/fast/Running \"nvm uninstall\" with incorrect file permissions fails nicely" "b/test/fast/Running \"nvm uninstall\" with incorrect file permissions fails nicely" new file mode 100755 index 0000000..1fad640 --- /dev/null +++ "b/test/fast/Running \"nvm uninstall\" with incorrect file permissions fails nicely" @@ -0,0 +1,14 @@ +#!/bin/sh + +cd ../.. +mkdir v0.0.1 +mkdir src/node-v0.0.1 + +sudo touch v0.0.1/sudo + +. ./nvm.sh + +RETURN_MESSAGE="$(nvm uninstall v0.0.1 2>&1)" +CHECK_FOR="Cannot uninstall, incorrect permissions on installation folder" + +test "${RETURN_MESSAGE#*$CHECK_FOR}" != "$RETURN_MESSAGE" || exit 1 diff --git "a/test/slow/nvm uninstall/Running \"nvm uninstall 0.12.6\" uninstalls v0.12.6" "b/test/slow/nvm uninstall/Running \"nvm uninstall 0.12.6\" uninstalls v0.12.6" new file mode 100755 index 0000000..9af0171 --- /dev/null +++ "b/test/slow/nvm uninstall/Running \"nvm uninstall 0.12.6\" uninstalls v0.12.6" @@ -0,0 +1,28 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +# Source nvm +. ../../../nvm.sh + +# Version to install/uninstall +NVM_TEST_VERSION=0.12.6 + +# Make sure it's not already here +[ -e ../../../$NVM_TEST_VERSION ] && rm -R ../../../$NVM_TEST_VERSION + +# Install it +nvm install $NVM_TEST_VERSION + +# Make sure it installed +nvm ls | grep "$NVM_TEST_VERSION" || die "Failed to install node" + +# Switch to another version so we can uninstall +nvm use 0.12.7 + +# Uninstall it +nvm uninstall $NVM_TEST_VERSION + +# Make sure it uninstalled +nvm ls | grep "$NVM_TEST_VERSION" +[ "$?" != "0" ] || die "Failed to uninstall node" diff --git "a/test/slow/nvm uninstall/Running \"nvm uninstall\" with incorrect file permissions fails nicely" "b/test/slow/nvm uninstall/Running \"nvm uninstall\" with incorrect file permissions fails nicely" new file mode 100755 index 0000000..eff2bc4 --- /dev/null +++ "b/test/slow/nvm uninstall/Running \"nvm uninstall\" with incorrect file permissions fails nicely" @@ -0,0 +1,30 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +# Source nvm +. ../../../nvm.sh + +# Version to install/uninstall +NVM_TEST_VERSION=5.10.1 + +# Make sure it's not already here +[ -e ../../../$NVM_TEST_VERSION ] && rm -R ../../../$NVM_TEST_VERSION + +# Install it +nvm install $NVM_TEST_VERSION + +# Make sure it installed +nvm ls | grep "$NVM_TEST_VERSION" || die "Failed to install node" + +# Install global module as root +npm_path=$(which npm) +sudo "$npm_path" install jspm -g + +# Switch to another version so we can uninstall +nvm use 0.12.7 + +# Attempt to uninstall it +RETURN_MESSAGE="$(nvm uninstall $NVM_TEST_VERSION 2>&1)" +CHECK_FOR="Cannot uninstall, incorrect permissions on installation folder" +test "${RETURN_MESSAGE#*$CHECK_FOR}" != "$RETURN_MESSAGE" || die "Failed to show error message" diff --git a/test/slow/nvm uninstall/setup_dir b/test/slow/nvm uninstall/setup_dir new file mode 100755 index 0000000..f385d98 --- /dev/null +++ b/test/slow/nvm uninstall/setup_dir @@ -0,0 +1,4 @@ +#!/bin/sh + +. ../../../nvm.sh +nvm install 0.12.7 diff --git a/test/slow/nvm uninstall/teardown_dir b/test/slow/nvm uninstall/teardown_dir new file mode 100755 index 0000000..f505f21 --- /dev/null +++ b/test/slow/nvm uninstall/teardown_dir @@ -0,0 +1,5 @@ +#!/bin/sh + +. ../../../nvm.sh +nvm uninstall 0.12.7 +nvm deactivate