Skip to content

Commit 406a92c

Browse files
committed
Add tests for --levels/-L option
Signed-off-by: John Pennycook <[email protected]>
1 parent 18180f8 commit 406a92c

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

tests/files/test_filetree.py

+68
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

@@ -97,6 +99,72 @@ def test_print(self):
9799
[f"{meta} \u2500\u2500 {expected_name} -> {expected_link}"],
98100
)
99101

102+
def test_levels(self):
103+
"""Check report --levels flag works correctly"""
104+
# Set up subdirectories for this test
105+
tmp = tempfile.TemporaryDirectory()
106+
path = Path(tmp.name)
107+
os.makedirs(path / "first" / "second" / "third")
108+
open(path / "first" / "one.cpp", mode="w").close()
109+
open(path / "first" / "second" / "two.cpp", mode="w").close()
110+
111+
codebase = CodeBase(path)
112+
configuration = {
113+
"X": [
114+
{
115+
"file": str(path / "first" / "one.cpp"),
116+
"defines": [],
117+
"include_paths": [],
118+
"include_files": [],
119+
},
120+
],
121+
"Y": [
122+
{
123+
"file": str(path / "first" / "second" / "two.cpp"),
124+
"defines": [],
125+
"include_paths": [],
126+
"include_files": [],
127+
},
128+
],
129+
}
130+
state = finder.find(
131+
path,
132+
codebase,
133+
configuration,
134+
show_progress=False,
135+
)
136+
137+
# By default, we should see all levels of the tree.
138+
stream = io.StringIO()
139+
report.files(codebase, state, stream=stream)
140+
output = stream.getvalue()
141+
self.assertTrue(str(path) in output)
142+
self.assertTrue("first/" in output)
143+
self.assertTrue("one.cpp" in output)
144+
self.assertTrue("two.cpp" in output)
145+
146+
# With two levels, the "second" directory should be collapsed.
147+
# This will hide "two.cpp" from the output.
148+
stream = io.StringIO()
149+
report.files(codebase, state, stream=stream, levels=2)
150+
output = stream.getvalue()
151+
self.assertTrue(str(path) in output)
152+
self.assertTrue("first/" in output)
153+
self.assertTrue("one.cpp" in output)
154+
self.assertFalse("two.cpp" in output)
155+
156+
# With just one level, the "first" directory should be collapsed.
157+
# This will hide "one.cpp" and "two.cpp" from the output.
158+
stream = io.StringIO()
159+
report.files(codebase, state, stream=stream, levels=1)
160+
output = stream.getvalue()
161+
self.assertTrue(str(path) in output)
162+
self.assertTrue("first/" in output)
163+
self.assertFalse("one.cpp" in output)
164+
self.assertFalse("two.cpp" in output)
165+
166+
tmp.cleanup()
167+
100168

101169
if __name__ == "__main__":
102170
unittest.main()

0 commit comments

Comments
 (0)