2 from clang
.cindex
import Config
4 if "CLANG_LIBRARY_PATH" in os
.environ
:
5 Config
.set_library_path(os
.environ
["CLANG_LIBRARY_PATH"])
7 from clang
.cindex
import CompilationDatabase
8 from clang
.cindex
import CompilationDatabaseError
9 from clang
.cindex
import CompileCommands
10 from clang
.cindex
import CompileCommand
15 from .util
import skip_if_no_fspath
16 from .util
import str_to_path
19 kInputsDir
= os
.path
.join(os
.path
.dirname(__file__
), "INPUTS")
22 @unittest.skipIf(sys
.platform
== "win32", "TODO: Fix these tests on Windows")
23 class TestCDB(unittest
.TestCase
):
24 def test_create_fail(self
):
25 """Check we fail loading a database with an assertion"""
26 path
= os
.path
.dirname(__file__
)
28 # clang_CompilationDatabase_fromDirectory calls fprintf(stderr, ...)
29 # Suppress its output.
31 with
open(os
.devnull
, "wb") as null
:
32 os
.dup2(null
.fileno(), 2)
33 with self
.assertRaises(CompilationDatabaseError
) as cm
:
34 cdb
= CompilationDatabase
.fromDirectory(path
)
39 self
.assertEqual(e
.cdb_error
, CompilationDatabaseError
.ERROR_CANNOTLOADDATABASE
)
41 def test_create(self
):
42 """Check we can load a compilation database"""
43 cdb
= CompilationDatabase
.fromDirectory(kInputsDir
)
45 def test_lookup_succeed(self
):
46 """Check we get some results if the file exists in the db"""
47 cdb
= CompilationDatabase
.fromDirectory(kInputsDir
)
48 cmds
= cdb
.getCompileCommands("/home/john.doe/MyProject/project.cpp")
49 self
.assertNotEqual(len(cmds
), 0)
52 def test_lookup_succeed_pathlike(self
):
53 """Same as test_lookup_succeed, but with PathLikes"""
54 cdb
= CompilationDatabase
.fromDirectory(str_to_path(kInputsDir
))
55 cmds
= cdb
.getCompileCommands(
56 str_to_path("/home/john.doe/MyProject/project.cpp")
58 self
.assertNotEqual(len(cmds
), 0)
60 def test_all_compilecommand(self
):
61 """Check we get all results from the db"""
62 cdb
= CompilationDatabase
.fromDirectory(kInputsDir
)
63 cmds
= cdb
.getAllCompileCommands()
64 self
.assertEqual(len(cmds
), 3)
67 "wd": "/home/john.doe/MyProject",
68 "file": "/home/john.doe/MyProject/project.cpp",
75 "/home/john.doe/MyProject/project.cpp",
79 "wd": "/home/john.doe/MyProjectA",
80 "file": "/home/john.doe/MyProject/project2.cpp",
87 "/home/john.doe/MyProject/project2.cpp",
91 "wd": "/home/john.doe/MyProjectB",
92 "file": "/home/john.doe/MyProject/project2.cpp",
100 "/home/john.doe/MyProject/project2.cpp",
104 for i
in range(len(cmds
)):
105 self
.assertEqual(cmds
[i
].directory
, expected
[i
]["wd"])
106 self
.assertEqual(cmds
[i
].filename
, expected
[i
]["file"])
107 for arg
, exp
in zip(cmds
[i
].arguments
, expected
[i
]["line"]):
108 self
.assertEqual(arg
, exp
)
110 def test_1_compilecommand(self
):
111 """Check file with single compile command"""
112 cdb
= CompilationDatabase
.fromDirectory(kInputsDir
)
113 file = "/home/john.doe/MyProject/project.cpp"
114 cmds
= cdb
.getCompileCommands(file)
115 self
.assertEqual(len(cmds
), 1)
116 self
.assertEqual(cmds
[0].directory
, os
.path
.dirname(file))
117 self
.assertEqual(cmds
[0].filename
, file)
124 "/home/john.doe/MyProject/project.cpp",
126 for arg
, exp
in zip(cmds
[0].arguments
, expected
):
127 self
.assertEqual(arg
, exp
)
129 def test_2_compilecommand(self
):
130 """Check file with 2 compile commands"""
131 cdb
= CompilationDatabase
.fromDirectory(kInputsDir
)
132 cmds
= cdb
.getCompileCommands("/home/john.doe/MyProject/project2.cpp")
133 self
.assertEqual(len(cmds
), 2)
136 "wd": "/home/john.doe/MyProjectA",
143 "/home/john.doe/MyProject/project2.cpp",
147 "wd": "/home/john.doe/MyProjectB",
153 "project2-feature.o",
155 "/home/john.doe/MyProject/project2.cpp",
159 for i
in range(len(cmds
)):
160 self
.assertEqual(cmds
[i
].directory
, expected
[i
]["wd"])
161 for arg
, exp
in zip(cmds
[i
].arguments
, expected
[i
]["line"]):
162 self
.assertEqual(arg
, exp
)
164 def test_compilecommand_iterator_stops(self
):
165 """Check that iterator stops after the correct number of elements"""
166 cdb
= CompilationDatabase
.fromDirectory(kInputsDir
)
168 for cmd
in cdb
.getCompileCommands("/home/john.doe/MyProject/project2.cpp"):
170 self
.assertLessEqual(count
, 2)
172 def test_compilationDB_references(self
):
173 """Ensure CompilationsCommands are independent of the database"""
174 cdb
= CompilationDatabase
.fromDirectory(kInputsDir
)
175 cmds
= cdb
.getCompileCommands("/home/john.doe/MyProject/project.cpp")
178 workingdir
= cmds
[0].directory
180 def test_compilationCommands_references(self
):
181 """Ensure CompilationsCommand keeps a reference to CompilationCommands"""
182 cdb
= CompilationDatabase
.fromDirectory(kInputsDir
)
183 cmds
= cdb
.getCompileCommands("/home/john.doe/MyProject/project.cpp")
188 workingdir
= cmd0
.directory