-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
123 lines (103 loc) · 3.98 KB
/
example.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
import sys
import subprocess
import chardet
from colorama import init
from termcolor import colored
from schema_entry import EntryPoint
init()
class Check(EntryPoint):
"""测试本项目."""
_name = "example.py"
class Static(EntryPoint):
"""项目的静态类型检测,并输出到typecheck文件夹."""
schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"package": {
"type": "string",
"default": "schema_entry",
"description": "待检测的模块"
},
"output": {
"type": "string",
"default": "docs/typecheck",
"description": "检测结果位置"
}
},
"required": ["output"]
}
class UnitTest(EntryPoint):
"""本项目的单元测试,并输出到文件夹."""
schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"package": {
"type": "string",
"default": "schema_entry",
"description": "待检测的模块"
},
"output": {
"type": "string",
"default": "docs/test_coverage",
"description": "检测结果位置"
}
},
"required": ["output"]
}
root = Check()
static = root.regist_sub(Static)
utest = root.regist_sub(UnitTest)
@static.as_main
def do_static(package: str, output: str) -> None:
cmd = f"mypy --ignore-missing-imports --show-column-numbers --follow-imports=silent --check-untyped-defs --disallow-untyped-defs --no-implicit-optional --warn-unused-ignores --html-report={output} {package}"
res = subprocess.run(cmd, capture_output=True, shell=True)
if res.returncode != 0:
if res.stderr:
encoding = chardet.detect(res.stderr).get("encoding")
content = res.stderr.decode(encoding).strip()
else:
encoding = chardet.detect(res.stdout).get("encoding")
content = res.stdout.decode(encoding).strip()
print(colored(content, 'white', 'on_magenta'))
else:
content = ""
if res.stdout:
encoding = chardet.detect(res.stdout).get("encoding")
content = res.stdout.decode(encoding).strip()
print(colored(content, 'white', 'on_cyan'))
@utest.as_main
def do_utest(package: str, output: str) -> None:
cmd = f"python -m coverage run --source={package} -m unittest discover -v -s . -p *test*.py"
res = subprocess.run(cmd, capture_output=True, shell=True)
if res.returncode != 0:
if res.stderr:
encoding = chardet.detect(res.stderr).get("encoding")
content = res.stderr.decode(encoding).strip()
else:
encoding = chardet.detect(res.stdout).get("encoding")
content = res.stdout.decode(encoding).strip()
print(colored(content, 'white', 'on_magenta'))
else:
if res.stdout:
encoding = chardet.detect(res.stdout).get("encoding")
content = res.stdout.decode(encoding).strip()
print(colored(content, 'white', 'on_cyan'))
cmd = f"python -m coverage html -d {output}"
res = subprocess.run(cmd, capture_output=True, shell=True)
if res.returncode != 0:
if res.stderr:
encoding = chardet.detect(res.stderr).get("encoding")
content = res.stderr.decode(encoding).strip()
else:
encoding = chardet.detect(res.stdout).get("encoding")
content = res.stdout.decode(encoding).strip()
print(colored(content, 'white', 'on_magenta'))
else:
if res.stdout:
encoding = chardet.detect(res.stdout).get("encoding")
content = res.stdout.decode(encoding).strip()
print(colored(content, 'white', 'on_cyan'))
if __name__ == "__main__":
root(sys.argv[1:])