-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpats_engine.bash
More file actions
112 lines (97 loc) · 2.43 KB
/
pats_engine.bash
File metadata and controls
112 lines (97 loc) · 2.43 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
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
# AUTHOR:
# Phil Porada - philporada@gmail.com
# LICENSE: GPLv3
# HOW:
# This file is sourced by each test_*.bash file
# You can check the return code of each test_*.bash file to automate
#
######################################
USE_COLOR=0
GRN=""
RED=""
RST=""
YEL=""
DEBUG_OUTPUT=0
COMPACT_OUTPUT=0
ERRORS=0
usage() {
echo -e "
USAGE:
./"$(basename ${0})" [-c] [-d] [-h] [-t]
-c | Color output, defaults to uncolored
-d | Show output from the commands you're testing, defaults to off
-h | Show this help menu
-t | Output in compact format. Example: ..F....FFF.
LICENSE:
GNU GPLv3
AUTHOR:
Phil Porada
"
}
# Checks return values and outputs pass/fail
checker() {
local RETVAL=${1}
shift
if [ "${RETVAL}" -eq 0 ] && [ "${COMPACT_OUTPUT}" -eq 0 ]; then
CHECKER="${GRN}ok${RST}"
elif [ "${RETVAL}" -eq 0 ] && [ "${COMPACT_OUTPUT}" -eq 1 ]; then
CHECKER="${GRN}.${RST}"
elif [ "${RETVAL}" -ne 0 ] && [ "${COMPACT_OUTPUT}" -eq 0 ]; then
CHECKER="${RED}not ok${RST}"
else
CHECKER="${RED}F${RST}"
fi
if [ "${RETVAL}" -ne 0 ]; then
ERRORS=$((ERRORS + 1))
fi
}
# Prepares part of the output string
test_name() {
local TEST_NAME="${@}"
shift
if [ "${DEBUG_OUTPUT}" -eq 1 ]; then
OUTPUT="${YEL}DEBUG${RST} - ${TEST_NAME}"
else
OUTPUT="${TEST_NAME}"
fi
}
# Runs the specified commands to retrieve a return value
# Finishes building the output string
test_runner() {
local TEST_SCRIPT="${@}"
shift
if [ "${DEBUG_OUTPUT}" -eq 0 ]; then
eval "${TEST_SCRIPT}" > /dev/null 2>&1
else
eval "${TEST_SCRIPT}"
fi
checker $?
if [ "${DEBUG_OUTPUT}" -eq 0 ] && [ "${COMPACT_OUTPUT}" -eq 0 ]; then
echo "${CHECKER} - ${OUTPUT}"
elif [ "${DEBUG_OUTPUT}" -eq 1 ] && [ "${COMPACT_OUTPUT}" -eq 0 ]; then
echo "${CHECKER} - ${OUTPUT}"
elif [ "${DEBUG_OUTPUT}" -eq 0 ] && [ "${COMPACT_OUTPUT}" -eq 1 ]; then
echo -n "${CHECKER}"
else
echo "${CHECKER} - ${OUTPUT}"
fi
}
while getopts ":cdht" opt; do
case $opt in
c) USE_COLOR=1
GRN="$(tput setaf 2)"
RED="$(tput setaf 1)"
YEL="$(tput setaf 3)"
RST="$(tput sgr0)"
;;
d) DEBUG_OUTPUT=1;;
h) usage
exit 0
;;
t) COMPACT_OUTPUT=1;;
\?) echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done