-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathrun-unit-tests.sh
More file actions
executable file
·120 lines (99 loc) · 2.72 KB
/
run-unit-tests.sh
File metadata and controls
executable file
·120 lines (99 loc) · 2.72 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
113
114
115
116
117
118
119
120
#!/bin/bash
set -u
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
ROOT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
BUILD_DIR=${BUILD_DIR:-$(pwd)}
UNIT_TESTS_DIR=${UNIT_TESTS_DIR:-tests.unit}
WORK_DIR=$(mktemp -d)
CC_BIN=${CC:-clang}
TEST_CFLAGS=${TEST_CFLAGS:-"-g -O1 -fno-omit-frame-pointer -fsanitize=address,undefined"}
TEST_LDFLAGS=${TEST_LDFLAGS:-"-fsanitize=address,undefined"}
read -r -a EXTRA_CFLAGS <<< "$TEST_CFLAGS"
read -r -a EXTRA_LDFLAGS <<< "$TEST_LDFLAGS"
PROJECT_SOURCES=(
"$ROOT_DIR/src/ipset.c"
"$ROOT_DIR/src/ipset_binary.c"
"$ROOT_DIR/src/ipset_combine.c"
"$ROOT_DIR/src/ipset_common.c"
"$ROOT_DIR/src/ipset_copy.c"
"$ROOT_DIR/src/ipset_diff.c"
"$ROOT_DIR/src/ipset_dns.c"
"$ROOT_DIR/src/ipset_exclude.c"
"$ROOT_DIR/src/ipset_load.c"
"$ROOT_DIR/src/ipset_merge.c"
"$ROOT_DIR/src/ipset_optimize.c"
"$ROOT_DIR/src/ipset_print.c"
"$ROOT_DIR/src/ipset_reduce.c"
)
cleanup() {
rm -rf "$WORK_DIR"
}
trap cleanup EXIT
if [ ! -f "$BUILD_DIR/config.h" ] && [ -f "$ROOT_DIR/config.h" ]; then
BUILD_DIR="$ROOT_DIR"
fi
if [ ! -f "$BUILD_DIR/config.h" ]; then
echo -e "${RED}Error: config.h not found in $BUILD_DIR or $ROOT_DIR${NC}"
exit 1
fi
if [ ! -d "$ROOT_DIR/$UNIT_TESTS_DIR" ]; then
echo -e "${RED}Error: unit test directory $ROOT_DIR/$UNIT_TESTS_DIR not found${NC}"
exit 1
fi
run_unit_test() {
local src="$1"
local name
local bin
local rc
name=$(basename "$src" .c)
bin="$WORK_DIR/$name"
echo -e "${YELLOW}Running unit test: $name${NC}"
if ! "$CC_BIN" \
-DHAVE_CONFIG_H \
-I"$BUILD_DIR" \
-I"$ROOT_DIR" \
-I"$ROOT_DIR/src" \
-pthread \
"${EXTRA_CFLAGS[@]}" \
"$src" \
"${PROJECT_SOURCES[@]}" \
"${EXTRA_LDFLAGS[@]}" \
-o "$bin"; then
echo -e "${RED}Unit test build failed${NC}"
return 1
fi
ASAN_OPTIONS=${ASAN_OPTIONS:-detect_leaks=1:abort_on_error=1} \
UBSAN_OPTIONS=${UBSAN_OPTIONS:-print_stacktrace=1:halt_on_error=1} \
"$bin"
rc=$?
if [ $rc -ne 0 ]; then
echo -e "${RED}Unit test failed: Exit code $rc${NC}"
return 1
fi
echo -e "${GREEN}Unit test passed${NC}"
}
total=0
passed=0
failed=0
for src in "$ROOT_DIR"/"$UNIT_TESTS_DIR"/*.c; do
[ -f "$src" ] || continue
total=$((total + 1))
if run_unit_test "$src"; then
passed=$((passed + 1))
else
failed=$((failed + 1))
fi
echo ""
done
echo -e "${YELLOW}Unit Test Summary:${NC}"
echo -e "Total tests: $total"
if [ $passed -gt 0 ]; then
echo -e "${GREEN}Passed tests: $passed${NC}"
fi
if [ $failed -gt 0 ]; then
echo -e "${RED}Failed tests: $failed${NC}"
fi
[ $failed -eq 0 ]