| #!/usr/bin/env python |
| # Copyright (c) 2012 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. |
| |
| """Runs all unit tests under this base directory.""" |
| |
| import os |
| import subprocess |
| import sys |
| import unittest |
| |
| |
| BUILDBOT_PATH = os.path.dirname(os.path.abspath(__file__)) |
| |
| |
| GO_DIRS = [ |
| '.', |
| 'ct', |
| 'golden', |
| 'monitoring', |
| 'perf', |
| ] |
| |
| GO_TEST_FAILED = ( |
| '''====================================================================== |
| Go test failed: %s |
| CWD: %s |
| ---------------------------------------------------------------------- |
| %s |
| ---------------------------------------------------------------------- |
| ''') |
| |
| NO_CRAWL_DIRS = [ |
| '.git', |
| '.svn', |
| 'autoroll_git', |
| 'common', |
| 'git_poller_skia.git', |
| 'third_party', |
| ] |
| |
| |
| def FilterDirectory(dirpath, filenames): |
| """ Determine whether to look for tests in the given directory. |
| |
| dirpath: string; path of the directory in question. |
| filenames: list of strings; the files in the directory. |
| """ |
| if not dirpath or not filenames: |
| return False |
| for no_crawl_dir in NO_CRAWL_DIRS: |
| if no_crawl_dir in dirpath: |
| return False |
| return True |
| |
| |
| def RunGoTest(cmd, cwd): |
| p = subprocess.Popen(cmd, cwd=cwd, |
| stderr=subprocess.STDOUT, |
| stdout=subprocess.PIPE) |
| output = p.communicate()[0] |
| if p.returncode != 0: |
| return [GO_TEST_FAILED % (' '.join(cmd), cwd, output)] |
| return [] |
| |
| |
| def GoTests(cwd): |
| # TODO(borenet): Switch to 'make test' once we're sure the karma-based tests |
| # will run in headless mode. |
| return RunGoTest(['make', 'testgo'], cwd) |
| |
| |
| def GoVet(): |
| return RunGoTest(['go', 'vet', './...'], '.') |
| |
| |
| def GoImports(): |
| cmd = ['goimports', '-l', '.'] |
| try: |
| diff_files = subprocess.check_output(cmd, cwd='.', |
| stderr=subprocess.PIPE).splitlines() |
| except subprocess.CalledProcessError: |
| return [GO_TEST_FAILED % ( |
| ' '.join(cmd), '.', |
| ('goimports failed to run! Is it installed? You may need to run:\n' |
| 'go get http://code.google.com/p/go.tools/cmd/goimports'))] |
| |
| if len(diff_files) > 0: |
| return [GO_TEST_FAILED % ( |
| ' '.join(cmd), '.', |
| ('goimports found diffs in the following files: %s' % |
| ', '.join(diff_files)))] |
| return [] |
| |
| |
| if __name__ == '__main__': |
| print 'Searching for tests.' |
| tests_to_run = [] |
| |
| for (dirpath, dirnames, filenames) in os.walk(BUILDBOT_PATH, topdown=True): |
| dirnames[:] = [d for d in dirnames if not d in NO_CRAWL_DIRS] |
| test_modules = [os.path.join(dirpath, filename) for filename in filenames |
| if filename.endswith('_test.py')] |
| if not test_modules: |
| continue |
| tests_to_run.extend(test_modules) |
| |
| go_tests = [GoVet, GoImports] |
| go_tests_cwd = [GoTests] |
| |
| num_tests = (len(tests_to_run) + len(go_tests) + |
| len(GO_DIRS) * len(go_tests_cwd)) |
| print 'Found %d tests.' % num_tests |
| errors = [] |
| for test in tests_to_run: |
| proc = subprocess.Popen(['python', test], stdout=subprocess.PIPE, |
| stderr=subprocess.STDOUT) |
| if proc.wait() != 0: |
| errors.append(proc.communicate()[0]) |
| |
| for go_dir in GO_DIRS: |
| for test in go_tests_cwd: |
| errors.extend(test(go_dir)) |
| for go_test in go_tests: |
| errors.extend(go_test()) |
| |
| if errors: |
| for error in errors: |
| print error |
| sys.exit(1) |
| else: |
| print 'All tests succeeded.' |