Skip to content

Commit

Permalink
[Tests] ensure things that care about colors run in a terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 29, 2024
1 parent ff1257e commit d2f31a2
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 55 deletions.
5 changes: 5 additions & 0 deletions test/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,8 @@ _json_parse() {
nvm_json_extract() {
nvm_json_tokenize | _json_parse | grep -e "${1}" | awk '{print $2 $3}'
}

# ensures that commands are run as if in a terminal
run_in_terminal() {
script -q -a /dev/null sh -c "\. ../nvm.sh ; $*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

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

if [ -n "$ZSH_VERSION" ]; then
if [ -n "${ZSH_VERSION}" ]; then
# set clobber option in order to test that this does not produce any
# incompatibilities
setopt noclobber
fi

nvm alias test-stable-1 0.0.2 || die '`nvm alias test-stable-1 0.0.2` failed'
nvm alias test-stable-1 0.0.2 || die "\`nvm alias test-stable-1 0.0.2\` failed"

OUTPUT="$(nvm alias test-stable-1 | strip_colors)"
OUTPUT="$(run_in_terminal nvm alias test-stable-1 | strip_colors)"
EXPECTED_OUTPUT='test-stable-1 -> 0.0.2 (-> v0.0.2)'
echo "$OUTPUT" | \grep -F "$EXPECTED_OUTPUT" || die "nvm alias test-stable-1 0.0.2 did not set test-stable-1 to 0.0.2: got '$OUTPUT'"
echo "${OUTPUT}" | \grep -F "${EXPECTED_OUTPUT}" || die "nvm alias test-stable-1 0.0.2 did not set test-stable-1 to 0.0.2: got '${OUTPUT}'"

nvm alias test-stable-1 0.0.1 || die '`nvm alias test-stable-1 0.0.1` failed'
nvm alias test-stable-1 0.0.1 || die "\`nvm alias test-stable-1 0.0.1\` failed"

OUTPUT="$(nvm alias test-stable-1 | strip_colors)"
OUTPUT="$(run_in_terminal nvm alias test-stable-1 | strip_colors)"
EXPECTED_OUTPUT='test-stable-1 -> 0.0.1 (-> v0.0.1)'
echo "$OUTPUT" | \grep -F "$EXPECTED_OUTPUT" || die "nvm alias test-stable-1 0.0.1 did not set test-stable-1 to 0.0.1: got '$OUTPUT'"
echo "$OUTPUT" | \grep -F "${EXPECTED_OUTPUT}" || die "nvm alias test-stable-1 0.0.1 did not set test-stable-1 to 0.0.1: got '${OUTPUT}'"
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

NVM_ALIAS_OUTPUT=$(nvm alias | strip_colors)
NVM_ALIAS_OUTPUT=$(run_in_terminal nvm alias | strip_colors)

EXPECTED_STABLE="$(nvm_print_implicit_alias local stable)"
STABLE_VERSION="$(nvm_version "$EXPECTED_STABLE")"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ make_fake_node v0.9.1
make_fake_iojs v0.2.1

EXPECTED_STABLE="$(nvm_print_implicit_alias local stable)"
STABLE_VERSION="$(nvm_version "$EXPECTED_STABLE")"
STABLE_VERSION="$(nvm_version "${EXPECTED_STABLE}")"

EXPECTED_UNSTABLE="$(nvm_print_implicit_alias local unstable)"
UNSTABLE_VERSION="$(nvm_version "$EXPECTED_UNSTABLE")"
UNSTABLE_VERSION="$(nvm_version "${EXPECTED_UNSTABLE}")"

[ "_$STABLE_VERSION" != "_$UNSTABLE_VERSION" ] \
[ "_${STABLE_VERSION}" != "_${UNSTABLE_VERSION}" ] \
|| die "stable and unstable versions are the same!"

nvm alias stable "$EXPECTED_UNSTABLE"
nvm alias unstable "$EXPECTED_STABLE"
nvm alias stable "${EXPECTED_UNSTABLE}"
nvm alias unstable "${EXPECTED_STABLE}"
nvm alias node stable
nvm alias iojs unstable

NVM_ALIAS_OUTPUT=$(nvm alias | strip_colors)
NVM_ALIAS_OUTPUT=$(run_in_terminal nvm alias | strip_colors)

echo "$NVM_ALIAS_OUTPUT" | command grep -F "stable -> $EXPECTED_UNSTABLE (-> $UNSTABLE_VERSION)" \
|| die "nvm alias did not contain the overridden 'stable' alias; got '$NVM_ALIAS_OUTPUT'"
echo "${NVM_ALIAS_OUTPUT}" | command grep -F "stable -> ${EXPECTED_UNSTABLE} (-> ${UNSTABLE_VERSION})" \
|| die "nvm alias did not contain the overridden 'stable' alias; got '${NVM_ALIAS_OUTPUT}'"

echo "$NVM_ALIAS_OUTPUT" | command grep -F "unstable -> $EXPECTED_STABLE (-> $STABLE_VERSION)" \
|| die "nvm alias did not contain the overridden 'unstable' alias; got '$NVM_ALIAS_OUTPUT'"
echo "${NVM_ALIAS_OUTPUT}" | command grep -F "unstable -> ${EXPECTED_STABLE} (-> ${STABLE_VERSION})" \
|| die "nvm alias did not contain the overridden 'unstable' alias; got '${NVM_ALIAS_OUTPUT}'"

echo "$NVM_ALIAS_OUTPUT" | command grep -F "node -> stable (-> $UNSTABLE_VERSION)" \
|| die "nvm alias did not contain the overridden 'node' alias; got '$NVM_ALIAS_OUTPUT'"
echo "${NVM_ALIAS_OUTPUT}" | command grep -F "node -> stable (-> ${UNSTABLE_VERSION})" \
|| die "nvm alias did not contain the overridden 'node' alias; got '${NVM_ALIAS_OUTPUT}'"

echo "$NVM_ALIAS_OUTPUT" | command grep -F "iojs -> unstable (-> $STABLE_VERSION)" \
|| die "nvm alias did not contain the overridden 'iojs' alias; got '$NVM_ALIAS_OUTPUT'"
echo "${NVM_ALIAS_OUTPUT}" | command grep -F "iojs -> unstable (-> ${STABLE_VERSION})" \
|| die "nvm alias did not contain the overridden 'iojs' alias; got '${NVM_ALIAS_OUTPUT}'"

cleanup
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

NVM_ALIAS_OUTPUT="$(nvm alias | strip_colors)"
NVM_ALIAS_OUTPUT="$(run_in_terminal nvm alias | strip_colors)"
echo "$NVM_ALIAS_OUTPUT" | \grep -F 'test-stable-1 -> 0.0.1 (-> v0.0.1)' \
|| die "did not find test-stable-1 alias; got '$NVM_ALIAS_OUTPUT'"
echo "$NVM_ALIAS_OUTPUT" | \grep -F 'test-stable-2 -> 0.0.2 (-> v0.0.2)' \
Expand Down
6 changes: 3 additions & 3 deletions test/fast/Aliases/nvm_ensure_default_set
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ nvm_ensure_default_set 0.3 || die "'nvm_ensure_default_set' with an existing def

nvm unalias default || die "'nvm unalias default' failed"

OUTPUT="$(nvm_ensure_default_set 0.2)"
OUTPUT="$(run_in_terminal nvm_ensure_default_set 0.2)"
EXPECTED_OUTPUT="Creating default alias: default -> 0.2 (-> iojs-v0.2.10)"
EXIT_CODE="$?"

[ "_$(echo "$OUTPUT" | strip_colors)" = "_$EXPECTED_OUTPUT" ] || die "'nvm_ensure_default_set 0.2' did not output '$EXPECTED_OUTPUT', got '$OUTPUT'"
[ "_$EXIT_CODE" = "_0" ] || die "'nvm_ensure_default_set 0.2' did not exit with 0, got $EXIT_CODE"
[ "_$(echo "${OUTPUT}" | strip_colors)" = "_${EXPECTED_OUTPUT}" ] || die "'nvm_ensure_default_set 0.2' did not output '${EXPECTED_OUTPUT}', got '${OUTPUT}'"
[ "_${EXIT_CODE}" = "_0" ] || die "'nvm_ensure_default_set 0.2' did not exit with 0, got ${EXIT_CODE}"
48 changes: 25 additions & 23 deletions test/fast/Unit tests/nvm ls-remote
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@

set -e

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

cleanup() {
unset -f nvm_download nvm_ls_remote nvm_ls_remote_iojs
if [ -n TEMP_NVM_COLORS ]; then
export NVM_COLORS=TEMP_NVM_COLORS
if [ -n "${TEMP_NVM_COLORS}" ]; then
export NVM_COLORS="${TEMP_NVM_COLORS}"
fi
unset TEMP_NVM_COLORS
}

\. ../../../nvm.sh
if [ -n ${NVM_COLORS} ]; then
export TEMP_NVM_COLORS=NVM_COLORS

if [ -n "${NVM_COLORS}" ]; then
export TEMP_NVM_COLORS="${NVM_COLORS}"
unset NVM_COLORS
fi

nvm deactivate 2>/dev/null || die 'unable to deactivate'
nvm deactivate 2>/dev/null || die '1: unable to deactivate'

\. ../../common.sh

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

# shellcheck disable=SC2317
nvm_download() {
if [ "$*" = "-L -s $(nvm_get_mirror node std)/index.tab -o -" ]; then
cat "$MOCKS_DIR/nodejs.org-dist-index.tab"
Expand All @@ -33,41 +35,41 @@ nvm_download() {
}

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<")"
OUTPUT="$(run_in_terminal nvm ls-remote --lts | sed 's/[ \t]*$//')"
EXPECTED_OUTPUT="$(cat "${EXPECTED_OUTPUT_PATH}" | sed 's/[ \t]*$//' )"
[ "_$OUTPUT" = "_${EXPECTED_OUTPUT}" ] || die "2: nvm ls-remote --lts did not output expected sorted versions; got >${OUTPUT}< expected 1 >${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<")"
OUTPUT="$(run_in_terminal nvm ls-remote 'lts/*' | sed 's/[ \t]*$//')"
EXPECTED_OUTPUT="$(cat "${EXPECTED_OUTPUT_PATH}" | sed 's/[ \t]*$//' )"
[ "_$OUTPUT" = "_${EXPECTED_OUTPUT}" ] || die "3: nvm ls-remote lts/* did not output expected sorted versions; got >${OUTPUT}< expected 2 >${EXPECTED_OUTPUT}<"

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

INDEX=1
printf '%s\n' "${LTS_LIST}" | while IFS= read -r LTS; do
ACTUAL="$(nvm ls-remote "lts/-${INDEX}" | sed 's/[ \t]*$//')"
ACTUAL="$(run_in_terminal nvm ls-remote "lts/-${INDEX}" | sed 's/[ \t]*$//')"
MESSAGE="for lts/-${INDEX} (${LTS})"
EXPECTED="$(nvm ls-remote "lts/${LTS}" | sed 's/[ \t]*$//')"
[ "${ACTUAL}" = "${EXPECTED}" ] || die "${MESSAGE}: expected >${EXPECTED}<, got >${ACTUAL}<"
EXPECTED="$(run_in_terminal nvm ls-remote "lts/${LTS}" | sed 's/[ \t]*$//')"
[ "${ACTUAL}" = "${EXPECTED}" ] || die "4: ${MESSAGE}: expected >${EXPECTED}<, got >${ACTUAL}<"

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<")"
OUTPUT="$(run_in_terminal nvm ls-remote | sed 's/[ \t]*$//')"
EXPECTED_OUTPUT="$(cat "${EXPECTED_OUTPUT_PATH}" | sed 's/[ \t]*$//' )"
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "5: bare nvm ls-remote did not output expected sorted versions; got >${OUTPUT}< expected >${EXPECTED_OUTPUT}<"

cleanup
6 changes: 3 additions & 3 deletions test/fast/Unit tests/nvm_ensure_default_set
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ die () { echo "$@" ; exit 1; }
\. ../../../nvm.sh

EXPECTED_OUTPUT="nvm_ensure_default_set: a version is required"
OUTPUT="$(nvm_ensure_default_set 2>&1 >/dev/null)"
OUTPUT="$(run_in_terminal nvm_ensure_default_set 2>&1 >/dev/null | strip_colors)"
EXIT_CODE="$?"
[ "_$(echo "$OUTPUT" | strip_colors)" = "_$EXPECTED_OUTPUT" ] || die "'nvm_ensure_default_set' did not output "$EXPECTED_OUTPUT", got "$OUTPUT""
[ "_$EXIT_CODE" = "_1" ] || die "'nvm_ensure_default_set' did not exit with 1, got "$EXIT_CODE""
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "'nvm_ensure_default_set' did not output >${EXPECTED_OUTPUT}<, got >${OUTPUT}<"
[ "_$EXIT_CODE" = "_1" ] || die "'nvm_ensure_default_set' did not exit with 1, got >${EXIT_CODE}<"

# see test/fast/Aliases for remaining nvm_ensure_default_set tests
6 changes: 3 additions & 3 deletions test/fast/Unit tests/nvm_ls_remote
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ cleanup() {

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

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

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

EXPECTED_OUTPUT_PATH="$MOCKS_DIR/nvm_ls_remote.txt"
Expand Down

0 comments on commit d2f31a2

Please sign in to comment.