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