From e7b42198b4e5c1b710350198271aae1492077694 Mon Sep 17 00:00:00 2001 From: Peter Dave Hello Date: Tue, 20 Feb 2018 03:34:43 +0800 Subject: [PATCH] [Fix] Improve .nvmrc reading process Fixes #1015. Fixes #1712. --- nvm.sh | 2 +- ... use\" should drop CR char automatically." | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100755 "test/fast/Running \"nvm use\" should drop CR char automatically." diff --git a/nvm.sh b/nvm.sh index bf28b7d..23b539a 100644 --- a/nvm.sh +++ b/nvm.sh @@ -293,7 +293,7 @@ nvm_rc_version() { nvm_err "No .nvmrc file found" return 1 fi - read -r NVM_RC_VERSION < "${NVMRC_PATH}" || command printf '' + NVM_RC_VERSION="$(command head -n 1 "${NVMRC_PATH}" | command tr -d '\r')" || command printf '' if [ ! -n "${NVM_RC_VERSION}" ]; then nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\"" return 2 diff --git "a/test/fast/Running \"nvm use\" should drop CR char automatically." "b/test/fast/Running \"nvm use\" should drop CR char automatically." new file mode 100755 index 0000000..3889f47 --- /dev/null +++ "b/test/fast/Running \"nvm use\" should drop CR char automatically." @@ -0,0 +1,33 @@ +#!/bin/sh + +set -ex + +die () { echo "$@" ; cleanup ; exit 1; } + +cleanup() { + unset VERSION1 VERSION2 VERSION3 + rm .nvmrc +} + +\. ../../nvm.sh + +# normal .nvmrc +printf '0.999.0\n' > .nvmrc +nvm_rc_version +VERSION1="${NVM_RC_VERSION}" + +# .nvmrc with CR char +printf '0.999.0\r\n' > .nvmrc +nvm_rc_version +VERSION2="${NVM_RC_VERSION}" + +[ "${VERSION1}" = "${VERSION2}" ] + +# .nvmrc without any newline char +printf '0.999.0' > .nvmrc +nvm_rc_version +VERSION3="${NVM_RC_VERSION}" + +[ "${VERSION1}" = "${VERSION3}" ] + +cleanup