Skip to content

Commit ee3c3e9

Browse files
committed
Add tests for --levels/-L option
Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent e8caa73 commit ee3c3e9

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

tests/files/test_filetree.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def setUpClass(self):
3232
self.path = Path(self.tmp.name)
3333
open(self.path / "file.cpp", mode="w").close()
3434
open(self.path / "other.cpp", mode="w").close()
35-
open(self.path / "unused.cpp", mode="w").close()
3635
os.symlink(self.path / "file.cpp", self.path / "symlink.cpp")
3736

3837
@classmethod
@@ -153,6 +152,72 @@ def test_report(self):
153152
self.assertTrue("[-B | 1 | 100.00 | 50.00]" in output)
154153
self.assertTrue("[A- | 1 | 100.00 | 50.00]" in output)
155154

155+
def test_levels(self):
156+
"""Check report --levels flag works correctly"""
157+
# Set up subdirectories for this test
158+
tmp = tempfile.TemporaryDirectory()
159+
path = Path(tmp.name)
160+
os.makedirs(path / "first" / "second" / "third")
161+
open(path / "first" / "one.cpp", mode="w").close()
162+
open(path / "first" / "second" / "two.cpp", mode="w").close()
163+
164+
codebase = CodeBase(path)
165+
configuration = {
166+
"X": [
167+
{
168+
"file": str(path / "first" / "one.cpp"),
169+
"defines": [],
170+
"include_paths": [],
171+
"include_files": [],
172+
},
173+
],
174+
"Y": [
175+
{
176+
"file": str(path / "first" / "second" / "two.cpp"),
177+
"defines": [],
178+
"include_paths": [],
179+
"include_files": [],
180+
},
181+
],
182+
}
183+
state = finder.find(
184+
path,
185+
codebase,
186+
configuration,
187+
show_progress=False,
188+
)
189+
190+
# By default, we should see all levels of the tree.
191+
stream = io.StringIO()
192+
report.files(codebase, state, stream=stream)
193+
output = stream.getvalue()
194+
self.assertTrue(str(path) in output)
195+
self.assertTrue("first/" in output)
196+
self.assertTrue("one.cpp" in output)
197+
self.assertTrue("two.cpp" in output)
198+
199+
# With two levels, the "second" directory should be collapsed.
200+
# This will hide "two.cpp" from the output.
201+
stream = io.StringIO()
202+
report.files(codebase, state, stream=stream, levels=2)
203+
output = stream.getvalue()
204+
self.assertTrue(str(path) in output)
205+
self.assertTrue("first/" in output)
206+
self.assertTrue("one.cpp" in output)
207+
self.assertFalse("two.cpp" in output)
208+
209+
# With just one level, the "first" directory should be collapsed.
210+
# This will hide "one.cpp" and "two.cpp" from the output.
211+
stream = io.StringIO()
212+
report.files(codebase, state, stream=stream, levels=1)
213+
output = stream.getvalue()
214+
self.assertTrue(str(path) in output)
215+
self.assertTrue("first/" in output)
216+
self.assertFalse("one.cpp" in output)
217+
self.assertFalse("two.cpp" in output)
218+
219+
tmp.cleanup()
220+
156221

157222
if __name__ == "__main__":
158223
unittest.main()

0 commit comments

Comments
 (0)