Skip to content

Commit d37e8c0

Browse files
committed
Add regression test for unused files in FileTree
Unused files are correctly highlighted within the tree, but do not correctly contribute to the summary statistics calculated for their parent directories. Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 00f4a4f commit d37e8c0

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

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)