[Fix] --lts=<name>: match codenames containing the name, not as a regex
`nvm_ls_remote_index_tab` compared the codename with awk's `!~`, so the name was a regex: `--lts=.` matched every LTS line, and `--lts=i.on` or `--lts=(` didn't mean what they say. Use `index()` so a name still matches every codename that contains it, but literally, and do the same in `nvm_filter_lts_versions`, for `--lts=<name>` in an alias. Locally, `--lts=<name>` resolved the `lts/<name>` alias, which only exists for full codenames, so `nvm use --lts=ir` failed where `nvm install --lts=ir` installed Iron. Add `nvm_resolve_lts_name`, which picks the newest codename in `$NVM_DIR/.lts-status` that contains the name, and resolve `lts/<codename>` with it in `nvm use`, `uninstall`, `exec`, `run`, and in `nvm install` for its offline lookup and the default and `--alias=` it writes. Without a recorded status, or when no codename contains the name, the name is used as given. Signed-off-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com> Co-authored-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com> Co-authored-by: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
1 parent
dbda4b9387
commit
62adf80079
6 files changed
+249
-7
No files matched your search
@@ -72,6 +72,32 @@ nvm install -s --lts --default "${version}" > /dev/null 2>&1 || die "\`nvm insta
|
||||
OUTPUT="$(nvm_alias default)"
|
||||
[ "${OUTPUT}" = "${version}" ] || die "expected the default alias to be ${version} for \`--lts ${version}\`, got >${OUTPUT}<"
|
||||
|
||||
# `--lts=<name>` aliases the newest codename containing the name
|
||||
reset
|
||||
printf 'v22.0.0 jod\n%s iron\nv18.0.0 hydrogen\n' "${version}" > "$(nvm_lts_status_path)"
|
||||
nvm install -s --lts=ir --default > /dev/null 2>&1 || die '`nvm install -s --lts=ir --default` failed'
|
||||
OUTPUT="$(nvm_alias default)"
|
||||
[ "${OUTPUT}" = 'lts/iron' ] || die "expected the default alias to be lts/iron after \`--lts=ir\`, got >${OUTPUT}<"
|
||||
|
||||
# again, now that the version is installed
|
||||
nvm alias default v18.0.0 > /dev/null 2>&1
|
||||
nvm install -s --lts=ir --default > /dev/null 2>&1 || die '`nvm install -s --lts=ir --default` failed for an installed version'
|
||||
OUTPUT="$(nvm_alias default)"
|
||||
[ "${OUTPUT}" = 'lts/iron' ] || die "expected the default alias to be lts/iron for an installed version, got >${OUTPUT}<"
|
||||
nvm install -s --lts=ir --alias=foo > /dev/null 2>&1 || die '`nvm install -s --lts=ir --alias=foo` failed for an installed version'
|
||||
OUTPUT="$(nvm_alias foo)"
|
||||
[ "${OUTPUT}" = 'lts/iron' ] || die "expected the foo alias to be lts/iron for an installed version, got >${OUTPUT}<"
|
||||
|
||||
reset
|
||||
nvm install -s --lts=ir --alias=foo > /dev/null 2>&1 || die '`nvm install -s --lts=ir --alias=foo` failed'
|
||||
OUTPUT="$(nvm_alias foo)"
|
||||
[ "${OUTPUT}" = 'lts/iron' ] || die "expected the foo alias to be lts/iron after \`--lts=ir\`, got >${OUTPUT}<"
|
||||
|
||||
# offline, `--lts=<name>` finds the installed version of that codename
|
||||
nvm_echo "${version}" > "$(nvm_alias_path)/lts/iron"
|
||||
OUTPUT="$(nvm_remote_version() { return 3; }; nvm install --offline --lts=ir 2>&1)" || die "\`nvm install --offline --lts=ir\` failed: >${OUTPUT}<"
|
||||
rm -f "$(nvm_lts_status_path)"
|
||||
|
||||
reset
|
||||
nvm install --save "${version}" > /dev/null 2>&1 || die "\`nvm install --save ${version}\` failed"
|
||||
OUTPUT="$(cat .nvmrc 2> /dev/null)"
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/bin/sh
|
||||
|
||||
die () { echo "$@" ; exit 1; }
|
||||
|
||||
: 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() {
|
||||
cat "$PWD/mocks/nodejs.org-dist-index.tab"
|
||||
}
|
||||
|
||||
\. ../../common.sh
|
||||
|
||||
# no codename contains these characters literally, though most match them as a regex
|
||||
for NVM_LTS in . 'i.on' 'ar.*' '(' '^i' 'n$'; do
|
||||
try nvm_ls_remote
|
||||
[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "\`NVM_LTS='${NVM_LTS}' nvm_ls_remote\` should output N/A; got >$CAPTURED_STDOUT<"
|
||||
[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "\`NVM_LTS='${NVM_LTS}' nvm_ls_remote\` should exit 3; got $CAPTURED_EXIT_CODE"
|
||||
done
|
||||
unset NVM_LTS
|
||||
|
||||
# a name matches every codename that contains it
|
||||
OUTPUT="$(NVM_LTS=on nvm_ls_remote)"
|
||||
CODENAMES="$(nvm_echo "$OUTPUT" | command awk '{ print $2 }' | command sort -u | command tr '\n' ' ')"
|
||||
[ "_$CODENAMES" = "_Argon Boron Carbon Iron " ] || die "\`NVM_LTS=on nvm_ls_remote\` should list the Argon, Boron, Carbon and Iron lines; got >$CODENAMES<"
|
||||
|
||||
for NVM_LTS in iron ir; do
|
||||
OUTPUT="$(nvm_ls_remote)"
|
||||
[ -n "$OUTPUT" ] || die "\`NVM_LTS=${NVM_LTS} nvm_ls_remote\` output nothing"
|
||||
OTHER_LINES="$(nvm_echo "$OUTPUT" | command awk '$2 != "Iron"')"
|
||||
[ -z "$OTHER_LINES" ] || die "\`NVM_LTS=${NVM_LTS} nvm_ls_remote\` listed versions outside the Iron line: >$OTHER_LINES<"
|
||||
done
|
||||
Executable
+57
@@ -0,0 +1,57 @@
|
||||
#!/bin/sh
|
||||
|
||||
ORIG_NVM_DIR="${NVM_DIR}"
|
||||
TMP_DIR=''
|
||||
|
||||
cleanup () {
|
||||
case "${TMP_DIR}" in
|
||||
/*) rm -rf "${TMP_DIR}" ;;
|
||||
esac
|
||||
NVM_DIR="${ORIG_NVM_DIR}"
|
||||
export NVM_DIR
|
||||
}
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
TMP_DIR="$(mktemp -d)"
|
||||
NVM_DIR="${TMP_DIR}/nvm"
|
||||
export NVM_DIR
|
||||
mkdir -p "${NVM_DIR}" || die 'could not create NVM_DIR'
|
||||
|
||||
[ "_$(nvm_resolve_lts_name on)" = '_on' ] || die "without recorded LTS status, the name should be kept; got >$(nvm_resolve_lts_name on)<"
|
||||
|
||||
row () {
|
||||
printf '%s\t2024-01-01\tsrc\t-\t-\t-\t-\t-\t-\t%s\t-\n' "$1" "$2"
|
||||
}
|
||||
# not newest first, so the newest codename has to come from the versions
|
||||
{ row v20.19.0 Iron; row v26.12.1 -; row v24.11.0 Krypton; row v4.9.1 Argon; row v24.0.0 -; row v22.11.0 Jod; row v8.17.0 Carbon; } | nvm_record_lts_status || die 'could not record LTS status'
|
||||
|
||||
check () {
|
||||
ACTUAL="$(nvm_resolve_lts_name "$1")"
|
||||
[ "_${ACTUAL}" = "_$2" ] || die "\`nvm_resolve_lts_name '$1'\` should print >$2<; got >${ACTUAL}<"
|
||||
}
|
||||
|
||||
# the newest codename containing the name
|
||||
check on krypton
|
||||
check o krypton
|
||||
check ar carbon
|
||||
check arg argon
|
||||
check ir iron
|
||||
check iron iron
|
||||
check j jod
|
||||
# not a regex, and not a codename yet, so kept as given
|
||||
check . .
|
||||
check 'i.on' 'i.on'
|
||||
check '(' '('
|
||||
check nosuch nosuch
|
||||
# names nvm resolves some other way
|
||||
check '*' '*'
|
||||
check -1 -1
|
||||
check '' ''
|
||||
# LTS names are lowercase; an uppercase one is kept, for `nvm_normalize_lts` to reject
|
||||
check Iron Iron
|
||||
|
||||
cleanup
|
||||
Reference in new issue
Block a user