OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2013 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 """Test harness for chromium clang tools.""" | 6 """Test harness for chromium clang tools.""" |
7 | 7 |
8 import difflib | 8 import difflib |
9 import glob | 9 import glob |
10 import json | 10 import json |
(...skipping 24 matching lines...) Expand all Loading... | |
35 'command': 'clang++ -std=c++11 -fsyntax-only %s -c %s' % ( | 35 'command': 'clang++ -std=c++11 -fsyntax-only %s -c %s' % ( |
36 include_path_flags, f), | 36 include_path_flags, f), |
37 'file': f} for f in files], indent=2) | 37 'file': f} for f in files], indent=2) |
38 | 38 |
39 | 39 |
40 def _NumberOfTestsToString(tests): | 40 def _NumberOfTestsToString(tests): |
41 """Returns an English describing the number of tests.""" | 41 """Returns an English describing the number of tests.""" |
42 return '%d test%s' % (tests, 's' if tests != 1 else '') | 42 return '%d test%s' % (tests, 's' if tests != 1 else '') |
43 | 43 |
44 | 44 |
45 def _RunToolAndApplyEdits(tools_clang_scripts_directory, | |
46 tool_to_test, | |
47 test_directory_for_tool, | |
48 actual_files): | |
49 try: | |
50 # Stage the test files in the git index. If they aren't staged, then | |
51 # run_tools.py will skip them when applying replacements. | |
danakj
2016/12/23 15:45:06
run_tools.py?
Łukasz Anforowicz
2016/12/27 22:33:27
Done.
| |
52 args = ['add'] | |
53 args.extend(actual_files) | |
54 _RunGit(args) | |
55 | |
56 # Launch the following pipeline: | |
57 # run_tool.py ... | extract_edits.py | apply_edits.py ... | |
58 args = ['python', | |
59 os.path.join(tools_clang_scripts_directory, 'run_tool.py'), | |
60 tool_to_test, | |
61 test_directory_for_tool] | |
62 args.extend(actual_files) | |
63 run_tool = subprocess.Popen(args, stdout=subprocess.PIPE) | |
64 | |
65 args = ['python', | |
66 os.path.join(tools_clang_scripts_directory, 'extract_edits.py')] | |
67 extract_edits = subprocess.Popen(args, stdin=run_tool.stdout, | |
68 stdout=subprocess.PIPE) | |
69 | |
70 args = ['python', | |
71 os.path.join(tools_clang_scripts_directory, 'apply_edits.py'), | |
72 '/'] # compile.db created by test_tool.py uses absolute paths. | |
danakj
2016/12/23 15:45:06
isn't the path optional? why specify it then?
Łukasz Anforowicz
2016/12/27 22:33:27
The clang tool will *normally* emit paths relative
| |
73 apply_edits = subprocess.Popen(args, stdin=extract_edits.stdout, | |
74 stdout=subprocess.PIPE) | |
75 | |
76 # Wait for the pipeline to finish running + check exit codes. | |
77 stdout, _ = apply_edits.communicate() | |
78 for process in [run_tool, extract_edits, apply_edits]: | |
79 process.wait() | |
80 if process.returncode != 0: | |
81 print "Failure while running the tool." | |
82 return process.returncode | |
83 | |
84 # Reformat the resulting edits via: git cl format. | |
85 args = ['cl', 'format'] | |
86 args.extend(actual_files) | |
87 _RunGit(args) | |
88 | |
89 return 0 | |
90 | |
91 finally: | |
92 # No matter what, unstage the git changes we made earlier to avoid polluting | |
93 # the index. | |
94 args = ['reset', '--quiet', 'HEAD'] | |
95 args.extend(actual_files) | |
96 _RunGit(args) | |
97 | |
98 | |
45 def main(argv): | 99 def main(argv): |
46 if len(argv) < 1: | 100 if len(argv) < 1: |
47 print 'Usage: test_tool.py <clang tool>' | 101 print 'Usage: test_tool.py <clang tool>' |
48 print ' <clang tool> is the clang tool to be tested.' | 102 print ' <clang tool> is the clang tool to be tested.' |
49 sys.exit(1) | 103 sys.exit(1) |
50 | 104 |
51 tool_to_test = argv[0] | 105 tool_to_test = argv[0] |
52 print '\nTesting %s\n' % tool_to_test | 106 print '\nTesting %s\n' % tool_to_test |
53 tools_clang_scripts_directory = os.path.dirname(os.path.realpath(__file__)) | 107 tools_clang_scripts_directory = os.path.dirname(os.path.realpath(__file__)) |
54 tools_clang_directory = os.path.dirname(tools_clang_scripts_directory) | 108 tools_clang_directory = os.path.dirname(tools_clang_scripts_directory) |
(...skipping 14 matching lines...) Expand all Loading... | |
69 # search path. | 123 # search path. |
70 include_paths.append( | 124 include_paths.append( |
71 os.path.realpath(os.path.join(tools_clang_directory, | 125 os.path.realpath(os.path.join(tools_clang_directory, |
72 '../..', | 126 '../..', |
73 'testing/gtest/include'))) | 127 'testing/gtest/include'))) |
74 | 128 |
75 if len(actual_files) == 0: | 129 if len(actual_files) == 0: |
76 print 'Tool "%s" does not have compatible test files.' % tool_to_test | 130 print 'Tool "%s" does not have compatible test files.' % tool_to_test |
77 return 1 | 131 return 1 |
78 | 132 |
79 try: | 133 # Set up the test environment. |
80 # Set up the test environment. | 134 for source, actual in zip(source_files, actual_files): |
81 for source, actual in zip(source_files, actual_files): | 135 shutil.copyfile(source, actual) |
82 shutil.copyfile(source, actual) | 136 # Generate a temporary compilation database to run the tool over. |
83 # Stage the test files in the git index. If they aren't staged, then | 137 with open(compile_database, 'w') as f: |
84 # run_tools.py will skip them when applying replacements. | 138 f.write(_GenerateCompileCommands(actual_files, include_paths)) |
85 args = ['add'] | |
86 args.extend(actual_files) | |
87 _RunGit(args) | |
88 # Generate a temporary compilation database to run the tool over. | |
89 with open(compile_database, 'w') as f: | |
90 f.write(_GenerateCompileCommands(actual_files, include_paths)) | |
91 | 139 |
92 args = ['python', | 140 # Run the tool. |
93 os.path.join(tools_clang_scripts_directory, 'run_tool.py'), | 141 exitcode = _RunToolAndApplyEdits(tools_clang_scripts_directory, tool_to_test, |
94 tool_to_test, | 142 test_directory_for_tool, actual_files) |
95 test_directory_for_tool] | 143 if (exitcode != 0): |
96 args.extend(actual_files) | 144 return exitcode |
97 run_tool = subprocess.Popen(args, stdout=subprocess.PIPE) | |
98 stdout, _ = run_tool.communicate() | |
99 if run_tool.returncode != 0: | |
100 print 'run_tool failed:\n%s' % stdout | |
101 return 1 | |
102 | 145 |
103 args = ['cl', 'format'] | 146 # Compare actual-vs-expected results. |
104 args.extend(actual_files) | 147 passed = 0 |
105 _RunGit(args) | 148 failed = 0 |
149 for expected, actual in zip(expected_files, actual_files): | |
150 print '[ RUN ] %s' % os.path.relpath(actual) | |
151 expected_output = actual_output = None | |
152 with open(expected, 'r') as f: | |
153 expected_output = f.readlines() | |
154 with open(actual, 'r') as f: | |
155 actual_output = f.readlines() | |
156 if actual_output != expected_output: | |
157 failed += 1 | |
158 for line in difflib.unified_diff(expected_output, actual_output, | |
159 fromfile=os.path.relpath(expected), | |
160 tofile=os.path.relpath(actual)): | |
161 sys.stdout.write(line) | |
162 print '[ FAILED ] %s' % os.path.relpath(actual) | |
163 # Don't clean up the file on failure, so the results can be referenced | |
164 # more easily. | |
165 continue | |
166 print '[ OK ] %s' % os.path.relpath(actual) | |
167 passed += 1 | |
168 os.remove(actual) | |
106 | 169 |
107 passed = 0 | 170 if failed == 0: |
108 failed = 0 | 171 os.remove(compile_database) |
109 for expected, actual in zip(expected_files, actual_files): | |
110 print '[ RUN ] %s' % os.path.relpath(actual) | |
111 expected_output = actual_output = None | |
112 with open(expected, 'r') as f: | |
113 expected_output = f.readlines() | |
114 with open(actual, 'r') as f: | |
115 actual_output = f.readlines() | |
116 if actual_output != expected_output: | |
117 failed += 1 | |
118 for line in difflib.unified_diff(expected_output, actual_output, | |
119 fromfile=os.path.relpath(expected), | |
120 tofile=os.path.relpath(actual)): | |
121 sys.stdout.write(line) | |
122 print '[ FAILED ] %s' % os.path.relpath(actual) | |
123 # Don't clean up the file on failure, so the results can be referenced | |
124 # more easily. | |
125 continue | |
126 print '[ OK ] %s' % os.path.relpath(actual) | |
127 passed += 1 | |
128 os.remove(actual) | |
129 | 172 |
130 if failed == 0: | 173 print '[==========] %s ran.' % _NumberOfTestsToString(len(source_files)) |
131 os.remove(compile_database) | 174 if passed > 0: |
132 | 175 print '[ PASSED ] %s.' % _NumberOfTestsToString(passed) |
133 print '[==========] %s ran.' % _NumberOfTestsToString(len(source_files)) | 176 if failed > 0: |
134 if passed > 0: | 177 print '[ FAILED ] %s.' % _NumberOfTestsToString(failed) |
135 print '[ PASSED ] %s.' % _NumberOfTestsToString(passed) | 178 return 1 |
136 if failed > 0: | |
137 print '[ FAILED ] %s.' % _NumberOfTestsToString(failed) | |
138 return 1 | |
139 finally: | |
140 # No matter what, unstage the git changes we made earlier to avoid polluting | |
141 # the index. | |
142 args = ['reset', '--quiet', 'HEAD'] | |
143 args.extend(actual_files) | |
144 _RunGit(args) | |
145 | 179 |
146 | 180 |
147 if __name__ == '__main__': | 181 if __name__ == '__main__': |
148 sys.exit(main(sys.argv[1:])) | 182 sys.exit(main(sys.argv[1:])) |
OLD | NEW |