OILS / test / byo-client.sh View on Github | oilshell.org

120 lines, 55 significant
1#!/usr/bin/env bash
2#
3# Run test executables that obey the BYO protocol.
4#
5# TODO: doc/byo.md
6#
7# Usage:
8# test/byo-client.sh run-tests ARGS...
9#
10# The ARGS... are run with an environment variable, e.g.
11#
12# ./myscript.py
13# python3 ./myscript.py
14# ./myscript.sh
15# dash ./myscript.sh
16# osh ./myscript.sh
17#
18# The client creates a clean process state and directory state for each tests.
19
20: ${LIB_OSH=stdlib/osh}
21source $LIB_OSH/bash-strict.sh
22source $LIB_OSH/two.sh
23source $LIB_OSH/task-five.sh
24
25readonly TAB=$'\t'
26
27detect() {
28 if test $# -eq 0; then
29 die "Expected argv to run"
30 fi
31
32 local out
33
34 local status=0
35 set +o errexit
36 out=$(BYO_COMMAND=detect "$@" < /dev/null)
37 status=$?
38 set -o errexit
39
40 if test $status -ne 66; then
41 die "$(printf '%q ' "$@") doesn't implement BYO: expected status 66, got $status"
42 fi
43
44 # Verbose
45 if false; then
46 echo
47 echo "BYO commands detected in $(printf '%q ' "$@"):"
48 echo "$out"
49 fi
50}
51
52run-tests() {
53 # argv is the command to run, like bash foo.sh
54 #
55 # It's an array rather than a single command, so you can run the same scripts
56 # under multiple shells:
57 #
58 # bash myscript.sh
59 # osh myscript.sh
60
61 if test $# -eq 0; then
62 die "Expected argv to run"
63 fi
64
65 detect "$@"
66
67 log '---'
68 log "byo run-tests: $@"
69 log
70
71
72 # TODO:
73 # --no-chdir Change directory by default, but this option disables it
74 # --no-stdout-log stdout is not redirected to its own, named file
75 # --max-jobs Parallelism
76 #
77 # And how do we run test binaries that are just one big process?
78 # - Python - inside a file, we probably don't have any tests to run in parallel
79 # - C++ - ditto
80 # - R - we have a few unit tests
81 #
82 # So maybe we need BYO_LIST_TEST_BIN - list the test binaries?
83 # Or mycpp/TEST.sh etc. should list them. Yes that is true!
84 # We can also list things to build.
85 #
86 # BYO_LIST_NINJA=1
87
88 mkdir -p _tmp
89 local tmp=_tmp/byo-list.txt
90
91 # First list the tests
92 BYO_COMMAND=list-tests "$@" > $tmp
93
94 local i=0
95 local status
96
97 while read -r test_name; do
98
99 echo "${TAB}${test_name}"
100
101 set +o errexit
102 BYO_COMMAND=run-test BYO_ARG="$test_name" "$@"
103 status=$?
104 set -o errexit
105
106 if test $status -eq 0; then
107 echo "${TAB}OK"
108 else
109 echo "${TAB}FAIL with status $status"
110 exit 1
111 fi
112
113 i=$(( i + 1 ))
114 done < $tmp
115
116 echo
117 echo "$0: $i tests passed."
118}
119
120task-five "$@"