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 {
73 function remember_mtime {
75 eval "orig_mtime_${1//[^A-Za-z]/}=$(stat --format=%Y $1)"
79 function assert_touched {
80 mtvar=orig_mtime_${1//[^A-Za-z]/}
82 t2="$(stat --format=%Y $1)"
83 echo "Times for '$1': $t1, $t2"
84 if [ "$t1" != "$t2" ]; then
85 echo "Good, '$1' was touched."
87 echo "Expected '$1' to be touched but it wasn't!"
91 function assert_not_touched {
92 mtvar=orig_mtime_${1//[^A-Za-z]/}
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."
99 echo "Expected '$1' to be not touched but it was!"
104 # Simple makefile for stripping two kinds of comments.
105 # Tests an implicit rule and two competing explicit rules.
106 # Watch those dollar signs!
107 cat >Makefile <<'EOF'
111 $(call mg-define-rule,%,%.ssc,sleep 1 && grep 'warn' $$< && sed -e 's_//.*$$$$__' $$< >$$t)
112 $(call mg-define-rule,index,index.in,sort $$< >$$t)
114 cat >hc-rule.mk <<'EOF'
115 $(call mg-define-rule,%,%.hc,sed -e 's_#.*$$$$__' $$< >$$t)
120 This is the foo file.
121 # You don't get to see this.
122 But you do get to see this!
123 // Needles: you can lean but you can't hide!
126 the bar file has a different personality
129 warn: tell me about it!
131 cat >index.in <<'EOF'
136 # Run and make sure the files were compiled correctly.
138 start_section "Initial full build"
139 do_mgear foo bar index
141 assert_contents foo <<'EOF'
142 This is the foo file.
144 But you do get to see this!
145 // Needles: you can lean but you can't hide!
147 assert_contents bar <<'EOF'
148 the bar file has a different personality
151 warn: tell me about it!
153 assert_contents index <<'EOF'
159 assert_generated index
160 assert_saw "sed -e 's_#.*\$__' foo.hc >foo.tmp"
161 assert_saw "sleep 1 && grep 'warn' bar.ssc && sed -e 's_//.*\$__' bar.ssc >bar.tmp"
162 assert_saw 'warn: tell me about it!'
163 assert_saw sort index.in >index.tmp
165 # Run it again. Make sure the warning is replayed and bar isn't overridden due
166 # to bar.g accidentally being too old.
168 start_section "Replay bar warning"
169 do_mgear foo bar index
173 assert_uptodate index
174 assert_saw -i '#.*warning.*replay' # Indication of warning replay
175 assert_saw 'warn: tell me about it!' # Actual warning
176 assert_not_saw -i overrid
178 # Now override bar and make sure it stays that way and the warning isn't replayed.
179 start_section "Override bar"
180 sleep 1 # No racy cleanliness
184 assert_saw -i overrid
185 assert_contents bar <<<NEWCONTENT
187 # Change foo.hc and make sure foo is updated properly.
189 start_section "Change foo.hc"
190 sleep 1 # No racy cleanliness
191 echo 'look: # Last-minute addition.' >>foo.hc
194 assert_uptodate index
196 assert_contents foo <<'EOF'
197 This is the foo file.
199 But you do get to see this!
200 // Needles: you can lean but you can't hide!
204 # Change the rule for # comments to strip spaces before a #.
205 # Make sure foo is updated properly.
207 start_section "Command change for % <- %.hc"
208 cat >hc-rule.mk <<'EOF'
209 $(call mg-define-rule,%,%.hc,sed -e 's_ *#.*$$$$__' $$< >$$t)
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 # 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.
225 start_section "Inconsequential change to foo.hc"
226 sleep 1 # No racy cleanliness
227 remember_mtime foo foo.g
228 sed -e '$s/look: /look:/' -i foo.hc
233 assert_not_touched foo
238 echo "TEST SUITE SUCCEEDED"