Skip to content

Commit 164c3f1

Browse files
committed
Test handling of realistic out-of-tree builds
The JSON compilation databases used in this test are designed to mimic those generated by CMake. Notably, the "file" field of such databases is an absolute path to a file in the source directory. Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 84665d7 commit 164c3f1

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

tests/build-dir/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (C) 2021 Intel Corporation
2+
# SPDX-License-Identifier: BSD-3-Clause

tests/build-dir/foo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void foo() { return; }

tests/build-dir/test_build_dir.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
4+
import json
5+
import logging
6+
import tempfile
7+
import unittest
8+
from pathlib import Path
9+
10+
from codebasin import config, finder
11+
from codebasin.walkers.platform_mapper import PlatformMapper
12+
13+
14+
class TestBuildDirectories(unittest.TestCase):
15+
"""
16+
Test ability to correctly handle out-of-tree builds.
17+
"""
18+
19+
def setUp(self):
20+
self.rootdir = str(Path(__file__).parent)
21+
logging.getLogger("codebasin").disabled = True
22+
23+
def test_absolute_paths(self):
24+
"""
25+
Test database with build "directory" path but source "file" path.
26+
All "file" fields are absolute paths.
27+
"""
28+
29+
source = str(Path(__file__).parent.joinpath("foo.cpp"))
30+
31+
# CBI only understands how to load compilation databases from file.
32+
# For now, create temporary files every time we test.
33+
dir1 = str(Path(__file__).parent.joinpath("build1/"))
34+
build1 = tempfile.NamedTemporaryFile()
35+
json1 = [
36+
{
37+
"command": f"/usr/bin/c++ -o foo.cpp.o -c {source}",
38+
"directory": f"{dir1}",
39+
"file": f"{source}",
40+
},
41+
]
42+
with open(build1.name, "w") as f:
43+
json.dump(json1, f)
44+
45+
dir2 = str(Path(__file__).parent.joinpath("build2/"))
46+
build2 = tempfile.NamedTemporaryFile()
47+
json2 = [
48+
{
49+
"command": f"/usr/bin/c++ -o foo.cpp.o -c {source}",
50+
"directory": f"{dir2}",
51+
"file": f"{source}",
52+
},
53+
]
54+
with open(build2.name, "w") as f:
55+
json.dump(json2, f)
56+
57+
codebase = {
58+
"files": [source],
59+
"platforms": ["one", "two"],
60+
"exclude_files": set(),
61+
"exclude_patterns": [],
62+
"rootdir": self.rootdir,
63+
}
64+
65+
configuration = {}
66+
for name, path in [("one", build1.name), ("two", build2.name)]:
67+
db = config.load_database(path, self.rootdir)
68+
configuration.update({name: db})
69+
70+
expected_setmap = {frozenset(["one", "two"]): 1}
71+
72+
state = finder.find(self.rootdir, codebase, configuration)
73+
mapper = PlatformMapper(codebase)
74+
setmap = mapper.walk(state)
75+
self.assertDictEqual(setmap, expected_setmap, "Mismatch in setmap")
76+
77+
78+
if __name__ == "__main__":
79+
unittest.main()

0 commit comments

Comments
 (0)