Skip to content

Commit

Permalink
[New] add lts/-1 etc, to point to an LTS line relative to "latest"
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Mar 4, 2020
1 parent c372ff0 commit 4626d1a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
19 changes: 18 additions & 1 deletion nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,25 @@ nvm_alias() {
return 1
fi

local NVM_ALIAS_DIR
NVM_ALIAS_DIR="$(nvm_alias_path)"

if [ "$(expr "${ALIAS}" : '^lts/-[1-9][0-9]*$')" -gt 0 ]; then
local N
N="$(echo "${ALIAS}" | cut -d '-' -f 2)"
local RESULT
RESULT="$(command ls "${NVM_ALIAS_DIR}/lts" | command tail -n "${N}" | command head -n 1)"
if [ "${RESULT}" != '*' ]; then
nvm_alias "lts/${RESULT}"
return $?
else
nvm_err 'That many LTS releases do not exist yet.'
return 2
fi
fi

local NVM_ALIAS_PATH
NVM_ALIAS_PATH="$(nvm_alias_path)/${ALIAS}"
NVM_ALIAS_PATH="${NVM_ALIAS_DIR}/${ALIAS}"
if [ ! -f "${NVM_ALIAS_PATH}" ]; then
nvm_err 'Alias does not exist.'
return 2
Expand Down
5 changes: 5 additions & 0 deletions test/fast/Unit tests/mocks/LTS_names.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
erbium
dubnium
carbon
boron
argon
34 changes: 34 additions & 0 deletions test/fast/Unit tests/nvm_alias LTS-N
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

die () { echo "$@" ; cleanup ; exit 1; }


cleanup() {
unset -f nvm_download
}

\. ../../../nvm.sh

set -ex

MOCKS_DIR="${PWD}/mocks"

# sample output at the time the test was written
TAB_PATH="${MOCKS_DIR}/nodejs.org-dist-index.tab"
nvm_download() {
cat "${TAB_PATH}"
}

nvm_ls_remote >/dev/null || die "nvm_ls_remote_failed?!"

LTS_NAMES_PATH="${MOCKS_DIR}/LTS_names.txt"

N=0
while IFS= read -r LTS; do
N=$(($N+1))
EXPECTED="$(nvm_alias "lts/${LTS}")"
ACTUAL="$(nvm_alias "lts/-${N}")"
[ "${EXPECTED}" = "${ACTUAL}" ] || die "\`nvm_alias lts/-${N}\` was \`${ACTUAL}\`; expected \`${EXPECTED}\`"
done < "${LTS_NAMES_PATH}"

cleanup
5 changes: 5 additions & 0 deletions update_test_mocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ set -e

ALIAS_PATH="$MOCKS_DIR/nvm_make_alias LTS alias calls.txt"
: > "$ALIAS_PATH"
LTS_NAMES_PATH="$MOCKS_DIR/LTS_names.txt"
: > "$LTS_NAMES_PATH"
nvm_make_alias() {
# prevent local alias creation, and store arguments
echo "${1}|${2}" >> "$ALIAS_PATH"
if [ "${1}" != 'lts/*' ]; then
echo "${1#lts/}" >> "$LTS_NAMES_PATH"
fi
}
nvm ls-remote --lts > /dev/null

Expand Down

0 comments on commit 4626d1a

Please sign in to comment.