-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_unit_tests.py
84 lines (70 loc) · 3.18 KB
/
get_unit_tests.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
"""Unit tests retriever needs '-p'
-p, path: is a flag for a directory of a code base that should contain unit tests
get_unit_tests will fetch those tests and log them with their modules and paths for reporting reasons
ex: python3 get_unit_tests -p /home/cerebral/cs/app-frontends/apps/
"""
import re
import mmap
from cli_args_system import Args
from pathlib import Path
anomalies = [b"$emit", b"submit(", b"commit(", b"split(", b"@Test(\n", b"[C", b", 'C"]
snippets = [b'it(', b'@Test(', b', async () => {', b', assert', b', () => {', b', () => {', b', () =>', b', fakeAsync(() => {', b', doneFn => {', b', done => {', b', async doneFn => {', b', async done => {', b', async(() => {', b', {', b", '')", b'Rendered);', b'Significant);', b'Zero);', b' expect(isFovExtendable(mockFovFilter)).toBeFalsy());', b', function () {});', b'EmptyContentIs', b'LoaderIs', b'ErrorBoundaryIs', b'FiltersBarMainIs', b'FiltersBarDrilldownIs', b'FiltersBarPageIs', b'ComponentIs', b'ComponentIsNot', b'LineChartIs', b", '' skip: true })", b'// ', b"t close menu'"]
def units_fetcher(path):
raw_units = []
specs = []
for path in Path(path).rglob('*.spec.ts'):
specs.append(path)
for spec in specs:
with open(spec, 'rb') as file:
f = mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ)
for line in iter(f.readline, b""):
if (b"it('") in line:
raw_units.append({
"test_case_name": line,
"file_path": spec
})
if (b"@Test(") in line:
raw_units.append({
"test_case_name": line,
"file_path": spec
})
return raw_units, len(raw_units)
def anomalies_remover(raw_units):
units = []
for a in anomalies:
raw_units = [r_u for r_u in raw_units if a not in r_u["test_case_name"] ]
units = raw_units
return units, len(units)
def snippets_remover(units):
for idx in range(len(units)):
for s in snippets:
if s in units[idx]["test_case_name"]:
units[idx]["test_case_name"] = units[idx]["test_case_name"].replace(s, b'')
units[idx]["test_case_name"] = units[idx]["test_case_name"].strip()
return units
def write_me_pls(units):
f = open("units.log", "a")
for u in units:
module = re.search('apps/(.+?)/', u["file_path"].as_posix()).group(1)
f.write("'Module Name': ")
f.write(str(module))
f.write("\n")
f.write("'Test Case Name': ")
f.write(u["test_case_name"].decode("utf-8"))
f.write("\n")
f.write("'Path': ")
f.write(u["file_path"].as_posix())
f.write("\n")
f.write("~~~~~~~~~~~~~")
f.write("\n")
f.close()
if __name__ == "__main__":
args = Args(convert_numbers=False)
# Example: "/home/cerebral/cs/app-frontends/apps/"
path = args.flag_str('p','path')
raw_units, raw_units_total = units_fetcher(path)
units, units_total = anomalies_remover(raw_units)
units = snippets_remover(units)
write_me_pls(units)
print("raw_units_total", raw_units_total)
print("units_total", units_total)