| 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 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 | 28 |
| 29 # infra/go/ | 29 # infra/go/ |
| 30 INFRA_GO_DIR = os.path.dirname(os.path.abspath(__file__)) | 30 INFRA_GO_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 31 | 31 |
| 32 # Allowed keys in *.infra_testing dict. | 32 # Allowed keys in *.infra_testing dict. |
| 33 EXPECTED_INFO_KEYS = frozenset([ | 33 EXPECTED_INFO_KEYS = frozenset([ |
| 34 'skip_testing', | 34 'skip_testing', |
| 35 'expected_coverage_min', | 35 'expected_coverage_min', |
| 36 'expected_coverage_max', | 36 'expected_coverage_max', |
| 37 'build_tags', |
| 37 ]) | 38 ]) |
| 38 | 39 |
| 39 | 40 |
| 40 # Return value of run_package_tests. | 41 # Return value of run_package_tests. |
| 41 TestResults = collections.namedtuple('TestResults', [ | 42 TestResults = collections.namedtuple('TestResults', [ |
| 42 # Name of the package being tested. | 43 # Name of the package being tested. |
| 43 'package', | 44 'package', |
| 44 # True if all unit tests pass. | 45 # True if all unit tests pass. |
| 45 'tests_pass', | 46 'tests_pass', |
| 46 # Percentage of source code covered, or None if not available. | 47 # Percentage of source code covered, or None if not available. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 | 135 |
| 135 if package not in _package_info_cache: | 136 if package not in _package_info_cache: |
| 136 _package_info_cache[package] = do_read() | 137 _package_info_cache[package] = do_read() |
| 137 return _package_info_cache[package] | 138 return _package_info_cache[package] |
| 138 | 139 |
| 139 | 140 |
| 140 def should_skip(package): | 141 def should_skip(package): |
| 141 """True to skip package tests, reads 'skip_testing' from *.infra_testing.""" | 142 """True to skip package tests, reads 'skip_testing' from *.infra_testing.""" |
| 142 return get_package_info(package).get('skip_testing', False) | 143 return get_package_info(package).get('skip_testing', False) |
| 143 | 144 |
| 145 def get_build_tags(package): |
| 146 """True to skip package tests, reads 'skip_testing' from *.infra_testing.""" |
| 147 tags = get_package_info(package).get('build_tags', ()) |
| 148 if tags: |
| 149 return '-tags='+(','.join(tags)) |
| 150 return None |
| 144 | 151 |
| 145 def get_expected_coverage(package): | 152 def get_expected_coverage(package): |
| 146 """Returns allowed code coverage percentage as a pair (min, max).""" | 153 """Returns allowed code coverage percentage as a pair (min, max).""" |
| 147 info = get_package_info(package) | 154 info = get_package_info(package) |
| 148 min_cover = info.get('expected_coverage_min', 100.0) | 155 min_cover = info.get('expected_coverage_min', 100.0) |
| 149 max_cover = info.get('expected_coverage_max', 100.0) | 156 max_cover = info.get('expected_coverage_max', 100.0) |
| 150 if max_cover < min_cover: | 157 if max_cover < min_cover: |
| 151 max_cover = min_cover | 158 max_cover = min_cover |
| 152 return (min_cover, max_cover) | 159 return (min_cover, max_cover) |
| 153 | 160 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 169 package: package name (e.g. infra/package). | 176 package: package name (e.g. infra/package). |
| 170 coverage_file: prefix for code coverage files (*.out and *.html). | 177 coverage_file: prefix for code coverage files (*.out and *.html). |
| 171 | 178 |
| 172 Returns: | 179 Returns: |
| 173 TestResults tuple. | 180 TestResults tuple. |
| 174 """ | 181 """ |
| 175 assert os.path.isabs(coverage_file), coverage_file | 182 assert os.path.isabs(coverage_file), coverage_file |
| 176 | 183 |
| 177 # Ask go test to collect coverage to a file, to convert it to HTML later. | 184 # Ask go test to collect coverage to a file, to convert it to HTML later. |
| 178 cmd = ['go', 'test', package] | 185 cmd = ['go', 'test', package] |
| 186 build_tags = get_build_tags(package) |
| 187 if build_tags: |
| 188 cmd.append(build_tags) |
| 179 makedirs(os.path.dirname(coverage_file)) | 189 makedirs(os.path.dirname(coverage_file)) |
| 180 coverage_out = '%s.out' % coverage_file | 190 coverage_out = '%s.out' % coverage_file |
| 181 cmd.extend(['-coverprofile', coverage_out]) | 191 cmd.extend(['-coverprofile', coverage_out]) |
| 182 | 192 |
| 183 # Run the test. | 193 # Run the test. |
| 184 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 194 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 185 out, err = proc.communicate() | 195 out, err = proc.communicate() |
| 186 if proc.returncode: | 196 if proc.returncode: |
| 187 return TestResults( | 197 return TestResults( |
| 188 package=package, | 198 package=package, |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 elif len(args) == 1: | 328 elif len(args) == 1: |
| 319 package_root = args[0] | 329 package_root = args[0] |
| 320 else: | 330 else: |
| 321 print >> sys.stderr, sys.modules['__main__'].__doc__.strip() | 331 print >> sys.stderr, sys.modules['__main__'].__doc__.strip() |
| 322 return 1 | 332 return 1 |
| 323 return run_tests(package_root, os.path.join(INFRA_GO_DIR, 'coverage')) | 333 return run_tests(package_root, os.path.join(INFRA_GO_DIR, 'coverage')) |
| 324 | 334 |
| 325 | 335 |
| 326 if __name__ == '__main__': | 336 if __name__ == '__main__': |
| 327 sys.exit(main(sys.argv[1:])) | 337 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |