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