Skip to content

Commit

Permalink
[Tests] ensure that unit tests use only mocked LTS names
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Oct 31, 2024
1 parent 6b70c40 commit e5e0ed6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
18 changes: 14 additions & 4 deletions test/fast/Unit tests/nvm_alias LTS-N
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ die () { echo "$@" ; cleanup ; exit 1; }

cleanup() {
unset -f nvm_download
rm -rf "${NVM_DIR}/alias/lts"
mv "${NVM_DIR}/alias/lts-backup" "${NVM_DIR}/alias/lts"
}

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

set -ex

MOCKS_DIR="${PWD}/mocks"

LTS_NAMES_PATH="${MOCKS_DIR}/LTS_names.txt"
LTS_LIST="$(cat "${LTS_NAMES_PATH}" | tail -n +2)"

mv "${NVM_DIR}/alias/lts" "${NVM_DIR}/alias/lts-backup" ||:
mkdir -p "${NVM_DIR}/alias/lts"

for LTS in $LTS_LIST; do
cp "${NVM_DIR}/alias/lts-backup/${LTS}" "${NVM_DIR}/alias/lts/"
done

set -ex

# sample output at the time the test was written
TAB_PATH="${MOCKS_DIR}/nodejs.org-dist-index.tab"
nvm_download() {
Expand All @@ -21,8 +33,6 @@ nvm_download() {

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
if [ $N -gt 0 ]; then
Expand Down
24 changes: 19 additions & 5 deletions test/fast/Unit tests/nvm_normalize_lts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,29 @@

cleanup() {
unset nvm_get_os
rm -rf "${NVM_DIR}/alias/lts"
mv "${NVM_DIR}/alias/lts-backup" "${NVM_DIR}/alias/lts"
}

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

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

MOCKS_DIR="../Unit tests/mocks"

LTS_NAMES_PATH="${MOCKS_DIR}/LTS_names.txt"
LTS_LIST="$(cat "${LTS_NAMES_PATH}" | tail -n +2)"

STAR="$(cat "${MOCKS_DIR}/lts-star.txt")"

mv "${NVM_DIR}/alias/lts" "${NVM_DIR}/alias/lts-backup" ||:
mkdir -p "${NVM_DIR}/alias/lts"

printf '%s\n' "${LTS_LIST}" | while IFS= read -r LTS; do
cp "${NVM_DIR}/alias/lts-backup/${LTS}" "${NVM_DIR}/alias/lts/"
done
echo "${STAR}" > "${NVM_DIR}/alias/lts/*"

ACTUAL="$(nvm_normalize_lts "foo")"
EXPECTED='foo'
[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"
Expand All @@ -20,15 +37,12 @@ if ACTUAL="$(nvm_normalize_lts lts/ARGON)"; then
die "expected failure, got >${ACTUAL}<"
fi

MOCKS_DIR="../Unit tests/mocks"
STAR="$(cat "$MOCKS_DIR/lts-star.txt")"

ACTUAL="$(nvm_normalize_lts "${STAR}")"
EXPECTED="${STAR}"
[ "${ACTUAL}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<, got >${ACTUAL}<"

LTS_NAMES_PATH="${MOCKS_DIR}/LTS_names.txt"
LTS_LIST="$(cat "${LTS_NAMES_PATH}" | tail -n +2)"
# or, back up LTS aliases, copy backups that are in LTS names
#

INDEX=1
printf '%s\n' "${LTS_LIST}" | while IFS= read -r LTS; do
Expand Down

0 comments on commit e5e0ed6

Please sign in to comment.