From 685312d60078c67c3c7e30fa837685e957a168fd Mon Sep 17 00:00:00 2001 From: Peter Dave Hello Date: Thu, 12 Apr 2018 14:14:58 +0800 Subject: [PATCH] [Fix] Remove $NVM_DIR trailing slash automatically, fix #1708 --- nvm.sh | 5 +++++ ... remove the last trailing slash in $NVM_DIR | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 test/fast/nvm should remove the last trailing slash in $NVM_DIR diff --git a/nvm.sh b/nvm.sh index 0ca5fd7..1a98f3c 100644 --- a/nvm.sh +++ b/nvm.sh @@ -257,6 +257,11 @@ if [ -z "${NVM_DIR-}" ]; then # shellcheck disable=SC1001 NVM_DIR="$(nvm_cd ${NVM_CD_FLAGS} "$(dirname "${NVM_SCRIPT_SOURCE:-$0}")" > /dev/null && \pwd)" export NVM_DIR +else + while echo "${NVM_DIR}" | nvm_grep -q "/$"; do + export NVM_DIR="${NVM_DIR%/}" + done + nvm_err "Warning: \$NVM_DIR should not have trailing slash" fi unset NVM_SCRIPT_SOURCE 2> /dev/null diff --git a/test/fast/nvm should remove the last trailing slash in $NVM_DIR b/test/fast/nvm should remove the last trailing slash in $NVM_DIR new file mode 100644 index 0000000..4babb54 --- /dev/null +++ b/test/fast/nvm should remove the last trailing slash in $NVM_DIR @@ -0,0 +1,18 @@ +#!/bin/sh + +set -ex + +die () { echo "$@" ; exit 1; } + +export NVM_DIR_BASE="/tmp" +export NVM_DIR="${NVM_DIR_BASE}/" + +\. ../../nvm.sh + +[ "${NVM_DIR}" = "${NVM_DIR_BASE}" ] || die 'nvm should remove the last trailing slash in "$NVM_DIR"' + +export NVM_DIR="${NVM_DIR_BASE}//" + +\. ../../nvm.sh + +[ "${NVM_DIR}" = "${NVM_DIR_BASE}" ] || die 'nvm should remove all the last trailing slashes in "$NVM_DIR"'