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 'perf', | 19 'perf', |
20 'monitoring', | 20 'monitoring', |
21 'golden', | 21 'golden', |
22 ] | 22 ] |
23 | 23 |
24 GO_TEST_FAILED = ( | 24 GO_TEST_FAILED = ( |
25 '''====================================================================== | 25 '''====================================================================== |
26 Go test failed: %s | 26 Go test failed: %s |
27 CWD: %s | |
27 ---------------------------------------------------------------------- | 28 ---------------------------------------------------------------------- |
28 %s | 29 %s |
29 ---------------------------------------------------------------------- | 30 ---------------------------------------------------------------------- |
30 ''') | 31 ''') |
31 | 32 |
32 NO_CRAWL_DIRS = [ | 33 NO_CRAWL_DIRS = [ |
33 '.git', | 34 '.git', |
34 '.svn', | 35 '.svn', |
35 'autoroll_git', | 36 'autoroll_git', |
36 'common', | 37 'common', |
37 'git_poller_skia.git', | 38 'git_poller_skia.git', |
38 'third_party', | 39 'third_party', |
39 ] | 40 ] |
40 | 41 |
41 | 42 |
42 def FilterDirectory(dirpath, filenames): | 43 def FilterDirectory(dirpath, filenames): |
43 """ Determine whether to look for tests in the given directory. | 44 """ Determine whether to look for tests in the given directory. |
44 | 45 |
45 dirpath: string; path of the directory in question. | 46 dirpath: string; path of the directory in question. |
46 filenames: list of strings; the files in the directory. | 47 filenames: list of strings; the files in the directory. |
47 """ | 48 """ |
48 if not dirpath or not filenames: | 49 if not dirpath or not filenames: |
49 return False | 50 return False |
50 for no_crawl_dir in NO_CRAWL_DIRS: | 51 for no_crawl_dir in NO_CRAWL_DIRS: |
51 if no_crawl_dir in dirpath: | 52 if no_crawl_dir in dirpath: |
52 return False | 53 return False |
53 return True | 54 return True |
54 | 55 |
55 | 56 |
56 def TestGo(testname): | 57 def RunGoTest(cmd, cwd): |
57 # TODO(borenet): Switch to 'make test' once we're sure the karma-based tests | 58 p = subprocess.Popen(cmd, cwd=cwd, |
58 # will run in headless mode. | |
59 p = subprocess.Popen(['make', 'testgo'], cwd=testname, | |
60 stderr=subprocess.STDOUT, | 59 stderr=subprocess.STDOUT, |
61 stdout=subprocess.PIPE) | 60 stdout=subprocess.PIPE) |
62 output = p.communicate()[0] | 61 output = p.communicate()[0] |
63 if p.returncode != 0: | 62 if p.returncode != 0: |
64 return GO_TEST_FAILED % (testname, output) | 63 return [GO_TEST_FAILED % (' '.join(cmd), cwd, output)] |
65 return None | 64 return [] |
66 | 65 |
67 | 66 |
68 def GoImports(): | 67 def GoTests(cwd): |
68 # TODO(borenet): Switch to 'make test' once we're sure the karma-based tests | |
69 # will run in headless mode. | |
70 return RunGoTest(['make', 'testgo'], cwd) | |
71 | |
72 | |
73 def GoVet(cwd): | |
74 # We need to actually point to go packages. Find any directories in cwd | |
75 # which contain .go files and run "go vet" on them. | |
jcgregorio
2014/12/05 15:57:33
Doesn't this work?
$ go vet ./...
borenet
2014/12/05 16:07:00
It sure does!
| |
76 modules = [] | |
77 go_path = os.path.join(cwd, 'go') | |
78 for item in os.listdir(go_path): | |
79 item_path = os.path.join(go_path, item) | |
80 if os.path.isdir(item_path): | |
81 for f in os.listdir(item_path): | |
82 file_path = os.path.join(item_path, f) | |
83 if os.path.isfile(file_path) and f.endswith('.go'): | |
84 modules.append(item_path) | |
85 break | |
86 | |
87 errors = [] | |
88 for m in modules: | |
89 errors.extend(RunGoTest(['go', 'vet'], m)) | |
90 return errors | |
91 | |
92 | |
93 def GoImports(cwd): | |
94 cmd = ['goimports', '-l', 'go'] | |
69 try: | 95 try: |
70 diff_files = subprocess.check_output(['goimports', '-l', '.'], | 96 diff_files = subprocess.check_output(cmd, cwd=cwd, |
71 stderr=subprocess.PIPE).splitlines() | 97 stderr=subprocess.PIPE).splitlines() |
72 except subprocess.CalledProcessError: | 98 except subprocess.CalledProcessError: |
73 return ('goimports failed to run! Is it installed? You may need to run: \n' | 99 return [GO_TEST_FAILED % ( |
74 'go get http://code.google.com/p/go.tools/cmd/goimports') | 100 ' '.join(cmd), cwd, |
101 ('goimports failed to run! Is it installed? You may need to run:\n' | |
102 'go get http://code.google.com/p/go.tools/cmd/goimports'))] | |
75 | 103 |
76 if len(diff_files) > 0: | 104 if len(diff_files) > 0: |
77 return ('goimports found diffs in the following files: %s' % | 105 return [GO_TEST_FAILED % ( |
78 ', '.join(diff_files)) | 106 ' '.join(cmd), cwd, |
79 return None | 107 ('goimports found diffs in the following files: %s' % |
108 ', '.join(diff_files)))] | |
109 return [] | |
80 | 110 |
81 | 111 |
82 if __name__ == '__main__': | 112 if __name__ == '__main__': |
83 print 'Searching for tests.' | 113 print 'Searching for tests.' |
84 tests_to_run = [] | 114 tests_to_run = [] |
85 | 115 |
86 for (dirpath, dirnames, filenames) in os.walk(BUILDBOT_PATH, topdown=True): | 116 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] | 117 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 | 118 test_modules = [os.path.join(dirpath, filename) for filename in filenames |
89 if filename.endswith('_test.py')] | 119 if filename.endswith('_test.py')] |
90 if not test_modules: | 120 if not test_modules: |
91 continue | 121 continue |
92 tests_to_run.extend(test_modules) | 122 tests_to_run.extend(test_modules) |
93 | 123 |
94 builtin_tests = [GoImports] | 124 go_tests = [GoTests, |
125 GoImports, | |
126 GoVet] | |
95 | 127 |
96 num_tests = len(tests_to_run) + len(GO_TESTS) + len(builtin_tests) | 128 num_tests = len(tests_to_run) + len(GO_DIRS) * len(go_tests) |
97 print 'Found %d tests.' % num_tests | 129 print 'Found %d tests.' % num_tests |
98 errors = [] | 130 errors = [] |
99 for test in tests_to_run: | 131 for test in tests_to_run: |
100 proc = subprocess.Popen(['python', test], stdout=subprocess.PIPE, | 132 proc = subprocess.Popen(['python', test], stdout=subprocess.PIPE, |
101 stderr=subprocess.STDOUT) | 133 stderr=subprocess.STDOUT) |
102 if proc.wait() != 0: | 134 if proc.wait() != 0: |
103 errors.append(proc.communicate()[0]) | 135 errors.append(proc.communicate()[0]) |
104 | 136 |
105 for go_test in GO_TESTS: | 137 for go_dir in GO_DIRS: |
106 error = TestGo(go_test) | 138 for test in go_tests: |
107 if error: | 139 errors.extend(test(go_dir)) |
108 errors.append(error) | |
109 | |
110 for builtin_test in builtin_tests: | |
111 error = builtin_test() | |
112 if error: | |
113 errors.append(error) | |
114 | 140 |
115 if errors: | 141 if errors: |
116 for error in errors: | 142 for error in errors: |
117 print error | 143 print error |
118 print 'Failed %d of %d.' % (len(errors), num_tests) | |
119 sys.exit(1) | 144 sys.exit(1) |
120 else: | 145 else: |
121 print 'All tests succeeded.' | 146 print 'All tests succeeded.' |
OLD | NEW |