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

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