2 from clang.cindex import Config
3 if 'CLANG_LIBRARY_PATH' in os.environ:
4 Config.set_library_path(os.environ['CLANG_LIBRARY_PATH'])
6 from clang.cindex import CompilationDatabase
7 from clang.cindex import CompilationDatabaseError
8 from clang.cindex import CompileCommands
9 from clang.cindex import CompileCommand
14 from .util import skip_if_no_fspath
15 from .util import str_to_path
18 kInputsDir = os.path.join(os.path.dirname(__file__), 'INPUTS')
21 @unittest.skipIf(sys.platform == 'win32', "TODO: Fix these tests on Windows")
22 class TestCDB(unittest.TestCase):
23 def test_create_fail(self):
24 """Check we fail loading a database with an assertion"""
25 path = os.path.dirname(__file__)
26 with self.assertRaises(CompilationDatabaseError) as cm:
27 cdb = CompilationDatabase.fromDirectory(path)
29 self.assertEqual(e.cdb_error,
30 CompilationDatabaseError.ERROR_CANNOTLOADDATABASE)
32 def test_create(self):
33 """Check we can load a compilation database"""
34 cdb = CompilationDatabase.fromDirectory(kInputsDir)
36 def test_lookup_succeed(self):
37 """Check we get some results if the file exists in the db"""
38 cdb = CompilationDatabase.fromDirectory(kInputsDir)
39 cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
40 self.assertNotEqual(len(cmds), 0)
43 def test_lookup_succeed_pathlike(self):
44 """Same as test_lookup_succeed, but with PathLikes"""
45 cdb = CompilationDatabase.fromDirectory(str_to_path(kInputsDir))
46 cmds = cdb.getCompileCommands(str_to_path('/home/john.doe/MyProject/project.cpp'))
47 self.assertNotEqual(len(cmds), 0)
49 def test_all_compilecommand(self):
50 """Check we get all results from the db"""
51 cdb = CompilationDatabase.fromDirectory(kInputsDir)
52 cmds = cdb.getAllCompileCommands()
53 self.assertEqual(len(cmds), 3)
55 { 'wd': '/home/john.doe/MyProject',
56 'file': '/home/john.doe/MyProject/project.cpp',
57 'line': ['clang++', '-o', 'project.o', '-c',
58 '/home/john.doe/MyProject/project.cpp']},
59 { 'wd': '/home/john.doe/MyProjectA',
60 'file': '/home/john.doe/MyProject/project2.cpp',
61 'line': ['clang++', '-o', 'project2.o', '-c',
62 '/home/john.doe/MyProject/project2.cpp']},
63 { 'wd': '/home/john.doe/MyProjectB',
64 'file': '/home/john.doe/MyProject/project2.cpp',
65 'line': ['clang++', '-DFEATURE=1', '-o', 'project2-feature.o', '-c',
66 '/home/john.doe/MyProject/project2.cpp']},
69 for i in range(len(cmds)):
70 self.assertEqual(cmds[i].directory, expected[i]['wd'])
71 self.assertEqual(cmds[i].filename, expected[i]['file'])
72 for arg, exp in zip(cmds[i].arguments, expected[i]['line']):
73 self.assertEqual(arg, exp)
75 def test_1_compilecommand(self):
76 """Check file with single compile command"""
77 cdb = CompilationDatabase.fromDirectory(kInputsDir)
78 file = '/home/john.doe/MyProject/project.cpp'
79 cmds = cdb.getCompileCommands(file)
80 self.assertEqual(len(cmds), 1)
81 self.assertEqual(cmds[0].directory, os.path.dirname(file))
82 self.assertEqual(cmds[0].filename, file)
83 expected = [ 'clang++', '-o', 'project.o', '-c',
84 '/home/john.doe/MyProject/project.cpp']
85 for arg, exp in zip(cmds[0].arguments, expected):
86 self.assertEqual(arg, exp)
88 def test_2_compilecommand(self):
89 """Check file with 2 compile commands"""
90 cdb = CompilationDatabase.fromDirectory(kInputsDir)
91 cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project2.cpp')
92 self.assertEqual(len(cmds), 2)
94 { 'wd': '/home/john.doe/MyProjectA',
95 'line': ['clang++', '-o', 'project2.o', '-c',
96 '/home/john.doe/MyProject/project2.cpp']},
97 { 'wd': '/home/john.doe/MyProjectB',
98 'line': ['clang++', '-DFEATURE=1', '-o', 'project2-feature.o', '-c',
99 '/home/john.doe/MyProject/project2.cpp']}
101 for i in range(len(cmds)):
102 self.assertEqual(cmds[i].directory, expected[i]['wd'])
103 for arg, exp in zip(cmds[i].arguments, expected[i]['line']):
104 self.assertEqual(arg, exp)
106 def test_compilecommand_iterator_stops(self):
107 """Check that iterator stops after the correct number of elements"""
108 cdb = CompilationDatabase.fromDirectory(kInputsDir)
110 for cmd in cdb.getCompileCommands('/home/john.doe/MyProject/project2.cpp'):
112 self.assertLessEqual(count, 2)
114 def test_compilationDB_references(self):
115 """Ensure CompilationsCommands are independent of the database"""
116 cdb = CompilationDatabase.fromDirectory(kInputsDir)
117 cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
120 workingdir = cmds[0].directory
122 def test_compilationCommands_references(self):
123 """Ensure CompilationsCommand keeps a reference to CompilationCommands"""
124 cdb = CompilationDatabase.fromDirectory(kInputsDir)
125 cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
130 workingdir = cmd0.directory