OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Generate and process code coverage on POSIX systems. |
| 7 |
| 8 Written for and tested on Mac. |
| 9 Not tested on Linux yet. |
| 10 |
| 11 --directory=DIR: specify directory that contains gcda files, and where |
| 12 a "coverage" directory will be created containing the output html. |
| 13 |
| 14 TODO(jrg): make list of unit tests an arg to this script |
| 15 """ |
| 16 |
| 17 import logging |
| 18 import optparse |
| 19 import os |
| 20 import subprocess |
| 21 import sys |
| 22 |
| 23 class Coverage(object): |
| 24 """Doitall class for code coverage.""" |
| 25 |
| 26 # Unit test files to run. |
| 27 UNIT_TESTS = [ |
| 28 'base_unittests', |
| 29 # 'unit_tests, |
| 30 ] |
| 31 |
| 32 def __init__(self, directory): |
| 33 super(Coverage, self).__init__() |
| 34 self.directory = directory |
| 35 self.output_directory = os.path.join(self.directory, 'coverage') |
| 36 if not os.path.exists(self.output_directory): |
| 37 os.mkdir(self.output_directory) |
| 38 self.lcov_directory = os.path.join(sys.path[0], |
| 39 '../../third_party/lcov/bin') |
| 40 self.lcov = os.path.join(self.lcov_directory, 'lcov') |
| 41 self.mcov = os.path.join(self.lcov_directory, 'mcov') |
| 42 self.genhtml = os.path.join(self.lcov_directory, 'genhtml') |
| 43 self.coverage_info_file = self.directory + 'coverage.info' |
| 44 self.ConfirmPlatformAndPaths() |
| 45 |
| 46 def ConfirmPlatformAndPaths(self): |
| 47 """Confirm OS and paths (e.g. lcov).""" |
| 48 if not self.IsPosix(): |
| 49 logging.fatal('Not posix.') |
| 50 programs = [self.lcov, self.genhtml] |
| 51 if self.IsMac(): |
| 52 programs.append(self.mcov) |
| 53 for program in programs: |
| 54 if not os.path.exists(program): |
| 55 logging.fatal('lcov program missing: ' + program) |
| 56 |
| 57 def IsPosix(self): |
| 58 """Return True if we are POSIX.""" |
| 59 return self.IsMac() or self.IsLinux() |
| 60 |
| 61 def IsMac(self): |
| 62 return sys.platform == 'darwin' |
| 63 |
| 64 def IsLinux(self): |
| 65 return sys.platform == 'linux2' |
| 66 |
| 67 def ClearData(self): |
| 68 """Clear old gcda files""" |
| 69 subprocess.call([self.lcov, |
| 70 '--directory', self.directory, |
| 71 '--zerocounters']) |
| 72 |
| 73 def RunTests(self): |
| 74 """Run all unit tests.""" |
| 75 for test in self.UNIT_TESTS: |
| 76 fulltest = os.path.join(self.directory, test) |
| 77 if not os.path.exists(fulltest): |
| 78 logging.fatal(fulltest + ' does not exist') |
| 79 # TODO(jrg): add timeout? |
| 80 # TODO(jrg): check return value and choke if it failed? |
| 81 # TODO(jrg): add --gtest_print_time like as run from XCode? |
| 82 subprocess.call([fulltest]) |
| 83 |
| 84 def GenerateOutput(self): |
| 85 """Convert profile data to html.""" |
| 86 if self.IsLinux(): |
| 87 subprocess.call([self.lcov, |
| 88 '--directory', self.directory, |
| 89 '--capture', |
| 90 '--output-file', |
| 91 self.coverage_info_file]) |
| 92 else: |
| 93 subprocess.call([self.mcov, |
| 94 '--directory', self.directory, |
| 95 '--output', self.coverage_info_file]) |
| 96 subprocess.call([self.genhtml, |
| 97 self.coverage_info_file, |
| 98 '--output-directory', |
| 99 self.output_directory]) |
| 100 |
| 101 def main(): |
| 102 parser = optparse.OptionParser() |
| 103 parser.add_option('-d', |
| 104 '--directory', |
| 105 dest='directory', |
| 106 default=None, |
| 107 help='Directory of unit test files') |
| 108 (options, args) = parser.parse_args() |
| 109 if not options.directory: |
| 110 parser.error('Directory not specified') |
| 111 |
| 112 coverage = Coverage(options.directory) |
| 113 coverage.ClearData() |
| 114 coverage.RunTests() |
| 115 coverage.GenerateOutput() |
| 116 return 0 |
| 117 |
| 118 |
| 119 if __name__ == '__main__': |
| 120 sys.exit(main()) |
OLD | NEW |