1#!/bin/sh2set -e34wait_for_sleep() {5 # Wait until the given input is in sleep state. Assuming that it6 # is in sleep state because it called read(3) on stdin.78 awkscript="{ if (\$1 == ${1}) print(substr(\$2, 0, 1)) }"9 while [ "$(ps -A -o pid,stat | awk "${awkscript}" )" != "S" ]; do10 true11 done12}1314session="input-test"15testdir="/tmp/input-test"16outfile="${testdir}/output"1718INPUT="${INPUT:-$(pwd)/../input}"19if [ ! -x "${INPUT}" ]; then20 echo "Couldn't find input executable '${INPUT}'" 1>&221 exit 122fi2324# Don't pickup user configuration file25INPUT="env INPUTRC=/dev/null '${INPUT}'"2627mkdir "${testdir}"28trap "rm -rf '${testdir}' ; tmux kill-session -t '${session}' 2>/dev/null || true" INT EXIT2930for test in *; do31 [ -d "${test}" ] || continue3233 name=${test##*/}34 printf "Running test case '%s': " "${name}"3536 cmd="${INPUT} > '${outfile}'"37 if [ -s "${test}/opts" ]; then38 set -- $(cat "${test}/opts")39 cmd="${INPUT} $@ > '${outfile}'"40 fi41 tmux new-session -d -s "${session}" "${cmd}"4243 pid="$(tmux list-panes -t "${session}" -F "#{pane_pid}")"44 while read -r line; do45 wait_for_sleep "${pid}"46 tmux send-keys -t "${session}" "${line}"47 done < "${test}/input"4849 if [ ! -e "${test}/exits" ]; then50 wait_for_sleep "${pid}"51 tmux send-keys -t "${session}" C-d5253 # Can't use wait(1) because proc was started in different env.54 while tmux has-session -t "${session}" 2>/dev/null; do55 true56 done57 fi5859 if ! cmp -s "${outfile}" "${test}/output"; then60 printf "FAIL: Output didn't match.\n\n"61 diff -u "${outfile}" "${test}/output"62 exit 163 fi6465 printf "OK.\n"66done