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

132 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
20set -o nounset
21set -o pipefail
22set -o errexit
23shopt -s strict:all 2>/dev/null || true # dogfood for OSH
24
25readonly TAB=$'\t'
26
27source stdlib/osh/two.sh
28
29detect() {
30 if test $# -eq 0; then
31 die "Expected argv to run"
32 fi
33
34 local out
35
36 local status=0
37 set +o errexit
38 out=$(BYO_COMMAND=detect "$@" < /dev/null)
39 status=$?
40 set -o errexit
41
42 if test $status -ne 66; then
43 die "$(printf '%q ' "$@") doesn't implement BYO: expected status 66, got $status"
44 fi
45
46 # Verbose
47 if false; then
48 echo
49 echo "BYO commands detected in $(printf '%q ' "$@"):"
50 echo "$out"
51 fi
52}
53
54run-tests() {
55 # argv is the command to run, like bash foo.sh
56 #
57 # It's an array rather than a single command, so you can run the same scripts
58 # under multiple shells:
59 #
60 # bash myscript.sh
61 # osh myscript.sh
62
63 if test $# -eq 0; then
64 die "Expected argv to run"
65 fi
66
67 detect "$@"
68
69 log '---'
70 log "byo run-tests: $@"
71 log
72
73
74 # TODO:
75 # --no-chdir Change directory by default, but this option disables it
76 # --no-stdout-log stdout is not redirected to its own, named file
77 # --max-jobs Parallelism
78 #
79 # And how do we run test binaries that are just one big process?
80 # - Python - inside a file, we probably don't have any tests to run in parallel
81 # - C++ - ditto
82 # - R - we have a few unit tests
83 #
84 # So maybe we need BYO_LIST_TEST_BIN - list the test binaries?
85 # Or mycpp/TEST.sh etc. should list them. Yes that is true!
86 # We can also list things to build.
87 #
88 # BYO_LIST_NINJA=1
89
90 mkdir -p _tmp
91 local tmp=_tmp/byo-list.txt
92
93 # First list the tests
94 BYO_COMMAND=list-tests "$@" > $tmp
95
96 local i=0
97 local status
98
99 while read -r test_name; do
100
101 echo "${TAB}${test_name}"
102
103 set +o errexit
104 BYO_COMMAND=run-test BYO_ARG="$test_name" "$@"
105 status=$?
106 set -o errexit
107
108 if test $status -eq 0; then
109 echo "${TAB}OK"
110 else
111 echo "${TAB}FAIL with status $status"
112 exit 1
113 fi
114
115 i=$(( i + 1 ))
116 done < $tmp
117
118 echo
119 echo "$0: $i tests passed."
120}
121
122# TODO:
123# BYO_PROBE=1
124#
125# must print capabilities
126#
127# - testing - success/fail
128# - benchmarks - output TSV file I think? Or a directory of TSV files?
129# - BYO_RESULTS
130# - shell auto-completion
131
132"$@"