[Fix] nvm_ls_remote, nvm_remote_version: match dots between version components literally
The version pattern went to grep with its dots unescaped, so each one matched any character: `20.1` also matched `v2041.0.0`, and `20.1.1` matched `v20.111.1`, so `nvm_remote_version 20.1.1` resolved to `v20.111.1`. Escape each dot that is followed by a digit, keeping any other regex syntax in the pattern, like `v[68]`.
This commit is contained in:
1 parent
699c898671
commit
ed2de88826
2 files changed
+56
-3
No files matched your search
@@ -1359,8 +1359,11 @@ nvm_remote_version() {
|
||||
if [ -n "${PATTERN}" ] && [ "_${VERSION}" != "_N/A" ] && ! nvm_validate_implicit_alias "${PATTERN}" 2>/dev/null; then
|
||||
local VERSION_NUM
|
||||
VERSION_NUM="$(nvm_echo "${VERSION}" | command awk '{print $1}')"
|
||||
# the `v` prefix and the trailing non-digit keep `20` from matching `v10.20.0` or `v200.0.0`
|
||||
if ! nvm_echo "${VERSION_NUM} " | nvm_grep -q -e "$(nvm_ensure_version_prefix "${PATTERN%.}")[^0-9]"; then
|
||||
# the `v` prefix and the trailing non-digit keep `20` from matching `v10.20.0` or `v200.0.0`,
|
||||
# and escaping the dots between version components keeps `20.1.1` from matching `v20.111.1`
|
||||
local VERSION_RE
|
||||
VERSION_RE="$(nvm_ensure_version_prefix "${PATTERN%.}" | command sed 's/\.\([0-9]\)/\\.\1/g')"
|
||||
if ! nvm_echo "${VERSION_NUM} " | nvm_grep -q -e "${VERSION_RE}[^0-9]"; then
|
||||
VERSION='N/A'
|
||||
fi
|
||||
fi
|
||||
@@ -2770,12 +2773,17 @@ nvm_ls_remote_index_tab() {
|
||||
fi
|
||||
|
||||
local VERSIONS
|
||||
local PATTERN_RE
|
||||
PATTERN_RE=''
|
||||
if [ -n "${PATTERN}" ] && [ "${PATTERN}" != '*' ]; then
|
||||
if [ "${FLAVOR}" = 'iojs' ]; then
|
||||
PATTERN="$(nvm_ensure_version_prefix "$(nvm_strip_iojs_prefix "${PATTERN}")")"
|
||||
else
|
||||
PATTERN="$(nvm_ensure_version_prefix "${PATTERN}")"
|
||||
fi
|
||||
# match the dots between version components literally, so that `20.1.1` does not also
|
||||
# match `v20.111.1`; other regex syntax in the pattern (like `v[68]` or `.*`) is kept
|
||||
PATTERN_RE="$(nvm_echo "${PATTERN}" | command sed 's/\.\([0-9]\)/\\.\1/g')"
|
||||
else
|
||||
unset PATTERN
|
||||
fi
|
||||
@@ -2849,7 +2857,7 @@ EOF
|
||||
}
|
||||
prev=$10;
|
||||
}' \
|
||||
| nvm_grep -w -e "${PATTERN:-.*}" \
|
||||
| nvm_grep -w -e "${PATTERN_RE:-.*}" \
|
||||
| $SORT_COMMAND; } << EOF
|
||||
$VERSION_LIST
|
||||
EOF
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#!/bin/sh
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
cleanup() {
|
||||
unset -f nvm_download nvm_record_lts_status
|
||||
}
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
# these mocks must not be recorded as the LTS status of $NVM_DIR's versions
|
||||
nvm_record_lts_status() { :; }
|
||||
|
||||
nvm_download() {
|
||||
command printf '%s\n' \
|
||||
'version date files npm v8 uv zlib openssl modules lts security' \
|
||||
'v2041.0.0 2026-01-01 linux-x64 10.0.0 1 1 1 1 115 - -' \
|
||||
'v20.111.1 2026-01-01 linux-x64 10.0.0 1 1 1 1 115 - -' \
|
||||
'v20.10.0 2026-01-01 linux-x64 10.0.0 1 1 1 1 115 - -' \
|
||||
'v20.1.1 2026-01-01 linux-x64 10.0.0 1 1 1 1 115 - -' \
|
||||
'v20.1.0 2026-01-01 linux-x64 10.0.0 1 1 1 1 115 - -'
|
||||
}
|
||||
|
||||
\. ../../common.sh
|
||||
|
||||
# Dots in the pattern are literal, so they must not match other digits.
|
||||
OUTPUT="$(nvm_ls_remote 20.1)"
|
||||
EXPECTED_OUTPUT='v20.1.0
|
||||
v20.1.1'
|
||||
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "nvm_ls_remote 20.1: expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<"
|
||||
|
||||
OUTPUT="$(nvm_ls_remote 20.1.1)"
|
||||
[ "_${OUTPUT}" = '_v20.1.1' ] || die "nvm_ls_remote 20.1.1: expected >v20.1.1<, got >${OUTPUT}<"
|
||||
|
||||
try nvm_ls_remote 20.1.2
|
||||
[ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "nvm_ls_remote 20.1.2: expected >N/A<, got >${CAPTURED_STDOUT}<"
|
||||
|
||||
OUTPUT="$(nvm_remote_version 20.1.1)"
|
||||
[ "_${OUTPUT}" = '_v20.1.1' ] || die "nvm_remote_version 20.1.1: expected >v20.1.1<, got >${OUTPUT}<"
|
||||
|
||||
try nvm_remote_version 20.1.2
|
||||
[ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "nvm_remote_version 20.1.2: expected >N/A<, got >${CAPTURED_STDOUT}<"
|
||||
|
||||
cleanup
|
||||
Reference in new issue
Block a user