-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_including_to_included_analyzer.py
235 lines (185 loc) · 8.94 KB
/
test_including_to_included_analyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import os
import unittest
import common_utils
import generate_inclusions
import git_utils
import inclusions_config
import inclusions_database
from including_to_included_analyzer import IncludingToIncludedAnalyzer
from including_to_included_analyzer import ComputeGroupNumInclusionsDeltaBetween
class TestGenerateIncludingToIncludedAnalyzer(unittest.TestCase):
# Creates an IncludingToIncludedAnalyzer instance that operates on
# the test config associated with these tests and uses
# |including_file_filters|.
def CreateAnalyzer(self,
including_file_filters,
included_files_to_limit_to=None):
with common_utils.TemporaryDirectory() as output_dir:
# TODO: Should I have a test database that I read off disk?
config_filename = (
"./test/data/configs/test_including_to_included_analyzer_config.py")
generate_inclusions.generate_inclusions("dummy", config_filename,
output_dir)
repo_rev = git_utils.GetUsageAnalyzerRepoRevision()
database_name = "_".join(["test", repo_rev, "inclusions_db.py"])
database_filepath = os.path.join(output_dir, "test", repo_rev,
database_name)
assert os.path.isfile(database_filepath)
analyzer = IncludingToIncludedAnalyzer(
database_filepath,
including_file_filters,
included_files_to_limit_to=included_files_to_limit_to)
return analyzer
def test_LimitIncludedFiles(self):
analyzer = self.CreateAnalyzer([], ["foo/foo.h"])
expected_including_file_dict = {
"bar/bar.h": ["foo/foo.h"],
}
self.assertEqual(expected_including_file_dict, analyzer.including_file_dict)
def test_GenerateNumInclusionsForFilterFunction(self):
analyzer = self.CreateAnalyzer([])
# Test with a filter that allows all including files.
expected_output = {
"total": 3,
"bar/bar.h": 2,
"bar/baz/bar_core_factory.h": 1
}
output = analyzer.GenerateNumInclusionsForFilterFunction(lambda k: True)
self.assertEqual(expected_output, output)
# Test with a filter that allows only "bar/bar.h".
expected_output = {"total": 2, "bar/bar.h": 2}
output = analyzer.GenerateNumInclusionsForFilterFunction(
lambda k: k == "bar/bar.h")
self.assertEqual(expected_output, output)
def test_GenerateGroupNumInclusionsForFiltersDefaultFilters(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
analyzer = self.CreateAnalyzer([])
expected_output = {"total": 3, "bar": 2, "bar/baz": 1}
output = analyzer.GenerateGroupNumInclusionsForFilters(KeyPartitionFunction)
self.assertEqual(expected_output, output)
def test_GenerateGroupNumInclusionsForFiltersConstructionFilter(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
analyzer = self.CreateAnalyzer([r"bar/baz.*"])
expected_output = {"total": 2, "bar": 2}
output = analyzer.GenerateGroupNumInclusionsForFilters(KeyPartitionFunction)
self.assertEqual(expected_output, output)
def test_GenerateGroupNumInclusionsForFiltersMultipleFilters(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
analyzer = self.CreateAnalyzer([r"bar/baz.*"])
expected_output = {"total": 0}
output = analyzer.GenerateGroupNumInclusionsForFilters(
KeyPartitionFunction, filters=["bar/bar\..*"])
self.assertEqual(expected_output, output)
def test_GenerateGroupSizesForFiltersDefaultFilters(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
analyzer = self.CreateAnalyzer([])
expected_output = {"total": 2, "bar": 1, "bar/baz": 1}
output = analyzer.GenerateGroupSizesForFilters(KeyPartitionFunction)
self.assertEqual(expected_output, output)
def test_GenerateGroupSizesForFiltersConstructionFilter(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
analyzer = self.CreateAnalyzer([r"bar/baz.*"])
expected_output = {"total": 1, "bar": 1}
output = analyzer.GenerateGroupSizesForFilters(KeyPartitionFunction)
self.assertEqual(expected_output, output)
def test_GenerateGroupSizesForFiltersMultipleFilters(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
analyzer = self.CreateAnalyzer([r"bar/baz.*"])
expected_output = {"total": 0}
output = analyzer.GenerateGroupSizesForFilters(
KeyPartitionFunction, filters=["bar/bar\..*"])
self.assertEqual(expected_output, output)
def test_GenerateGroupNumInclusions(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
analyzer = self.CreateAnalyzer([])
expected_all_inclusions = {"total": 3, "bar": 2, "bar/baz": 1}
expected_prod_inclusions = {"total": 3, "bar": 2, "bar/baz": 1}
expected_prod_non_factory_inclusions = {"total": 2, "bar": 2}
expected_output = [["# inclusions", expected_all_inclusions],
["from prod", expected_prod_inclusions],
[
"from prod non-factory",
expected_prod_non_factory_inclusions
]]
output = analyzer.GenerateGroupNumInclusions(KeyPartitionFunction)
self.assertEqual(expected_output, output)
def test_GenerateGroupsOrderedByNumInclusions(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
analyzer = self.CreateAnalyzer([])
expected_output = ["total", "bar", "bar/baz"]
output = analyzer.GroupsOrderedByNumInclusions(KeyPartitionFunction)
self.assertEqual(expected_output, output)
def test_GenerateGroupSizes(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
analyzer = self.CreateAnalyzer([])
expected_all_including_files = {"total": 2, "bar": 1, "bar/baz": 1}
expected_prod_including_files = {"total": 2, "bar": 1, "bar/baz": 1}
expected_prod_non_factory_including_files = {"total": 1, "bar": 1}
expected_output = [["# including files", expected_all_including_files],
["prod", expected_prod_including_files],
[
"prod non-factory",
expected_prod_non_factory_including_files
]]
output = analyzer.GenerateGroupSizes(KeyPartitionFunction)
self.assertEqual(expected_output, output)
def test_GenerateGroupNumInclusionsAnalysisAsCsv(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
analyzer = self.CreateAnalyzer([])
expected_output = "key name,# inclusions,from prod,from prod non-factory\r\n"
expected_output += "total,3,3,2\r\n"
expected_output += "bar,2,2,2\r\n"
expected_output += "bar/baz,1,1,0\r\n"
output = analyzer.GenerateGroupAnalysisAsCsv(
"num_inclusions", KeyPartitionFunction, "key name")
self.assertEqual(expected_output, output)
def test_GenerateGroupSizesAnalysisAsCsv(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
analyzer = self.CreateAnalyzer([])
expected_output = "key name,# including files,prod,prod non-factory\r\n"
expected_output += "total,2,2,1\r\n"
expected_output += "bar/baz,1,1,0\r\n"
expected_output += "bar,1,1,1\r\n"
output = analyzer.GenerateGroupAnalysisAsCsv(
"group_size", KeyPartitionFunction, "key name")
self.assertEqual(expected_output, output)
def test_GenerateGroupSizesAnalysisAsCsvCustomKeyOrder(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
analyzer = self.CreateAnalyzer([])
key_order = ["total", "bar", "bar/baz"]
expected_output = "key name,# including files,prod,prod non-factory\r\n"
expected_output += "total,2,2,1\r\n"
expected_output += "bar,1,1,1\r\n"
expected_output += "bar/baz,1,1,0\r\n"
output = analyzer.GenerateGroupAnalysisAsCsv(
"group_size", KeyPartitionFunction, "key name", key_order=key_order)
self.assertEqual(expected_output, output)
def test_ComputeGroupNumInclusionsDeltaBetween(self):
def KeyPartitionFunction(filename):
return os.path.dirname(filename)
expected_all_inclusions = {"total": 0, "baz": 1, "bar": 1, "qux": -2}
expected_prod_inclusions = {"total": 1, "baz": 1, "bar": 1, "qux": -1}
expected_prod_non_factory_inclusions = {"total": 2, "baz": 1, "bar": 1}
expected_output = [["# inclusions", expected_all_inclusions],
["from prod", expected_prod_inclusions],
[
"from prod non-factory",
expected_prod_non_factory_inclusions
]]
output = ComputeGroupNumInclusionsDeltaBetween(
"./test/data/databases/fake_999999_inclusions_db.py",
"./test/data/databases/fake_aaaaaaa_inclusions_db.py",
KeyPartitionFunction)
self.assertEqual(expected_output, output)