Skip to content

Commit

Permalink
[Tests] fix broken tests exposed by 863bd63
Browse files Browse the repository at this point in the history
Leaves 2 non-executable, + some TODOs
  • Loading branch information
ljharb committed Aug 27, 2024
1 parent 0ce8f5a commit 9ae7f95
Show file tree
Hide file tree
Showing 18 changed files with 255 additions and 158 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ indent_size = false
[test/fast/Listing versions/Running 'nvm ls --no-alias' does not call into nvm_alias]
indent_size = false

[test/fast/Unit tests/mocks/**]
[test/fast/Unit test/mocks/**]
insert_final_newline = off

[test/**/.urchin*]
Expand Down
12 changes: 6 additions & 6 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2199,7 +2199,7 @@ nvm_install_binary_extract() {
command mkdir -p "${VERSION_PATH}" || return 1

if [ "${NVM_OS}" = 'win' ]; then
command mv "${TMPDIR}/"*/* "${VERSION_PATH}" || return 1
command mv "${TMPDIR}/"*/* "${VERSION_PATH}/" || return 1
command chmod +x "${VERSION_PATH}"/node.exe || return 1
command chmod +x "${VERSION_PATH}"/npm || return 1
command chmod +x "${VERSION_PATH}"/npx 2>/dev/null
Expand Down Expand Up @@ -2278,8 +2278,8 @@ nvm_install_binary() {

# Read nosource from arguments
if [ "${nosource-}" = '1' ]; then
nvm_err 'Binary download failed. Download from source aborted.'
return 0
nvm_err 'Binary download failed. Download from source aborted.'
return 0
fi

nvm_err 'Binary download failed, trying source.'
Expand Down Expand Up @@ -2922,11 +2922,11 @@ nvm_is_natural_num() {

nvm_write_nvmrc() {
local VERSION_STRING
VERSION_STRING=$(nvm_version "${1-$VERSION_STRING}")
if [ "$VERSION_STRING" = '' ] || [ "$VERSION_STRING" = 'N/A' ]; then
VERSION_STRING=$(nvm_version "${1-}")
if [ "${VERSION_STRING}" = '' ] || [ "${VERSION_STRING}" = 'N/A' ]; then
return 1
fi
echo "$VERSION_STRING" | tee "$PWD"/.nvmrc > /dev/null || {
echo "${VERSION_STRING}" | tee "$PWD"/.nvmrc > /dev/null || {
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_err "Warning: Unable to write version number ($VERSION_STRING) to .nvmrc"
fi
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"test/installation/node": "shell=$(basename -- $(ps -o comm= $(ps -o ppid= -p $PPID)) | sed 's/^-//'); make TEST_SUITE=installation_node test-$shell",
"test/installation/iojs": "shell=$(basename -- $(ps -o comm= $(ps -o ppid= -p $PPID)) | sed 's/^-//'); make TEST_SUITE=installation_iojs test-$shell",
"test/sourcing": "shell=$(basename -- $(ps -o comm= $(ps -o ppid= -p $PPID)) | sed 's/^-//'); make TEST_SUITE=sourcing test-$shell",
"test:check-exec": "(IFS=$'\\n'; for file in $(git ls-files test); do if [ ! -x \"$file\" ] && [[ \"$file\" != *.* ]] && [[ \"$file\" != test/fixtures/* ]]; then echo \"$file\"; fi; done) | tee /dev/stderr | awk 'END {if (NR > 0) exit 1}'",
"test:check-nonexec": "(IFS=$'\\n'; for file in $(git ls-files test); do if [ -x \"$file\" ] && [ ! -d \"$file\" ] && { [[ \"$file\" =~ '\\.(json|txt|sh|js|log)$' ]] || [[ \"$file\" =~ '^test/(mocks|fixtures)/.*' ]]; }; then echo \"$file\"; fi; done) | tee /dev/stderr | awk 'END {if (NR > 0) exit 1}'",
"doctoc": "doctoc --title='## Table of Contents' --github README.md",
"predoctoc:check": "cp README.md v-README.md.orig && npm run doctoc",
"doctoc:check": "diff -q README.md v-README.md.orig",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,58 @@

set -e

TEST_VERSION="v0.2.4"
TEST_VERSION='v0.2.4'

if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi

cleanup () {
cleanup() {
nvm cache clear
nvm deactivate
nvm unalias default
rm -rf ${NVM_DIR}/v* .nvmrc
rm -rf "${NVM_DIR}/v0.2.4" .nvmrc
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
unset -f nvm_ls_remote nvm_ls_remote_iojs
}

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

REMOTE="$PWD/mocks/nvm_ls_remote.txt"
REMOTE="${PWD}/mocks/nvm_ls_remote.txt"
nvm_ls_remote() {
cat "$REMOTE"
if [ -n "${PATTERN}" ]; then
cat "${REMOTE}" | \grep "${PATTERN}"
else
cat "${REMOTE}"
fi
}
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
nvm_ls_remote_iojs() {
cat "$REMOTE_IOJS"
local PATTERN
PATTERN="${1-}"
if [ -n "${PATTERN}" ]; then
cat "${REMOTE_IOJS}" | \grep "${PATTERN}"
else
cat "${REMOTE_IOJS}"
fi
}

make_fake_node "$TEST_VERSION"
make_fake_node "${TEST_VERSION}"

nvm install --save "$TEST_VERSION" || die "\`nvm install --save $TEST_VERSION\` failed"
nvm install -w "${TEST_VERSION}" || die "\`nvm install -w ${TEST_VERSION}\` failed"
OUTPUT="$(cat .nvmrc)"

nvm_is_valid_version "$(cat .nvmrc)" \
|| die "\`nvm install --save $TEST_VERSION\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"
nvm_is_valid_version "${OUTPUT}" \
|| die "\`nvm install -w ${TEST_VERSION}\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"

rm .nvmrc || die 'removing of .nvmrc failed'

nvm install --save "${TEST_VERSION}" || die "\`nvm install --save ${TEST_VERSION}\` failed"
OUTPUT="$(cat .nvmrc)"

nvm_is_valid_version "${OUTPUT}" \
|| die "\`nvm install --save ${TEST_VERSION}\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"

cleanup
61 changes: 61 additions & 0 deletions test/fast/Unit tests/Running 'nvm use --save' works as expected'
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/sh
\. ../../../nvm.sh
\. ../../common.sh

set -e

TEST_VERSION='v0.2.4'

if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi

cleanup() {
nvm cache clear
nvm deactivate
nvm unalias default
rm -rf "${NVM_DIR}/v0.2.4" .nvmrc
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
unset -f nvm_ls_remote nvm_ls_remote_iojs
}

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

REMOTE="${PWD}/mocks/nvm_ls_remote.txt"
nvm_ls_remote() {
if [ -n "${PATTERN}" ]; then
cat "${REMOTE}" | \grep "${PATTERN}"
else
cat "${REMOTE}"
fi
}
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
nvm_ls_remote_iojs() {
local PATTERN
PATTERN="${1-}"
if [ -n "${PATTERN}" ]; then
cat "${REMOTE_IOJS}" | \grep "${PATTERN}"
else
cat "${REMOTE_IOJS}"
fi
}

make_fake_node "${TEST_VERSION}"

nvm use -w "${TEST_VERSION}" || die "\`nvm install -w ${TEST_VERSION}\` failed"
OUTPUT="$(cat .nvmrc)"

nvm_is_valid_version "${OUTPUT}" \
|| die "\`nvm install -w ${TEST_VERSION}\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"

rm .nvmrc || die 'removing of .nvmrc failed'

nvm use --save "${TEST_VERSION}" || die "\`nvm install --save ${TEST_VERSION}\` failed"
OUTPUT="$(cat .nvmrc)"

nvm_is_valid_version "${OUTPUT}" \
|| die "\`nvm install --save ${TEST_VERSION}\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"

cleanup
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cleanup () {
nvm cache clear
nvm deactivate
nvm unalias default
rm -rf ${NVM_DIR}/v* .nvmrc
rm -rf "${NVM_DIR}/${TEST_VERSION:?}" .nvmrc
if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
unset -f nvm_ls_remote nvm_ls_remote_iojs
}
Expand All @@ -23,21 +23,29 @@ die () {
exit 1
}

REMOTE="$PWD/mocks/nvm_ls_remote.txt"
REMOTE="${PWD}/mocks/nvm_ls_remote.txt"
nvm_ls_remote() {
cat "$REMOTE"
cat "${REMOTE}"
}
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
REMOTE_IOJS="${PWD}/mocks/nvm_ls_remote_iojs.txt"
nvm_ls_remote_iojs() {
cat "$REMOTE_IOJS"
cat "${REMOTE_IOJS}"
}

make_fake_node "$TEST_VERSION"
make_fake_node "${TEST_VERSION}"

OUTPUT=$(nvm use --save --silent "$TEST_VERSION" || die "\`nvm use --save --silent $TEST_VERSION\` failed")
EXPECTED_OUTPUT=""
OUTPUT=$(nvm use --save --silent "${TEST_VERSION}" || die "\`nvm use --save --silent ${TEST_VERSION}\` failed")
EXPECTED_OUTPUT=''

[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
|| die "\`nvm use --save --silent $TEST_VERSION\` output was not silenced to '$EXPECTED_OUTPUT'; got '$OUTPUT'"
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
|| die "\`nvm use --save --silent ${TEST_VERSION}\` output was not silenced to '${EXPECTED_OUTPUT}'; got '${OUTPUT}'"

rm .nvmrc || die 'removing of .nvmrc failed'

OUTPUT=$(nvm use -w --silent "${TEST_VERSION}" || die "\`nvm use -w --silent ${TEST_VERSION}\` failed")
EXPECTED_OUTPUT=''

[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
|| die "\`nvm use -w --silent ${TEST_VERSION}\` output was not silenced to '${EXPECTED_OUTPUT}'; got '${OUTPUT}'"

cleanup
62 changes: 0 additions & 62 deletions test/fast/Unit tests/Running 'nvm use -w' works as expected'

This file was deleted.

26 changes: 13 additions & 13 deletions test/fast/Unit tests/nvm ls-remote
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ nvm deactivate 2>/dev/null || die 'unable to deactivate'

\. ../../common.sh

MOCKS_DIR="$PWD/mocks"
MOCKS_DIR="${PWD}/mocks"

nvm_download() {
if [ "$*" = "-L -s $(nvm_get_mirror node std)/index.tab -o -" ]; then
cat "$MOCKS_DIR/nodejs.org-dist-index.tab"
cat "${MOCKS_DIR}/nodejs.org-dist-index.tab"
return
fi
return 42
}

EXPECTED_OUTPUT_PATH="${MOCKS_DIR}/nvm ls-remote lts.txt"
OUTPUT="$(nvm ls-remote --lts | sed 's/[ \t]*$//')"
EXPECTED_OUTPUT="$(cat "$EXPECTED_OUTPUT_PATH" | sed 's/[ \t]*$//' )"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm ls-remote --lts did not output expected sorted versions; got $(echo ">$OUTPUT<") expected $(echo ">$EXPECTED_OUTPUT<")"
EXPECTED_OUTPUT="$(cat "${EXPECTED_OUTPUT_PATH}" | sed 's/[ \t]*$//' )"
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "nvm ls-remote --lts did not output expected sorted versions; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<"

EXPECTED_OUTPUT_PATH="${MOCKS_DIR}/nvm ls-remote lts.txt"
OUTPUT="$(nvm ls-remote "lts/*" | sed 's/[ \t]*$//')"
EXPECTED_OUTPUT="$(cat "$EXPECTED_OUTPUT_PATH" | sed 's/[ \t]*$//' )"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "nvm ls-remote lts/* did not output expected sorted versions; got $(echo ">$OUTPUT<") expected $(echo ">$EXPECTED_OUTPUT<")"
EXPECTED_OUTPUT="$(cat "${EXPECTED_OUTPUT_PATH}" | sed 's/[ \t]*$//' )"
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "nvm ls-remote lts/* did not output expected sorted versions; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<"

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

Expand All @@ -56,18 +56,18 @@ printf '%s\n' "${LTS_LIST}" | while IFS= read -r LTS; do
INDEX=$(($INDEX + 1))
done

REMOTE="$PWD/mocks/nvm_ls_remote.txt"
REMOTE="${PWD}/mocks/nvm_ls_remote.txt"
nvm_ls_remote() {
cat "$REMOTE"
cat "${REMOTE}"
}
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
REMOTE_IOJS="${PWD}/mocks/nvm_ls_remote_iojs.txt"
nvm_ls_remote_iojs() {
cat "$REMOTE_IOJS"
cat "${REMOTE_IOJS}"
}

EXPECTED_OUTPUT_PATH="${MOCKS_DIR}/nvm ls-remote.txt"
OUTPUT="$(nvm ls-remote | sed 's/[ \t]*$//')"
EXPECTED_OUTPUT="$(cat "$EXPECTED_OUTPUT_PATH" | sed 's/[ \t]*$//' )"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "bare nvm ls-remote did not output expected sorted versions; got $(echo ">$OUTPUT<") expected $(echo ">$EXPECTED_OUTPUT<")"
EXPECTED_OUTPUT="$(cat "${EXPECTED_OUTPUT_PATH}" | sed 's/[ \t]*$//' )"
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "bare nvm ls-remote did not output expected sorted versions; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<"

cleanup
5 changes: 2 additions & 3 deletions test/fast/Unit tests/nvm_download
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

cleanup () {
unset -f die cleanup NVM_AUTH_HEADER
unset -f die cleanup
docker stop httpbin && docker rm httpbin
}
die () { echo "$@" ; cleanup ; exit 1; }
Expand All @@ -17,13 +17,12 @@ nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/install.sh" >/de
! nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/wrong_install.sh" >/dev/null || die "nvm_download should fail to download no existing file"

# nvm_download should pass when calling with auth header
docker pull kennethreitz/httpbin && docker run --shell=bash -d --name httpbin -p 80:80 kennethreitz/httpbin
docker pull kennethreitz/httpbin && SHELL=bash docker run -d --name httpbin -p 80:80 kennethreitz/httpbin
sleep 1 # wait for httpbin to start
NVM_AUTH_HEADER="Bearer test-token" nvm_download "http://127.0.0.1/bearer" > /dev/null || die 'nvm_download with auth header should send correctly'

# nvm_download should fail when calling without auth header
nvm_download "http://127.0.0.1/bearer" > /dev/null && die 'nvm_download with no auth header should not send the header and should fail'
docker stop httpbin && docker rm httpbin

# ensure quoted extra args remain quoted
nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/install.sh" -o "; die quoted-command-not-quoted" || die 'command failed'
Expand Down
Loading

0 comments on commit 9ae7f95

Please sign in to comment.