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