| 1 |
import os |
| 2 |
|
| 3 |
from clang.cindex import CompilationDatabase, CompilationDatabaseError |
| 4 |
|
| 5 |
|
| 6 |
import gc |
| 7 |
import unittest |
| 8 |
import sys |
| 9 |
from pathlib import Path |
| 10 |
|
| 11 |
INPUTS_DIR = os.path.join(os.path.dirname(__file__), "INPUTS") |
| 12 |
|
| 13 |
|
| 14 |
@unittest.skipIf(sys.platform == "win32", "TODO: Fix these tests on Windows") |
| 15 |
class TestCDB(unittest.TestCase): |
| 16 |
def test_create_fail(self): |
| 17 |
"""Check we fail loading a database with an assertion""" |
| 18 |
path = os.path.dirname(__file__) |
| 19 |
|
| 20 |
# clang_CompilationDatabase_fromDirectory calls fprintf(stderr, ...) |
| 21 |
# Suppress its output. |
| 22 |
try: |
| 23 |
stderr = os.dup(2) |
| 24 |
with open(os.devnull, "wb") as null: |
| 25 |
os.dup2(null.fileno(), 2) |
| 26 |
with self.assertRaises(CompilationDatabaseError) as cm: |
| 27 |
CompilationDatabase.fromDirectory(path) |
| 28 |
# Ensures that stderr is reset even if the above code crashes |
| 29 |
finally: |
| 30 |
os.dup2(stderr, 2) |
| 31 |
os.close(stderr) |
| 32 |
|
| 33 |
e = cm.exception |
| 34 |
self.assertEqual(e.cdb_error, CompilationDatabaseError.ERROR_CANNOTLOADDATABASE) |
| 35 |
|
| 36 |
def test_create(self): |
| 37 |
"""Check we can load a compilation database""" |
| 38 |
CompilationDatabase.fromDirectory(INPUTS_DIR) |
| 39 |
|
| 40 |
def test_lookup_succeed(self): |
| 41 |
"""Check we get some results if the file exists in the db""" |
| 42 |
cdb = CompilationDatabase.fromDirectory(INPUTS_DIR) |
| 43 |
cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp") |
| 44 |
self.assertNotEqual(len(cmds), 0) |
| 45 |
|
| 46 |
def test_lookup_succeed_pathlike(self): |
| 47 |
"""Same as test_lookup_succeed, but with PathLikes""" |
| 48 |
cdb = CompilationDatabase.fromDirectory(Path(INPUTS_DIR)) |
| 49 |
cmds = cdb.getCompileCommands(Path("/home/john.doe/MyProject/project.cpp")) |
| 50 |
self.assertNotEqual(len(cmds), 0) |
| 51 |
|
| 52 |
def test_all_compilecommand(self): |
| 53 |
"""Check we get all results from the db""" |
| 54 |
cdb = CompilationDatabase.fromDirectory(INPUTS_DIR) |
| 55 |
cmds = cdb.getAllCompileCommands() |
| 56 |
self.assertEqual(len(cmds), 3) |
| 57 |
expected = [ |
| 58 |
{ |
| 59 |
"wd": "/home/john.doe/MyProject", |
| 60 |
"file": "/home/john.doe/MyProject/project.cpp", |
| 61 |
"line": [ |
| 62 |
"clang++", |
| 63 |
"--driver-mode=g++", |
| 64 |
"-o", |
| 65 |
"project.o", |
| 66 |
"-c", |
| 67 |
"/home/john.doe/MyProject/project.cpp", |
| 68 |
], |
| 69 |
}, |
| 70 |
{ |
| 71 |
"wd": "/home/john.doe/MyProjectA", |
| 72 |
"file": "/home/john.doe/MyProject/project2.cpp", |
| 73 |
"line": [ |
| 74 |
"clang++", |
| 75 |
"--driver-mode=g++", |
| 76 |
"-o", |
| 77 |
"project2.o", |
| 78 |
"-c", |
| 79 |
"/home/john.doe/MyProject/project2.cpp", |
| 80 |
], |
| 81 |
}, |
| 82 |
{ |
| 83 |
"wd": "/home/john.doe/MyProjectB", |
| 84 |
"file": "/home/john.doe/MyProject/project2.cpp", |
| 85 |
"line": [ |
| 86 |
"clang++", |
| 87 |
"--driver-mode=g++", |
| 88 |
"-DFEATURE=1", |
| 89 |
"-o", |
| 90 |
"project2-feature.o", |
| 91 |
"-c", |
| 92 |
"/home/john.doe/MyProject/project2.cpp", |
| 93 |
], |
| 94 |
}, |
| 95 |
] |
| 96 |
for i in range(len(cmds)): |
| 97 |
self.assertEqual(cmds[i].directory, expected[i]["wd"]) |
| 98 |
self.assertEqual(cmds[i].filename, expected[i]["file"]) |
| 99 |
for arg, exp in zip(cmds[i].arguments, expected[i]["line"]): |
| 100 |
self.assertEqual(arg, exp) |
| 101 |
|
| 102 |
def test_1_compilecommand(self): |
| 103 |
"""Check file with single compile command""" |
| 104 |
cdb = CompilationDatabase.fromDirectory(INPUTS_DIR) |
| 105 |
file = "/home/john.doe/MyProject/project.cpp" |
| 106 |
cmds = cdb.getCompileCommands(file) |
| 107 |
self.assertEqual(len(cmds), 1) |
| 108 |
self.assertEqual(cmds[0].directory, os.path.dirname(file)) |
| 109 |
self.assertEqual(cmds[0].filename, file) |
| 110 |
expected = [ |
| 111 |
"clang++", |
| 112 |
"--driver-mode=g++", |
| 113 |
"-o", |
| 114 |
"project.o", |
| 115 |
"-c", |
| 116 |
"/home/john.doe/MyProject/project.cpp", |
| 117 |
] |
| 118 |
for arg, exp in zip(cmds[0].arguments, expected): |
| 119 |
self.assertEqual(arg, exp) |
| 120 |
|
| 121 |
def test_2_compilecommand(self): |
| 122 |
"""Check file with 2 compile commands""" |
| 123 |
cdb = CompilationDatabase.fromDirectory(INPUTS_DIR) |
| 124 |
cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project2.cpp") |
| 125 |
self.assertEqual(len(cmds), 2) |
| 126 |
expected = [ |
| 127 |
{ |
| 128 |
"wd": "/home/john.doe/MyProjectA", |
| 129 |
"line": [ |
| 130 |
"clang++", |
| 131 |
"--driver-mode=g++", |
| 132 |
"-o", |
| 133 |
"project2.o", |
| 134 |
"-c", |
| 135 |
"/home/john.doe/MyProject/project2.cpp", |
| 136 |
], |
| 137 |
}, |
| 138 |
{ |
| 139 |
"wd": "/home/john.doe/MyProjectB", |
| 140 |
"line": [ |
| 141 |
"clang++", |
| 142 |
"--driver-mode=g++", |
| 143 |
"-DFEATURE=1", |
| 144 |
"-o", |
| 145 |
"project2-feature.o", |
| 146 |
"-c", |
| 147 |
"/home/john.doe/MyProject/project2.cpp", |
| 148 |
], |
| 149 |
}, |
| 150 |
] |
| 151 |
for i in range(len(cmds)): |
| 152 |
self.assertEqual(cmds[i].directory, expected[i]["wd"]) |
| 153 |
for arg, exp in zip(cmds[i].arguments, expected[i]["line"]): |
| 154 |
self.assertEqual(arg, exp) |
| 155 |
|
| 156 |
def test_compilecommand_iterator_stops(self): |
| 157 |
"""Check that iterator stops after the correct number of elements""" |
| 158 |
cdb = CompilationDatabase.fromDirectory(INPUTS_DIR) |
| 159 |
count = 0 |
| 160 |
for cmd in cdb.getCompileCommands("/home/john.doe/MyProject/project2.cpp"): |
| 161 |
count += 1 |
| 162 |
self.assertLessEqual(count, 2) |
| 163 |
|
| 164 |
def test_compilationDB_references(self): |
| 165 |
"""Ensure CompilationsCommands are independent of the database""" |
| 166 |
cdb = CompilationDatabase.fromDirectory(INPUTS_DIR) |
| 167 |
cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp") |
| 168 |
del cdb |
| 169 |
gc.collect() |
| 170 |
cmds[0].directory |
| 171 |
|
| 172 |
def test_compilationCommands_references(self): |
| 173 |
"""Ensure CompilationsCommand keeps a reference to CompilationCommands""" |
| 174 |
cdb = CompilationDatabase.fromDirectory(INPUTS_DIR) |
| 175 |
cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp") |
| 176 |
del cdb |
| 177 |
cmd0 = cmds[0] |
| 178 |
del cmds |
| 179 |
gc.collect() |
| 180 |
cmd0.directory |