OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Test harness for chromium clang tools.""" |
| 7 |
| 8 import difflib |
| 9 import glob |
| 10 import json |
| 11 import os |
| 12 import os.path |
| 13 import subprocess |
| 14 import shutil |
| 15 import sys |
| 16 |
| 17 |
| 18 def _GenerateCompileCommands(files): |
| 19 """Returns a JSON string containing a compilation database for the input.""" |
| 20 return json.dumps([{'directory': '.', |
| 21 'command': 'clang++ -fsyntax-only -c %s' % f, |
| 22 'file': f} for f in files], indent=2) |
| 23 |
| 24 |
| 25 def _NumberOfTestsToString(tests): |
| 26 """Returns an English describing the number of tests.""" |
| 27 return "%d test%s" % (tests, 's' if tests != 1 else '') |
| 28 |
| 29 |
| 30 def main(argv): |
| 31 if len(argv) < 1: |
| 32 print 'Usage: test_tool.py <clang tool>' |
| 33 print ' <clang tool> is the clang tool to be tested.' |
| 34 sys.exit(1) |
| 35 |
| 36 tool_to_test = argv[0] |
| 37 tools_clang_scripts_directory = os.path.dirname(os.path.realpath(__file__)) |
| 38 tools_clang_directory = os.path.dirname(tools_clang_scripts_directory) |
| 39 test_directory_for_tool = os.path.join( |
| 40 tools_clang_directory, tool_to_test, 'tests') |
| 41 compile_database = os.path.join(test_directory_for_tool, |
| 42 'compile_commands.json') |
| 43 source_files = glob.glob(os.path.join(test_directory_for_tool, |
| 44 '*-original.cc')) |
| 45 actual_files = ['-'.join([source_file.rsplit('-', 2)[0], 'actual.cc']) |
| 46 for source_file in source_files] |
| 47 expected_files = ['-'.join([source_file.rsplit('-', 2)[0], 'expected.cc']) |
| 48 for source_file in source_files] |
| 49 |
| 50 try: |
| 51 # Set up the test environment. |
| 52 for source, actual in zip(source_files, actual_files): |
| 53 shutil.copyfile(source, actual) |
| 54 # Stage the test files in the git index. If they aren't staged, then |
| 55 # run_tools.py will skip them when applying replacements. |
| 56 args = ['git', 'add'] |
| 57 args.extend(actual_files) |
| 58 subprocess.check_call(args) |
| 59 # Generate a temporary compilation database to run the tool over. |
| 60 with open(compile_database, 'w') as f: |
| 61 f.write(_GenerateCompileCommands(actual_files)) |
| 62 |
| 63 args = ['python', |
| 64 os.path.join(tools_clang_scripts_directory, 'run_tool.py'), |
| 65 tool_to_test, |
| 66 test_directory_for_tool] |
| 67 args.extend(actual_files) |
| 68 run_tool = subprocess.Popen(args, stdout=subprocess.PIPE) |
| 69 stdout, _ = run_tool.communicate() |
| 70 if run_tool.returncode != 0: |
| 71 print 'run_tool failed:\n%s' % stdout |
| 72 sys.exit(1) |
| 73 |
| 74 passed = 0 |
| 75 failed = 0 |
| 76 for expected, actual in zip(expected_files, actual_files): |
| 77 print '[ RUN ] %s' % os.path.relpath(actual) |
| 78 expected_output = actual_output = None |
| 79 with open(expected, 'r') as f: |
| 80 expected_output = f.readlines() |
| 81 with open(actual, 'r') as f: |
| 82 actual_output = f.readlines() |
| 83 if actual_output != expected_output: |
| 84 print '[ FAILED ] %s' % os.path.relpath(actual) |
| 85 failed += 1 |
| 86 for line in difflib.unified_diff(expected_output, actual_output, |
| 87 fromfile=os.path.relpath(expected), |
| 88 tofile=os.path.relpath(actual)): |
| 89 sys.stdout.write(line) |
| 90 # Don't clean up the file on failure, so the results can be referenced |
| 91 # more easily. |
| 92 continue |
| 93 print '[ OK ] %s' % os.path.relpath(actual) |
| 94 passed += 1 |
| 95 os.remove(actual) |
| 96 |
| 97 if failed == 0: |
| 98 os.remove(compile_database) |
| 99 |
| 100 print '[==========] %s ran.' % _NumberOfTestsToString(len(source_files)) |
| 101 if passed > 0: |
| 102 print '[ PASSED ] %s.' % _NumberOfTestsToString(passed) |
| 103 if failed > 0: |
| 104 print '[ FAILED ] %s.' % _NumberOfTestsToString(failed) |
| 105 finally: |
| 106 # No matter what, unstage the git changes we made earlier to avoid polluting |
| 107 # the index. |
| 108 args = ['git', 'reset', '--quiet', 'HEAD'] |
| 109 args.extend(actual_files) |
| 110 subprocess.call(args) |
| 111 |
| 112 |
| 113 if __name__ == '__main__': |
| 114 sys.exit(main(sys.argv[1:])) |
OLD | NEW |