aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/local/main.sh
blob: cba7ab7d202caf6161b501bf6736a96f486bc48f (plain) (blame)
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
#!/usr/bin/env bash

# Copyright (c) 2026 Egor Tensin <egor@tensin.name>
# This file is part of the "config-links" project.
# For details, see https://github.com/egor-tensin/config-links
# Distributed under the MIT License.

set -o errexit -o nounset -o pipefail
shopt -s inherit_errexit 2> /dev/null || true
shopt -s lastpipe

script_dir="$( dirname -- "${BASH_SOURCE[0]}" )"
script_dir="$( cd -- "$script_dir" && pwd )"
readonly script_dir

declare -a failed_tests=()

run_test_file() {
    local test_file
    for test_file; do
        echo
        echo ======================================================================
        echo "Running test: $test_file"
        echo ======================================================================

        source "$script_dir/lib/common.sh"
        source "$script_dir/$test_file"

        echo "Should fail? ${test_should_fail:-No}"

        set +e
        (
            set -o errexit -o nounset -o pipefail
            shopt -s inherit_errexit 2> /dev/null || true
            shopt -s lastpipe

            if [ "$( type -t test_cleanup )" == function ]; then
                trap test_cleanup EXIT
            else
                trap test_cleanup_default EXIT
            fi

            test_run
        )
        local ec="$?"
        set -e

        if [ "$ec" -eq 0 ] || [ -n "${test_should_fail:+Yes}" ]; then
            echo ----------------------------------------------------------------------
            echo "OK: $test_file"
            echo ----------------------------------------------------------------------
        else
            echo ----------------------------------------------------------------------
            echo "FAIL: $test_file"
            echo ----------------------------------------------------------------------
            failed_tests+=("$test_file")
        fi
    done
}

main() {
    local -a test_files=()

    find "$script_dir" \
        -mindepth 1 \
        -maxdepth 1 \
        -type f \
        -regex '.*/tst-.*\.sh$' \
        -regextype posix-basic \
        -printf '%P\0' |
        sort -z |
    while IFS= read -d '' -r test_file; do
        test_files+=("$test_file")
    done

    run_test_file ${test_files[@]+"${test_files[@]}"}

    if [ "${#failed_tests[@]}" -gt 0 ]; then
        echo
        echo ======================================================================
        echo "FAILED TESTS:" ${failed_tests[@]+"${failed_tests[@]}"}
        echo ======================================================================
        return 1
    fi
}

main