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

128 lines, 57 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
17# List all functions defined in this file (and not in sourced files).
18_bash-print-funcs() {
19 ### Print shell functions in this file that don't start with _ (bash reflection)
20
21 local funcs
22 funcs=($(compgen -A function))
23 # extdebug makes `declare -F` print the file path, but, annoyingly, only
24 # if you pass the function names as arguments.
25 shopt -s extdebug
26 declare -F "${funcs[@]}" | grep --fixed-strings " $0" | awk '{print $1}'
27 shopt -u extdebug
28}
29
30_gawk-print-funcs() {
31 ### Print shell functions in this file that don't start with _ (awk parsing)
32
33 # Using gawk because it has match()
34 # - doesn't start with _
35
36 # space = / ' '* /
37 # shfunc = / %begin
38 # <capture !['_' ' '] ![' ']*>
39 # '()' space '{' space
40 # %end /
41 # docstring = / %begin
42 # space '###' ' '+
43 # <capture dot*>
44 # %end /
45 gawk '
46 match($0, /^([^_ ][^ ]*)\(\)[ ]*{[ ]*$/, m) {
47 #print NR " shfunc " m[1]
48 print m[1]
49 #print m[0]
50 }
51
52 match($0, /^[ ]*###[ ]+(.*)$/, m) {
53 print NR " docstring " m[1]
54 }
55' $0
56}
57
58_print-funcs() {
59 _bash-print-funcs
60 return
61
62 # TODO: make gawk work, with docstrings
63 if command -v gawk > /dev/null; then
64 _gawk-print-funcs
65 else
66 _bash-print-funcs
67 fi
68}
69
70
71byo-maybe-run() {
72 local command=${BYO_COMMAND:-}
73
74 case $command in
75 '')
76 # Do nothing if it's not specified
77 return
78 ;;
79
80 detect)
81 # all the commands supported, except 'detect'
82 echo list-tests
83 echo run-test
84
85 exit 66 # ASCII code for 'B' - what the protocol specifies
86 ;;
87
88 list-tests)
89 # TODO: use _bash-print-funcs? This fixes the transitive test problem,
90 # which happened in soil/web-remote-test.sh
91 # But it should work with OSH, not just bash! We need shopt -s extdebug
92 compgen -A function | grep '^test-'
93 exit 0
94 ;;
95
96 run-test)
97 local test_name=${BYO_ARG:-}
98 if test -z "$test_name"; then
99 die "BYO run-test: Expected BYO_ARG"
100 fi
101
102 # Avoid issues polluting recursive calls!
103 unset BYO_COMMAND BYO_ARG
104
105 # Shell convention: we name functions test-*
106 "$test_name"
107
108 # Only run if not set -e. Either way it's equivalent
109 exit $?
110 ;;
111
112 *)
113 die "Invalid BYO command '$command'"
114 ;;
115 esac
116
117 # Do nothing if BYO_COMMAND is not set.
118 # The program continues to its "main".
119}
120
121byo-must-run() {
122 local command=${BYO_COMMAND:-}
123 if test -z "$command"; then
124 die "Expected BYO_COMMAND= in environment"
125 fi
126
127 byo-maybe-run
128}