OILS / stdlib / byo-server-lib.sh View on Github | oilshell.org

65 lines, 35 significant
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
12log() {
13 echo "$@" >& 2
14}
15
16die() {
17 log "$0: fatal: $@"
18 exit 1
19}
20
21
22byo-maybe-main() {
23 local command=${BYO_COMMAND:-}
24
25 case $command in
26 '')
27 # Do nothing if it's not specified
28 return
29 ;;
30
31 detect)
32 # all the commands supported, except 'detect'
33 echo list-tests
34 echo run-test
35
36 exit 66 # ASCII code for 'B' - what the protocol specifies
37 ;;
38
39 list-tests)
40 # bash extension that OSH also implements
41 compgen -A function | grep '^test-'
42 exit 0
43 ;;
44
45 run-test)
46 local test_name=${BYO_ARG:-}
47 if test -z "$test_name"; then
48 die "BYO run-test: Expected BYO_ARG"
49 fi
50
51 # Shell convention: we name functions test-*
52 $test_name
53
54 # Only run if not set -e. Either way it's equivalent
55 exit $?
56 ;;
57
58 *)
59 die "Invalid BYO command '$command'"
60 ;;
61 esac
62
63 # Do nothing if BYO_COMMAND is not set.
64 # The program continues to its "main".
65}