]> git.neil.brown.name Git - edlib.git/blob - tests
TODO: clean out done items.
[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 export ASPELL_CONF="personal $TEST_DIR/DOC/extra/aspell.en.pws;repl $TEST_DIR/DOC/extra/aspell.en.prepl"
36
37 runtest()
38 {
39         case $1 in
40         new ) EDLIB_RECORD=$2 \
41                 screen -m sh -c 'stty rows 30 cols 80; ./edlib $TEST_DIR'
42         ;;
43         run ) EDLIB_RECORD=$2 EDLIB_REPLAY=$3 \
44                 screen -D -m sh -c 'stty rows 30 cols 80; ./edlib $TEST_DIR'
45         ;;
46         view ) EDLIB_RECORD=/dev/null EDLIB_REPLAY=$2 \
47                 screen -m sh -c 'stty rows 30 cols 80; ./edlib $TEST_DIR'
48         ;;
49         extend ) EDLIB_RECORD=$2 EDLIB_REPLAY=$3 \
50                 screen -m sh -c 'stty rows 30 cols 80; ./edlib $TEST_DIR'
51         ;;
52         esac
53 }
54
55 cmd=$0
56
57 setup_test_dir() {
58     if [ -d $TEST_DIR -a -d $TEST_DIR/.git ]; then
59         rm -rf $TEST_DIR/DOC/extra
60         (cd $TEST_DIR ; git reset > /dev/null --hard $TEST_COMMIT ;
61          git clean -fxd; git config core.abbrev 12 )
62     else
63         rm -rf $TEST_DIR
64         git clone . $TEST_DIR
65         (cd $TEST_DIR ; git reset --hard $TEST_COMMIT; git config core.abbrev 12 )
66     fi
67     rm -rf $TEST_DIR/DOC/extra
68     mkdir -p $TEST_DIR/DOC/extra
69     cp -a tests.d/[A-Za-z]* $TEST_DIR/DOC/extra
70 }
71
72 new_test() {
73     if [ $# -ne 1 ]; then
74         echo >&2 "$0: please provide name for new test"
75         exit 1
76     fi
77     t=$1
78     case $t in
79         [0-9][0-9]-* ) : OK ;;
80         * )  echo >&2 "Please name the test NN-something"
81              exit 1
82     esac
83     tt=tests.d/$t
84     if [ -e "$tt" ]; then
85         echo >&2 "$0: $tt already exists, please choose another name"
86         exit 1
87     fi
88     if ! touch "$tt"; then
89         echo >&2 "$0: Cannot create $tt - strange"
90         exit 1
91     fi
92     setup_test_dir
93     rm -f tests.d/.tmp
94     runtest new tests.d/.tmp
95     if ! grep -s '^Close' tests.d/.tmp > /dev/null ; then
96         echo >&2 "$0: test file not Closed - probably crash"
97         exit 1
98     fi
99     setup_test_dir
100     runtest run $tt tests.d/.tmp
101     diff -u tests.d/.tmp $tt
102     #rm tests.d/.tmp
103     exit 0
104 }
105
106 extend_test() {
107     if [ $# -ne 1 ]; then
108         echo >&2 "$0: please provide name for test to extend"
109         exit 1
110     fi
111     t=$1
112     tt=tests.d/$t
113     if [ ! -e "$tt" ]; then
114         echo >&2 "$0: test $t doesn't exist"
115         exit 1
116     fi
117     rm -f tests.d/.tmp tests.d/.tmp2
118     # find :C-X :C-C that closes the editor.
119     n=$(grep -n 'Key ":C-X"' $tt | tail -n 1| sed 's/:.*//')
120     if [ -z "$n" ]; then
121         echo >&2 "$0: cannot find tailing :C-X"
122         exit 1;
123     fi
124     head -n $[n-1] $tt > tests.d/.tmp2
125     runtest extend tests.d/.tmp tests.d/.tmp2
126     if ! grep -s '^Close' tests.d/.tmp > /dev/null ; then
127         echo >&2 "$0: test file not Closed - probably crash"
128         exit 1
129     fi
130     setup_test_dir
131     runtest run $tt tests.d/.tmp
132     #diff -u tests.d/.tmp $tt
133     rm -f tests.d/.tmp tests.d/.tmp2
134     exit 0
135 }
136
137 run_one_test() {
138     if [ ! -f "tests.d/$1" ]; then
139         echo "$cmd: test $1 doesn't exist"
140         exit_code=2
141         return
142     fi
143     setup_test_dir
144     echo -n "run $1 ..."
145     runtest run tests.d/.out tests.d/$1
146     if diff -u "tests.d/$1" tests.d/.out;
147     then echo "succeeded."
148     else echo "FAILED."
149         exit_code=1
150         line=`diff tests.d/$1 tests.d/.out | sed -n '1s/^\([0-9]*\).*/\1/p'`
151         echo "Use: ./tests view $1 $line"
152     fi
153     rm -f tests.d/.out
154 }
155
156 run_tests() {
157     if [ $# -gt 1 ]; then
158         echo >&2 "$0: please provide at most one test to run"
159         exit 1
160     fi
161     exit_code=0
162     if [ $# -eq 0 ]; then
163         for f in tests.d/[0-9]*; do
164             b=${f#tests.d/}
165             run_one_test "$b"
166             if [ $exit_code -gt 0 ]; then
167                 if [ "x$b" = "x02-grep" ]; then
168                     echo "02-grep failure NOT fatal"
169                     exit_code=0
170                 else
171                     break
172                 fi
173             fi
174         done
175     elif [ " $1" = " -a" ]; then
176         for f in tests.d/[0-9]*; do
177             b=${f#tests.d/}
178             run_one_test "$b"
179         done
180     else
181         run_one_test "$1"
182     fi
183     exit $exit_code
184 }
185
186 refresh_one() {
187     b=$1
188     f=tests.d/$b
189     if [ ! -f "$f" ]; then
190         echo >&2 "Cannot find test $f"
191         return
192     fi
193     setup_test_dir
194     # ensure we see delayed updates
195     export EDLIB_REPLAY_DELAY=500
196     #grep -v '^Display' $f > tests.d/.in
197     cat $f > tests.d/.in
198     runtest run tests.d/.out tests.d/.in
199     mv tests.d/.out $f
200     rm -f tests.d/.in
201 }
202
203 refresh_one_fast() {
204     b=$1
205     f=tests.d/$b
206     if [ ! -f "$f" ]; then
207         echo >&2 "Cannot find test $f"
208         return
209     fi
210     setup_test_dir
211     runtest run tests.d/.out "tests.d/$1"
212     mv tests.d/.out tests.d/"$1"
213 }
214
215 refresh_tests() {
216     case "$#:$1:$2" in
217         1:-f:* ) tst=""; cmd=refresh_one_fast ;;
218         1:* ) tst=$1; cmd=refresh_one ;;
219         2:*:-f ) tst=$1; cmd=refresh_one_fast ;;
220         0: ) tst=""; cmd=refresh_one ;;
221         * )
222            echo >&2 "$0: please provide at most one test to refresh"
223            exit 1
224     esac
225     if [ -z "$tst" ]; then
226         for f in tests.d/[0-9]*; do
227             $cmd ${f#tests.d/}
228         done
229     else
230         $cmd "$tst"
231     fi
232 }
233
234 view_test() {
235     if [ $# -lt 1 ]; then
236         echo >&2 "$0: please provide a test name to view"
237         exit 1
238     fi
239     t=$1
240     if [ ! -f tests.d/$1 ]; then
241         echo >&2 "Test $1 doesn't exist."
242         exit 1
243     fi
244     if [ $# -ge 2 -a " $2" != " -" ]; then
245         head -n $2 tests.d/$t > tests.d/.in
246     else
247         cat tests.d/$t > tests.d/.in
248     fi
249     delay=20
250     if [ $# -ge 3 ]; then
251         delay=$3
252     fi
253     export EDLIB_REPLAY_DELAY=$delay
254     runtest view tests.d/.in
255     rm -f tests.d/.in
256 }
257
258
259 cmd=$1
260 shift
261 case $cmd in
262         new) new_test ${1+"$@"};;
263         run) setup_test_dir; run_tests ${1+"$@"};;
264         refresh) setup_test_dir; refresh_tests ${1+"$@"};;
265         view ) setup_test_dir; view_test ${1+"$@"};;
266         extend ) setup_test_dir; extend_test ${1+"$@"};;
267         * )
268                 echo >&2 "Usage: $0 new test-name"
269                 echo >&2 "       $0 run [test-name|-a]"
270                 echo >&2 "       $0 refresh [test-name]"
271                 echo >&2 "       $0 view test-name [lines [delay-ms]]"
272                 echo >&2 "       $0 extend test-name"
273                 exit 1;
274 esac
275 exit 0