diff --git a/nvm.sh b/nvm.sh index 2d503d7a..e4576577 100755 --- a/nvm.sh +++ b/nvm.sh @@ -2479,10 +2479,15 @@ nvm_ls() { *-*) NVM_PRERELEASE_PATTERN="${PATTERN}" ;; esac if [ -n "${NVM_DIRS_TO_SEARCH1}${NVM_DIRS_TO_SEARCH2}${NVM_DIRS_TO_SEARCH3}" ]; then + # the directories are matched literally, so escape characters that are special in sed + local NVM_DIR_SED + NVM_DIR_SED="$(nvm_echo "${NVM_DIR}" | command sed 's/[][\.*^$#]/\\&/g')" + local NVM_VERSION_DIR_IOJS_SED + NVM_VERSION_DIR_IOJS_SED="$(nvm_echo "${NVM_VERSION_DIR_IOJS}" | command sed 's/[][\.*^$#]/\\&/g')" VERSIONS="$(command find "${NVM_DIRS_TO_SEARCH1}"/* "${NVM_DIRS_TO_SEARCH2}"/* "${NVM_DIRS_TO_SEARCH3}"/* -name . -o -type d -prune -o -path "${PATTERN}*" \ | command sed -e " - s#${NVM_VERSION_DIR_IOJS}/#versions/${NVM_IOJS_PREFIX}/#; - s#^${NVM_DIR}/##; + s#${NVM_VERSION_DIR_IOJS_SED}/#versions/${NVM_IOJS_PREFIX}/#; + s#^${NVM_DIR_SED}/##; \\#^[^v]# d; \\#^versions\$# d; s#^versions/##; diff --git a/test/fast/Unit tests/nvm_ls matches NVM_DIR literally b/test/fast/Unit tests/nvm_ls matches NVM_DIR literally new file mode 100755 index 00000000..0b7a65db --- /dev/null +++ b/test/fast/Unit tests/nvm_ls matches NVM_DIR literally @@ -0,0 +1,37 @@ +#!/bin/sh + +die () { echo "$@" ; cleanup ; exit 1; } + +cleanup() { + rm -rf "${TEST_DIR-}" + unset TEST_DIR NVM_DIR DIR +} + +: nvm.sh +\. ../../../nvm.sh + +\. ../../common.sh + +TEST_DIR="${PWD}/nvm_ls_literal_nvm_dir_tmp" + +# each of these made sed match no installed versions, or fail to compile +for DIR in 'nvm\test' 'a[x]b' 'h#sh' 'st*r'; do + NVM_DIR="${TEST_DIR}/${DIR}" + for VERSION in versions/node/v24.11.0 v0.10.5 versions/io.js/v1.0.0; do + mkdir -p "${NVM_DIR}/${VERSION}/bin" || die "failed to create ${VERSION} dir under ${NVM_DIR}" + make_echo "${NVM_DIR}/${VERSION}/bin/node" "${VERSION##*/}" || die "failed to create ${VERSION} node binary under ${NVM_DIR}" + done + + # one version per layout; no pattern, or `v`, would also list a system node + try nvm_ls 0.10 + [ "_${CAPTURED_STDOUT}" = '_v0.10.5' ] || die "\`nvm_ls 0.10\` under \`${NVM_DIR}\` should output >v0.10.5<; got >${CAPTURED_STDOUT}<" + + try nvm_ls 24 + [ "_${CAPTURED_STDOUT}" = '_v24.11.0' ] || die "\`nvm_ls 24\` under \`${NVM_DIR}\` should output >v24.11.0<; got >${CAPTURED_STDOUT}<" + + try nvm_ls iojs + [ "_${CAPTURED_STDOUT}" = '_iojs-v1.0.0' ] || die "\`nvm_ls iojs\` under \`${NVM_DIR}\` should output >iojs-v1.0.0<; got >${CAPTURED_STDOUT}<" +done +unset VERSION + +cleanup