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

72 lines, 37 significant
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}
15source $LIB_OSH/two.sh
16
17byo-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 # Avoid issues polluting recursive calls!
47 unset BYO_COMMAND BYO_ARG
48
49 # Shell convention: we name functions test-*
50 "$test_name"
51
52 # Only run if not set -e. Either way it's equivalent
53 exit $?
54 ;;
55
56 *)
57 die "Invalid BYO command '$command'"
58 ;;
59 esac
60
61 # Do nothing if BYO_COMMAND is not set.
62 # The program continues to its "main".
63}
64
65byo-must-run() {
66 local command=${BYO_COMMAND:-}
67 if test -z "$command"; then
68 die "Expected BYO_COMMAND= in environment"
69 fi
70
71 byo-maybe-run
72}