]> git.neil.brown.name Git - edlib.git/blob - tests
tile: don't use "Free", just "Close".
[edlib.git] / tests
1 #!/bin/bash
2
3 # This script handles management of testing.
4 # It supports the creation, verification, and updating
5 # of tests.
6 #
7 # Tests are run in the context of an edlib git tree
8 # with a given hash - currently the same hash for all tests.
9 # Each test provides a list of keystrokes and mouse actions
10 # together with occasions checksums of the current display.
11 # We run a test and record the output at the same time, then
12 # confirm that the recording matching the input test.
13
14 # Usage:
15 # ./tests new name
16 #     Record a new test and store it with given name.  Then re-run and confirm.
17 # ./tests run [name|failed]
18 #     If a name is given, just that test is run.  If no name is given, all tests
19 #     are run.  If any test fails, that failure is recorded.
20 #     If the special name 'failed' is given, then only failed tests are run.
21 # ./tests refresh
22 #     Run all tests and replace the test with the new log - expecting it to be different.
23
24 # Ensure configuration (e.g. of aspell) in $HOME doesn't affect result
25 export HOME=/tmp
26
27 # ensure timestamps don't change
28 export EDLIB_TESTING=1
29 # Don't check for autosaves
30 export EDLIB_AUTOSAVE=/no/autosave/dir
31
32 export TEST_DIR=/tmp/edlib-tests
33 export TEST_COMMIT=5875a3514f4e753fa183a2e19000e803384fae77
34
35 runtest()
36 {
37         case $1 in
38         new ) EDLIB_RECORD=$2 \
39                 screen -m sh -c 'stty rows 30 cols 80; ./edlib $TEST_DIR'
40         ;;
41         run ) EDLIB_RECORD=$2 EDLIB_REPLAY=$3 \
42                 screen -D -m sh -c 'stty rows 30 cols 80; ./edlib $TEST_DIR'
43         ;;
44         view ) EDLIB_RECORD=/dev/null EDLIB_REPLAY=$2 \
45                 screen -m sh -c 'stty rows 30 cols 80; ./edlib $TEST_DIR'
46         ;;
47         extend ) EDLIB_RECORD=$2 EDLIB_REPLAY=$3 \
48                 screen -m sh -c 'stty rows 30 cols 80; ./edlib $TEST_DIR'
49         ;;
50         esac
51 }
52
53 cmd=$0
54
55 setup_test_dir() {
56     if [ -d $TEST_DIR -a -d $TEST_DIR/.git ]; then
57         rm -rf $TEST_DIR/DOC/extra
58         (cd $TEST_DIR ; git reset > /dev/null --hard $TEST_COMMIT ;
59          git clean -fxd; git config core.abbrev 12 )
60     else
61         rm -rf $TEST_DIR
62         git clone . $TEST_DIR
63         (cd $TEST_DIR ; git reset --hard $TEST_COMMIT; git config core.abbrev 12 )
64     fi
65     rm -rf $TEST_DIR/DOC/extra
66     mkdir -p $TEST_DIR/DOC/extra
67     cp -a tests.d/[A-Za-z]* $TEST_DIR/DOC/extra
68 }
69
70 new_test() {
71     if [ $# -ne 1 ]; then
72         echo >&2 "$0: please provide name for new test"
73         exit 1
74     fi
75     t=$1
76     case $t in
77         [0-9][0-9]-* ) : OK ;;
78         * )  echo >&2 "Please name the test NN-something"
79              exit 1
80     esac
81     tt=tests.d/$t
82     if [ -e "$tt" ]; then
83         echo >&2 "$0: $tt already exists, please choose another name"
84         exit 1
85     fi
86     if ! touch "$tt"; then
87         echo >&2 "$0: Cannot create $tt - strange"
88         exit 1
89     fi
90     setup_test_dir
91     rm -f tests.d/.tmp
92     runtest new tests.d/.tmp
93     if ! grep -s '^Close' tests.d/.tmp > /dev/null ; then
94         echo >&2 "$0: test file not Closed - probably crash"
95         exit 1
96     fi
97     setup_test_dir
98     runtest run $tt tests.d/.tmp
99     diff -u tests.d/.tmp $tt
100     #rm tests.d/.tmp
101     exit 0
102 }
103
104 extend_test() {
105     if [ $# -ne 1 ]; then
106         echo >&2 "$0: please provide name for test to extend"
107         exit 1
108     fi
109     t=$1
110     tt=tests.d/$t
111     if [ ! -e "$tt" ]; then
112         echo >&2 "$0: test $t doesn't exist"
113         exit 1
114     fi
115     rm -f tests.d/.tmp tests.d/.tmp2
116     # find :C-X :C-C that closes the editor.
117     n=$(grep -n 'Key ":C-X"' $tt | tail -n 1| sed 's/:.*//')
118     if [ -z "$n" ]; then
119         echo >&2 "$0: cannot find tailing :C-X"
120         exit 1;
121     fi
122     head -n $[n-1] $tt > tests.d/.tmp2
123     runtest extend tests.d/.tmp tests.d/.tmp2
124     if ! grep -s '^Close' tests.d/.tmp > /dev/null ; then
125         echo >&2 "$0: test file not Closed - probably crash"
126         exit 1
127     fi
128     setup_test_dir
129     runtest run $tt tests.d/.tmp
130     #diff -u tests.d/.tmp $tt
131     rm -f tests.d/.tmp tests.d/.tmp2
132     exit 0
133 }
134
135 run_one_test() {
136     if [ ! -f "tests.d/$1" ]; then
137         echo "$cmd: test $1 doesn't exist"
138         exit_code=2
139         return
140     fi
141     setup_test_dir
142     echo -n "run $1 ..."
143     runtest run tests.d/.out tests.d/$1
144     if diff -u "tests.d/$1" tests.d/.out;
145     then echo "succeeded."
146     else echo "FAILED."
147         exit_code=1
148         line=`diff tests.d/$1 tests.d/.out | sed -n '1s/^\([0-9]*\).*/\1/p'`
149         echo "Use: ./tests view $1 $line"
150     fi
151     rm -f tests.d/.out
152 }
153
154 run_tests() {
155     if [ $# -gt 1 ]; then
156         echo >&2 "$0: please provide at most one test to run"
157         exit 1
158     fi
159     exit_code=0
160     if [ $# -eq 0 ]; then
161         for f in tests.d/[0-9]*; do
162             b=${f#tests.d/}
163             run_one_test "$b"
164             if [ $exit_code -gt 0 ]; then
165                 if [ "x$b" = "x02-grep" ]; then
166                     echo "02-grep failure NOT fatal"
167                     exit_code=0
168                 else
169                     break
170                 fi
171             fi
172         done
173     elif [ " $1" = " -a" ]; then
174         for f in tests.d/[0-9]*; do
175             b=${f#tests.d/}
176             run_one_test "$b"
177         done
178     else
179         run_one_test "$1"
180     fi
181     exit $exit_code
182 }
183
184 refresh_one() {
185     b=$1
186     f=tests.d/$b
187     if [ ! -f "$f" ]; then
188         echo >&2 "Cannot find test $f"
189         return
190     fi
191     setup_test_dir
192     # ensure we see delayed updates
193     export EDLIB_REPLAY_DELAY=500
194     #grep -v '^Display' $f > tests.d/.in
195     cat $f > tests.d/.in
196     runtest run tests.d/.out tests.d/.in
197     mv tests.d/.out $f
198     rm -f tests.d/.in
199 }
200
201 refresh_one_fast() {
202     b=$1
203     f=tests.d/$b
204     if [ ! -f "$f" ]; then
205         echo >&2 "Cannot find test $f"
206         return
207     fi
208     setup_test_dir
209     runtest run tests.d/.out "tests.d/$1"
210     mv tests.d/.out tests.d/"$1"
211 }
212
213 refresh_tests() {
214     case "$#:$1:$2" in
215         1:-f:* ) tst=""; cmd=refresh_one_fast ;;
216         1:* ) tst=$1; cmd=refresh_one ;;
217         2:*:-f ) tst=$1; cmd=refresh_one_fast ;;
218         0: ) tst=""; cmd=refresh_one ;;
219         * )
220            echo >&2 "$0: please provide at most one test to refresh"
221            exit 1
222     esac
223     if [ -z "$tst" ]; then
224         for f in tests.d/[0-9]*; do
225             $cmd ${f#tests.d/}
226         done
227     else
228         $cmd "$tst"
229     fi
230 }
231
232 view_test() {
233     if [ $# -lt 1 ]; then
234         echo >&2 "$0: please provide a test name to view"
235         exit 1
236     fi
237     t=$1
238     if [ ! -f tests.d/$1 ]; then
239         echo >&2 "Test $1 doesn't exist."
240         exit 1
241     fi
242     if [ $# -ge 2 -a " $2" != " -" ]; then
243         head -n $2 tests.d/$t > tests.d/.in
244     else
245         cat tests.d/$t > tests.d/.in
246     fi
247     delay=20
248     if [ $# -ge 3 ]; then
249         delay=$3
250     fi
251     export EDLIB_REPLAY_DELAY=$delay
252     runtest view tests.d/.in
253     rm -f tests.d/.in
254 }
255
256
257 cmd=$1
258 shift
259 case $cmd in
260         new) new_test ${1+"$@"};;
261         run) setup_test_dir; run_tests ${1+"$@"};;
262         refresh) setup_test_dir; refresh_tests ${1+"$@"};;
263         view ) setup_test_dir; view_test ${1+"$@"};;
264         extend ) setup_test_dir; extend_test ${1+"$@"};;
265         * )
266                 echo >&2 "Usage: $0 new test-name"
267                 echo >&2 "       $0 run [test-name|-a]"
268                 echo >&2 "       $0 refresh [test-name]"
269                 echo >&2 "       $0 view test-name [lines [delay-ms]]"
270                 echo >&2 "       $0 extend test-name"
271                 exit 1;
272 esac
273 exit 0