t: execute multiple arguments in shell wrapper
[barcat.git] / t / regress.t
1 #!/bin/sh
2
3 cd "${0%/*}" || exit 1
4
5 test_count=0
6 fail_count=0
7
8 COLUMNS=40
9 colorize=
10 test -t 1 && colorize=1
11 color () {
12         test -n "$colorize" &&
13         printf '\e[%sm' $@
14 }
15 regenerate=
16 diffcmd () {
17         comm --nocheck-order --output-delimiter=::: -3 $@ |
18         perl -pe"END{exit !!\$.} s/^:::/$(color 31)>/ || s/^/$(color 32)</"
19 }
20
21 for option in "$@"
22 do
23         case "$option" in
24         -G) regenerate=1 && shift;;
25         -*) echo "Usage: $0 [-G] [<files>...]"; exit 64;;
26         esac
27 done
28
29 for candidate in ${@:-t*.out}
30 do
31         test_count=$((test_count+1))
32         file="${candidate%.out}"
33         input="${file%%_-*}.in"
34         name="$(echo ${file#*-} | tr _ \ )"
35
36         set -- barcat
37         [ -r "$input" ] && set -- "$@" "$input"
38         case "$name" in *\ -*) set -- "$@" -"${name#* -}";; esac
39         case "$name" in
40                 *' ?'*) set -- sh -c "\$0 \$@ | test \$\? = ${name#* \?}" "$@";;
41                 *' |'*) set -- sh -c "\$0 \$@ | ${name#* |}" "$@";;
42                 *)      set -- "$1" $2 $3
43         esac
44
45         if test -n "$regenerate"
46         then
47                 if test -e $file.sh
48                 then
49                         echo "ok $test_count # skip $file.out"
50                         continue
51                 fi
52                 "$@" >$file.out 2>&1
53         elif test -e "$file.out"
54         then
55                 "$@" 2>&1 | diffcmd "$file.out" -
56         else
57                 color 33
58                 echo "not ok $test_count - $name # TODO"
59                 color 0
60                 continue
61         fi
62
63         if test 0 != $?
64         then
65                 fail_count=$((fail_count+1))
66                 color 1\;31
67                 printf 'not '
68         fi
69         echo "ok $test_count - $name"
70         color 0
71 done
72
73 if test $fail_count = 0
74 then
75         color 32
76         echo "# passed all $test_count test(s)"
77 else
78         color 31
79         echo "# failed $fail_count among $test_count test(s)"
80         fail_count=1  # exit code
81 fi
82
83 color 0\;36
84 echo "1..$test_count"
85 color 0
86 exit $fail_count