forked from asterisk/testsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage.py
executable file
·127 lines (100 loc) · 3.63 KB
/
usage.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
#!/usr/bin/env python
'''Asterisk external test suite usage report
Copyright (C) 2016, Digium, Inc.
Scott Griepentrog <[email protected]>
This program is free software, distributed under the terms of
the GNU General Public License Version 2.
'''
import sys
import yaml
try:
from yaml import CSafeLoader as MyLoader
except ImportError:
from yaml import SafeLoader as MyLoader
sys.path.append('lib/python')
TESTS_CONFIG = "tests.yaml"
TEST_CONFIG = "test-config.yaml"
class Test:
def __init__(self, path):
self.path = path
self.test_config = load_yaml_config("%s/%s" % (path, TEST_CONFIG))
properties = self.test_config.get('properties', {})
self.tags = properties.get('tags', ['none'])
self.dependencies = [repr(d)
for d in properties.get('dependencies', [])]
test_modules = self.test_config.get('test-modules', {})
test_objects = test_modules.get('test-object', {})
if not isinstance(test_objects, list):
test_objects = [test_objects]
self.test_objects = [obj.get('typename', 'test-run')
for obj in test_objects]
modules = test_modules.get('modules', {})
self.test_modules = [module.get('typename', '-error-')
for module in modules]
class TestSuite:
def __init__(self):
self.tests = self._parse_test_yaml("tests")
def _parse_test_yaml(self, test_dir):
tests = []
config = load_yaml_config("%s/%s" % (test_dir, TESTS_CONFIG))
if not config:
return tests
for t in config["tests"]:
for val in t:
path = "%s/%s" % (test_dir, t[val])
if val == "test":
tests.append(Test(path))
elif val == "dir":
tests += self._parse_test_yaml(path)
return tests
def unique(self, key):
result = set()
for test in self.tests:
result = result.union(getattr(test, key))
result = list(set(result))
result.sort(key=str.lower)
return result
def occurances(self, **kwargs):
match = []
for test in self.tests:
for key, value in kwargs.items():
if value in getattr(test, key):
match.append(test)
continue
return len(match)
def results_for(self, key):
print(key.title() + ":")
things = self.unique(key)
width = max(len(t) for t in things)
results = [(self.occurances(**{key: t}), t) for t in things]
results.sort(key=lambda tup: tup[0], reverse=True)
for (count, name) in results:
print("\t%-*s %5d" % (width, name, count))
print("")
def load_yaml_config(path):
"""Load contents of a YAML config file to a dictionary"""
try:
f = open(path, "r")
except IOError:
# Ignore errors for the optional tests/custom folder.
if path != "tests/custom/tests.yaml":
print("Failed to open %s" % path)
return None
except:
print("Unexpected error: %s" % sys.exc_info()[0])
return None
config = yaml.load(f, Loader=MyLoader)
f.close()
return config
def main(argv=None):
print("Testsuite Module Usage and Coverage Report")
print("")
test_suite = TestSuite()
print("Number of tests:", len(test_suite.tests))
print("")
test_suite.results_for('tags')
test_suite.results_for('test_objects')
test_suite.results_for('test_modules')
test_suite.results_for('dependencies')
if __name__ == "__main__":
sys.exit(main() or 0)