Skip to content

Commit 060c70d

Browse files
authored
Merge pull request #133 from Pennycook/symlink-files
Fix handling of symlink files
2 parents daf5de1 + 084d131 commit 060c70d

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

codebasin/finder.py

+6
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ def get_setmap(self, codebase: CodeBase) -> dict[frozenset, int]:
9898
"""
9999
setmap = collections.defaultdict(int)
100100
for fn in codebase:
101+
# Don't count symlinks if their target is in the code base.
102+
# The target will be counted separately.
103+
path = Path(fn)
104+
if path.is_symlink() and path.resolve() in codebase:
105+
continue
106+
101107
tree = self.get_tree(fn)
102108
association = self.get_map(fn)
103109
for node in [n for n in tree.walk() if isinstance(n, CodeNode)]:

tests/duplicates/test_duplicates.py

+38-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33

44
import logging
5+
import os
6+
import tempfile
57
import unittest
68
from pathlib import Path
79

@@ -50,8 +52,8 @@ def test_duplicates(self):
5052
setmap = state.get_setmap(codebase)
5153
self.assertDictEqual(setmap, expected_setmap, "Mismatch in setmap")
5254

53-
def test_symlinks(self):
54-
"""Check that symlinks do not count towards divergence."""
55+
def test_symlink_directories(self):
56+
"""Check that symlink directories do not count towards divergence."""
5557

5658
cpufile = str(self.rootdir / "cpu/foo.cpp")
5759
cpu2file = str(self.rootdir / "cpu2/foo.cpp")
@@ -83,6 +85,40 @@ def test_symlinks(self):
8385
setmap = state.get_setmap(codebase)
8486
self.assertDictEqual(setmap, expected_setmap, "Mismatch in setmap")
8587

88+
def test_symlink_files(self):
89+
"""Check that symlink files do not count towards divergence."""
90+
tmp = tempfile.TemporaryDirectory()
91+
p = Path(tmp.name)
92+
with open(p / "base.cpp", mode="w") as f:
93+
f.write("void foo();")
94+
os.symlink(p / "base.cpp", p / "symlink.cpp")
95+
96+
codebase = CodeBase(p)
97+
configuration = {
98+
"test": [
99+
{
100+
"file": str(p / "base.cpp"),
101+
"defines": [],
102+
"include_paths": [],
103+
"include_files": [],
104+
},
105+
{
106+
"file": str(p / "symlink.cpp"),
107+
"defines": [],
108+
"include_paths": [],
109+
"include_files": [],
110+
},
111+
],
112+
}
113+
114+
expected_setmap = {frozenset(["test"]): 1}
115+
116+
state = finder.find(self.rootdir, codebase, configuration)
117+
setmap = state.get_setmap(codebase)
118+
self.assertDictEqual(setmap, expected_setmap, "Mismatch in setmap")
119+
120+
tmp.cleanup()
121+
86122

87123
if __name__ == "__main__":
88124
unittest.main()

0 commit comments

Comments
 (0)