1#!/bin/sh
2set -e
3
4wait_for_sleep() {
5 # Wait until the given input is in sleep state. Assuming that it
6 # is in sleep state because it called read(3) on stdin.
7
8 awkscript="{ if (\$1 == ${1}) print(substr(\$2, 0, 1)) }"
9 while [ "$(ps -A -o pid,stat | awk "${awkscript}" )" != "S" ]; do
10 true
11 done
12}
13
14session="input-test"
15testdir="/tmp/input-test"
16outfile="${testdir}/output"
17
18INPUT="${INPUT:-$(pwd)/../input}"
19if [ ! -x "${INPUT}" ]; then
20 echo "Couldn't find input executable '${INPUT}'" 1>&2
21 exit 1
22fi
23
24# Don't pickup user configuration file
25INPUT="env INPUTRC=/dev/null '${INPUT}'"
26
27mkdir "${testdir}"
28trap "rm -rf '${testdir}' ; tmux kill-session -t '${session}' 2>/dev/null || true" INT EXIT
29
30for test in *; do
31 [ -d "${test}" ] || continue
32
33 name=${test##*/}
34 printf "Running test case '%s': " "${name}"
35
36 cmd="${INPUT} > '${outfile}'"
37 if [ -s "${test}/opts" ]; then
38 set -- $(cat "${test}/opts")
39 cmd="${INPUT} $@ > '${outfile}'"
40 fi
41 tmux new-session -d -s "${session}" "${cmd}"
42
43 pid="$(tmux list-panes -t "${session}" -F "#{pane_pid}")"
44 while read -r line; do
45 wait_for_sleep "${pid}"
46 tmux send-keys -t "${session}" "${line}"
47 done < "${test}/input"
48
49 if [ ! -e "${test}/exits" ]; then
50 wait_for_sleep "${pid}"
51 tmux send-keys -t "${session}" C-d
52
53 # Can't use wait(1) because proc was started in different env.
54 while tmux has-session -t "${session}" 2>/dev/null; do
55 true
56 done
57 fi
58
59 if ! cmp -s "${outfile}" "${test}/output"; then
60 printf "FAIL: Output didn't match.\n\n"
61 diff -u "${outfile}" "${test}/output"
62 exit 1
63 fi
64
65 printf "OK.\n"
66done