| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# run-tests - run a given series of test cases for beep |
| 4 |
# Copyright (C) 2018-2019 Hans Ulrich Niedermann |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation; either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with this program; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
CMP=cmp |
| 21 |
DIFF=diff |
| 22 |
SED=sed |
| 23 |
SHELL=sh |
| 24 |
|
| 25 |
# TODO: If we have more tone generating tests, find a well-known |
| 26 |
# melody without repeating notes and play that melody so that a |
| 27 |
# human can hear when the melody has holes. |
| 28 |
FREQS=(440 494 554 587 659 740 831 880 988 1109 1175 1319 1480 1661 1760) |
| 29 |
|
| 30 |
: set -e |
| 31 |
: set -x |
| 32 |
|
| 33 |
test_dir="$1" |
| 34 |
shift |
| 35 |
|
| 36 |
pass=0 |
| 37 |
fail=0 |
| 38 |
|
| 39 |
if test -t 1; then |
| 40 |
hilite="$(tput setaf 3)" |
| 41 |
normal="$(tput sgr0)" |
| 42 |
green="$(tput setaf 2)" |
| 43 |
red="$(tput setaf 1)$(tput bold)" |
| 44 |
else |
| 45 |
hilite="" |
| 46 |
normal="" |
| 47 |
green="" |
| 48 |
red="" |
| 49 |
fi |
| 50 |
passmsg="${green}PASS${normal}" |
| 51 |
failmsg="${red}FAIL${normal}" |
| 52 |
|
| 53 |
canonicalize_output() { |
| 54 |
${SED} -e "s|${BEEP}|BEEP_EXECUTABLE|g" -e "s|^${BEEP##*/}:|BEEP_EXECUTABLE:|g" -e "s|$(echo "${PACKAGE_VERSION}" | ${SED} 's/\./\\./g')|PACKAGE_VERSION|g" |
| 55 |
} |
| 56 |
|
| 57 |
# taken from contrib/ |
| 58 |
failure_beeps() { |
| 59 |
if test -e "beep"; then |
| 60 |
./beep -f 880 -l 200 -n -f 659 -n -f 523 -n -f 370 -l 400 |
| 61 |
fi |
| 62 |
} |
| 63 |
|
| 64 |
# taken from contrib/ |
| 65 |
success_beeps() { |
| 66 |
if test -e "beep"; then |
| 67 |
./beep -f 220 -l 100 -n -f 275 -l 100 -n -f 330 -l 100 -n -f 440 -l 100 -n -f 550 -l 100 -n -f 660 -l 100 -n -f 880 |
| 68 |
fi |
| 69 |
} |
| 70 |
|
| 71 |
: set -x |
| 72 |
|
| 73 |
for executable; do |
| 74 |
export BEEP="$PWD/$executable" |
| 75 |
freq_idx=0 |
| 76 |
for test in $(ls -1 tests/*.{bash,sh}); do |
| 77 |
export FREQ="${FREQS[${freq_idx}]}" |
| 78 |
base="$(basename $(basename "$test" .bash) .sh)" |
| 79 |
dir="$(dirname "$test")" |
| 80 |
actual="${dir}/${executable}--${base}.actual" |
| 81 |
printf "${hilite}%-13s${normal} ${hilite}%-32s${normal} " "${executable}" "${base}" |
| 82 |
if "${test}" > "${actual}.new" 2>&1; then |
| 83 |
canonicalize_output < "${actual}.new" > "${actual}" |
| 84 |
expects_output=false |
| 85 |
for file in "${dir}/${base}.expected".[0-9]; do |
| 86 |
if test -f "$file"; then |
| 87 |
expects_output=true |
| 88 |
break |
| 89 |
fi |
| 90 |
done |
| 91 |
if "${expects_output}"; then |
| 92 |
expect_fail="true" |
| 93 |
for expected in "${dir}/${base}.expected".[0-9]; do |
| 94 |
if ${CMP} --quiet "${expected}" "${actual}"; then |
| 95 |
pass="$(expr "${pass}" + 1)" |
| 96 |
echo "${passmsg}${expected/#*expected}" |
| 97 |
expect_fail="false" |
| 98 |
break |
| 99 |
fi |
| 100 |
done |
| 101 |
if "$expect_fail"; then |
| 102 |
fail="$(expr "${fail}" + 1)" |
| 103 |
echo "${failmsg}" |
| 104 |
for expected in "${dir}/${base}.expected".[0-9]; do |
| 105 |
${DIFF} -u "${expected}" "${actual}" |
| 106 |
done |
| 107 |
fi |
| 108 |
else |
| 109 |
if test -s "${actual}"; then |
| 110 |
fail="$(expr "${fail}" + 1)" |
| 111 |
echo "${failmsg}" |
| 112 |
cat "${actual}" |
| 113 |
else |
| 114 |
pass="$(expr "${pass}" + 1)" |
| 115 |
echo "${passmsg}" |
| 116 |
fi |
| 117 |
fi |
| 118 |
else |
| 119 |
canonicalize_output < "${actual}.new" > "${actual}" |
| 120 |
fail="$(expr "${fail}" + 1)" |
| 121 |
echo "${failmsg}" |
| 122 |
cat "${actual}" |
| 123 |
fi |
| 124 |
freq_idx="$(expr "$freq_idx" + 1)" |
| 125 |
if test "$freq_idx" -ge "${#FREQS[@]}"; then |
| 126 |
freq_idx=0 |
| 127 |
fi |
| 128 |
sleep 0.35 || sleep 1 |
| 129 |
done |
| 130 |
done |
| 131 |
|
| 132 |
echo "${pass} passed, ${fail} failed, $(expr "${pass}" + "${fail}") total." |
| 133 |
|
| 134 |
if test "${fail}" -gt 0; then \ |
| 135 |
failure_beeps |
| 136 |
exit 1 |
| 137 |
else |
| 138 |
success_beeps |
| 139 |
exit 0 |
| 140 |
fi |