diff options
| author | Egor Tensin <egor@tensin.name> | 2026-10-14 22:09:18 +0500 |
|---|---|---|
| committer | Egor Tensin <egor@tensin.name> | 2026-10-14 22:09:18 +0500 |
| commit | 7038150dd6350c3ece56ef966392a73323b05c45 (patch) | |
| tree | c0ae5a0c532298ede01f6f36a64b94245e353436 | |
| parent | pollute the global namespace less (diff) | |
| download | test-harness-7038150dd6350c3ece56ef966392a73323b05c45.tar.gz test-harness-7038150dd6350c3ece56ef966392a73323b05c45.zip | |
main.sh: add proper command line parameters & usage info
| -rwxr-xr-x | main.sh | 70 |
1 files changed, 60 insertions, 10 deletions
@@ -15,8 +15,6 @@ readonly _script_dir _script_name="$( basename -- "${BASH_SOURCE[0]}" )" readonly _script_name -readonly tests_dir="$_script_dir/.." - source "$_script_dir/lib/log.sh" _test_run_file() { @@ -58,25 +56,77 @@ _test_run_file() { done } +_test_usage() { + local msg + for msg; do + echo "$_script_name: $msg" + done + + echo "usage: $_script_name [-h|--help] [-d|--dir DIR] [-f|--filter NAME_PATTERN] + -h,--help show this message and exit + -d,--dir directory with tests + -f,--filter only run tests with names matching the pattern (uses fnmatch(3))" +} + +_test_parse_args() { + tests_dir="$_script_dir/.." + _test_pattern='*' + + while [ "$#" -gt 0 ]; do + local key="$1" + shift + + case "$key" in + -h|--help) + _test_usage + exit 0 + ;; + -d|--dir|-f|--filter) + ;; + *) + _test_usage "unrecognized parameter: $key" >&2 + return 1 + ;; + esac + + if [ "$#" -eq 0 ]; then + _test_usage "missing argument for parameter: $key" >&2 + return 1 + fi + + local value="$1" + shift + + case "$key" in + -d|--dir) + tests_dir="$value" + ;; + -f|--filter) + _test_pattern="$value" + ;; + *) + _test_usage "unrecognized parameter: $key" >&2 + return 1 + ;; + esac + done + + tests_dir="$( realpath -e -- "$tests_dir" )" +} + _test_main() { - if [ "$#" -gt 1 ]; then - echo "usage: $_script_name [PATTERN]" >&2 - return 1 - fi + _test_parse_args "$@" local -a test_files=() local test_file - local pattern='*' - [ "$#" -gt 0 ] && pattern="$1" - find "$tests_dir" \ -mindepth 1 \ -maxdepth 1 \ -type f \ -regex '.*/tst-.*\.sh$' \ -regextype posix-basic \ - -name "$pattern" \ + -name "$_test_pattern" \ -printf '%P\0' \ | sort -z \ | \ |
