diff options
author | Kim Grasman <kim.grasman@gmail.com> | 2019-09-29 16:30:13 +0200 |
---|---|---|
committer | Kim Gräsman <kim.grasman@gmail.com> | 2019-09-30 21:58:01 +0200 |
commit | 3776ea410e00e83e4d7d29166efc8e23e5bf20a3 (patch) | |
tree | df664f194b9299a6a66197564b90d225f54e7a37 | |
parent | f5125d48f98448597806b39fc45bf81053517f2f (diff) |
[iwyu_tool] Add test for compilation db path canonicalization
This drove out a refactor to read the file separately from
canonicalization for easier testing.
-rwxr-xr-x | iwyu_tool.py | 25 | ||||
-rwxr-xr-x | iwyu_tool_test.py | 21 |
2 files changed, 33 insertions, 13 deletions
diff --git a/iwyu_tool.py b/iwyu_tool.py index 7b11156..94f2fdb 100755 --- a/iwyu_tool.py +++ b/iwyu_tool.py @@ -295,19 +295,10 @@ class Invocation(object): return Process.start(self) -def parse_compilation_db(compilation_db_path): - """ Parse JSON compilation database and return a canonicalized form. """ - if os.path.isdir(compilation_db_path): - compilation_db_path = os.path.join(compilation_db_path, - 'compile_commands.json') - - # Read compilation db from disk. - compilation_db_path = os.path.realpath(compilation_db_path) - with open(compilation_db_path, 'r') as fileobj: - compilation_db = json.load(fileobj) - - # Expand symlinks. +def fixup_compilation_db(compilation_db): + """ Canonicalize paths in JSON compilation database. """ for entry in compilation_db: + # Expand relative paths and symlinks entry['file'] = os.path.realpath(entry['file']) return compilation_db @@ -366,11 +357,19 @@ def main(compilation_db_path, source_files, verbose, formatter, jobs, """ Entry point. """ try: - compilation_db = parse_compilation_db(compilation_db_path) + if os.path.isdir(compilation_db_path): + compilation_db_path = os.path.join(compilation_db_path, + 'compile_commands.json') + + # Read compilation db from disk. + compilation_db_path = os.path.realpath(compilation_db_path) + with open(compilation_db_path, 'r') as fileobj: + compilation_db = json.load(fileobj) except IOError as why: print('Failed to parse JSON compilation database: %s' % why) return 1 + compilation_db = fixup_compilation_db(compilation_db) compilation_db = slice_compilation_db(compilation_db, source_files) # Transform compilation db entries into a list of IWYU invocations. diff --git a/iwyu_tool_test.py b/iwyu_tool_test.py index c9b3ddd..2d2f0a9 100755 --- a/iwyu_tool_test.py +++ b/iwyu_tool_test.py @@ -8,6 +8,7 @@ # License. See LICENSE.TXT for details. # ##===----------------------------------------------------------------------===## +import os import sys import time import random @@ -272,5 +273,25 @@ class BootstrapTests(unittest.TestCase): self.main.call_args['extra_args']) +class CompilationDBTests(unittest.TestCase): + def setUp(self): + self.cwd = os.path.realpath(os.getcwd()) + + def test_fixup_compilation_db(self): + """ Compilation database path canonicalization. """ + compilation_db = [ + { + "directory": "/home/user/llvm/build/test", + "file": "Test.cpp" + } + ] + + canonical = iwyu_tool.fixup_compilation_db(compilation_db) + + # Check that file path is made absolute. + entry = canonical[0] + self.assertEqual(os.path.join(self.cwd, 'Test.cpp'), entry['file']) + + if __name__ == '__main__': unittest.main() |