Skip to content

Commit

Permalink
test: slightly improve the output of the end-to-end tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adirelle committed May 10, 2024
1 parent 76a73dc commit 675e582
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 47 deletions.
48 changes: 17 additions & 31 deletions e2e/assert.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
#!/usr/bin/env bash
# shellcheck shell=bash

# Define "success" and "fail" with(out) coloring
if [[ ( -n ${CI:-} || -t 2 ) && -z ${NO_COLOR:-} ]]; then
# Success in green
succeed() {
echo $'\e[92m'"SUCCESS: $*"$'\e[0m' >&2
}
# Failure in red
fail() {
echo $'\e[91m'"FAILURE: $*"$'\e[0m' >&2
exit 1
}
# Skipped in yellow
skip() {
echo $'\e[93m'"SKIPPED: $*"$'\e[0m' >&2
exit 0
}
else
succeed() {
echo "SUCCESS: $*" >&2
}
fail() {
echo "FAILURE: $*" >&2
exit 1
}
skip() {
echo "SKIPPED:$*" >&2
exit 0
}
fi
# shellcheck source-path=SCRIPTDIR
source "$(dirname "${BASH_SOURCE[0]}")"/style.sh

succeed() {
ok "$*"
}

fail() {
err "$*"
exit 1
}

skip() {
notice "${*:-test skipped for some reason.}"
exit 0
}

quiet_assert_succeed() {
local status=0
Expand Down Expand Up @@ -72,7 +59,6 @@ assert_not() {
fi
}


assert_contains() {
local actual
actual="$(quiet_assert_succeed "$1")"
Expand Down
24 changes: 16 additions & 8 deletions e2e/run_all_tests
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"

# shellcheck source-path=SCRIPTDIR
source "$SCRIPT_DIR/style.sh"

ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
FILES="$(find "$SCRIPT_DIR" -name 'test_*' -type f -printf '%P\n' | sort)"
test_count=0
test_successes=0
test_failures=0
passed_tests=()
failed_tests=()

if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then
{
echo "| Test | Duration | Result |"
echo "| ---- | -------- | ------ |"
} >>"$GITHUB_STEP_SUMMARY"
fi

for f in $FILES; do
# split tests into two tranches to reduce test time
if [ -n "${TEST_TRANCHE_COUNT:-}" ]; then
Expand All @@ -23,14 +27,18 @@ for f in $FILES; do
fi
fi
if "$ROOT/e2e/run_test" "$f"; then
test_successes=$((test_successes + 1))
passed_tests=("${passed_tests[@]}" "$f")
else
test_failures=$((test_failures + 1))
failed_tests=("${failed_tests[@]}" "$f")
fi
echo
test_count=$((test_count + 1))
done

echo "e2e: $test_count tests passed, $test_successes succeeded, $test_failures failed"

[[ $test_failures = 0 ]]
notice "e2e: ran $test_count tests"
if [[ -n "${passed_tests[*]}" ]]; then
ok "e2e: ${#passed_tests[*]} tests passed: ${passed_tests[*]}"
fi
if [[ -n "${failed_tests[*]}" ]]; then
err "e2e: ${#failed_tests[*]} tests failed: ${failed_tests[*]}"
exit 1
fi
17 changes: 9 additions & 8 deletions e2e/run_test
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"

# shellcheck source-path=SCRIPTDIR
source "$SCRIPT_DIR/style.sh"

ROOT="$(cd "$SCRIPT_DIR"/.. && pwd)"

TEST="$1"
TEST_SCRIPT="$SCRIPT_DIR/$TEST"

setup_isolated_env() {
TEST_ISOLATED_DIR="$(mktemp --tmpdir --directory "$(basename "$TEST").XXXXXX")"
echo "Test directory: $TEST_ISOLATED_DIR" >&2

# Use a fake HOME and a fake temporary directory
TEST_HOME="$TEST_ISOLATED_DIR/home"
Expand Down Expand Up @@ -60,32 +62,31 @@ within_isolated_env() {
MISE_TRUSTED_CONFIG_PATHS="$TEST_HOME" \
MISE_YES=1 \
NPM_CONFIG_FUND=false \
GITHUB_ACTION="${GITHUB_ACTION:-}" \
"$@" || return $?
}

run_test() {
echo "::group::E2E $TEST"

setup_isolated_env
create_isolated_env

START=$(date +%s)
START="$(date +%s)"
local status=0
within_isolated_env "$TEST_SCRIPT" || status=$?
END=$(date +%s)
END="$(date +%s)"
echo "$TEST: $((END - START))s" >&2

if [[ "$status" == 0 ]]; then
remove_isolated_env
STATUS_MSG=":white_check_mark:"
else
echo "::error file=$TEST::E2E Test Failed (code: $status)"
err "E2E Test Failed (code: $status)"
STATUS_MSG=":x:"
fi
echo "$TEST: $((END - START))s"
if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then
echo "| $TEST | $((END - START))s | $STATUS_MSG" >>"$GITHUB_STEP_SUMMARY"
fi
return "$status"
}

run_test
as_group "E2E $TEST" run_test
48 changes: 48 additions & 0 deletions e2e/style.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# shellcheck shell=bash

if [[ -n ${GITHUB_ACTION:-} ]]; then
# Use special GA formatting
_STYLE_OK='::notice::'
_STYLE_ERR='::error::'
_STYLE_NOTICE='::warning::'
_STYLE_RESET=
_GROUP_START='::group::'
_GROUP_END='::endgroup::'
elif [[ -t 2 ]]; then
# Use ANSI coloring in terminal
_STYLE_OK=$'\e[92m' # green
_STYLE_ERR=$'\e[91m' # red
_STYLE_NOTICE=$'\e[93m' # yellow
_STYLE_RESET=$'\e[0m' # full reset
_GROUP_START=$'\e[1m>>> ' # bold
_GROUP_END=
else
# No styling
_STYLE_OK='SUCCESS: '
_STYLE_ERR='ERROR: '
_STYLE_NOTICE='WARNING: '
_STYLE_RESET=''
_GROUP_START='>>> '
_GROUP_END=
fi

ok() {
echo "${_STYLE_OK}$*${_STYLE_RESET}" >&2
}

err() {
echo "${_STYLE_ERR}$*${_STYLE_RESET}" >&2
}

notice() {
echo "${_STYLE_NOTICE}$*${_STYLE_RESET}" >&2
}

as_group() {
local status=0
echo "${_GROUP_START}$1${_STYLE_RESET}" >&2
shift
"$*" || status=$?
echo "${_GROUP_END}" >&2
return "$status"
}

0 comments on commit 675e582

Please sign in to comment.