t: colorize results on tty output
[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*.in}
30 do
31         test_count=$((test_count+1))
32         file="${candidate%.in}"
33         test -r "$file.in" || continue
34
35         name="$(echo ${file#*-} | tr _ \ )"
36         cmd="barcat $file.in"
37         case "$name" in *\ -*) cmd="$cmd -${name#* -}";; esac
38
39         if test -n "$regenerate"
40         then
41                 if test -e $file.sh
42                 then
43                         echo "ok $test_count # skip $file.out"
44                         continue
45                 fi
46                 $cmd >$file.out 2>&1
47         else
48                 if test -e $file.sh;  then $cmd 2>&1 | ./$file.sh; fi &&
49                 if test -e $file.out; then $cmd 2>&1 | diffcmd "$file.out" -; fi
50         fi
51
52         if test 0 != $?
53         then
54                 fail_count=$((fail_count+1))
55                 color 1\;31
56                 printf 'not '
57         fi
58         echo "ok $test_count - $name"
59         color 0
60 done
61
62 if test $fail_count = 0
63 then
64         color 32
65         echo "# passed all $test_count test(s)"
66 else
67         color 31
68         echo "# failed $fail_count among $test_count test(s)"
69         fail_count=1  # exit code
70 fi
71
72 color 0\;36
73 echo "1..$test_count"
74 color 0
75 exit $fail_count