OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Runs Go unit tests verifying code coverage. | 6 """Runs Go unit tests verifying code coverage. |
7 | 7 |
8 Expects Go toolset to be in PATH, GOPATH and GOROOT correctly set. Use ./env.py | 8 Expects Go toolset to be in PATH, GOPATH and GOROOT correctly set. Use ./env.py |
9 to set them up. | 9 to set them up. |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... | |
25 | 25 |
26 | 26 |
27 # infra/go/ | 27 # infra/go/ |
28 INFRA_GO_DIR = os.path.dirname(os.path.abspath(__file__)) | 28 INFRA_GO_DIR = os.path.dirname(os.path.abspath(__file__)) |
29 | 29 |
30 # Allowed keys in *.infra_testing dict. | 30 # Allowed keys in *.infra_testing dict. |
31 EXPECTED_INFO_KEYS = frozenset([ | 31 EXPECTED_INFO_KEYS = frozenset([ |
32 'skip_testing', | 32 'skip_testing', |
33 'expected_coverage_min', | 33 'expected_coverage_min', |
34 'expected_coverage_max', | 34 'expected_coverage_max', |
35 'build_tags', | |
35 ]) | 36 ]) |
36 | 37 |
37 | 38 |
38 # Return value of run_package_tests. | 39 # Return value of run_package_tests. |
39 TestResults = collections.namedtuple('TestResults', [ | 40 TestResults = collections.namedtuple('TestResults', [ |
40 # Name of the package being tested. | 41 # Name of the package being tested. |
41 'package', | 42 'package', |
42 # True if all unit tests pass. | 43 # True if all unit tests pass. |
43 'tests_pass', | 44 'tests_pass', |
44 # Percentage of source code covered, or None if not available. | 45 # Percentage of source code covered, or None if not available. |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 | 133 |
133 if package not in _package_info_cache: | 134 if package not in _package_info_cache: |
134 _package_info_cache[package] = do_read() | 135 _package_info_cache[package] = do_read() |
135 return _package_info_cache[package] | 136 return _package_info_cache[package] |
136 | 137 |
137 | 138 |
138 def should_skip(package): | 139 def should_skip(package): |
139 """True to skip package tests, reads 'skip_testing' from *.infra_testing.""" | 140 """True to skip package tests, reads 'skip_testing' from *.infra_testing.""" |
140 return get_package_info(package).get('skip_testing', False) | 141 return get_package_info(package).get('skip_testing', False) |
141 | 142 |
143 def get_build_tags(package): | |
144 """True to skip package tests, reads 'skip_testing' from *.infra_testing.""" | |
145 tags = get_package_info(package).get('build_tags', ()) | |
146 if tags: | |
147 return '-tags='+(','.join(tags)) | |
148 return None | |
142 | 149 |
143 def get_expected_coverage(package): | 150 def get_expected_coverage(package): |
144 """Returns allowed code coverage percentage as a pair (min, max).""" | 151 """Returns allowed code coverage percentage as a pair (min, max).""" |
145 info = get_package_info(package) | 152 info = get_package_info(package) |
146 min_cover = info.get('expected_coverage_min', 100.0) | 153 min_cover = info.get('expected_coverage_min', 100.0) |
147 max_cover = info.get('expected_coverage_max', 100.0) | 154 max_cover = info.get('expected_coverage_max', 100.0) |
148 if max_cover < min_cover: | 155 if max_cover < min_cover: |
149 max_cover = min_cover | 156 max_cover = min_cover |
150 return (min_cover, max_cover) | 157 return (min_cover, max_cover) |
151 | 158 |
(...skipping 14 matching lines...) Expand all Loading... | |
166 Args: | 173 Args: |
167 package: package name (e.g. infra/package). | 174 package: package name (e.g. infra/package). |
168 coverage_file: prefix for code coverage files (*.out and *.html). | 175 coverage_file: prefix for code coverage files (*.out and *.html). |
169 | 176 |
170 Returns: | 177 Returns: |
171 TestResults tuple. | 178 TestResults tuple. |
172 """ | 179 """ |
173 assert os.path.isabs(coverage_file), coverage_file | 180 assert os.path.isabs(coverage_file), coverage_file |
174 | 181 |
175 # Ask go test to collect coverage to a file, to convert it to HTML later. | 182 # Ask go test to collect coverage to a file, to convert it to HTML later. |
176 cmd = ['go', 'test', package] | 183 cmd = ['go', 'test', package, ] |
Vadim Sh.
2015/03/30 06:05:40
nit: remove ", "
| |
184 build_tags = get_build_tags(package) | |
185 if build_tags: | |
186 cmd.append(build_tags) | |
177 makedirs(os.path.dirname(coverage_file)) | 187 makedirs(os.path.dirname(coverage_file)) |
178 coverage_out = '%s.out' % coverage_file | 188 coverage_out = '%s.out' % coverage_file |
179 cmd.extend(['-coverprofile', coverage_out]) | 189 cmd.extend(['-coverprofile', coverage_out]) |
180 | 190 |
181 # Run the test. | 191 # Run the test. |
182 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 192 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
183 out, err = proc.communicate() | 193 out, err = proc.communicate() |
184 if proc.returncode: | 194 if proc.returncode: |
185 return TestResults( | 195 return TestResults( |
186 package=package, | 196 package=package, |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
317 elif len(args) == 1: | 327 elif len(args) == 1: |
318 package_root = args[0] | 328 package_root = args[0] |
319 else: | 329 else: |
320 print >> sys.stderr, sys.modules['__main__'].__doc__.strip() | 330 print >> sys.stderr, sys.modules['__main__'].__doc__.strip() |
321 return 1 | 331 return 1 |
322 return run_tests(package_root, os.path.join(INFRA_GO_DIR, 'coverage')) | 332 return run_tests(package_root, os.path.join(INFRA_GO_DIR, 'coverage')) |
323 | 333 |
324 | 334 |
325 if __name__ == '__main__': | 335 if __name__ == '__main__': |
326 sys.exit(main(sys.argv[1:])) | 336 sys.exit(main(sys.argv[1:])) |
OLD | NEW |