Tests are good.
authorBenjamin Smedberg <benjamin@smedbergs.us>
Fri, 23 Jan 2009 17:17:17 -0500
changeset 2 f423decb7ebd
parent 1 816a91c3caf5
child 3 c0b0fd711b1e
push id2
push userbsmedberg@mozilla.com
push date2009-01-23 22:23 +0000
Tests are good.
tests/runtests.py
tests/var-change-flavor.mk
tests/var-commandline.mk
--- a/tests/runtests.py
+++ b/tests/runtests.py
@@ -1,19 +1,23 @@
 """
-Run the test(s) listed on the command line. If a directory is listed,
-we'll recursively walk the directory for files named .mk and run each.
+Run the test(s) listed on the command line. If a directory is listed, the script will recursively
+walk the directory for files named .mk and run each.
+
+For each test, we run gmake -f test.mk. By default, make must exit with an exit code of 0, and must print 'TEST-PASS'.
 
-For each test, we simply run gmake -f test.mk. By default, make must exit
-with an exit code of 0, and must print 'TEST-PASS'.
+The test file may contain lines at the beginning to alter the default behavior. These are all evaluated as python:
+
+#T commandline: ['extra', 'params', 'here']
+#T returncode: 2
 """
 
 from subprocess import Popen, PIPE, STDOUT
 from optparse import OptionParser
-import os
+import os, re, sys
 
 o = OptionParser()
 opts, args = o.parse_args()
 
 if len(args) == 0:
     args = ['.']
 
 makefiles = []
@@ -24,24 +28,46 @@ for a in args:
         for path, dirnames, filenames in os.walk(a):
             for f in filenames:
                 if f.endswith('.mk'):
                     makefiles.append('%s/%s' % (path, f))
     else:
         print >>sys.stderr, "Error: Unknown file on command line"
         sys.exit(1)
 
+tre = re.compile('^#T ([a-z]+): (.*)$')
+
 for makefile in makefiles:
-    print "Running: %s" % makefile,
+    print "Testing: %s" % makefile,
+
+    cline = ['gmake', '-f', makefile]
+    returncode = 0
 
-    p = Popen(['gmake', '-f', makefile], stdout=PIPE, stderr=STDOUT)
+    mdata = open(makefile)
+    for line in mdata:
+        m = tre.search(line)
+        if m is None:
+            break
+        key, data = m.group(1, 2)
+        data = eval(data)
+        if key == 'commandline':
+            cline.extend(data)
+        elif key == 'returncode':
+            returncode = data
+        else:
+            print >>sys.stderr, "Unexpected #T key: %s" % key
+            sys.exit(1)
+
+    mdata.close()
+
+    p = Popen(cline, stdout=PIPE, stderr=STDOUT)
     stdout, d = p.communicate()
-    if p.returncode != 0:
+    if p.returncode != returncode:
         print "FAIL"
         print stdout
-    if stdout.find('TEST-FAIL') != -1:
+    elif stdout.find('TEST-FAIL') != -1:
         print "FAIL"
         print stdout
     elif stdout.find('TEST-PASS') != -1:
         print "PASS"
     else:
         print "FAIL (no passing output)"
         print stdout
--- a/tests/var-change-flavor.mk
+++ b/tests/var-change-flavor.mk
@@ -1,12 +1,12 @@
 VAR = value1
 VAR := value2
 
 VAR2 := val1
 VAR2 = val2
 
 default:
-	@test "$(flavor VAR)" = "simple"
-	@test "$(VAR)" = "value2"
-	@test "$(flavor VAR2)" = "recursive"
-	@test "$(VAR2)" = "val2"
+	test "$(flavor VAR)" = "simple"
+	test "$(VAR)" = "value2"
+	test "$(flavor VAR2)" = "recursive"
+	test "$(VAR2)" = "val2"
 	@echo "TEST-PASS"
new file mode 100644
--- /dev/null
+++ b/tests/var-commandline.mk
@@ -0,0 +1,8 @@
+#T commandline: ['TESTVAR=$(MAKEVAL)', 'TESTVAR2:=$(MAKEVAL)']
+
+MAKEVAL=testvalue
+
+all:
+	test "$(TESTVAR)" = "testvalue"
+	test "$(TESTVAR2)" = ""
+	@echo "TEST-PASS"
\ No newline at end of file