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