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
@@ -30,6 +32,7 @@ def setUpClass(self):
30
32
self .path = Path (self .tmp .name )
31
33
open (self .path / "file.cpp" , mode = "w" ).close ()
32
34
open (self .path / "other.cpp" , mode = "w" ).close ()
35
+ open (self .path / "unused.cpp" , mode = "w" ).close ()
33
36
os .symlink (self .path / "file.cpp" , self .path / "symlink.cpp" )
34
37
35
38
@classmethod
@@ -97,6 +100,59 @@ def test_print(self):
97
100
[f"{ meta } \u2500 \u2500 { expected_name } -> { expected_link } " ],
98
101
)
99
102
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
+
100
156
101
157
if __name__ == "__main__" :
102
158
unittest .main ()
0 commit comments