OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2014 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 for TranslationUnitGenerator tool.""" |
| 7 |
| 8 import difflib |
| 9 import glob |
| 10 import json |
| 11 import ntpath |
| 12 import os |
| 13 import os.path |
| 14 import subprocess |
| 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 -std=c++11 -c %s' % f, |
| 22 'file': f} for f in files], indent=2) |
| 23 |
| 24 |
| 25 def _NumberOfTestsToString(tests): |
| 26 """Returns an English sentence describing the number of tests.""" |
| 27 return "%d test%s" % (tests, 's' if tests != 1 else '') |
| 28 |
| 29 |
| 30 # Before running this test script, please build the translation_unit clang tool |
| 31 # first. This is explained here: |
| 32 # https://code.google.com/p/chromium/wiki/ClangToolRefactoring |
| 33 def main(): |
| 34 tools_clang_directory = os.path.dirname(os.path.dirname( |
| 35 os.path.realpath(__file__))) |
| 36 tools_clang_scripts_directory = os.path.join(tools_clang_directory, 'scripts') |
| 37 test_directory_for_tool = os.path.join( |
| 38 tools_clang_directory, 'translation_unit', 'test_files') |
| 39 compile_database = os.path.join(test_directory_for_tool, |
| 40 'compile_commands.json') |
| 41 source_files = glob.glob(os.path.join(test_directory_for_tool, '*.cc')) |
| 42 |
| 43 # Generate a temporary compilation database to run the tool over. |
| 44 with open(compile_database, 'w') as f: |
| 45 f.write(_GenerateCompileCommands(source_files)) |
| 46 |
| 47 args = ['python', |
| 48 os.path.join(tools_clang_scripts_directory, 'run_tool.py'), |
| 49 'translation_unit', |
| 50 test_directory_for_tool] |
| 51 args.extend(source_files) |
| 52 run_tool = subprocess.Popen(args, stdout=subprocess.PIPE) |
| 53 stdout, _ = run_tool.communicate() |
| 54 if run_tool.returncode != 0: |
| 55 print 'run_tool failed:\n%s' % stdout |
| 56 sys.exit(1) |
| 57 |
| 58 passed = 0 |
| 59 failed = 0 |
| 60 for actual in source_files: |
| 61 actual += '.filepaths' |
| 62 expected = actual + '.expected' |
| 63 print '[ RUN ] %s' % os.path.relpath(actual) |
| 64 expected_output = actual_output = None |
| 65 with open(expected, 'r') as f: |
| 66 expected_output = f.readlines() |
| 67 with open(actual, 'r') as f: |
| 68 actual_output = f.readlines() |
| 69 has_same_filepaths = True |
| 70 for expected_line, actual_line in zip(expected_output, actual_output): |
| 71 if ntpath.basename(expected_line) != ntpath.basename(actual_line): |
| 72 sys.stdout.write(ntpath.basename(expected_line)) |
| 73 sys.stdout.write(ntpath.basename(actual_line)) |
| 74 has_same_filepaths = False |
| 75 break |
| 76 if not has_same_filepaths: |
| 77 failed += 1 |
| 78 for line in difflib.unified_diff(expected_output, actual_output, |
| 79 fromfile=os.path.relpath(expected), |
| 80 tofile=os.path.relpath(actual)): |
| 81 sys.stdout.write(line) |
| 82 print '[ FAILED ] %s' % os.path.relpath(actual) |
| 83 # Don't clean up the file on failure, so the results can be referenced |
| 84 # more easily. |
| 85 continue |
| 86 print '[ OK ] %s' % os.path.relpath(actual) |
| 87 passed += 1 |
| 88 os.remove(actual) |
| 89 |
| 90 if failed == 0: |
| 91 os.remove(compile_database) |
| 92 |
| 93 print '[==========] %s ran.' % _NumberOfTestsToString(len(source_files)) |
| 94 if passed > 0: |
| 95 print '[ PASSED ] %s.' % _NumberOfTestsToString(passed) |
| 96 if failed > 0: |
| 97 print '[ FAILED ] %s.' % _NumberOfTestsToString(failed) |
| 98 |
| 99 |
| 100 if __name__ == '__main__': |
| 101 sys.exit(main()) |
OLD | NEW |