Add implementation, demo and tests for `make clean'.
[mgear/mgear.git] / testsuite
index 3329a66..a0dc8aa 100755 (executable)
--- a/testsuite
+++ b/testsuite
@@ -1,7 +1,7 @@
 #!/bin/bash
-# Test suite for Mage.
+# Test suite for mgear.
 
-echo "Test suite for Mage"
+echo "Test suite for mgear"
 
 cd "$(dirname "$0")"
 Z=test-zone
@@ -18,7 +18,7 @@ section=Initialization
 trap 'echo; echo "TEST SUITE FAILED in section $section!" >&2' ERR
 #set -x
 
-ln -s ../mage.mk mage.mk
+ln -s ../mgear.mk .
 
 function fail {
        false
@@ -30,9 +30,9 @@ function start_section {
        echo "SECTION: $1"
 }
 
-function do_mage {
+function do_mgear {
        echo "Running: make $*"
-       make "$@" 2>&1 | tee mage.log
+       make "$@" 2>&1 | tee mgear.log
 }
 
 function assert_contents {
@@ -46,20 +46,20 @@ function assert_contents {
 
 # Options can be passed to grep: assert_saw -i override
 function assert_saw {
-       if grep -q "$@" mage.log; then
+       if grep -q "$@" mgear.log; then
                echo "Good, saw '${@:$#}' in build log."
        else
                echo "Expected '${@:$#}' in build log but didn't see it!  Log:"
-               cat mage.log
+               cat mgear.log
                fail
        fi
 }
 function assert_not_saw {
-       if ! grep -q "$@" mage.log; then
+       if ! grep -q "$@" mgear.log; then
                echo "Good, saw '${@:$#}' in build log."
        else
                echo "Did not expect '${@:$#}' in build log but saw it!  Log:"
-               cat mage.log
+               cat mgear.log
                fail
        fi
 }
@@ -70,6 +70,15 @@ function assert_generated {
        assert_saw "$1.tmp"
 }
 
+function assert {
+       if eval $*; then
+               echo "Good, $* returned true."
+       else
+               echo "Expected $* to return true but it returned false!"
+               fail
+       fi
+}
+
 function remember_mtime {
        while [ $# != 0 ]; do
                eval "orig_mtime_${1//[^A-Za-z]/}=$(stat --format=%Y $1)"
@@ -104,13 +113,20 @@ function assert_not_touched {
 # Simple makefile for stripping two kinds of comments.
 # Tests an implicit rule and two competing explicit rules.
 # Watch those dollar signs!
+
 cat >Makefile <<'EOF'
-include mage.mk
+include mgear.mk
 .SECONDARY:
+
 include hc-rule.mk
 $(call mg-define-rule,%,%.ssc,sleep 1 && grep 'warn' $$< && sed -e 's_//.*$$$$__' $$< >$$t)
 $(call mg-define-rule,index,index.in,sort $$< >$$t)
+
+clean:
+       $(call mg-clean-cmd,.)
+.PHONY: clean
 EOF
+
 cat >hc-rule.mk <<'EOF'
 $(call mg-define-rule,%,%.hc,sed -e 's_#.*$$$$__' $$< >$$t)
 EOF
@@ -136,7 +152,7 @@ EOF
 # Run and make sure the files were compiled correctly.
 
 start_section "Initial full build"
-do_mage foo bar index
+do_mgear foo bar index
 
 assert_contents foo <<'EOF'
 This is the foo file.
@@ -166,7 +182,7 @@ assert_saw sort index.in >index.tmp
 # to bar.g accidentally being too old.
 
 start_section "Replay bar warning"
-do_mage foo bar index
+do_mgear foo bar index
 
 assert_uptodate foo
 assert_uptodate bar
@@ -179,7 +195,7 @@ assert_not_saw -i overrid
 start_section "Override bar"
 sleep 1 # No racy cleanliness
 echo NEWCONTENT >bar
-do_mage bar
+do_mgear bar
 
 assert_saw -i overrid
 assert_contents bar <<<NEWCONTENT
@@ -189,7 +205,7 @@ assert_contents bar <<<NEWCONTENT
 start_section "Change foo.hc"
 sleep 1 # No racy cleanliness
 echo 'look: # Last-minute addition.' >>foo.hc
-do_mage index foo
+do_mgear index foo
 
 assert_uptodate index
 assert_generated foo
@@ -208,7 +224,7 @@ start_section "Command change for % <- %.hc"
 cat >hc-rule.mk <<'EOF'
 $(call mg-define-rule,%,%.hc,sed -e 's_ *#.*$$$$__' $$< >$$t)
 EOF
-do_mage foo
+do_mgear foo
 
 assert_generated foo
 assert_contents foo <<'EOF'
@@ -226,12 +242,51 @@ start_section "Inconsequential change to foo.hc"
 sleep 1 # No racy cleanliness
 remember_mtime foo foo.g
 sed -e '$s/look: /look:/' -i foo.hc
-do_mage foo
+do_mgear foo
 
 assert_generated foo
 assert_touched foo.g
 assert_not_touched foo
 
+# Test cleaning.  Remember, bar is still overridden.
+
+start_section "Cleaning"
+do_mgear clean
+
+assert_saw 'rm.*foo' # Some sort of indication of foo's deletion
+assert ! [ -f foo ] # foo deleted
+assert ! [ -f foo.g ] # foo.g deleted
+assert [ -f bar ] # bar not actually deleted...
+assert_contents bar <<<NEWCONTENT # and contents still correct.
+
+# Test rebuild and clean after override removal.
+
+start_section "Rebuild after override removal"
+rm bar
+do_mgear bar
+assert_generated bar
+assert_contents bar <<'EOF' # contents of bar restored according to rule
+the bar file has a different personality
+
+# I slip through
+warn: tell me about it!
+EOF
+
+start_section "Clean after override removal"
+do_mgear clean
+assert_saw 'rm.*bar'
+assert ! [ -f bar ] # bar finally deleted
+
+# Override bar again.  Make sure an override works if bar.g doesn't exist just
+# as well as if bar.g is older than bar.
+
+start_section "Override bar before it is ever built"
+echo NEWERCONTENT >bar
+do_mgear bar
+
+assert_saw -i overrid
+assert_contents bar <<<NEWERCONTENT
+
 cd ..
 rm -rf test-zone
 echo