| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Run test executables that obey the BYO protocol.
 | 
| 4 | #
 | 
| 5 | # TODO: doc/byo.md
 | 
| 6 | #
 | 
| 7 | # Usage:
 | 
| 8 | #   test/byo-client.sh run-tests ARGS...
 | 
| 9 | #
 | 
| 10 | # The ARGS... are run with an environment variable, e.g.
 | 
| 11 | #
 | 
| 12 | #   ./myscript.py
 | 
| 13 | #   python3 ./myscript.py
 | 
| 14 | #   ./myscript.sh
 | 
| 15 | #   dash ./myscript.sh
 | 
| 16 | #   osh ./myscript.sh
 | 
| 17 | #
 | 
| 18 | # The client creates a clean process state and directory state for each tests.
 | 
| 19 | 
 | 
| 20 | set -o nounset
 | 
| 21 | set -o pipefail
 | 
| 22 | set -o errexit
 | 
| 23 | shopt -s strict:all 2>/dev/null || true  # dogfood for OSH
 | 
| 24 | 
 | 
| 25 | readonly TAB=$'\t'
 | 
| 26 | 
 | 
| 27 | log() {
 | 
| 28 |   echo "$0: $@" >&2
 | 
| 29 | }
 | 
| 30 | 
 | 
| 31 | die() {
 | 
| 32 |   log "$@"
 | 
| 33 |   exit 1
 | 
| 34 | }
 | 
| 35 | 
 | 
| 36 | run-tests() {
 | 
| 37 |   # argv is the command to run, like bash foo.sh
 | 
| 38 |   #
 | 
| 39 |   # It's an array rather than a single command, so you can run the same scripts
 | 
| 40 |   # under multiple shells:
 | 
| 41 |   #
 | 
| 42 |   #   bash myscript.sh
 | 
| 43 |   #   osh myscript.sh
 | 
| 44 | 
 | 
| 45 |   if test $# -eq 0; then
 | 
| 46 |     die "Expected argv to run"
 | 
| 47 |   fi
 | 
| 48 | 
 | 
| 49 |   # TODO:
 | 
| 50 |   # - change directories
 | 
| 51 |   # - provide option to redirect stdout
 | 
| 52 | 
 | 
| 53 |   mkdir -p _tmp
 | 
| 54 |   local tmp=_tmp/byo-list.txt
 | 
| 55 | 
 | 
| 56 |   # First list the tests
 | 
| 57 |   BYO_LIST_TESTS=1 "$@" > $tmp
 | 
| 58 | 
 | 
| 59 |   local i=0
 | 
| 60 |   local status
 | 
| 61 | 
 | 
| 62 |   while read -r test_name; do
 | 
| 63 | 
 | 
| 64 |     echo "${TAB}${test_name}"
 | 
| 65 | 
 | 
| 66 |     set +o errexit
 | 
| 67 |     BYO_RUN_TEST="$test_name" "$@"
 | 
| 68 |     status=$?
 | 
| 69 |     set -o errexit
 | 
| 70 | 
 | 
| 71 |     if test $status -eq 0; then
 | 
| 72 |        echo "${TAB}OK"
 | 
| 73 |     else
 | 
| 74 |       echo "${TAB}FAIL with status $status"
 | 
| 75 |       exit 1
 | 
| 76 |     fi
 | 
| 77 | 
 | 
| 78 |     i=$(( i + 1 ))
 | 
| 79 |   done < $tmp
 | 
| 80 | 
 | 
| 81 |   echo
 | 
| 82 |   echo "$0: $i tests passed."
 | 
| 83 | }
 | 
| 84 | 
 | 
| 85 | # TODO:
 | 
| 86 | # BYO_PROBE=1
 | 
| 87 | #
 | 
| 88 | # must print capabilities
 | 
| 89 | #
 | 
| 90 | # - testing - success/fail
 | 
| 91 | # - benchmarks - output TSV file I think?  Or a directory of TSV files?
 | 
| 92 | #   - BYO_RESULTS
 | 
| 93 | # - shell auto-completion
 | 
| 94 | 
 | 
| 95 | "$@"
 |