all repos — dot @ master

Shell, editor and terminal configuration, and the tools that go with them

secrets/test/run.sh (view raw)

 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
#!/bin/bash
# Test runner for szecret tests
# Runs all test files or specific ones passed as arguments
# Each test file runs in its own isolated environment

set -euo pipefail

this_dir="$(cd "$(dirname "$0")" && pwd)"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

# Find all test files (excluding common.sh and run.sh)
find_test_files() {
    find "${this_dir}" -maxdepth 1 -name '*.sh' -type f \
        | xargs -n1 basename \
        | grep -Ev '^(common|run)\.sh$' \
        | sort
}

run_test_file() {
    local test_file="$1"
    local test_path="${this_dir}/${test_file}"

    if [[ ! -x "${test_path}" ]]; then
        chmod +x "${test_path}"
    fi

    echo "--- ${test_file%.sh} ---"

    # Run each test file in its own subprocess with isolated environment
    if "${test_path}"; then
        return 0
    else
        return 1
    fi
}

main() {
    echo "=== szecret test suite ==="
    echo

    local test_files=()
    local failed=0

    if [[ $# -gt 0 ]]; then
        # Run specific test files
        for arg in "$@"; do
            # Allow both "list" and "list.sh"
            local file="${arg%.sh}.sh"
            if [[ -f "${this_dir}/${file}" ]]; then
                test_files+=("${file}")
            else
                echo "Unknown test file: ${arg}" >&2
                exit 1
            fi
        done
    else
        # Run all test files
        while IFS= read -r file; do
            test_files+=("${file}")
        done < <(find_test_files)
    fi

    for test_file in "${test_files[@]}"; do
        if ! run_test_file "${test_file}"; then
            failed=1
        fi
        echo
    done

    if [[ ${failed} -eq 0 ]]; then
        echo -e "${GREEN}=== All tests passed ===${NC}"
    else
        echo -e "${RED}=== Some tests failed ===${NC}"
        exit 1
    fi
}

main "$@"