-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash-test
More file actions
executable file
·102 lines (87 loc) · 2.42 KB
/
bash-test
File metadata and controls
executable file
·102 lines (87 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
# Copyright (c) 2025 Mihai Stancu (https://github.com/curatorium)
# @name test
# @type command
# @desc Test runner. Sources a test file, runs it, and prints a summary.
#
# @usage test <file.test> [name]
function test:main() {
[[ "${BASH_SOURCE[1]}" != "${0}" ]] && return;
local file="${1?usage: test <file.test> [name]}"; [[ "$file" != *.test ]] && file="$file.test";
local name="${2:-}";
SRC_DIR=$PWD;
RESULTS_FILE=$(mktemp);
trap 'rm -f $RESULTS_FILE' EXIT;
echo "0 0 0" > "$RESULTS_FILE";
source "$file";
if [[ -n "$name" ]]; then
t "$name";
else
declare -f output >/dev/null 2>&1 && output;
fi
test:summary;
}
# @name t
# @type function
# @desc Run a test function and return ✅/❌/⚠️
#
# @usage $(t name)
#
# @arg <name> -- The test function to execute (test: prefix added automatically).
#
# @return 0 always (result is printed as emoji)
function t() {
TEST_DIR=$(mktemp -d);
local output rc;
# Save inherit_errexit.
local inherit_errexit; inherit_errexit=$(shopt -p inherit_errexit);
# Disable inherit_errexit -- test needs it disabled.
shopt -u inherit_errexit;
output=$(
# Restore inherit_errexit so tests work under their own assumptions
$inherit_errexit;
trap 'declare -f teardown >/dev/null 2>&1 && teardown; rm -rf "$TEST_DIR"' EXIT;
cd "$TEST_DIR" || exit;
declare -f setup >/dev/null 2>&1 && setup;
"test:$1" 2>&1
) && rc=0 || rc=$?;
# Restore inherit_errexit
$inherit_errexit;
local pass fail err;
read -r pass fail err < "$RESULTS_FILE";
if [[ $rc -eq 0 ]]; then
echo "$((pass+1)) $fail $err" > "$RESULTS_FILE";
echo "✅";
elif [[ $rc -eq 1 ]]; then
echo "$pass $((fail+1)) $err" > "$RESULTS_FILE";
echo "❌";
[[ -n "$output" ]] && echo " └─ $output" >&2;
else
echo "$pass $fail $((err+1))" > "$RESULTS_FILE";
echo "⚠️";
[[ -n "$output" ]] && echo " └─ $output" >&2;
fi
}
# @name test:summary
# @type function
# @desc Print summary table and exit with appropriate code.
#
# @usage test:summary
#
# @return 0 when all tests pass
# @return 1 when any test fails or errors
function test:summary() {
local pass fail err;
read -r pass fail err < "$RESULTS_FILE";
rm -f "$RESULTS_FILE";
echo "";
echo "## Summary";
echo "";
echo "| ✅ Pass | ❌ Fail | ⚠️ Error |";
echo "|---------|---------|----------|";
echo "| $pass | $fail | $err |";
echo "";
[[ $fail -eq 0 && $err -eq 0 ]] && exit 0 || exit 1;
}
source .deps/assert.sh;
test:main "$@";