Move more code from test.sh scripts into common (env.sh) script file.
[dragonfly.git] / usr.bin / make / tests / env.sh
1 #!/bin/sh
2
3 # $DragonFly: src/usr.bin/make/tests/Attic/env.sh,v 1.4 2005/02/25 11:57:32 okumoto Exp $
4
5 MAKE=/usr/bin/make
6
7 #
8 # We can't check a file into cvs without a DragonFly RCS Id tag, so
9 # we need to remove it before we compare. 
10 #
11 hack_cmp()
12 {
13         if [ -f $1 ]; then
14                 cat $1 |\
15                         sed -e '1d' |\
16                         diff -q - $2 \
17                         1> /dev/null 2> /dev/null
18                 return $?
19         else
20                 return 1        # FAIL
21         fi
22 }
23
24 #
25 # We can't check a file into cvs without a DragonFly RCS Id tag, so
26 # we need to remove it before we compare. 
27 #
28 hack_diff()
29 {
30         echo diff $1 $2
31         if [ -f $1 ]; then
32                 cat $1 |\
33                         sed -e '1d' |\
34                         diff - $2
35                 return $?
36         else
37                 return 1        # FAIL
38         fi
39 }
40
41 std_compare()
42 {
43         hack_cmp expected.stdout stdout || FAIL="stdout "$FAIL
44         hack_cmp expected.stderr stderr || FAIL="stderr "$FAIL
45         hack_cmp expected.status status || FAIL="status "$FAIL
46
47         if [ -z "$FAIL" ]; then
48                 :
49         else
50                 echo "Test failed ( $FAIL) `pwd`"
51         fi
52 }
53
54 std_diff()
55 {
56         $0 test
57         echo "------------------------"
58         echo "- `pwd`"
59         echo "------------------------"
60         hack_diff expected.stdout stdout
61         hack_diff expected.stderr stderr
62         hack_diff expected.status status
63 }
64
65 std_update()
66 {
67         $0 test
68         [ -f expected.stdout ] || echo '$'DragonFly'$' > expected.stdout
69         [ -f expected.stderr ] || echo '$'DragonFly'$' > expected.stderr
70         [ -f expected.status ] || echo '$'DragonFly'$' > expected.status
71         sed -e '2,$d' < expected.stdout > new.expected.stdout
72         sed -e '2,$d' < expected.stderr > new.expected.stderr
73         sed -e '2,$d' < expected.status > new.expected.status
74         cat stdout >> new.expected.stdout
75         cat stderr >> new.expected.stderr
76         cat status >> new.expected.status
77         mv new.expected.stdout expected.stdout
78         mv new.expected.stderr expected.stderr
79         mv new.expected.status expected.status
80 }
81
82 std_clean()
83 {
84         rm -f Makefile
85         rm -f stdout
86         rm -f stderr
87         rm -f status
88 }
89
90 std_run()
91 {
92         $0 test
93         $0 compare
94         $0 clean
95 }
96
97 #
98 # Run 
99 #
100 eval_subdir_cmd()
101 {
102         local START_BASE
103
104         if [ ! -d $1 ]; then
105                 echo "Test directory '$1' missing in directory '$START_BASE'"
106                 return
107         fi
108
109         if [ ! -f $1/test.sh ]; then
110                 echo "Test script missing in directory '$START_BASE/$1'"
111                 return
112         fi
113
114         START_BASE=${START_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                 case $1 in
126                 test|compare|diff|update|clean|run)
127                         for d in $DIR; do
128                                 eval_subdir_cmd $d $1
129                         done
130                         ;;
131                 *)
132                         echo "Usage: $0 run | compare | update | clean"
133                         ;;
134                 esac
135         else
136                 case $1 in
137                 test)
138                         run_test
139                         ;;
140                 compare)
141                         std_compare
142                         ;;
143                 diff)
144                         std_diff
145                         ;;
146                 update)
147                         std_update
148                         ;;
149                 run)
150                         std_run
151                         ;;
152                 clean)
153                         std_clean
154                         ;;
155                 *)
156                         echo "Usage: $0 run | compare | update | clean"
157                         ;;
158                 esac
159         fi
160 }
161
162 #
163 # Parse command line arguments.
164 #
165 args=`getopt m:v $*`
166 if [ $? != 0 ]; then
167         echo 'Usage: ...'
168         exit 2
169 fi
170 set -- $args
171 for i; do
172         case "$i" in
173         -m)
174                 MAKE="$2"
175                 shift
176                 shift
177                 ;;
178         -v)
179                 VERBOSE=1
180                 shift
181                 ;;
182         --)
183                 shift
184                 break
185                 ;;
186         esac
187 done
188
189 START_BASE=${START_BASE:-.}
190
191 export MAKE
192 export VERBOSE
193 export START_BASE