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 "$@" 2>&1 | tee mgear.log
38 function assert_contents {
39 if diff -u - "$1"; then
40 echo "File '$1' looks good."
42 echo "File '$1' has the wrong contents!"
47 # Options can be passed to grep: assert_saw -i override
49 if grep -q "$@" mgear.log; then
50 echo "Good, saw '${@:$#}' in build log."
52 echo "Expected '${@:$#}' in build log but didn't see it! Log:"
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! Log:"
66 function assert_uptodate {
67 assert_saw "make: \`$1' is up to date."
69 function assert_generated {
75 echo "Good, $* returned true."
77 echo "Expected $* to return true but it returned false!"
82 function remember_mtime {
84 eval "orig_mtime_${1//[^A-Za-z]/}=$(stat --format=%Y $1)"
88 function assert_touched {
89 mtvar=orig_mtime_${1//[^A-Za-z]/}
91 t2="$(stat --format=%Y $1)"
92 echo "Times for '$1': $t1, $t2"
93 if [ "$t1" != "$t2" ]; then
94 echo "Good, '$1' was touched."
96 echo "Expected '$1' to be touched but it wasn't!"
100 function assert_not_touched {
101 mtvar=orig_mtime_${1//[^A-Za-z]/}
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."
108 echo "Expected '$1' to be not touched but it was!"
113 # Simple makefile for stripping two kinds of comments.
114 # Tests an implicit rule and two competing explicit rules.
115 # Watch those dollar signs!
117 cat >Makefile <<'EOF'
122 $(call mg-define-rule,%,%.ssc,sleep 1 && grep 'warn' $$< && sed -e 's_//.*$$$$__' $$< >$$t)
123 $(call mg-define-rule,index,index.in,sort $$< >$$t)
126 $(call mg-clean-cmd,.)
130 cat >hc-rule.mk <<'EOF'
131 $(call mg-define-rule,%,%.hc,sed -e 's_#.*$$$$__' $$< >$$t)
136 This is the foo file.
137 # You don't get to see this.
138 But you do get to see this!
139 // Needles: you can lean but you can't hide!
142 the bar file has a different personality
145 warn: tell me about it!
147 cat >index.in <<'EOF'
152 # Run and make sure the files were compiled correctly.
154 start_section "Initial full build"
155 do_mgear foo bar index
157 assert_contents foo <<'EOF'
158 This is the foo file.
160 But you do get to see this!
161 // Needles: you can lean but you can't hide!
163 assert_contents bar <<'EOF'
164 the bar file has a different personality
167 warn: tell me about it!
169 assert_contents index <<'EOF'
175 assert_generated index
176 assert_saw "sed -e 's_#.*\$__' foo.hc >foo.tmp"
177 assert_saw "sleep 1 && grep 'warn' bar.ssc && sed -e 's_//.*\$__' bar.ssc >bar.tmp"
178 assert_saw 'warn: tell me about it!'
179 assert_saw sort index.in >index.tmp
181 # Run it again. Make sure the warning is replayed and bar isn't overridden due
182 # to bar.g accidentally being too old.
184 start_section "Replay bar warning"
185 do_mgear foo bar index
189 assert_uptodate index
190 assert_saw -i '#.*warning.*replay' # Indication of warning replay
191 assert_saw 'warn: tell me about it!' # Actual warning
192 assert_not_saw -i overrid
194 # Now override bar and make sure it stays that way and the warning isn't replayed.
195 start_section "Override bar"
196 sleep 1 # No racy cleanliness
200 assert_saw -i overrid
201 assert_contents bar <<<NEWCONTENT
203 # Change foo.hc and make sure foo is updated properly.
205 start_section "Change foo.hc"
206 sleep 1 # No racy cleanliness
207 echo 'look: # Last-minute addition.' >>foo.hc
210 assert_uptodate index
212 assert_contents foo <<'EOF'
213 This is the foo file.
215 But you do get to see this!
216 // Needles: you can lean but you can't hide!
220 # Change the rule for # comments to strip spaces before a #.
221 # Make sure foo is updated properly.
223 start_section "Command change for % <- %.hc"
224 cat >hc-rule.mk <<'EOF'
225 $(call mg-define-rule,%,%.hc,sed -e 's_ *#.*$$$$__' $$< >$$t)
230 assert_contents foo <<'EOF'
231 This is the foo file.
233 But you do get to see this!
234 // Needles: you can lean but you can't hide!
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.
241 start_section "Inconsequential change to foo.hc"
242 sleep 1 # No racy cleanliness
243 remember_mtime foo foo.g
244 sed -e '$s/look: /look:/' -i foo.hc
249 assert_not_touched foo
251 # Test cleaning. Remember, bar is still overridden.
253 start_section "Cleaning"
256 assert_saw 'rm.*foo' # Some sort of indication of foo's deletion
257 assert ! [ -f foo ] # foo deleted
258 assert ! [ -f foo.g ] # foo.g deleted
259 assert [ -f bar ] # bar not actually deleted...
260 assert_contents bar <<<NEWCONTENT # and contents still correct.
262 # Test rebuild and clean after override removal.
264 start_section "Rebuild after override removal"
268 assert_contents bar <<'EOF' # contents of bar restored according to rule
269 the bar file has a different personality
272 warn: tell me about it!
275 start_section "Clean after override removal"
278 assert ! [ -f bar ] # bar finally deleted
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.
283 start_section "Override bar before it is ever built"
284 echo NEWERCONTENT >bar
287 assert_saw -i overrid
288 assert_contents bar <<<NEWERCONTENT
293 echo "TEST SUITE SUCCEEDED"