Changed regression tests to write to /tmp instead of in the src dir.
[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.6 2005/03/01 22:42:28 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 results are the same as the expected. 
26 # We can't check a file into CVS without a DragonFly RCS Id tag, so
27 # we need to remove it before we compare. 
28 #
29 # $1    Input file
30 #
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 # We can't check a file into CVS without a DragonFly RCS Id tag, so
51 # we need to remove it before we compare. 
52 #
53 hack_diff()
54 {
55         local EXPECTED RESULT
56         EXPECTED="expected.$1"
57         RESULT=$WORK_BASE/$1
58
59         echo diff $EXPECTED $RESULT
60         if [ -f $EXPECTED ]; then
61                 cat $EXPECTED |\
62                         sed -e '1d' |\
63                         diff - $RESULT
64                 return $?
65         else
66                 return 1        # FAIL
67         fi
68 }
69
70 #
71 # Default run_test() function.  It should be replace by the
72 # user specified regression test.
73 #
74 # Both the variables SRC_BASE WORK_BASE are available.
75 #
76 setup_test()
77 {
78         echo "Missing setup_test() function in $SRC_BASE/test.sh"
79 }
80
81 #
82 # Default run_test() function.  It can be replace by the
83 # user specified regression test.
84 #
85 # Both the variables SRC_BASE WORK_BASE are available.
86 #
87 # Note: this function executes from a subshell.
88 #
89 run_test()
90 (
91         cd $WORK_BASE;
92         $MAKE 1> stdout 2> stderr
93         echo $? > status
94 )
95
96 #
97 # Execute cmd in subdirectory. 
98 #
99 eval_subdir_cmd()
100 {
101         local SRC_BASE WORK_BASE
102
103         if [ ! -d $1 ]; then
104                 echo "Test directory '$1' missing in directory '$SRC_BASE'"
105                 return
106         fi
107
108         if [ ! -f $1/test.sh ]; then
109                 echo "Test script missing in directory '$SRC_BASE/$1'"
110                 return
111         fi
112
113         SRC_BASE=${SRC_BASE}/$1
114         WORK_BASE=${WORK_BASE}/$1
115         (cd $1; sh ./test.sh $2)
116 }
117
118 #
119 # Note: Uses global variable $DIR which might be assigned by
120 #       the script which sourced this file.
121 #
122 eval_cmd()
123 {
124         if [ "${DIR}" ]; then
125                 #
126                 # When there are subdirectories defined, recurse
127                 # down into them if the cmd is valid.
128                 #
129                 case $1 in
130                 clean|compare|desc|diff|run|test|update)
131                         for d in $DIR; do
132                                 eval_subdir_cmd $d $1
133                         done
134                         ;;
135                 *)
136                         print_usage;
137                         ;;
138                 esac
139         else
140                 #
141                 #
142                 #
143                 case $1 in
144                 clean)
145                         rm -f Makefile
146                         rm -f stdout
147                         rm -f stderr
148                         rm -f status
149                         ;;
150                 compare)
151                         hack_cmp stdout || FAIL="stdout $FAIL"
152                         hack_cmp stderr || FAIL="stderr $FAIL"
153                         hack_cmp status || FAIL="status $FAIL"
154
155                         if [ ! -z "$FAIL" ]; then
156                                 FAIL=`echo $FAIL`
157                                 echo "$SRC_BASE: Test failed {$FAIL}"
158                         fi
159                         ;;
160                 desc)
161                         echo -n "$SRC_BASE: "
162                         desc_test
163                         ;;
164                 diff)
165                         sh $0 test
166                         echo "------------------------"
167                         echo "- $SRC_BASE"
168                         echo "------------------------"
169                         hack_diff stdout
170                         hack_diff stderr
171                         hack_diff status
172                         ;;
173                 run)
174                         sh $0 test
175                         sh $0 compare
176                         sh $0 clean
177                         ;;
178                 test)
179                         [ -d $WORK_BASE ] || mkdir -p $WORK_BASE
180                         setup_test
181                         run_test
182                         ;;
183                 update)
184                         sh $0 test
185                         # Create expected files if they don't exist.
186                         [ -f expected.stdout ] || echo $IDTAG > expected.stdout
187                         [ -f expected.stderr ] || echo $IDTAG > expected.stderr
188                         [ -f expected.status ] || echo $IDTAG > expected.status
189
190                         # Remove previous contents of expected files, and
191                         # replace them with the new contents.
192                         sed -e '2,$d' < expected.stdout > new.expected.stdout
193                         sed -e '2,$d' < expected.stderr > new.expected.stderr
194                         sed -e '2,$d' < expected.status > new.expected.status
195                         cat stdout >> new.expected.stdout
196                         cat stderr >> new.expected.stderr
197                         cat status >> new.expected.status
198                         mv new.expected.stdout expected.stdout
199                         mv new.expected.stderr expected.stderr
200                         mv new.expected.status expected.status
201                         ;;
202                 *)
203                         print_usage
204                         ;;
205                 esac
206         fi
207 }
208
209 #
210 # Parse command line arguments.
211 #
212 args=`getopt m:v $*`
213 if [ $? != 0 ]; then
214         echo 'Usage: ...'
215         exit 2
216 fi
217 set -- $args
218 for i; do
219         case "$i" in
220         -m)
221                 MAKE="$2"
222                 shift
223                 shift
224                 ;;
225         -v)
226                 VERBOSE=1
227                 shift
228                 ;;
229         --)
230                 shift
231                 break
232                 ;;
233         esac
234 done
235
236 SRC_BASE=${SRC_BASE:-""}
237 WORK_BASE=${WORK_BASE:-"/tmp/$USER.make.test"}
238 MAKE=${MAKE:-/usr/bin/make}
239 SCRATCH=${SCRATCH:-/tmp}
240
241 export MAKE
242 export VERBOSE
243 export SRC_BASE
244 export WORK_BASE