|
1 | 1 | # Copyright (C) 2019-2024 Intel Corporation
|
2 | 2 | # SPDX-License-Identifier: BSD-3-Clause
|
3 | 3 |
|
| 4 | +import io |
4 | 5 | import logging
|
5 | 6 | import os
|
6 | 7 | import tempfile
|
7 | 8 | import unittest
|
8 | 9 | from pathlib import Path
|
9 | 10 |
|
| 11 | +from codebasin import CodeBase, finder, report |
10 | 12 | from codebasin.report import FileTree
|
11 | 13 |
|
12 | 14 |
|
@@ -97,6 +99,72 @@ def test_print(self):
|
97 | 99 | [f"{meta} \u2500\u2500 {expected_name} -> {expected_link}"],
|
98 | 100 | )
|
99 | 101 |
|
| 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 | + |
100 | 168 |
|
101 | 169 | if __name__ == "__main__":
|
102 | 170 | unittest.main()
|
0 commit comments