File: run-tests

package info (click to toggle)
beep 1.4.9-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 680 kB
  • sloc: ansic: 1,488; sh: 256; python: 26; makefile: 12
file content (140 lines) | stat: -rw-r--r-- 3,745 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
#
# run-tests - run a given series of test cases for beep
# Copyright (C) 2018-2019 Hans Ulrich Niedermann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

CMP=cmp
DIFF=diff
SED=sed
SHELL=sh

# TODO: If we have more tone generating tests, find a well-known
#       melody without repeating notes and play that melody so that a
#       human can hear when the melody has holes.
FREQS=(440 494 554 587 659 740 831 880 988 1109 1175 1319 1480 1661 1760)

: set -e
: set -x

test_dir="$1"
shift

pass=0
fail=0

if test -t 1; then
    hilite="$(tput setaf 3)"
    normal="$(tput sgr0)"
    green="$(tput setaf 2)"
    red="$(tput setaf 1)$(tput bold)"
else
    hilite=""
    normal=""
    green=""
    red=""
fi
passmsg="${green}PASS${normal}"
failmsg="${red}FAIL${normal}"

canonicalize_output() {
    ${SED} -e "s|${BEEP}|BEEP_EXECUTABLE|g" -e "s|^${BEEP##*/}:|BEEP_EXECUTABLE:|g" -e "s|$(echo "${PACKAGE_VERSION}" | ${SED} 's/\./\\./g')|PACKAGE_VERSION|g"
}

# taken from contrib/
failure_beeps() {
    if test -e "beep"; then
	./beep -f 880 -l 200  -n -f 659  -n -f 523  -n -f 370 -l 400
    fi
}

# taken from contrib/
success_beeps() {
    if test -e "beep"; then
	./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
    fi
}

: set -x

for executable; do
    export BEEP="$PWD/$executable"
    freq_idx=0
    for test in $(ls -1 tests/*.{bash,sh}); do
	export FREQ="${FREQS[${freq_idx}]}"
	base="$(basename $(basename "$test" .bash) .sh)"
	dir="$(dirname "$test")"
	actual="${dir}/${executable}--${base}.actual"
	printf "${hilite}%-13s${normal} ${hilite}%-32s${normal} " "${executable}" "${base}"
	if "${test}" > "${actual}.new" 2>&1; then
	    canonicalize_output < "${actual}.new" > "${actual}"
	    expects_output=false
	    for file in "${dir}/${base}.expected".[0-9]; do
		if test -f "$file"; then
		    expects_output=true
		    break
		fi
	    done
	    if "${expects_output}"; then
		expect_fail="true"
		for expected in "${dir}/${base}.expected".[0-9]; do
		    if ${CMP} --quiet "${expected}" "${actual}"; then
			pass="$(expr "${pass}" + 1)"
			echo "${passmsg}${expected/#*expected}"
			expect_fail="false"
			break
		    fi
		done
		if "$expect_fail"; then
		    fail="$(expr "${fail}" + 1)"
		    echo "${failmsg}"
		    for expected in "${dir}/${base}.expected".[0-9]; do
			${DIFF} -u "${expected}" "${actual}"
		    done
		fi
	    else
		if test -s "${actual}"; then
		    fail="$(expr "${fail}" + 1)"
		    echo "${failmsg}"
		    cat "${actual}"
		else
		    pass="$(expr "${pass}" + 1)"
		    echo "${passmsg}"
		fi
	    fi
	else
	    canonicalize_output < "${actual}.new" > "${actual}"
	    fail="$(expr "${fail}" + 1)"
	    echo "${failmsg}"
	    cat "${actual}"
	fi
	freq_idx="$(expr "$freq_idx" + 1)"
	if test "$freq_idx" -ge "${#FREQS[@]}"; then
	    freq_idx=0
	fi
	sleep 0.35 || sleep 1
    done
done

echo "${pass} passed, ${fail} failed, $(expr "${pass}" + "${fail}") total."

if test "${fail}" -gt 0; then \
    failure_beeps
    exit 1
else
    success_beeps
    exit 0
fi