| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 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 for TranslationUnitGenerator tool.""" | 6 """Test for TranslationUnitGenerator tool.""" |
| 7 | 7 |
| 8 import difflib | 8 import difflib |
| 9 import glob | 9 import glob |
| 10 import json | 10 import json |
| 11 import ntpath | 11 import ntpath |
| 12 import os | 12 import os |
| 13 import os.path | 13 import os.path |
| 14 import subprocess | 14 import subprocess |
| 15 import sys | 15 import sys |
| 16 | 16 |
| 17 | 17 |
| 18 def _GenerateCompileCommands(files): | 18 def _GenerateCompileCommands(template_path, test_files_dir): |
| 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 return json.dumps([{'directory': '.', | 20 with open(template_path) as fh: |
| 21 'command': 'clang++ -fsyntax-only -std=c++11 -c %s' % f, | 21 return fh.read().replace('$test_files_dir', test_files_dir) |
| 22 'file': f} for f in files], indent=2) | |
| 23 | 22 |
| 24 | 23 |
| 25 def _NumberOfTestsToString(tests): | 24 def _NumberOfTestsToString(tests): |
| 26 """Returns an English sentence describing the number of tests.""" | 25 """Returns an English sentence describing the number of tests.""" |
| 27 return "%d test%s" % (tests, 's' if tests != 1 else '') | 26 return "%d test%s" % (tests, 's' if tests != 1 else '') |
| 28 | 27 |
| 29 | 28 |
| 30 # Before running this test script, please build the translation_unit clang tool | 29 # Before running this test script, please build the translation_unit clang tool |
| 31 # first. This is explained here: | 30 # first. This is explained here: |
| 32 # https://code.google.com/p/chromium/wiki/ClangToolRefactoring | 31 # https://code.google.com/p/chromium/wiki/ClangToolRefactoring |
| 33 def main(): | 32 def main(): |
| 34 tools_clang_directory = os.path.dirname(os.path.dirname( | 33 tools_clang_directory = os.path.dirname(os.path.dirname( |
| 35 os.path.realpath(__file__))) | 34 os.path.realpath(__file__))) |
| 36 tools_clang_scripts_directory = os.path.join(tools_clang_directory, 'scripts') | 35 tools_clang_scripts_directory = os.path.join(tools_clang_directory, 'scripts') |
| 37 test_directory_for_tool = os.path.join( | 36 test_directory_for_tool = os.path.join( |
| 38 tools_clang_directory, 'translation_unit', 'test_files') | 37 tools_clang_directory, 'translation_unit', 'test_files') |
| 39 compile_database = os.path.join(test_directory_for_tool, | 38 compile_database = os.path.join(test_directory_for_tool, |
| 40 'compile_commands.json') | 39 'compile_commands.json') |
| 40 compile_database_template = compile_database + '.template' |
| 41 source_files = glob.glob(os.path.join(test_directory_for_tool, '*.cc')) | 41 source_files = glob.glob(os.path.join(test_directory_for_tool, '*.cc')) |
| 42 | 42 |
| 43 # Generate a temporary compilation database to run the tool over. | 43 # Generate a temporary compilation database to run the tool over. |
| 44 with open(compile_database, 'w') as f: | 44 with open(compile_database, 'w') as f: |
| 45 f.write(_GenerateCompileCommands(source_files)) | 45 f.write(_GenerateCompileCommands(compile_database_template, |
| 46 test_directory_for_tool)) |
| 46 | 47 |
| 47 args = ['python', | 48 args = ['python', |
| 48 os.path.join(tools_clang_scripts_directory, 'run_tool.py'), | 49 os.path.join(tools_clang_scripts_directory, 'run_tool.py'), |
| 49 'translation_unit', | 50 'translation_unit', |
| 50 test_directory_for_tool] | 51 test_directory_for_tool] |
| 51 args.extend(source_files) | 52 args.extend(source_files) |
| 52 run_tool = subprocess.Popen(args, stdout=subprocess.PIPE) | 53 run_tool = subprocess.Popen(args, stdout=subprocess.PIPE) |
| 53 stdout, _ = run_tool.communicate() | 54 stdout, _ = run_tool.communicate() |
| 54 if run_tool.returncode != 0: | 55 if run_tool.returncode != 0: |
| 55 print 'run_tool failed:\n%s' % stdout | 56 print 'run_tool failed:\n%s' % stdout |
| 56 sys.exit(1) | 57 sys.exit(1) |
| 57 | 58 |
| 58 passed = 0 | 59 passed = 0 |
| 59 failed = 0 | 60 failed = 0 |
| 60 for actual in source_files: | 61 for actual in source_files: |
| 61 actual += '.filepaths' | 62 actual += '.filepaths' |
| 62 expected = actual + '.expected' | 63 expected = actual + '.expected' |
| 63 print '[ RUN ] %s' % os.path.relpath(actual) | 64 print '[ RUN ] %s' % os.path.relpath(actual) |
| 64 expected_output = actual_output = None | 65 expected_output = actual_output = None |
| 65 with open(expected, 'r') as f: | 66 with open(expected, 'r') as f: |
| 66 expected_output = f.readlines() | 67 expected_output = f.readlines() |
| 67 with open(actual, 'r') as f: | 68 with open(actual, 'r') as f: |
| 68 actual_output = f.readlines() | 69 actual_output = f.readlines() |
| 69 has_same_filepaths = True | 70 has_same_filepaths = True |
| 70 for expected_line, actual_line in zip(expected_output, actual_output): | 71 for expected_line, actual_line in zip(expected_output, actual_output): |
| 71 if '//' in actual_output: | 72 if '//' in actual_line: |
| 72 if actual_output.split('//')[1] != expected_output: | 73 actual_line = '//' + actual_line.split('//')[1] |
| 73 sys.stdout.write('expected: %s' % expected_output) | 74 |
| 74 sys.stdout.write('actual: %s' % actual_output.split('//')[1]) | |
| 75 break | |
| 76 else: | |
| 77 continue | |
| 78 if ntpath.basename(expected_line) != ntpath.basename(actual_line): | 75 if ntpath.basename(expected_line) != ntpath.basename(actual_line): |
| 79 sys.stdout.write('expected: %s' % ntpath.basename(expected_line)) | 76 sys.stdout.write('expected: %s' % ntpath.basename(expected_line)) |
| 80 sys.stdout.write('actual: %s' % ntpath.basename(actual_line)) | 77 sys.stdout.write('actual: %s' % ntpath.basename(actual_line)) |
| 81 has_same_filepaths = False | 78 has_same_filepaths = False |
| 82 break | 79 break |
| 83 if not has_same_filepaths: | 80 if not has_same_filepaths: |
| 84 failed += 1 | 81 failed += 1 |
| 85 for line in difflib.unified_diff(expected_output, actual_output, | 82 for line in difflib.unified_diff(expected_output, actual_output, |
| 86 fromfile=os.path.relpath(expected), | 83 fromfile=os.path.relpath(expected), |
| 87 tofile=os.path.relpath(actual)): | 84 tofile=os.path.relpath(actual)): |
| (...skipping 11 matching lines...) Expand all Loading... |
| 99 | 96 |
| 100 print '[==========] %s ran.' % _NumberOfTestsToString(len(source_files)) | 97 print '[==========] %s ran.' % _NumberOfTestsToString(len(source_files)) |
| 101 if passed > 0: | 98 if passed > 0: |
| 102 print '[ PASSED ] %s.' % _NumberOfTestsToString(passed) | 99 print '[ PASSED ] %s.' % _NumberOfTestsToString(passed) |
| 103 if failed > 0: | 100 if failed > 0: |
| 104 print '[ FAILED ] %s.' % _NumberOfTestsToString(failed) | 101 print '[ FAILED ] %s.' % _NumberOfTestsToString(failed) |
| 105 | 102 |
| 106 | 103 |
| 107 if __name__ == '__main__': | 104 if __name__ == '__main__': |
| 108 sys.exit(main()) | 105 sys.exit(main()) |
| OLD | NEW |