-
Notifications
You must be signed in to change notification settings - Fork 14
/
test.sh
executable file
·137 lines (122 loc) · 2.53 KB
/
test.sh
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env bash
# Copyright 2016-2018 - Silke Hofstra and contributors
#
# Licensed under the EUPL
#
# You may not use this work except in compliance with the Licence.
# You may obtain a copy of the Licence at:
#
# https://joinup.ec.europa.eu/collection/eupl
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the Licence is distributed on an "AS IS" basis,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied.
# See the Licence for the specific language governing
# permissions and limitations under the Licence.
#
set -euo pipefail
# Settings for the test
export DEBUG=
export CONFIG="/dev/null"
export LC_ALL="C"
# Override commands
export PATH="tests:$PATH"
# Go to correct directory
cd "$(dirname "$0")"
# Show bash version
echo "Testing using Bash ${BASH_VERSION}"
# Failure count
FAILURES=0
# Test utilities
_TEST() {
echo -e "${1}"
}
_SUBTEST() {
echo -n " + ${1} "
}
_COLOR() {
c="$1"
shift
echo -e "\\u001B[${c}m$*\\u001B[0m"
}
_PASS() {
echo -e "\\t[$(_COLOR 32 PASS)]"
}
_FAIL() {
echo -e "\\t[$(_COLOR 31 FAIL)]"
FAILURES=$((FAILURES+1))
}
_EXP() {
echo -e "$(_COLOR 33 EXPECTED)\\n"\
"$1\\n" \
"$(_COLOR 33 GOT)\\n" \
"$2\\n" \
"---"
}
_MATCH() {
out="$1"
shift
mat="$*"
_out="${out//[[:space:]]/}"
_mat="${mat//[[:space:]]/}"
if [ "$_out" = "$_mat" ]; then
_PASS
else
_FAIL
if [[ -n "${VERBOSE:-}" ]]; then
_EXP "$mat" "$out"
fi
fi
}
_RUN() {
[[ -z "${EXP_EXIT:-}" ]] && local EXP_EXIT=0
if [[ -n "${SKIP_ALL:-}" ]]; then
_SKIP
return
fi
IFS=" " read -ra args <<< "$1"
out=$(./pdns_api.sh "${args[@]}" 2>&1 || echo exit=$?)
shift
_MATCH "$out" "$@"
}
_SKIP() {
echo -e "\\t[$(_COLOR 34 SKIPPED)]"
}
_SKIP_ALL() {
export SKIP_ALL=1
}
_RELOAD_CONFIG() {
source tests/config
if [[ $# -gt 0 ]]; then
export PDNS_VERSION="$1"
fi
export SKIP_ALL=
}
# Run all tests
if [ $# -eq 0 ]; then
# Run the tests in the tests folder
for test in tests/*.sh; do
_RELOAD_CONFIG
# shellcheck source=/dev/null
source "${test}"
done
# Run the tests for API versions
for version in {1..1}; do
echo "=> API version ${version}"
for test in "tests/v${version}"/*.sh; do
_RELOAD_CONFIG "${version}"
# shellcheck source=/dev/null
source "${test}"
done
done
else
# Run the given tests
for test in "$@"; do
_RELOAD_CONFIG
# shellcheck source=/dev/null
source "${test}"
done
fi
echo "Tests complete: ${FAILURES} failures."
exit ${FAILURES}