aboutsummaryrefslogtreecommitdiffstats
path: root/sandbox/t/test-utils.sh
blob: f2d9a923df0c4a2a43f5e1885a262ce773e2b43a (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# t/test-utils.sh: Test framwork for poe/sandbox

while test $# -ne 0
do
	case "$1" in
	-v|--verbose)
		POE_TEST_VERBOSE=y
		shift
		;;
	*)
		echo "error: unknown option '$1'" >&2
		exit 1
		;;
	esac
done

# Internal functions

if test "x$TERM" != xdumb && test -t 1
then
	say_color_ok=$(tput setaf 2) # green
	say_color_error=$(tput setaf 1) # red
	say_color_info=$(tput setaf 3) # yellow
	say_color_= # normal
	say_color_reset=$(tput sgr0)
	say() {
		eval "color=\$say_color_$1"
		shift
		echo "$color$*$say_color_reset"
	}
else
	say() {
		echo $2
	}
fi

error() {
	say error "error: $*"
	POE_TEST_EXIT=y
	exit 1
}

test_count=0
test_success=0
test_failure=0

test_run() {
	{
		eval "
			test -n \"$POE_TEST_VERBOSE\" && set -x
			$1" </dev/null >&3 2>&4
	} 2>/dev/null
}


# Public helper functions

test_expect_success() {
	test $# = 2 ||
		error "test_expect_success expects 2 arguments"

	test_count=$(($test_count+1))
	if test_run "$2"
	then
		test_success=$(($test_success+1))
		say "" "ok $test_count - $1"
	else
		test_failure=$(($test_failure+1))
		say error "not ok $test_count - $1"
		printf '%s\n' "$2" | sed -e 's/^/#	/'
	fi
}

test_done() {
	POE_TEST_EXIT=y

	case $test_failure in
	0)
		say ok "# passed all $test_count test(s)"
		say "" "1..$test_count"
		exit 0
		;;
	*)
		say error "# failed $test_failure among $test_count test(s)"
		say "" "1..$test_count"
		exit 1
		;;
	esac
}


# Prepare for running tests

die() {
	x=$?
	test -n "$POE_TEST_EXIT" &&
		exit $x
	say error "fatal: unexpected exit with status $x"
	exit 1
}
trap 'die' EXIT

if test -n "$POE_TEST_VERBOSE"
then
	exec 3>&1 4>&2
else
	exec 3>/dev/null 4>/dev/null
fi


# Prepare $SANDBOX variable

x=$(dirname $(dirname $(readlink -f "$0")))
if test -x "$x/runner"
then
	SANDBOX=$x/runner
else
	error "sandbox runner binary not found in '$x'"
fi