Add implementation, demo and tests for `make clean'.
[mgear/mgear.git] / testsuite
CommitLineData
21a50371 1#!/bin/bash
099638eb 2# Test suite for mgear.
21a50371 3
099638eb 4echo "Test suite for mgear"
21a50371
MM
5
6cd "$(dirname "$0")"
7Z=test-zone
8rm -rf $Z
9mkdir $Z
10cd $Z
11
12#exec 3>&2
13#exec 1>test.log 2>&1
14exec </dev/null
15set -e
16set -o errtrace
17section=Initialization
18trap 'echo; echo "TEST SUITE FAILED in section $section!" >&2' ERR
19#set -x
20
099638eb 21ln -s ../mgear.mk .
21a50371
MM
22
23function fail {
24 false
25}
26
27function start_section {
28 section="$1"
29 echo
30 echo "SECTION: $1"
31}
32
099638eb 33function do_mgear {
21a50371 34 echo "Running: make $*"
099638eb 35 make "$@" 2>&1 | tee mgear.log
21a50371
MM
36}
37
38function assert_contents {
39 if diff -u - "$1"; then
40 echo "File '$1' looks good."
41 else
42 echo "File '$1' has the wrong contents!"
43 fail
44 fi
45}
46
47# Options can be passed to grep: assert_saw -i override
48function assert_saw {
099638eb 49 if grep -q "$@" mgear.log; then
21a50371
MM
50 echo "Good, saw '${@:$#}' in build log."
51 else
52 echo "Expected '${@:$#}' in build log but didn't see it! Log:"
099638eb 53 cat mgear.log
21a50371
MM
54 fail
55 fi
56}
57function assert_not_saw {
099638eb 58 if ! grep -q "$@" mgear.log; then
21a50371
MM
59 echo "Good, saw '${@:$#}' in build log."
60 else
61 echo "Did not expect '${@:$#}' in build log but saw it! Log:"
099638eb 62 cat mgear.log
21a50371
MM
63 fail
64 fi
65}
66function assert_uptodate {
67 assert_saw "make: \`$1' is up to date."
68}
69function assert_generated {
70 assert_saw "$1.tmp"
71}
72
59ba5775
MM
73function assert {
74 if eval $*; then
75 echo "Good, $* returned true."
76 else
77 echo "Expected $* to return true but it returned false!"
78 fail
79 fi
80}
81
21a50371
MM
82function remember_mtime {
83 while [ $# != 0 ]; do
84 eval "orig_mtime_${1//[^A-Za-z]/}=$(stat --format=%Y $1)"
85 shift
86 done
87}
88function assert_touched {
89 mtvar=orig_mtime_${1//[^A-Za-z]/}
90 t1="${!mtvar}"
91 t2="$(stat --format=%Y $1)"
92 echo "Times for '$1': $t1, $t2"
93 if [ "$t1" != "$t2" ]; then
94 echo "Good, '$1' was touched."
95 else
96 echo "Expected '$1' to be touched but it wasn't!"
97 fail
98 fi
99}
100function assert_not_touched {
101 mtvar=orig_mtime_${1//[^A-Za-z]/}
102 t1="${!mtvar}"
103 t2="$(stat --format=%Y $1)"
104 echo "Times for '$1': $t1, $t2"
105 if [ "$t1" == "$t2" ]; then
106 echo "Good, '$1' was not touched."
107 else
108 echo "Expected '$1' to be not touched but it was!"
109 fail
110 fi
111}
112
113# Simple makefile for stripping two kinds of comments.
114# Tests an implicit rule and two competing explicit rules.
115# Watch those dollar signs!
59ba5775 116
21a50371 117cat >Makefile <<'EOF'
099638eb 118include mgear.mk
21a50371 119.SECONDARY:
59ba5775 120
21a50371
MM
121include hc-rule.mk
122$(call mg-define-rule,%,%.ssc,sleep 1 && grep 'warn' $$< && sed -e 's_//.*$$$$__' $$< >$$t)
123$(call mg-define-rule,index,index.in,sort $$< >$$t)
59ba5775
MM
124
125clean:
126 $(call mg-clean-cmd,.)
127.PHONY: clean
21a50371 128EOF
59ba5775 129
21a50371
MM
130cat >hc-rule.mk <<'EOF'
131$(call mg-define-rule,%,%.hc,sed -e 's_#.*$$$$__' $$< >$$t)
132EOF
133
134# Input files.
135cat >foo.hc <<'EOF'
136This is the foo file.
137# You don't get to see this.
138But you do get to see this!
139// Needles: you can lean but you can't hide!
140EOF
141cat >bar.ssc <<'EOF'
142the bar file has a different personality
143// hey there
144# I slip through
145warn: tell me about it!
146EOF
147cat >index.in <<'EOF'
148foo
149bar
150EOF
151
152# Run and make sure the files were compiled correctly.
153
154start_section "Initial full build"
099638eb 155do_mgear foo bar index
21a50371
MM
156
157assert_contents foo <<'EOF'
158This is the foo file.
159
160But you do get to see this!
161// Needles: you can lean but you can't hide!
162EOF
163assert_contents bar <<'EOF'
164the bar file has a different personality
165
166# I slip through
167warn: tell me about it!
168EOF
169assert_contents index <<'EOF'
170bar
171foo
172EOF
173assert_generated foo
174assert_generated bar
175assert_generated index
176assert_saw "sed -e 's_#.*\$__' foo.hc >foo.tmp"
177assert_saw "sleep 1 && grep 'warn' bar.ssc && sed -e 's_//.*\$__' bar.ssc >bar.tmp"
178assert_saw 'warn: tell me about it!'
179assert_saw sort index.in >index.tmp
180
181# Run it again. Make sure the warning is replayed and bar isn't overridden due
182# to bar.g accidentally being too old.
183
184start_section "Replay bar warning"
099638eb 185do_mgear foo bar index
21a50371
MM
186
187assert_uptodate foo
188assert_uptodate bar
189assert_uptodate index
190assert_saw -i '#.*warning.*replay' # Indication of warning replay
191assert_saw 'warn: tell me about it!' # Actual warning
192assert_not_saw -i overrid
193
194# Now override bar and make sure it stays that way and the warning isn't replayed.
195start_section "Override bar"
196sleep 1 # No racy cleanliness
197echo NEWCONTENT >bar
099638eb 198do_mgear bar
21a50371
MM
199
200assert_saw -i overrid
201assert_contents bar <<<NEWCONTENT
202
203# Change foo.hc and make sure foo is updated properly.
204
205start_section "Change foo.hc"
206sleep 1 # No racy cleanliness
207echo 'look: # Last-minute addition.' >>foo.hc
099638eb 208do_mgear index foo
21a50371
MM
209
210assert_uptodate index
211assert_generated foo
212assert_contents foo <<'EOF'
213This is the foo file.
214
215But you do get to see this!
216// Needles: you can lean but you can't hide!
217look:
218EOF
219
220# Change the rule for # comments to strip spaces before a #.
221# Make sure foo is updated properly.
222
223start_section "Command change for % <- %.hc"
224cat >hc-rule.mk <<'EOF'
225$(call mg-define-rule,%,%.hc,sed -e 's_ *#.*$$$$__' $$< >$$t)
226EOF
099638eb 227do_mgear foo
21a50371
MM
228
229assert_generated foo
230assert_contents foo <<'EOF'
231This is the foo file.
232
233But you do get to see this!
234// Needles: you can lean but you can't hide!
235look:
236EOF
237
238# Remove a space before a # from foo.hc. This is an inconsequential change.
239# Make sure that foo.g is touched but foo is not.
240
241start_section "Inconsequential change to foo.hc"
242sleep 1 # No racy cleanliness
243remember_mtime foo foo.g
244sed -e '$s/look: /look:/' -i foo.hc
099638eb 245do_mgear foo
21a50371
MM
246
247assert_generated foo
248assert_touched foo.g
249assert_not_touched foo
250
59ba5775
MM
251# Test cleaning. Remember, bar is still overridden.
252
253start_section "Cleaning"
254do_mgear clean
255
256assert_saw 'rm.*foo' # Some sort of indication of foo's deletion
257assert ! [ -f foo ] # foo deleted
258assert ! [ -f foo.g ] # foo.g deleted
259assert [ -f bar ] # bar not actually deleted...
260assert_contents bar <<<NEWCONTENT # and contents still correct.
261
262# Test rebuild and clean after override removal.
263
264start_section "Rebuild after override removal"
265rm bar
266do_mgear bar
267assert_generated bar
268assert_contents bar <<'EOF' # contents of bar restored according to rule
269the bar file has a different personality
270
271# I slip through
272warn: tell me about it!
273EOF
274
275start_section "Clean after override removal"
276do_mgear clean
277assert_saw 'rm.*bar'
278assert ! [ -f bar ] # bar finally deleted
279
280# Override bar again. Make sure an override works if bar.g doesn't exist just
281# as well as if bar.g is older than bar.
282
283start_section "Override bar before it is ever built"
284echo NEWERCONTENT >bar
285do_mgear bar
286
287assert_saw -i overrid
288assert_contents bar <<<NEWERCONTENT
289
21a50371
MM
290cd ..
291rm -rf test-zone
292echo
293echo "TEST SUITE SUCCEEDED"