1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#!/usr/bin/env python
# Copyright (c) 2009 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Generate and process code coverage on POSIX systems.
Written for and tested on Mac and Linux. To use this script to
generate coverage numbers, please run from within a gyp-generated
project.
All platforms, to set up coverage:
cd ...../chromium ; src/tools/gyp/gyp_dogfood -Dcoverage=1 src/build/all.gyp
Run coverage on...
Mac:
( cd src/chrome ; xcodebuild -configuration Debug -target coverage )
Linux:
( cd src/chrome ; hammer coverage )
# In particular, don't try and run 'coverage' from src/build
--directory=DIR: specify directory that contains gcda files, and where
a "coverage" directory will be created containing the output html.
Example name: ..../chromium/src/xcodebuild/Debug
--genhtml: generate html output. If not specified only lcov is generated.
--all_unittests: if present, run all files named *_unittests that we
can find.
--fast_test: make the tests run real fast (just for testing)
--strict: if a test fails, we continue happily. --strict will cause
us to die immediately.
Strings after all options are considered tests to run. Test names
have all text before a ':' stripped to help with gyp compatibility.
For example, ../base/base.gyp:base_unittests is interpreted as a test
named "base_unittests".
"""
import glob
import logging
import optparse
import os
import shutil
import subprocess
import sys
class Coverage(object):
"""Doitall class for code coverage."""
def __init__(self, directory, options, args):
super(Coverage, self).__init__()
self.directory = directory
self.options = options
self.args = args
self.directory_parent = os.path.dirname(self.directory)
self.output_directory = os.path.join(self.directory, 'coverage')
if not os.path.exists(self.output_directory):
os.mkdir(self.output_directory)
self.lcov_directory = os.path.join(sys.path[0],
'../../third_party/lcov/bin')
self.lcov = os.path.join(self.lcov_directory, 'lcov')
self.mcov = os.path.join(self.lcov_directory, 'mcov')
self.genhtml = os.path.join(self.lcov_directory, 'genhtml')
self.coverage_info_file = os.path.join(self.directory, 'coverage.info')
self.ConfirmPlatformAndPaths()
self.tests = []
def FindTests(self):
"""Find unit tests to run; set self.tests to this list.
Assume all non-option items in the arg list are tests to be run.
"""
# Small tests: can be run in the "chromium" directory.
# If asked, run all we can find.
if self.options.all_unittests:
self.tests += glob.glob(os.path.join(self.directory, '*_unittests'))
# If told explicit tests, run those (after stripping the name as
# appropriate)
for testname in self.args:
if ':' in testname:
self.tests += [os.path.join(self.directory, testname.split(':')[1])]
else:
self.tests += [os.path.join(self.directory, testname)]
# Needs to be run in the "chrome" directory?
# ut = os.path.join(self.directory, 'unit_tests')
# if os.path.exists(ut):
# self.tests.append(ut)
# Medium tests?
# Not sure all of these work yet (e.g. page_cycler_tests)
# self.tests += glob.glob(os.path.join(self.directory, '*_tests'))
def ConfirmPlatformAndPaths(self):
"""Confirm OS and paths (e.g. lcov)."""
if not self.IsPosix():
logging.fatal('Not posix.')
sys.exit(1)
programs = [self.lcov, self.genhtml]
if self.IsMac():
programs.append(self.mcov)
for program in programs:
if not os.path.exists(program):
logging.fatal('lcov program missing: ' + program)
sys.exit(1)
def IsPosix(self):
"""Return True if we are POSIX."""
return self.IsMac() or self.IsLinux()
def IsMac(self):
return sys.platform == 'darwin'
def IsLinux(self):
return sys.platform == 'linux2'
def ClearData(self):
"""Clear old gcda files"""
subprocess.call([self.lcov,
'--directory', self.directory_parent,
'--zerocounters'])
shutil.rmtree(os.path.join(self.directory, 'coverage'))
def RunTests(self):
"""Run all unit tests."""
for fulltest in self.tests:
if not os.path.exists(fulltest):
logging.fatal(fulltest + ' does not exist')
if self.options.strict:
sys.exit(2)
# TODO(jrg): add timeout?
print >>sys.stderr, 'Running test: ' + fulltest
cmdlist = [fulltest, '--gtest_print_time']
# If asked, make this REAL fast for testing.
if self.options.fast_test:
cmdlist.append('--gtest_filter=TupleTest*')
retcode = subprocess.call(cmdlist)
if retcode:
logging.fatal('COVERAGE: test %s failed; return code: %d' %
(fulltest, retcode))
if self.options.strict:
sys.exit(retcode)
def GenerateLcov(self):
"""Convert profile data to lcov."""
if self.IsLinux():
command = [self.lcov,
'--directory', self.directory,
'--capture',
'--output-file',
self.coverage_info_file]
else:
command = [self.mcov,
'--directory', self.directory_parent,
'--output', self.coverage_info_file]
print >>sys.stderr, 'Assembly command: ' + ' '.join(command)
retcode = subprocess.call(command)
if retcode:
logging.fatal('COVERAGE: %s failed; return code: %d' %
(command[0], retcode))
if self.options.strict:
sys.exit(retcode)
def GenerateHtml(self):
"""Convert lcov to html."""
# TODO(jrg): This isn't happy when run with unit_tests since V8 has a
# different "base" so V8 includes can't be found in ".". Fix.
command = [self.genhtml,
self.coverage_info_file,
'--output-directory',
self.output_directory]
print >>sys.stderr, 'html generation command: ' + ' '.join(command)
retcode = subprocess.call(command)
if retcode:
logging.fatal('COVERAGE: %s failed; return code: %d' %
(command[0], retcode))
if self.options.strict:
sys.exit(retcode)
def main():
# Print out the args to help someone do it by hand if needed
print >>sys.stderr, sys.argv
parser = optparse.OptionParser()
parser.add_option('-d',
'--directory',
dest='directory',
default=None,
help='Directory of unit test files')
parser.add_option('-a',
'--all_unittests',
dest='all_unittests',
default=False,
help='Run all tests we can find (*_unittests)')
parser.add_option('-g',
'--genhtml',
dest='genhtml',
default=False,
help='Generate html from lcov output')
parser.add_option('-f',
'--fast_test',
dest='fast_test',
default=False,
help='Make the tests run REAL fast by doing little.')
parser.add_option('-s',
'--strict',
dest='strict',
default=False,
help='Be strict and die on test failure.')
(options, args) = parser.parse_args()
if not options.directory:
parser.error('Directory not specified')
coverage = Coverage(options.directory, options, args)
coverage.ClearData()
coverage.FindTests()
coverage.RunTests()
coverage.GenerateLcov()
if options.genhtml:
coverage.GenerateHtml()
return 0
if __name__ == '__main__':
sys.exit(main())
|