Merge from vendor branch FILE:
[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.8 2005/03/02 10:55:37 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 # Check if the test result is the same as the expected result.
26 # First remove the RCS Id tag, check for differences in the files.
27 # We need this function because the CVS repository will not allow
28 # us to check in a file without a DragonFly RCS Id tag.
29 #
30 # $1    Input file
31 #
32 hack_cmp()
33 {
34         local EXPECTED RESULT
35         EXPECTED="expected.$1"
36         RESULT=$WORK_BASE/$1
37
38         if [ -f $EXPECTED ]; then
39                 cat $EXPECTED |\
40                         sed -e '1d' |\
41                         diff -q - $RESULT \
42                         1> /dev/null 2> /dev/null
43                 return $?
44         else
45                 return 1        # FAIL
46         fi
47 }
48
49 #
50 # Check if the test result is the same as the expected result.
51 # First remove the RCS Id tag, print the differences in the files.
52 # We need this function because the CVS repository will not allow
53 # us to check in a file without a DragonFly RCS Id tag.
54 #
55 # $1    Input file
56 #
57 hack_diff()
58 {
59         local EXPECTED RESULT
60         EXPECTED="expected.$1"
61         RESULT=$WORK_BASE/$1
62
63         echo diff $EXPECTED $RESULT
64         if [ -f $EXPECTED ]; then
65                 cat $EXPECTED |\
66                         sed -e '1d' |\
67                         diff - $RESULT
68                 return $?
69         else
70                 return 1        # FAIL
71         fi
72 }
73
74 #
75 # Default run_test() function.  It should be replace by the
76 # user specified regression test.
77 #
78 # Both the variables SRC_BASE WORK_BASE are available.
79 #
80 setup_test()
81 {
82         echo "Missing setup_test() function in $SRC_BASE/test.sh"
83 }
84
85 #
86 # Default run_test() function.  It can be replace by the
87 # user specified regression test.
88 #
89 # Both the variables SRC_BASE WORK_BASE are available.
90 #
91 # Note: this function executes from a subshell.
92 #
93 run_test()
94 (
95         cd $WORK_BASE;
96         $MAKE_PROG 1> stdout 2> stderr
97         echo $? > status
98 )
99
100 #
101 # Execute cmd in subdirectory. 
102 #
103 eval_subdir_cmd()
104 {
105         local SRC_BASE WORK_BASE
106
107         if [ ! -d $1 ]; then
108                 echo "Test directory '$1' missing in directory '$SRC_BASE'"
109                 return
110         fi
111
112         if [ ! -f $1/test.sh ]; then
113                 echo "Test script missing in directory '$SRC_BASE/$1'"
114                 return
115         fi
116
117         SRC_BASE=${SRC_BASE}/$1
118         WORK_BASE=${WORK_BASE}/$1
119         (cd $1; sh ./test.sh $2)
120 }
121
122 #
123 # Note: Uses global variable $DIR which might be assigned by
124 #       the script which sourced this file.
125 #
126 eval_cmd()
127 {
128         if [ "${DIR}" ]; then
129                 #
130                 # When there are subdirectories defined, recurse
131                 # down into them if the cmd is valid.
132                 #
133                 case $1 in
134                 clean|compare|desc|diff|run|test|update|run_output)
135                         for d in $DIR; do
136                                 eval_subdir_cmd $d $1
137                         done
138                         ;;
139                 *)
140                         print_usage;
141                         ;;
142                 esac
143         else
144                 #
145                 #
146                 #
147                 case $1 in
148                 clean)
149                         rm -f Makefile
150                         rm -f stdout
151                         rm -f stderr
152                         rm -f status
153                         ;;
154                 compare)
155                         hack_cmp stdout || FAIL="stdout $FAIL"
156                         hack_cmp stderr || FAIL="stderr $FAIL"
157                         hack_cmp status || FAIL="status $FAIL"
158
159                         if [ ! -z "$FAIL" ]; then
160                                 FAIL=`echo $FAIL`
161                                 echo "$SRC_BASE: Test failed {$FAIL}"
162                         fi
163                         ;;
164                 desc)
165                         echo -n "$SRC_BASE: "
166                         desc_test
167                         ;;
168                 diff)
169                         sh $0 test
170                         echo "------------------------"
171                         echo "- $SRC_BASE"
172                         echo "------------------------"
173                         hack_diff stdout
174                         hack_diff stderr
175                         hack_diff status
176                         ;;
177                 run)
178                         sh $0 test
179                         sh $0 compare
180                         sh $0 clean
181                         ;;
182                 run_output)
183                         sh $0 test
184                         sh $0 compare
185                         echo "------------------------"
186                         echo "- stdout"
187                         echo "------------------------"
188                         cat $WORK_BASE/stdout
189                         echo "------------------------"
190                         echo "- stderr"
191                         echo "------------------------"
192                         cat $WORK_BASE/stderr
193                         echo "------------------------"
194                         echo "- status"
195                         echo "------------------------"
196                         echo -n "status ="
197                         cat $WORK_BASE/status
198                         sh $0 clean
199                         ;;
200                 test)
201                         [ -d $WORK_BASE ] || mkdir -p $WORK_BASE
202                         setup_test
203                         run_test
204                         ;;
205                 update)
206                         sh $0 test
207                         #
208                         # Preserve the RCS id tag at top of file.
209                         # Create initial tag if the file didn't
210                         # exist.
211                         #
212                         if [ -f "."$SRC_BASE/expected.stdout ]; then
213                                 head -n 1 "."$SRC_BASE/expected.stdout
214                         else
215                                 echo $IDTAG
216                         fi > $WORK_BASE/expected.stdout
217
218                         if [ -f "."$SRC_BASE/expected.stderr ]; then
219                                 head -n 1 "."$SRC_BASE/expected.stderr
220                         else
221                                 echo $IDTAG
222                         fi > $WORK_BASE/expected.stderr
223
224                         if [ -f "."$SRC_BASE/expected.status ]; then
225                                 head -n 1 "."$SRC_BASE/expected.status
226                         else
227                                 echo $IDTAG
228                         fi > $WORK_BASE/expected.status
229
230                         cat $WORK_BASE/stdout >> $WORK_BASE/expected.stdout
231                         cat $WORK_BASE/stderr >> $WORK_BASE/expected.stderr
232                         cat $WORK_BASE/status >> $WORK_BASE/expected.status
233                         mv $WORK_BASE/expected.stdout "."$SRC_BASE/expected.stdout
234                         mv $WORK_BASE/expected.stderr "."$SRC_BASE/expected.stderr
235                         mv $WORK_BASE/expected.status "."$SRC_BASE/expected.status
236                         ;;
237                 *)
238                         print_usage
239                         ;;
240                 esac
241         fi
242 }
243
244 #
245 # Parse command line arguments.
246 #
247 args=`getopt m:w:v $*`
248 if [ $? != 0 ]; then
249         echo 'Usage: ...'
250         exit 2
251 fi
252 set -- $args
253 for i; do
254         case "$i" in
255         -m)
256                 MAKE_PROG="$2"
257                 shift
258                 shift
259                 ;;
260         -w)
261                 WORK_BASE="$2"
262                 shift
263                 shift
264                 ;;
265         -v)
266                 VERBOSE=1
267                 shift
268                 ;;
269         --)
270                 shift
271                 break
272                 ;;
273         esac
274 done
275
276 SRC_BASE=${SRC_BASE:-""}
277 WORK_BASE=${WORK_BASE:-"/tmp/$USER.make.test"}
278 MAKE_PROG=${MAKE_PROG:-/usr/bin/make}
279
280 export MAKE_PROG
281 export VERBOSE
282 export SRC_BASE
283 export WORK_BASE