1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -e
4
5if [ "$NO_BUILD" ]; then
6    BUILD_DIR=""
7elif BUILD_DIR="$("$(dirname "$0")/../tools/find-build-dir.sh")"; then
8    ninja -C "$BUILD_DIR"
9else
10    echo "No build found, please set BUILD_DIR or NO_BUILD" >&2
11    exit 1
12fi
13
14if [ $# -gt 0 ]; then
15    args="$*"
16else
17    args="setup run clean-again"
18fi
19
20VALID_TARGETS="all setup run clean clean-again"
21
22is_valid_target() {
23    for target in $VALID_TARGETS; do
24        [ "$1" = "$target" ] && return 0
25    done
26    return 1
27}
28
29# reject invalid make targets in $args
30for arg in $args; do
31    if ! is_valid_target "$arg"; then
32        echo "Invalid target: $arg" >&2
33        exit 1
34    fi
35done
36
37CLEAN=0
38CLEANAGAIN=0
39
40# separate 'clean' and 'clean-again' operations
41[[ "$args" =~ "clean-again" ]] && CLEANAGAIN=1
42args=${args/clean-again}
43[[ "$args" =~ "clean" ]] && CLEAN=1
44args=${args/clean}
45
46declare -A results
47declare -A times
48
49COUNT=0
50FAILURES=0
51
52cd "$(dirname "$0")"
53
54pass_deny_list() {
55    for marker in $DENY_LIST_MARKERS $BLACKLIST_MARKERS; do
56        if [ -f "$1/$marker" ]; then
57            echo "========== DENY-LISTED: $1 ($marker) =========="
58            return 1
59        fi
60    done
61    return 0
62}
63
64SELECTED_TESTS="${SELECTED_TESTS:-TEST-??-*}"
65
66# Let's always do the cleaning operation first, because it destroys the image
67# cache.
68if [ $CLEAN = 1 ]; then
69    for TEST in $SELECTED_TESTS; do
70        ( set -x ; make -C "$TEST" clean )
71    done
72fi
73
74# Run actual tests (if requested)
75if [[ $args =~ [a-z] ]]; then
76    for TEST in $SELECTED_TESTS; do
77        COUNT=$((COUNT+1))
78
79        pass_deny_list "$TEST" || continue
80        start=$(date +%s)
81
82        echo -e "\n[$(date +%R:%S)] --x-- Running $TEST --x--"
83        set +e
84        # shellcheck disable=SC2086
85        ( set -x ; make -C "$TEST" $args )
86        RESULT=$?
87        set -e
88        echo "[$(date +%R:%S)] --x-- Result of $TEST: $RESULT --x--"
89
90        results["$TEST"]="$RESULT"
91        times["$TEST"]=$(( $(date +%s) - start ))
92
93        [ "$RESULT" -ne "0" ] && FAILURES=$((FAILURES+1))
94    done
95fi
96
97# Run clean-again, if requested, and if no tests failed
98if [[ $FAILURES -eq 0 && $CLEANAGAIN -eq 1 ]]; then
99    for TEST in "${!results[@]}"; do
100        ( set -x ; make -C "$TEST" clean-again )
101    done
102fi
103
104echo ""
105
106for TEST in "${!results[@]}"; do
107    RESULT="${results[$TEST]}"
108    time="${times[$TEST]}"
109    string=$([ "$RESULT" = "0" ] && echo "SUCCESS" || echo "FAIL")
110    printf "%-35s %-8s (%3s s)\n" "${TEST}:" "${string}" "$time"
111done | sort
112
113if [ "$FAILURES" -eq 0 ] ; then
114    echo -e "\nALL $COUNT TESTS PASSED"
115else
116    echo -e "\nTOTAL FAILURES: $FAILURES OF $COUNT"
117fi
118
119# If we have coverage files, merge them into a single report for upload
120if [ -n "${ARTIFACT_DIRECTORY}" ]; then
121    lcov_args=()
122
123    while read -r info_file; do
124        lcov_args+=(--add-tracefile "${info_file}")
125    done < <(find "${ARTIFACT_DIRECTORY}" -maxdepth 1 -name "*.coverage-info")
126
127    if [ ${#lcov_args[@]} -gt 1 ]; then
128        lcov "${lcov_args[@]}" --output-file "${ARTIFACT_DIRECTORY}/merged.coverage-info"
129    fi
130fi
131
132exit "$FAILURES"
133