| 1 | # Library to turn a file into a "BYO test server"
|
| 2 | #
|
| 3 | # Usage:
|
| 4 | # source --builtin test/byo-server-lib.sh # from Oils
|
| 5 | # source test/byo-server-lib.sh # can be used with bash
|
| 6 | #
|
| 7 | # The client creates a clean process state and directory state for each tests.
|
| 8 | #
|
| 9 | # (It relies on compgen -A, and maybe declare -f, so it's not POSIX shell.)
|
| 10 |
|
| 11 | # TODO: How do I get stdlib/two.sh
|
| 12 | log() {
|
| 13 | echo "$@" >& 2
|
| 14 | }
|
| 15 |
|
| 16 | die() {
|
| 17 | log "$0: fatal: $@"
|
| 18 | exit 1
|
| 19 | }
|
| 20 |
|
| 21 | byo-maybe-run() {
|
| 22 | local command=${BYO_COMMAND:-}
|
| 23 |
|
| 24 | case $command in
|
| 25 | '')
|
| 26 | # Do nothing if it's not specified
|
| 27 | return
|
| 28 | ;;
|
| 29 |
|
| 30 | detect)
|
| 31 | # all the commands supported, except 'detect'
|
| 32 | echo list-tests
|
| 33 | echo run-test
|
| 34 |
|
| 35 | exit 66 # ASCII code for 'B' - what the protocol specifies
|
| 36 | ;;
|
| 37 |
|
| 38 | list-tests)
|
| 39 | # bash extension that OSH also implements
|
| 40 | compgen -A function | grep '^test-'
|
| 41 | exit 0
|
| 42 | ;;
|
| 43 |
|
| 44 | run-test)
|
| 45 | local test_name=${BYO_ARG:-}
|
| 46 | if test -z "$test_name"; then
|
| 47 | die "BYO run-test: Expected BYO_ARG"
|
| 48 | fi
|
| 49 |
|
| 50 | # Shell convention: we name functions test-*
|
| 51 | $test_name
|
| 52 |
|
| 53 | # Only run if not set -e. Either way it's equivalent
|
| 54 | exit $?
|
| 55 | ;;
|
| 56 |
|
| 57 | *)
|
| 58 | die "Invalid BYO command '$command'"
|
| 59 | ;;
|
| 60 | esac
|
| 61 |
|
| 62 | # Do nothing if BYO_COMMAND is not set.
|
| 63 | # The program continues to its "main".
|
| 64 | }
|
| 65 |
|
| 66 | byo-must-run() {
|
| 67 | local command=${BYO_COMMAND:-}
|
| 68 | if test -z "$command"; then
|
| 69 | die "Expected BYO_COMMAND= in environment"
|
| 70 | fi
|
| 71 |
|
| 72 | byo-maybe-run
|
| 73 | }
|