2 # Test suite for mgear.
4 echo "Test suite for mgear"
17 section=Initialization
18 trap 'echo; echo "TEST SUITE FAILED in section $section!" >&2' ERR
27 function start_section {
34 echo "Running: make $*"
35 make --warn-undefined-variables "$@" 2>&1 | tee mgear.log
36 assert_not_saw 'warning: undefined variable'
39 function assert_contents {
40 if diff -u - "$1"; then
41 echo "File '$1' looks good."
43 echo "File '$1' has the wrong contents!"
48 # Options can be passed to grep: assert_saw -i override
50 if grep -q "$@" mgear.log; then
51 echo "Good, saw '${@:$#}' in build log."
53 echo "Expected '${@:$#}' in build log but didn't see it!"
57 function assert_not_saw {
58 if ! grep -q "$@" mgear.log; then
59 echo "Good, saw '${@:$#}' in build log."
61 echo "Did not expect '${@:$#}' in build log but saw it!"
65 function assert_uptodate {
66 assert_saw "make: \`$1' is up to date."
68 function assert_generated {
74 echo "Good, $* returned true."
76 echo "Expected $* to return true but it returned false!"
81 function remember_mtime {
83 eval "orig_mtime_${1//[^A-Za-z]/}=$(stat --format=%Y $1)"
87 function assert_touched {
88 mtvar=orig_mtime_${1//[^A-Za-z]/}
90 t2="$(stat --format=%Y $1)"
91 echo "Times for '$1': $t1, $t2"
92 if [ "$t1" != "$t2" ]; then
93 echo "Good, '$1' was touched."
95 echo "Expected '$1' to be touched but it wasn't!"
99 function assert_not_touched {
100 mtvar=orig_mtime_${1//[^A-Za-z]/}
102 t2="$(stat --format=%Y $1)"
103 echo "Times for '$1': $t1, $t2"
104 if [ "$t1" == "$t2" ]; then
105 echo "Good, '$1' was not touched."
107 echo "Expected '$1' to be not touched but it was!"
112 # Simple makefile for stripping two kinds of comments.
113 # Tests an implicit rule and two competing explicit rules.
114 # Watch those dollar signs!
116 cat >Makefile <<'EOF'
121 $(call mg-define-rule,%,%.ssc,sleep 1 && grep 'warn' $$< && sed -e 's_//.*$$$$__' $$< >$$t)
122 $(call mg-define-rule,index,index.in,sort $$< >$$t)
125 $(call mg-clean-cmd,.)
129 cat >hc-rule.mk <<'EOF'
130 $(call mg-define-rule,%,%.hc,sed -e 's_#.*$$$$__' $$< >$$t)
135 This is the foo file.
136 # You don't get to see this.
137 But you do get to see this!
138 // Needles: you can lean but you can't hide!
141 the bar file has a different personality
144 warn: tell me about it!
146 cat >index.in <<'EOF'
151 # Run and make sure the files were compiled correctly.
153 start_section "Initial full build"
154 do_mgear foo bar index
156 assert_contents foo <<'EOF'
157 This is the foo file.
159 But you do get to see this!
160 // Needles: you can lean but you can't hide!
162 assert_contents bar <<'EOF'
163 the bar file has a different personality
166 warn: tell me about it!
168 assert_contents index <<'EOF'
174 assert_generated index
175 assert_saw "sed -e 's_#.*\$__' foo.hc >foo.tmp"
176 assert_saw "sleep 1 && grep 'warn' bar.ssc && sed -e 's_//.*\$__' bar.ssc >bar.tmp"
177 assert_saw 'warn: tell me about it!'
178 assert_saw sort index.in >index.tmp
180 # Run it again. Make sure the warning is replayed and bar isn't overridden due
181 # to bar.g accidentally being too old.
183 start_section "Replay bar warning"
184 do_mgear foo bar index
188 assert_uptodate index
189 assert_saw -i '#.*warning.*replay' # Indication of warning replay
190 assert_saw 'warn: tell me about it!' # Actual warning
191 assert_not_saw -i overrid
193 # Now override bar and make sure it stays that way and the warning isn't replayed.
194 start_section "Override bar"
195 sleep 1 # No racy cleanliness
199 assert_saw -i overrid
200 assert_contents bar <<<NEWCONTENT
202 # Change foo.hc and make sure foo is updated properly.
204 start_section "Change foo.hc"
205 sleep 1 # No racy cleanliness
206 echo 'look: # Last-minute addition.' >>foo.hc
209 assert_uptodate index
211 assert_contents foo <<'EOF'
212 This is the foo file.
214 But you do get to see this!
215 // Needles: you can lean but you can't hide!
219 # Change the rule for # comments to strip spaces before a #.
220 # Make sure foo is updated properly.
222 start_section "Command change for % <- %.hc"
223 cat >hc-rule.mk <<'EOF'
224 $(call mg-define-rule,%,%.hc,sed -e 's_ *#.*$$$$__' $$< >$$t)
229 assert_contents foo <<'EOF'
230 This is the foo file.
232 But you do get to see this!
233 // Needles: you can lean but you can't hide!
237 # Remove a space before a # from foo.hc. This is an inconsequential change.
238 # Make sure that foo.g is touched but foo is not.
240 start_section "Inconsequential change to foo.hc"
241 sleep 1 # No racy cleanliness
242 remember_mtime foo foo.g
243 sed -e '$s/look: /look:/' -i foo.hc
248 assert_not_touched foo
250 # Test cleaning. Remember, bar is still overridden.
252 start_section "Cleaning"
255 assert_saw 'rm.*foo' # Some sort of indication of foo's deletion
256 assert ! [ -f foo ] # foo deleted
257 assert ! [ -f foo.g ] # foo.g deleted
258 assert [ -f bar ] # bar not actually deleted...
259 assert_contents bar <<<NEWCONTENT # and contents still correct.
261 # Test rebuild and clean after override removal.
263 start_section "Rebuild after override removal"
267 assert_contents bar <<'EOF' # contents of bar restored according to rule
268 the bar file has a different personality
271 warn: tell me about it!
274 start_section "Clean after override removal"
277 assert ! [ -f bar ] # bar finally deleted
279 # Override bar again. Make sure an override works if bar.g doesn't exist just
280 # as well as if bar.g is older than bar.
282 start_section "Override bar before it is ever built"
283 echo NEWERCONTENT >bar
286 assert_saw -i overrid
287 assert_contents bar <<<NEWERCONTENT
292 echo "TEST SUITE SUCCEEDED"