Improve report output.
[dragonfly.git] / usr.bin / make / tests / common.sh
1 #!/bin/sh
2 #
3 # Common code used run regression tests for usr.bin/make.
4 #
5 # $DragonFly: src/usr.bin/make/tests/common.sh,v 1.3 2005/02/26 11:51:54 okumoto Exp $
6
7 IDTAG='$'DragonFly'$'
8
9 #
10 # Output usage messsage.
11 #
12 print_usage()
13 {
14         echo "Usage: $0 command"
15         echo "  clean   - remove temp files"
16         echo "  compare - compare result of test to expected"
17         echo "  desc    - print description of test"
18         echo "  diff    - print diffs between results and expected"
19         echo "  run     - run the {test, compare, clean}"
20         echo "  test    - run test case"
21         echo "  update  - update the expected with current results"
22 }
23
24 #
25 # We can't check a file into CVS without a DragonFly RCS Id tag, so
26 # we need to remove it before we compare. 
27 #
28 hack_cmp()
29 {
30         if [ -f $1 ]; then
31                 cat $1 |\
32                         sed -e '1d' |\
33                         diff -q - $2 \
34                         1> /dev/null 2> /dev/null
35                 return $?
36         else
37                 return 1        # FAIL
38         fi
39 }
40
41 #
42 # We can't check a file into CVS without a DragonFly RCS Id tag, so
43 # we need to remove it before we compare. 
44 #
45 hack_diff()
46 {
47         echo diff $1 $2
48         if [ -f $1 ]; then
49                 cat $1 |\
50                         sed -e '1d' |\
51                         diff - $2
52                 return $?
53         else
54                 return 1        # FAIL
55         fi
56 }
57
58 #
59 # Default run_test() function.  It should be replace by the
60 # user specified regression test.
61 #
62 run_test()
63 {
64         echo "Missing run_test() function in $START_BASE/test.sh"
65 }
66
67 #
68 # Execute cmd in subdirectory. 
69 #
70 eval_subdir_cmd()
71 {
72         local START_BASE
73
74         if [ ! -d $1 ]; then
75                 echo "Test directory '$1' missing in directory '$START_BASE'"
76                 return
77         fi
78
79         if [ ! -f $1/test.sh ]; then
80                 echo "Test script missing in directory '$START_BASE/$1'"
81                 return
82         fi
83
84         START_BASE=${START_BASE}/$1
85         (cd $1; sh ./test.sh $2)
86 }
87
88 #
89 # Note: Uses global variable $DIR which might be assigned by
90 #       the script which sourced this file.
91 #
92 eval_cmd()
93 {
94         if [ "${DIR}" ]; then
95                 #
96                 # When there are subdirectories defined, recurse
97                 # down into them if the cmd is valid.
98                 #
99                 case $1 in
100                 clean|compare|desc|diff|run|test|update)
101                         for d in $DIR; do
102                                 eval_subdir_cmd $d $1
103                         done
104                         ;;
105                 *)
106                         print_usage;
107                         ;;
108                 esac
109         else
110                 #
111                 #
112                 #
113                 case $1 in
114                 clean)
115                         rm -f Makefile
116                         rm -f stdout
117                         rm -f stderr
118                         rm -f status
119                         ;;
120                 compare)
121                         hack_cmp expected.stdout stdout || FAIL="stdout, "$FAIL
122                         hack_cmp expected.stderr stderr || FAIL="stderr, "$FAIL
123                         hack_cmp expected.status status || FAIL="status, "$FAIL
124
125                         if [ -z "$FAIL" ]; then
126                                 :
127                         else
128                                 echo "$START_BASE: Test failed {$FAIL}" |\
129                                          sed -e 's/, }$/}/'
130                         fi
131                         ;;
132                 desc)
133                         echo -n "$START_BASE: "
134                         desc_test
135                         ;;
136                 diff)
137                         sh $0 test
138                         echo "------------------------"
139                         echo "- $START_BASE"
140                         echo "------------------------"
141                         hack_diff expected.stdout stdout
142                         hack_diff expected.stderr stderr
143                         hack_diff expected.status status
144                         ;;
145                 run)
146                         sh $0 test
147                         sh $0 compare
148                         sh $0 clean
149                         ;;
150                 test)
151                         run_test
152                         ;;
153                 update)
154                         sh $0 test
155                         # Create expected files if they don't exist.
156                         [ -f expected.stdout ] || echo $IDTAG > expected.stdout
157                         [ -f expected.stderr ] || echo $IDTAG > expected.stderr
158                         [ -f expected.status ] || echo $IDTAG > expected.status
159
160                         # Remove previous contents of expected files, and
161                         # replace them with the new contents.
162                         sed -e '2,$d' < expected.stdout > new.expected.stdout
163                         sed -e '2,$d' < expected.stderr > new.expected.stderr
164                         sed -e '2,$d' < expected.status > new.expected.status
165                         cat stdout >> new.expected.stdout
166                         cat stderr >> new.expected.stderr
167                         cat status >> new.expected.status
168                         mv new.expected.stdout expected.stdout
169                         mv new.expected.stderr expected.stderr
170                         mv new.expected.status expected.status
171                         ;;
172                 *)
173                         print_usage
174                         ;;
175                 esac
176         fi
177 }
178
179 #
180 # Parse command line arguments.
181 #
182 args=`getopt m:v $*`
183 if [ $? != 0 ]; then
184         echo 'Usage: ...'
185         exit 2
186 fi
187 set -- $args
188 for i; do
189         case "$i" in
190         -m)
191                 MAKE="$2"
192                 shift
193                 shift
194                 ;;
195         -v)
196                 VERBOSE=1
197                 shift
198                 ;;
199         --)
200                 shift
201                 break
202                 ;;
203         esac
204 done
205
206 START_BASE=${START_BASE:-.}
207 MAKE=${MAKE:-/usr/bin/make}
208
209 export MAKE
210 export VERBOSE
211 export START_BASE