Rename Mage to mgear, a less common and thus less confusable name.
[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
73function remember_mtime {
74 while [ $# != 0 ]; do
75 eval "orig_mtime_${1//[^A-Za-z]/}=$(stat --format=%Y $1)"
76 shift
77 done
78}
79function assert_touched {
80 mtvar=orig_mtime_${1//[^A-Za-z]/}
81 t1="${!mtvar}"
82 t2="$(stat --format=%Y $1)"
83 echo "Times for '$1': $t1, $t2"
84 if [ "$t1" != "$t2" ]; then
85 echo "Good, '$1' was touched."
86 else
87 echo "Expected '$1' to be touched but it wasn't!"
88 fail
89 fi
90}
91function assert_not_touched {
92 mtvar=orig_mtime_${1//[^A-Za-z]/}
93 t1="${!mtvar}"
94 t2="$(stat --format=%Y $1)"
95 echo "Times for '$1': $t1, $t2"
96 if [ "$t1" == "$t2" ]; then
97 echo "Good, '$1' was not touched."
98 else
99 echo "Expected '$1' to be not touched but it was!"
100 fail
101 fi
102}
103
104# Simple makefile for stripping two kinds of comments.
105# Tests an implicit rule and two competing explicit rules.
106# Watch those dollar signs!
107cat >Makefile <<'EOF'
099638eb 108include mgear.mk
21a50371
MM
109.SECONDARY:
110include hc-rule.mk
111$(call mg-define-rule,%,%.ssc,sleep 1 && grep 'warn' $$< && sed -e 's_//.*$$$$__' $$< >$$t)
112$(call mg-define-rule,index,index.in,sort $$< >$$t)
113EOF
114cat >hc-rule.mk <<'EOF'
115$(call mg-define-rule,%,%.hc,sed -e 's_#.*$$$$__' $$< >$$t)
116EOF
117
118# Input files.
119cat >foo.hc <<'EOF'
120This is the foo file.
121# You don't get to see this.
122But you do get to see this!
123// Needles: you can lean but you can't hide!
124EOF
125cat >bar.ssc <<'EOF'
126the bar file has a different personality
127// hey there
128# I slip through
129warn: tell me about it!
130EOF
131cat >index.in <<'EOF'
132foo
133bar
134EOF
135
136# Run and make sure the files were compiled correctly.
137
138start_section "Initial full build"
099638eb 139do_mgear foo bar index
21a50371
MM
140
141assert_contents foo <<'EOF'
142This is the foo file.
143
144But you do get to see this!
145// Needles: you can lean but you can't hide!
146EOF
147assert_contents bar <<'EOF'
148the bar file has a different personality
149
150# I slip through
151warn: tell me about it!
152EOF
153assert_contents index <<'EOF'
154bar
155foo
156EOF
157assert_generated foo
158assert_generated bar
159assert_generated index
160assert_saw "sed -e 's_#.*\$__' foo.hc >foo.tmp"
161assert_saw "sleep 1 && grep 'warn' bar.ssc && sed -e 's_//.*\$__' bar.ssc >bar.tmp"
162assert_saw 'warn: tell me about it!'
163assert_saw sort index.in >index.tmp
164
165# Run it again. Make sure the warning is replayed and bar isn't overridden due
166# to bar.g accidentally being too old.
167
168start_section "Replay bar warning"
099638eb 169do_mgear foo bar index
21a50371
MM
170
171assert_uptodate foo
172assert_uptodate bar
173assert_uptodate index
174assert_saw -i '#.*warning.*replay' # Indication of warning replay
175assert_saw 'warn: tell me about it!' # Actual warning
176assert_not_saw -i overrid
177
178# Now override bar and make sure it stays that way and the warning isn't replayed.
179start_section "Override bar"
180sleep 1 # No racy cleanliness
181echo NEWCONTENT >bar
099638eb 182do_mgear bar
21a50371
MM
183
184assert_saw -i overrid
185assert_contents bar <<<NEWCONTENT
186
187# Change foo.hc and make sure foo is updated properly.
188
189start_section "Change foo.hc"
190sleep 1 # No racy cleanliness
191echo 'look: # Last-minute addition.' >>foo.hc
099638eb 192do_mgear index foo
21a50371
MM
193
194assert_uptodate index
195assert_generated foo
196assert_contents foo <<'EOF'
197This is the foo file.
198
199But you do get to see this!
200// Needles: you can lean but you can't hide!
201look:
202EOF
203
204# Change the rule for # comments to strip spaces before a #.
205# Make sure foo is updated properly.
206
207start_section "Command change for % <- %.hc"
208cat >hc-rule.mk <<'EOF'
209$(call mg-define-rule,%,%.hc,sed -e 's_ *#.*$$$$__' $$< >$$t)
210EOF
099638eb 211do_mgear foo
21a50371
MM
212
213assert_generated foo
214assert_contents foo <<'EOF'
215This is the foo file.
216
217But you do get to see this!
218// Needles: you can lean but you can't hide!
219look:
220EOF
221
222# Remove a space before a # from foo.hc. This is an inconsequential change.
223# Make sure that foo.g is touched but foo is not.
224
225start_section "Inconsequential change to foo.hc"
226sleep 1 # No racy cleanliness
227remember_mtime foo foo.g
228sed -e '$s/look: /look:/' -i foo.hc
099638eb 229do_mgear foo
21a50371
MM
230
231assert_generated foo
232assert_touched foo.g
233assert_not_touched foo
234
235cd ..
236rm -rf test-zone
237echo
238echo "TEST SUITE SUCCEEDED"