#!/usr/bin/env python
-### TODO: allow to run several tests in one directory - possible
-### runtest1, runtest2 etc. Use it for generated headers test to check
-### local and global config separately.
-
import os
import os.path
import sys
import subprocess
import time
from xml.sax.saxutils import escape
+import fnmatch
+
+invokeDir = os.getcwd();
+testsRoot = os.path.dirname(os.path.abspath(__file__))
+if not os.path.exists(os.path.join(testsRoot, "tester.py")): raise "Can't find tests root directory!"
+os.environ['OMK_TESTSROOT'] = testsRoot
-sys.path.append("..")
+sys.path.append(os.path.join(testsRoot, ".."))
import rulesdef
class Results(dict):
self.time = time.gmtime()
self.datetime = time.strftime("%Y-%m-%d %H:%M:%S +0000", self.time)
self.filename = "results-"+time.strftime("%Y%m%d-%H%M%S", self.time)+".html"
+ self.stats = None
+
+ def genStats(self):
+ self.stats = Stats(self)
def toHtml(self):
s="""
<title>OMK test report %(datetime)s</title>
</head>
<body>
-<h2>Summary</h2>TODO
+<h2>Summary</h2>
""" % self.__dict__
+ s+=self.stats.toHtml()
s+="""
<h2>Chart</h2>
<table cellpadding='2' border='1'>
<pre>%(stderr)s</pre>""" % vals
return s
+class RulesStat:
+ def __init__(self, rules):
+ self.rules = rules
+ self.tests = 0
+ self.success = 0
+ self.errors = 0
+ self.canttest = 0
+ self.unknown = 0
+ def update(self, testCaseResult):
+ try:
+ resultEntry = testCaseResult[self.rules]
+ self.tests+=1
+ if resultEntry.exitcode == 0: self.success+=1
+ elif resultEntry.exitcode == 1: self.errors+=1
+ elif resultEntry.exitcode == 2: self.canttest+=1
+ else: self.unknown+=1
+ except KeyError:
+ pass
+ def toHtml(self):
+ if self.errors == 0 and self.canttest == 0: self.color=''
+ elif self.errors != 0: self.color=' bgcolor="red"'
+ elif self.canttest != 0: self.color=' bgcolor="yellow"'
+ else: self.color = ' bgcolor="gray"'
+ s="""
+ <tr%(color)s>
+ <td>%(rules)s</td>
+ <td>%(tests)d</td>
+ <td>%(success)d</td>
+ <td>%(errors)d</td>
+ <td>%(canttest)d</td>
+ <td>%(unknown)d</td>
+ </tr>
+ """ % self.__dict__
+ return s
+
+class Stats(dict):
+ def __init__(self, results):
+ rules = rulesdef.rules.keys()
+ for rule in rules:
+ rulesStat = RulesStat(rule)
+ self[rule]=rulesStat
+ for resultEntry in results.values():
+ rulesStat.update(resultEntry)
+
+ def toHtml(self):
+ s="""
+<table cellpadding='2' border='1'>
+<col />
+<col span='5' align='right' />
+<thead><tr>
+ <td>Rules</td>
+ <td>Total</td>
+ <td>Success</td>
+ <td>Errors</td>
+ <td>Can't test</td>
+ <td>Unknown</td>
+</tr></thead>
+<tbody>
+ """
+ rules = sorted(self.keys())
+ for r in rules:
+ s+=self[r].toHtml()
+ s+="""
+</tbody></table>"""
+ return s;
+
class TestCase:
- def __init__(self, directory):
+ def __init__(self, directory, executable):
self.directory = directory # Absolute directory
+ self.executable = executable
self.name = self._getName()
self._whichRules()
name = self.directory
if name.startswith(testsRoot+"/"):
name = name[len(testsRoot)+1:]
+ testSuffix = re.match("^runtest[-_. :]*(.*)", self.executable).group(1)
+ if testSuffix:
+ name+=" "+testSuffix
return name
def _whichRules(self):
rules to test"""
self.rules = []
try:
- f = open(os.path.join(self.directory, 'rules'))
+ f = open(os.path.join(self.directory, self.executable+'.rules'))
except IOError:
self.rules = rulesdef.rules.keys()
return
self.rules.append(r)
else:
# rule name
+ line = line.strip()
if line in rulesdef.rules: self.rules = [ line ]
#print self.rules
def run(self):
self.results = TestCaseResult(self.name)
- print "Testing %s:" % self.name,
+ print "Testing %s:\n\t" % self.name,
os.chdir(os.path.join(testsRoot, self.directory))
# if os.path.exists("Makefile.test"):
# self._exec = self._execMake
- if os.path.exists("runtest"):
+ if os.path.exists(self.executable):
self._exec = self._execRuntest
else: return
for rules in self.rules:
def _execRuntest(self, log):
startTime = time.clock()
- pipe = subprocess.Popen("./runtest", stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
+ pipe = subprocess.Popen("./"+self.executable, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
endTime = time.clock()
(output, errors) = pipe.communicate()
ret = pipe.returncode
print ret,
-testsRoot = os.path.dirname(os.path.abspath(__file__))
-if not os.path.exists(os.path.join(testsRoot, "runtests.py")): raise "Can't find tests root directory!"
-os.environ['OMK_TESTSROOT'] = testsRoot
-
results = Results()
-for dirpath, dirnames, filenames in os.walk(testsRoot):
- if not ("Makefile.test" in filenames or \
- "runtest" in filenames):
- continue
- t = TestCase(dirpath)
- t.run()
- results[t.name] = t.results
+for dirpath, dirnames, filenames in os.walk(invokeDir):
+ executables = fnmatch.filter(filenames, "runtest*")
+ if not executables: continue
+ for exe in executables:
+ if exe[-1] == "~": continue
+ if re.search(".rules$", exe): continue
+ t = TestCase(dirpath, exe)
+ t.run()
+ results[t.name] = t.results
-os.chdir(testsRoot)
+os.chdir(invokeDir)
+results.genStats()
results.save()
# Local Variables:
-# compile-command: "python runtests.py"
+# compile-command: "python tester.py"
# End: