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, didn't see '${@:$#}' 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 cmd-ssc=sleep 1 && grep 'warn' $< && sed -e 's_//.*$$__' $< >$@
122 $(call mg-define-rule,%,%.ssc,cmd-ssc)
123 cmd-index=sort $< >$@
124 $(call mg-define-rule,index,index.in,cmd-index)
127 $(call mg-clean-cmd,.)
131 cat >hc-rule.mk <<'EOF'
132 cmd-hc=sed -e 's_$(hash).*$$__' $< >$@
133 $(call mg-define-rule,%,%.hc,cmd-hc)
138 This is the foo file.
139 # You don't get to see this.
140 But you do get to see this!
141 // Needles: you can lean but you can't hide!
144 the bar file has a different personality
147 warn: tell me about it!
149 cat >index.in <<'EOF'
154 # Run and make sure the files were compiled correctly.
156 start_section "Initial full build"
157 do_mgear foo bar index
159 assert_contents foo <<'EOF'
160 This is the foo file.
162 But you do get to see this!
163 // Needles: you can lean but you can't hide!
165 assert_contents bar <<'EOF'
166 the bar file has a different personality
169 warn: tell me about it!
171 assert_contents index <<'EOF'
177 assert_generated index
178 assert_saw "sed -e 's_#.*\$__' foo.hc >foo.tmp"
179 assert_saw "sleep 1 && grep 'warn' bar.ssc && sed -e 's_//.*\$__' bar.ssc >bar.tmp"
180 assert_saw 'warn: tell me about it!'
181 assert_saw sort index.in >index.tmp
183 # Run it again. Make sure the warning is replayed and bar isn't overridden due
184 # to bar.g accidentally being too old.
186 start_section "Replay bar warning"
187 do_mgear foo bar index
191 assert_uptodate index
192 assert_saw -i '#.*warning.*replay' # Indication of warning replay
193 assert_saw 'warn: tell me about it!' # Actual warning
194 assert_not_saw -i overrid
196 # Now override bar and make sure it stays that way and the warning isn't replayed.
197 start_section "Override bar"
198 sleep 1 # No racy cleanliness
202 assert_saw -i overrid
203 assert_contents bar <<<NEWCONTENT
205 # Change foo.hc and make sure foo is updated properly.
207 start_section "Change foo.hc"
208 sleep 1 # No racy cleanliness
209 echo 'look: # Last-minute addition.' >>foo.hc
212 assert_uptodate index
214 assert_contents foo <<'EOF'
215 This is the foo file.
217 But you do get to see this!
218 // Needles: you can lean but you can't hide!
222 # Change the rule for # comments to strip spaces before a #.
223 # Make sure foo is updated properly.
224 # Also, make sure commas in commands work properly.
226 start_section "Command change for % <- %.hc"
227 cat >hc-rule.mk <<'EOF'
228 cmd-hc=sed -e 's, *$(hash).*$$,,' $< >$@
229 $(call mg-define-rule,%,%.hc,cmd-hc)
234 assert_contents foo <<'EOF'
235 This is the foo file.
237 But you do get to see this!
238 // Needles: you can lean but you can't hide!
242 # Remove a space before a # from foo.hc. This is an inconsequential change.
243 # Make sure that foo.g is touched but foo is not.
245 start_section "Inconsequential change to foo.hc"
246 sleep 1 # No racy cleanliness
247 remember_mtime foo foo.g
248 sed -e '$s/look: /look:/' -i foo.hc
253 assert_not_touched foo
255 # Test cleaning. Remember, bar is still overridden.
257 start_section "Cleaning"
260 assert_saw 'rm.*foo' # Some sort of indication of foo's deletion
261 assert ! [ -f foo ] # foo deleted
262 assert ! [ -f foo.g ] # foo.g deleted
263 assert [ -f bar ] # bar not actually deleted...
264 assert_contents bar <<<NEWCONTENT # and contents still correct.
266 # Test rebuild and clean after override removal.
268 start_section "Rebuild after override removal"
272 assert_contents bar <<'EOF' # contents of bar restored according to rule
273 the bar file has a different personality
276 warn: tell me about it!
279 start_section "Clean after override removal"
282 assert ! [ -f bar ] # bar finally deleted
284 # Override bar again. Make sure an override works if bar.g doesn't exist just
285 # as well as if bar.g is older than bar.
287 start_section "Override bar before it is ever built"
288 echo NEWERCONTENT >bar
291 assert_saw -i overrid
292 assert_contents bar <<<NEWERCONTENT
297 echo "TEST SUITE SUCCEEDED"