diff --git a/nvm.sh b/nvm.sh index 8943c3dc..81cf4d1b 100755 --- a/nvm.sh +++ b/nvm.sh @@ -815,9 +815,13 @@ nvm_tree_contains_path() { nvm_find_project_dir() { local path_ + local parent_ path_="${PWD}" - while [ "${path_}" != "" ] && [ "${path_}" != '.' ] && [ ! -f "${path_}/package.json" ] && [ ! -d "${path_}/node_modules" ]; do - path_=${path_%/*} + while [ "${path_}" != "" ] && [ ! -f "${path_}/package.json" ] && [ ! -d "${path_}/node_modules" ]; do + parent_=${path_%/*} + # stop when there is nowhere left to go up, e.g. `.` or a Windows drive root like `C:` + [ "${parent_}" != "${path_}" ] || break + path_="${parent_}" done nvm_echo "${path_}" } @@ -825,9 +829,13 @@ nvm_find_project_dir() { # Traverse up in directory tree to find containing folder nvm_find_up() { local path_ + local parent_ path_="${PWD}" - while [ "${path_}" != "" ] && [ "${path_}" != '.' ] && [ ! -f "${path_}/${1-}" ]; do - path_=${path_%/*} + while [ "${path_}" != "" ] && [ ! -f "${path_}/${1-}" ]; do + parent_=${path_%/*} + # stop when there is nowhere left to go up, e.g. `.` or a Windows drive root like `C:` + [ "${parent_}" != "${path_}" ] || break + path_="${parent_}" done nvm_echo "${path_}" } diff --git a/test/fast/Unit tests/nvm_find_project_dir b/test/fast/Unit tests/nvm_find_project_dir index bf1f489b..b6756b71 100755 --- a/test/fast/Unit tests/nvm_find_project_dir +++ b/test/fast/Unit tests/nvm_find_project_dir @@ -35,3 +35,6 @@ ACTUAL="$(PWD=$TEST_DIR/no-nesting-pkg nvm_find_project_dir)" ACTUAL="$(PWD="." nvm_find_project_dir)" [ "${ACTUAL}" = "." ] || die "insufficient permissions for pwd: got ${ACTUAL}" + +ACTUAL="$(PWD="Q:/nonexistent/dir" nvm_find_project_dir)" +[ "${ACTUAL}" = "Q:" ] || die "Windows-style drive path from pwd: got ${ACTUAL}" diff --git a/test/fast/Unit tests/nvm_find_up b/test/fast/Unit tests/nvm_find_up index fff3e5f3..97bd3bcd 100755 --- a/test/fast/Unit tests/nvm_find_up +++ b/test/fast/Unit tests/nvm_find_up @@ -23,5 +23,6 @@ TEST_DIR="$PWD" [ "~$(PWD=$TEST_DIR/tmp_nvm_find_up/a/b/c nvm_find_up 'test')" = "~$TEST_DIR/tmp_nvm_find_up/a/b/c" ] || die "failed to find in current dir" [ "~$(PWD=$TEST_DIR/tmp_nvm_find_up/a/b/c/d nvm_find_up 'test')" = "~$TEST_DIR/tmp_nvm_find_up/a/b/c" ] || die "failed to find 1 level up from current dir" [ "~$(PWD="." nvm_find_up 'test')" = "~." ] || die "failed to handle '.' output from pwd" +[ "~$(PWD="Q:/nonexistent/dir" nvm_find_up 'test')" = "~Q:" ] || die "failed to handle Windows-style drive path from pwd" cleanup