OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 all unit tests under this base directory.""" | 6 """Runs all unit tests under this base directory.""" |
7 | 7 |
8 import os | 8 import os |
9 import subprocess | 9 import subprocess |
10 import sys | 10 import sys |
11 import unittest | 11 import unittest |
12 | 12 |
13 | 13 |
14 BUILDBOT_PATH = os.path.dirname(os.path.abspath(__file__)) | 14 BUILDBOT_PATH = os.path.dirname(os.path.abspath(__file__)) |
15 | 15 |
16 | 16 |
17 GO_TESTS = [ | 17 GO_DIRS = [ |
18 '.', | 18 '.', |
19 'ct', | |
tfarina
2014/12/05 16:25:12
maybe we should start sorting this alphabetical? C
borenet
2014/12/05 18:14:06
Alphabetized. Now that goimports and "go vet" wor
| |
19 'perf', | 20 'perf', |
20 'monitoring', | 21 'monitoring', |
21 'golden', | 22 'golden', |
22 ] | 23 ] |
23 | 24 |
24 GO_TEST_FAILED = ( | 25 GO_TEST_FAILED = ( |
25 '''====================================================================== | 26 '''====================================================================== |
26 Go test failed: %s | 27 Go test failed: %s |
28 CWD: %s | |
27 ---------------------------------------------------------------------- | 29 ---------------------------------------------------------------------- |
28 %s | 30 %s |
29 ---------------------------------------------------------------------- | 31 ---------------------------------------------------------------------- |
30 ''') | 32 ''') |
31 | 33 |
32 NO_CRAWL_DIRS = [ | 34 NO_CRAWL_DIRS = [ |
33 '.git', | 35 '.git', |
34 '.svn', | 36 '.svn', |
35 'autoroll_git', | 37 'autoroll_git', |
36 'common', | 38 'common', |
37 'git_poller_skia.git', | 39 'git_poller_skia.git', |
38 'third_party', | 40 'third_party', |
39 ] | 41 ] |
40 | 42 |
41 | 43 |
42 def FilterDirectory(dirpath, filenames): | 44 def FilterDirectory(dirpath, filenames): |
43 """ Determine whether to look for tests in the given directory. | 45 """ Determine whether to look for tests in the given directory. |
44 | 46 |
45 dirpath: string; path of the directory in question. | 47 dirpath: string; path of the directory in question. |
46 filenames: list of strings; the files in the directory. | 48 filenames: list of strings; the files in the directory. |
47 """ | 49 """ |
48 if not dirpath or not filenames: | 50 if not dirpath or not filenames: |
49 return False | 51 return False |
50 for no_crawl_dir in NO_CRAWL_DIRS: | 52 for no_crawl_dir in NO_CRAWL_DIRS: |
51 if no_crawl_dir in dirpath: | 53 if no_crawl_dir in dirpath: |
52 return False | 54 return False |
53 return True | 55 return True |
54 | 56 |
55 | 57 |
56 def TestGo(testname): | 58 def RunGoTest(cmd, cwd): |
57 # TODO(borenet): Switch to 'make test' once we're sure the karma-based tests | 59 p = subprocess.Popen(cmd, cwd=cwd, |
58 # will run in headless mode. | |
59 p = subprocess.Popen(['make', 'testgo'], cwd=testname, | |
60 stderr=subprocess.STDOUT, | 60 stderr=subprocess.STDOUT, |
61 stdout=subprocess.PIPE) | 61 stdout=subprocess.PIPE) |
62 output = p.communicate()[0] | 62 output = p.communicate()[0] |
63 if p.returncode != 0: | 63 if p.returncode != 0: |
64 return GO_TEST_FAILED % (testname, output) | 64 return [GO_TEST_FAILED % (' '.join(cmd), cwd, output)] |
65 return None | 65 return [] |
66 | 66 |
67 | 67 |
68 def GoImports(): | 68 def GoTests(cwd): |
69 # TODO(borenet): Switch to 'make test' once we're sure the karma-based tests | |
70 # will run in headless mode. | |
71 return RunGoTest(['make', 'testgo'], cwd) | |
72 | |
73 | |
74 def GoVet(): | |
75 return RunGoTest(['go', 'vet', './...'], '.') | |
76 | |
77 | |
78 def GoImports(cwd): | |
79 cmd = ['goimports', '-l', 'go'] | |
69 try: | 80 try: |
70 diff_files = subprocess.check_output(['goimports', '-l', '.'], | 81 diff_files = subprocess.check_output(cmd, cwd=cwd, |
71 stderr=subprocess.PIPE).splitlines() | 82 stderr=subprocess.PIPE).splitlines() |
72 except subprocess.CalledProcessError: | 83 except subprocess.CalledProcessError: |
73 return ('goimports failed to run! Is it installed? You may need to run: \n' | 84 return [GO_TEST_FAILED % ( |
74 'go get http://code.google.com/p/go.tools/cmd/goimports') | 85 ' '.join(cmd), cwd, |
86 ('goimports failed to run! Is it installed? You may need to run:\n' | |
87 'go get http://code.google.com/p/go.tools/cmd/goimports'))] | |
75 | 88 |
76 if len(diff_files) > 0: | 89 if len(diff_files) > 0: |
77 return ('goimports found diffs in the following files: %s' % | 90 return [GO_TEST_FAILED % ( |
78 ', '.join(diff_files)) | 91 ' '.join(cmd), cwd, |
79 return None | 92 ('goimports found diffs in the following files: %s' % |
93 ', '.join(diff_files)))] | |
94 return [] | |
80 | 95 |
81 | 96 |
82 if __name__ == '__main__': | 97 if __name__ == '__main__': |
83 print 'Searching for tests.' | 98 print 'Searching for tests.' |
84 tests_to_run = [] | 99 tests_to_run = [] |
85 | 100 |
86 for (dirpath, dirnames, filenames) in os.walk(BUILDBOT_PATH, topdown=True): | 101 for (dirpath, dirnames, filenames) in os.walk(BUILDBOT_PATH, topdown=True): |
87 dirnames[:] = [d for d in dirnames if not d in NO_CRAWL_DIRS] | 102 dirnames[:] = [d for d in dirnames if not d in NO_CRAWL_DIRS] |
88 test_modules = [os.path.join(dirpath, filename) for filename in filenames | 103 test_modules = [os.path.join(dirpath, filename) for filename in filenames |
89 if filename.endswith('_test.py')] | 104 if filename.endswith('_test.py')] |
90 if not test_modules: | 105 if not test_modules: |
91 continue | 106 continue |
92 tests_to_run.extend(test_modules) | 107 tests_to_run.extend(test_modules) |
93 | 108 |
94 builtin_tests = [GoImports] | 109 go_tests = [GoVet] |
110 go_tests_cwd = [GoTests, | |
111 GoImports] | |
95 | 112 |
96 num_tests = len(tests_to_run) + len(GO_TESTS) + len(builtin_tests) | 113 num_tests = (len(tests_to_run) + len(go_tests) + |
114 len(GO_DIRS) * len(go_tests_cwd)) | |
97 print 'Found %d tests.' % num_tests | 115 print 'Found %d tests.' % num_tests |
98 errors = [] | 116 errors = [] |
99 for test in tests_to_run: | 117 for test in tests_to_run: |
100 proc = subprocess.Popen(['python', test], stdout=subprocess.PIPE, | 118 proc = subprocess.Popen(['python', test], stdout=subprocess.PIPE, |
101 stderr=subprocess.STDOUT) | 119 stderr=subprocess.STDOUT) |
102 if proc.wait() != 0: | 120 if proc.wait() != 0: |
103 errors.append(proc.communicate()[0]) | 121 errors.append(proc.communicate()[0]) |
104 | 122 |
105 for go_test in GO_TESTS: | 123 for go_dir in GO_DIRS: |
106 error = TestGo(go_test) | 124 for test in go_tests_cwd: |
107 if error: | 125 errors.extend(test(go_dir)) |
108 errors.append(error) | 126 for go_test in go_tests: |
109 | 127 errors.extend(go_test()) |
110 for builtin_test in builtin_tests: | |
111 error = builtin_test() | |
112 if error: | |
113 errors.append(error) | |
114 | 128 |
115 if errors: | 129 if errors: |
116 for error in errors: | 130 for error in errors: |
117 print error | 131 print error |
118 print 'Failed %d of %d.' % (len(errors), num_tests) | |
119 sys.exit(1) | 132 sys.exit(1) |
120 else: | 133 else: |
121 print 'All tests succeeded.' | 134 print 'All tests succeeded.' |
OLD | NEW |