diff --git a/.gitignore b/.gitignore index 0dc5fd950..d8795fa12 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,5 @@ config.log Cantera/matlab/build_cantera.m *.gcda *.gcno +coverage +coverage.info diff --git a/test_problems/SConscript b/test_problems/SConscript index 18f144b78..11eba570d 100644 --- a/test_problems/SConscript +++ b/test_problems/SConscript @@ -39,6 +39,9 @@ class Test(object): localenv.Alias('test-%s' % self.testName, run) testNames.append(self.testName) + # reset: just delete the ".passed" file so that this test will be re-run + localenv.Alias('test-reset', self.reset(localenv)) + def run(self, env, *args): source = list(args) if not source: @@ -56,6 +59,13 @@ class Test(object): test_ignoreLines=self.ignoreLines) return test + def reset(self, env, **kwargs): + f = pjoin(os.getcwd(), self.subdir, self.passedFile) + if os.path.exists(f): + uniqueName = 'reset-%s' % self.testName + target = env.Command(uniqueName, [], [Delete(f)]) + return target + def clean(self, env, **kwargs): # Name used for the output file if self.blessedName is not None and 'blessed' in self.blessedName: diff --git a/tools/coverage.py b/tools/coverage.py new file mode 100755 index 000000000..d09b05c83 --- /dev/null +++ b/tools/coverage.py @@ -0,0 +1,84 @@ +#!/usr/bin/python + +""" +Collect test coverage data and generate an html report. +""" + +import os +import subprocess +import shutil + +def getDirectories(): + """ + Return a list of all directories containing coverage data. + """ + sourcedirs = set() + rootdir = os.getcwd() + for dirpath, dirnames, filenames in os.walk(rootdir): + for fname in filenames: + if fname.endswith('.gcda'): + dirpath.replace(rootdir, '', 1) + sourcedirs.add(dirpath) + + return sourcedirs + + +def clean(): + """ + Remove all coverage data. + """ + sourcedirs = getDirectories() + if not sourcedirs: + return + + command = ['lcov', '--zerocounters'] + for d in sourcedirs: + command.append('-d') + command.append(d) + subprocess.call(command) + + +def test(): + """ + Run the full test suite. + """ + subprocess.call(['scons', 'test-reset']) + subprocess.call(['scons', 'test']) + + +def collect(): + """ + Collect the generated coverage data into 'coverage.info' + """ + sourcedirs = getDirectories() + if not sourcedirs: + print "Warning! Didn't find any coverage data." + return + + command = ['lcov', '-c', + '-b', '.', + '-o', 'coverage.info'] + for d in sourcedirs: + command.append('-d') + command.append(d) + subprocess.call(command) + + +def genhtml(): + """ + Produce an html report from the collected data. + """ + if os.path.exists('coverage'): + shutil.rmtree('coverage') + + os.mkdir('coverage') + subprocess.call(['genhtml', 'coverage.info', + '-o', 'coverage', + '-p', os.getcwd()]) + + +if __name__ == '__main__': + clean() + test() + collect() + genhtml()