make(1) tests: Messing with the CVS ID is no longer needed.
[dragonfly.git] / usr.bin / make / tests / common.sh
CommitLineData
512280a4
MO
1#!/bin/sh
2#
3# Common code used run regression tests for usr.bin/make.
4#
39b3afaf 5# $DragonFly: src/usr.bin/make/tests/common.sh,v 1.8 2005/03/02 10:55:37 okumoto Exp $
512280a4 6
512280a4 7#
b1d7a1dd
MO
8# Output usage messsage.
9#
10print_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#
d5436572
MO
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.
512280a4 27#
622a6468
MO
28# $1 Input file
29#
512280a4
MO
30hack_cmp()
31{
622a6468
MO
32 local EXPECTED RESULT
33 EXPECTED="expected.$1"
c36efc2f 34 RESULT=$WORK_BASE/$1
622a6468
MO
35
36 if [ -f $EXPECTED ]; then
37 cat $EXPECTED |\
512280a4 38 sed -e '1d' |\
622a6468 39 diff -q - $RESULT \
512280a4
MO
40 1> /dev/null 2> /dev/null
41 return $?
42 else
43 return 1 # FAIL
44 fi
45}
46
47#
d5436572
MO
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
512280a4
MO
54#
55hack_diff()
56{
622a6468
MO
57 local EXPECTED RESULT
58 EXPECTED="expected.$1"
c36efc2f 59 RESULT=$WORK_BASE/$1
622a6468
MO
60
61 echo diff $EXPECTED $RESULT
62 if [ -f $EXPECTED ]; then
63 cat $EXPECTED |\
512280a4 64 sed -e '1d' |\
622a6468 65 diff - $RESULT
512280a4
MO
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#
c36efc2f
MO
76# Both the variables SRC_BASE WORK_BASE are available.
77#
78setup_test()
512280a4 79{
c36efc2f 80 echo "Missing setup_test() function in $SRC_BASE/test.sh"
512280a4
MO
81}
82
83#
c36efc2f
MO
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#
91run_test()
92(
93 cd $WORK_BASE;
d5436572 94 $MAKE_PROG 1> stdout 2> stderr
c36efc2f
MO
95 echo $? > status
96)
97
98#
512280a4
MO
99# Execute cmd in subdirectory.
100#
101eval_subdir_cmd()
102{
c36efc2f 103 local SRC_BASE WORK_BASE
512280a4
MO
104
105 if [ ! -d $1 ]; then
c36efc2f 106 echo "Test directory '$1' missing in directory '$SRC_BASE'"
512280a4
MO
107 return
108 fi
109
110 if [ ! -f $1/test.sh ]; then
c36efc2f 111 echo "Test script missing in directory '$SRC_BASE/$1'"
512280a4
MO
112 return
113 fi
114
c36efc2f
MO
115 SRC_BASE=${SRC_BASE}/$1
116 WORK_BASE=${WORK_BASE}/$1
512280a4
MO
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#
124eval_cmd()
125{
126 if [ "${DIR}" ]; then
b1d7a1dd
MO
127 #
128 # When there are subdirectories defined, recurse
129 # down into them if the cmd is valid.
130 #
512280a4 131 case $1 in
39b3afaf 132 clean|compare|desc|diff|run|test|update|run_output)
512280a4
MO
133 for d in $DIR; do
134 eval_subdir_cmd $d $1
135 done
136 ;;
137 *)
b1d7a1dd 138 print_usage;
512280a4
MO
139 ;;
140 esac
141 else
b1d7a1dd
MO
142 #
143 #
144 #
512280a4 145 case $1 in
b1d7a1dd
MO
146 clean)
147 rm -f Makefile
148 rm -f stdout
149 rm -f stderr
150 rm -f status
512280a4
MO
151 ;;
152 compare)
622a6468
MO
153 hack_cmp stdout || FAIL="stdout $FAIL"
154 hack_cmp stderr || FAIL="stderr $FAIL"
155 hack_cmp status || FAIL="status $FAIL"
512280a4 156
622a6468 157 if [ ! -z "$FAIL" ]; then
483ba07b 158 FAIL=`echo $FAIL`
c36efc2f 159 echo "$SRC_BASE: Test failed {$FAIL}"
512280a4
MO
160 fi
161 ;;
b1d7a1dd 162 desc)
c36efc2f 163 echo -n "$SRC_BASE: "
b1d7a1dd
MO
164 desc_test
165 ;;
512280a4
MO
166 diff)
167 sh $0 test
168 echo "------------------------"
c36efc2f 169 echo "- $SRC_BASE"
512280a4 170 echo "------------------------"
622a6468
MO
171 hack_diff stdout
172 hack_diff stderr
173 hack_diff status
512280a4 174 ;;
b1d7a1dd
MO
175 run)
176 sh $0 test
177 sh $0 compare
178 sh $0 clean
179 ;;
39b3afaf
MO
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 ;;
b1d7a1dd 198 test)
c36efc2f
MO
199 [ -d $WORK_BASE ] || mkdir -p $WORK_BASE
200 setup_test
b1d7a1dd
MO
201 run_test
202 ;;
512280a4
MO
203 update)
204 sh $0 test
f27c2556
SW
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
512280a4 208 ;;
512280a4 209 *)
b1d7a1dd 210 print_usage
512280a4
MO
211 ;;
212 esac
213 fi
214}
215
216#
217# Parse command line arguments.
218#
d5436572 219args=`getopt m:w:v $*`
512280a4
MO
220if [ $? != 0 ]; then
221 echo 'Usage: ...'
222 exit 2
223fi
224set -- $args
225for i; do
226 case "$i" in
227 -m)
d5436572
MO
228 MAKE_PROG="$2"
229 shift
230 shift
231 ;;
232 -w)
233 WORK_BASE="$2"
512280a4
MO
234 shift
235 shift
236 ;;
237 -v)
238 VERBOSE=1
239 shift
240 ;;
241 --)
242 shift
243 break
244 ;;
245 esac
246done
247
c36efc2f
MO
248SRC_BASE=${SRC_BASE:-""}
249WORK_BASE=${WORK_BASE:-"/tmp/$USER.make.test"}
d5436572 250MAKE_PROG=${MAKE_PROG:-/usr/bin/make}
512280a4 251
d5436572 252export MAKE_PROG
512280a4 253export VERBOSE
c36efc2f
MO
254export SRC_BASE
255export WORK_BASE