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