Skip to content

Commit 3be870a

Browse files
authored
Merge pull request #166 from Pennycook/fix-tree
Fix bug in FileTree's handling of unused files
2 parents 6b76126 + b0526d2 commit 3be870a

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

codebasin/report.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,9 +780,13 @@ def files(
780780
for f in codebase:
781781
setmap = defaultdict(int)
782782
if state:
783-
for node, assoc in state.get_map(f).items():
784-
if isinstance(node, CodeNode):
785-
setmap[frozenset(assoc)] += node.num_lines
783+
association = state.get_map(f)
784+
for node in filter(
785+
lambda x: isinstance(x, CodeNode),
786+
state.get_tree(f).walk(),
787+
):
788+
platform = frozenset(association[node])
789+
setmap[platform] += node.num_lines
786790
tree.insert(f, setmap)
787791

788792
print("", file=stream)

tests/files/test_filetree.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Copyright (C) 2019-2024 Intel Corporation
22
# SPDX-License-Identifier: BSD-3-Clause
33

4+
import io
45
import logging
56
import os
67
import tempfile
78
import unittest
89
from pathlib import Path
910

11+
from codebasin import CodeBase, finder, report
1012
from codebasin.report import FileTree
1113

1214

@@ -30,6 +32,7 @@ def setUpClass(self):
3032
self.path = Path(self.tmp.name)
3133
open(self.path / "file.cpp", mode="w").close()
3234
open(self.path / "other.cpp", mode="w").close()
35+
open(self.path / "unused.cpp", mode="w").close()
3336
os.symlink(self.path / "file.cpp", self.path / "symlink.cpp")
3437

3538
@classmethod
@@ -97,6 +100,59 @@ def test_print(self):
97100
[f"{meta} \u2500\u2500 {expected_name} -> {expected_link}"],
98101
)
99102

103+
def test_report(self):
104+
"""Check report output is accurate"""
105+
stream = io.StringIO()
106+
codebase = CodeBase(self.path)
107+
configuration = {
108+
"X": [
109+
{
110+
"file": str(self.path / "file.cpp"),
111+
"defines": [],
112+
"include_paths": [],
113+
"include_files": [],
114+
},
115+
],
116+
"Y": [
117+
{
118+
"file": str(self.path / "other.cpp"),
119+
"defines": [],
120+
"include_paths": [],
121+
"include_files": [],
122+
},
123+
],
124+
}
125+
with open(self.path / "file.cpp", mode="w") as f:
126+
f.write("void foo();")
127+
with open(self.path / "other.cpp", mode="w") as f:
128+
f.write("void bar();")
129+
with open(self.path / "unused.cpp", mode="w") as f:
130+
f.write("void baz();")
131+
state = finder.find(
132+
self.path,
133+
codebase,
134+
configuration,
135+
show_progress=False,
136+
)
137+
report.files(codebase, state, stream=stream)
138+
output = stream.getvalue()
139+
140+
# Skip any header and focus on the tree at the end.
141+
lines = output.strip().split("\n")[-5:]
142+
143+
# Output should contain one line for the directory + each file.
144+
self.assertTrue(len(lines) == 5)
145+
146+
# Check the root directory values include the unused file.
147+
self.assertTrue("[AB | 3 | 66.67 | 33.33]" in lines[0])
148+
149+
# Check the other lines reflect the other files.
150+
# The order here isn't guaranteed, so don't check specific lines.
151+
self.assertTrue("[A- | 1 | 100.00 | 50.00]" in output)
152+
self.assertTrue("[-- | 1 | 0.00 | 0.00]" in output)
153+
self.assertTrue("[-B | 1 | 100.00 | 50.00]" in output)
154+
self.assertTrue("[A- | 1 | 100.00 | 50.00]" in output)
155+
100156

101157
if __name__ == "__main__":
102158
unittest.main()

0 commit comments

Comments
 (0)