summaryrefslogtreecommitdiffstats
path: root/tests/getopts.sh
blob: 255f2facb21fd03ce123c5e0ed837702a628f2bf (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/hint/bash

# Copyright © Tavian Barnes <tavianator@tavianator.com>
# SPDX-License-Identifier: 0BSD

## Argument parsing

if command -v nproc &>/dev/null; then
    JOBS=$(nproc)
else
    JOBS=1
fi
MAKE=
PATTERNS=()
SUDO=()
STOP=0
CLEAN=1
UPDATE=0
VERBOSE_COMMANDS=0
VERBOSE_ERRORS=0
VERBOSE_SKIPPED=0
VERBOSE_TESTS=0

# Print usage information
usage() {
    color cat <<EOF
Usage: ${GRN}$0${RST}
           [${BLU}-j${RST}${BLD}N${RST}] [${BLU}--make${RST}=${BLD}MAKE${RST}] [${BLU}--bfs${RST}=${BLD}path/to/bfs${RST}] [${BLU}--sudo${RST}[=${BLD}COMMAND${RST}]]
           [${BLU}--stop${RST}] [${BLU}--no-clean${RST}] [${BLU}--update${RST}] [${BLU}--verbose${RST}[=${BLD}LEVEL${RST}]] [${BLU}--help${RST}]
           [${BLU}--posix${RST}] [${BLU}--bsd${RST}] [${BLU}--gnu${RST}] [${BLU}--all${RST}] [${BLD}TEST${RST} [${BLD}TEST${RST} ...]]

  ${BLU}-j${RST}${BLD}N${RST}
      Run ${BLD}N${RST} tests in parallel (default: ${BLD}$JOBS${RST})

  ${BLU}--make${RST}=${BLD}MAKE${RST}
      Use the jobserver from ${BLD}MAKE${RST}, e.g. ${BLU}--make${RST}=${BLD}"make -j$JOBS"${RST}

  ${BLU}--bfs${RST}=${BLD}path/to/bfs${RST}
      Set the path to the bfs executable to test (default: ${BLD}./bin/bfs${RST})

  ${BLU}--sudo${RST}[=${BLD}COMMAND${RST}]
      Run tests that require root using ${GRN}sudo${RST} or the given ${BLD}COMMAND${RST}

  ${BLU}--stop${RST}
      Stop when the first error occurs

  ${BLU}--no-clean${RST}
      Keep the test directories around after the run

  ${BLU}--update${RST}
      Update the expected outputs for the test cases

  ${BLU}--verbose${RST}=${BLD}commands${RST}
      Log the commands that get executed
  ${BLU}--verbose${RST}=${BLD}errors${RST}
      Don't redirect standard error
  ${BLU}--verbose${RST}=${BLD}skipped${RST}
      Log which tests get skipped
  ${BLU}--verbose${RST}=${BLD}tests${RST}
      Log all tests that get run
  ${BLU}--verbose${RST}
      Log everything

  ${BLU}--help${RST}
      This message

  ${BLU}--posix${RST}, ${BLU}--bsd${RST}, ${BLU}--gnu${RST}, ${BLU}--all${RST}
      Choose which test cases to run (default: ${BLU}--all${RST})

  ${BLD}TEST${RST}
      Select individual test cases to run (e.g. ${BLD}posix/basic${RST}, ${BLD}"*exec*"${RST}, ...)
EOF
}

# Parse the command line
parse_args() {
    for arg; do
        case "$arg" in
            -j?*)
                JOBS="${arg#-j}"
                ;;
            --make=*)
                MAKE="${arg#*=}"
                ;;
            --bfs=*)
                BFS="${arg#*=}"
                ;;
            --posix)
                PATTERNS+=("posix/*")
                ;;
            --bsd)
                PATTERNS+=("posix/*" "common/*" "bsd/*")
                ;;
            --gnu)
                PATTERNS+=("posix/*" "common/*" "gnu/*")
                ;;
            --all)
                PATTERNS+=("*")
                ;;
            --sudo)
                SUDO=(sudo)
                ;;
            --sudo=*)
                read -a SUDO <<<"${arg#*=}"
                ;;
            --stop)
                STOP=1
                ;;
            --no-clean|--noclean)
                CLEAN=0
                ;;
            --update)
                UPDATE=1
                ;;
            --verbose=commands)
                VERBOSE_COMMANDS=1
                ;;
            --verbose=errors)
                VERBOSE_ERRORS=1
                ;;
            --verbose=skipped)
                VERBOSE_SKIPPED=1
                ;;
            --verbose=tests)
                VERBOSE_TESTS=1
                ;;
            --verbose)
                VERBOSE_COMMANDS=1
                VERBOSE_ERRORS=1
                VERBOSE_SKIPPED=1
                VERBOSE_TESTS=1
                ;;
            --help)
                usage
                exit 0
                ;;
            -*)
                color printf "${RED}error:${RST} Unrecognized option '%s'.\n\n" "$arg" >&2
                usage >&2
                exit 1
                ;;
            *)
                PATTERNS+=("$arg")
                ;;
        esac
    done

    read -a MAKE <<<"$MAKE"

    # Try to resolve the path to $BFS before we cd, while also supporting
    # --bfs="./bin/bfs -S ids"
    read -a BFS <<<"${BFS:-$BIN/bfs}"
    BFS[0]=$(_realpath "$(command -v "${BFS[0]}")")

    if ((${#PATTERNS[@]} == 0)); then
        PATTERNS=("*")
    fi

    TEST_CASES=()
    ALL_TESTS=($(cd "$TESTS" && quote {posix,common,bsd,gnu,bfs}/*.sh))
    for TEST in "${ALL_TESTS[@]}"; do
        TEST="${TEST%.sh}"
        for PATTERN in "${PATTERNS[@]}"; do
            if [[ $TEST == $PATTERN ]]; then
                TEST_CASES+=("$TEST")
                break
            fi
        done
    done

    if ((${#TEST_CASES[@]} == 0)); then
        color printf "${RED}error:${RST} No tests matched" >&2
        color printf " ${BLD}%s${RST}" "${PATTERNS[@]}" >&2
        color printf ".\n\n" >&2
        usage >&2
        exit 1
    fi
}