From d2f31a21f37e3c0ebf5365ac635af2e2e070fbe0 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 28 Jul 2024 21:00:48 -0700 Subject: [PATCH] [Tests] ensure things that care about colors run in a terminal --- test/common.sh | 5 ++ ...t\313\203' again should change the target" | 14 +++--- ...ts implicit aliases when they do not exist | 2 +- ...s instead of implicit aliases when present | 28 +++++------ ...unning 'nvm alias' should list all aliases | 2 +- test/fast/Aliases/nvm_ensure_default_set | 6 +-- test/fast/Unit tests/nvm ls-remote | 48 ++++++++++--------- test/fast/Unit tests/nvm_ensure_default_set | 6 +-- test/fast/Unit tests/nvm_ls_remote | 6 +-- 9 files changed, 62 insertions(+), 55 deletions(-) diff --git a/test/common.sh b/test/common.sh index f5bf6ffbf6..47afa3fb12 100644 --- a/test/common.sh +++ b/test/common.sh @@ -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 ; $*" +} diff --git "a/test/fast/Aliases/Running 'nvm alias \313\202aliasname\313\203 \313\202target\313\203' again should change the target" "b/test/fast/Aliases/Running 'nvm alias \313\202aliasname\313\203 \313\202target\313\203' again should change the target" index f366bebd34..de1a06e461 100755 --- "a/test/fast/Aliases/Running 'nvm alias \313\202aliasname\313\203 \313\202target\313\203' again should change the target" +++ "b/test/fast/Aliases/Running 'nvm alias \313\202aliasname\313\203 \313\202target\313\203' again should change the target" @@ -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}'" diff --git a/test/fast/Aliases/Running 'nvm alias' lists implicit aliases when they do not exist b/test/fast/Aliases/Running 'nvm alias' lists implicit aliases when they do not exist index 24a341c7ef..30ed498d51 100755 --- a/test/fast/Aliases/Running 'nvm alias' lists implicit aliases when they do not exist +++ b/test/fast/Aliases/Running 'nvm alias' lists implicit aliases when they do not exist @@ -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")" diff --git a/test/fast/Aliases/Running 'nvm alias' lists manual aliases instead of implicit aliases when present b/test/fast/Aliases/Running 'nvm alias' lists manual aliases instead of implicit aliases when present index 1857f44cb5..eb35385e29 100755 --- a/test/fast/Aliases/Running 'nvm alias' lists manual aliases instead of implicit aliases when present +++ b/test/fast/Aliases/Running 'nvm alias' lists manual aliases instead of implicit aliases when present @@ -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 diff --git a/test/fast/Aliases/Running 'nvm alias' should list all aliases b/test/fast/Aliases/Running 'nvm alias' should list all aliases index d620fe510f..38ab61f8de 100755 --- a/test/fast/Aliases/Running 'nvm alias' should list all aliases +++ b/test/fast/Aliases/Running 'nvm alias' should list all aliases @@ -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)' \ diff --git a/test/fast/Aliases/nvm_ensure_default_set b/test/fast/Aliases/nvm_ensure_default_set index 7e7dab8347..55708dbf70 100755 --- a/test/fast/Aliases/nvm_ensure_default_set +++ b/test/fast/Aliases/nvm_ensure_default_set @@ -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}" diff --git a/test/fast/Unit tests/nvm ls-remote b/test/fast/Unit tests/nvm ls-remote index 683fe927cb..7b7e7f4773 100755 --- a/test/fast/Unit tests/nvm ls-remote +++ b/test/fast/Unit tests/nvm ls-remote @@ -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" @@ -33,14 +35,14 @@ 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" @@ -48,26 +50,26 @@ 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 diff --git a/test/fast/Unit tests/nvm_ensure_default_set b/test/fast/Unit tests/nvm_ensure_default_set index 14b53ebe49..9186273bca 100755 --- a/test/fast/Unit tests/nvm_ensure_default_set +++ b/test/fast/Unit tests/nvm_ensure_default_set @@ -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 diff --git a/test/fast/Unit tests/nvm_ls_remote b/test/fast/Unit tests/nvm_ls_remote index be61cc4c6f..264573baa3 100755 --- a/test/fast/Unit tests/nvm_ls_remote +++ b/test/fast/Unit tests/nvm_ls_remote @@ -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"