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 |
11 import os | 11 import os |
12 import os.path | 12 import os.path |
13 import subprocess | 13 import subprocess |
14 import shutil | 14 import shutil |
15 import sys | 15 import sys |
16 | 16 |
17 | 17 |
18 def _GenerateCompileCommands(files): | 18 def _GenerateCompileCommands(files, include_paths): |
Nico
2013/06/11 19:58:13
fyi: ninja 1.3.4 (now in depot_tools) now has a -t
dcheng
2013/06/11 20:07:41
The problem is these test files don't end up in a
| |
19 """Returns a JSON string containing a compilation database for the input.""" | 19 """Returns a JSON string containing a compilation database for the input.""" |
20 include_path_flags = ''.join('-I %s' % include_path | |
21 for include_path in include_paths) | |
20 return json.dumps([{'directory': '.', | 22 return json.dumps([{'directory': '.', |
21 'command': 'clang++ -fsyntax-only -c %s' % f, | 23 'command': 'clang++ -fsyntax-only %s -c %s' % ( |
24 include_path_flags, f), | |
22 'file': f} for f in files], indent=2) | 25 'file': f} for f in files], indent=2) |
23 | 26 |
24 | 27 |
25 def _NumberOfTestsToString(tests): | 28 def _NumberOfTestsToString(tests): |
26 """Returns an English describing the number of tests.""" | 29 """Returns an English describing the number of tests.""" |
27 return "%d test%s" % (tests, 's' if tests != 1 else '') | 30 return "%d test%s" % (tests, 's' if tests != 1 else '') |
28 | 31 |
29 | 32 |
30 def main(argv): | 33 def main(argv): |
31 if len(argv) < 1: | 34 if len(argv) < 1: |
32 print 'Usage: test_tool.py <clang tool>' | 35 print 'Usage: test_tool.py <clang tool>' |
33 print ' <clang tool> is the clang tool to be tested.' | 36 print ' <clang tool> is the clang tool to be tested.' |
34 sys.exit(1) | 37 sys.exit(1) |
35 | 38 |
36 tool_to_test = argv[0] | 39 tool_to_test = argv[0] |
37 tools_clang_scripts_directory = os.path.dirname(os.path.realpath(__file__)) | 40 tools_clang_scripts_directory = os.path.dirname(os.path.realpath(__file__)) |
38 tools_clang_directory = os.path.dirname(tools_clang_scripts_directory) | 41 tools_clang_directory = os.path.dirname(tools_clang_scripts_directory) |
39 test_directory_for_tool = os.path.join( | 42 test_directory_for_tool = os.path.join( |
40 tools_clang_directory, tool_to_test, 'tests') | 43 tools_clang_directory, tool_to_test, 'tests') |
41 compile_database = os.path.join(test_directory_for_tool, | 44 compile_database = os.path.join(test_directory_for_tool, |
42 'compile_commands.json') | 45 'compile_commands.json') |
43 source_files = glob.glob(os.path.join(test_directory_for_tool, | 46 source_files = glob.glob(os.path.join(test_directory_for_tool, |
44 '*-original.cc')) | 47 '*-original.cc')) |
45 actual_files = ['-'.join([source_file.rsplit('-', 2)[0], 'actual.cc']) | 48 actual_files = ['-'.join([source_file.rsplit('-', 2)[0], 'actual.cc']) |
46 for source_file in source_files] | 49 for source_file in source_files] |
47 expected_files = ['-'.join([source_file.rsplit('-', 2)[0], 'expected.cc']) | 50 expected_files = ['-'.join([source_file.rsplit('-', 2)[0], 'expected.cc']) |
48 for source_file in source_files] | 51 for source_file in source_files] |
52 include_paths = [] | |
53 include_paths.append( | |
54 os.path.realpath(os.path.join(tools_clang_directory, '../..'))) | |
49 | 55 |
50 try: | 56 try: |
51 # Set up the test environment. | 57 # Set up the test environment. |
52 for source, actual in zip(source_files, actual_files): | 58 for source, actual in zip(source_files, actual_files): |
53 shutil.copyfile(source, actual) | 59 shutil.copyfile(source, actual) |
54 # Stage the test files in the git index. If they aren't staged, then | 60 # Stage the test files in the git index. If they aren't staged, then |
55 # run_tools.py will skip them when applying replacements. | 61 # run_tools.py will skip them when applying replacements. |
56 args = ['git', 'add'] | 62 args = ['git', 'add'] |
57 args.extend(actual_files) | 63 args.extend(actual_files) |
58 subprocess.check_call(args) | 64 subprocess.check_call(args) |
59 # Generate a temporary compilation database to run the tool over. | 65 # Generate a temporary compilation database to run the tool over. |
60 with open(compile_database, 'w') as f: | 66 with open(compile_database, 'w') as f: |
61 f.write(_GenerateCompileCommands(actual_files)) | 67 f.write(_GenerateCompileCommands(actual_files, include_paths)) |
62 | 68 |
63 args = ['python', | 69 args = ['python', |
64 os.path.join(tools_clang_scripts_directory, 'run_tool.py'), | 70 os.path.join(tools_clang_scripts_directory, 'run_tool.py'), |
65 tool_to_test, | 71 tool_to_test, |
66 test_directory_for_tool] | 72 test_directory_for_tool] |
67 args.extend(actual_files) | 73 args.extend(actual_files) |
68 run_tool = subprocess.Popen(args, stdout=subprocess.PIPE) | 74 run_tool = subprocess.Popen(args, stdout=subprocess.PIPE) |
69 stdout, _ = run_tool.communicate() | 75 stdout, _ = run_tool.communicate() |
70 if run_tool.returncode != 0: | 76 if run_tool.returncode != 0: |
71 print 'run_tool failed:\n%s' % stdout | 77 print 'run_tool failed:\n%s' % stdout |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 finally: | 111 finally: |
106 # No matter what, unstage the git changes we made earlier to avoid polluting | 112 # No matter what, unstage the git changes we made earlier to avoid polluting |
107 # the index. | 113 # the index. |
108 args = ['git', 'reset', '--quiet', 'HEAD'] | 114 args = ['git', 'reset', '--quiet', 'HEAD'] |
109 args.extend(actual_files) | 115 args.extend(actual_files) |
110 subprocess.call(args) | 116 subprocess.call(args) |
111 | 117 |
112 | 118 |
113 if __name__ == '__main__': | 119 if __name__ == '__main__': |
114 sys.exit(main(sys.argv[1:])) | 120 sys.exit(main(sys.argv[1:])) |
OLD | NEW |