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