This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
go_make_test.py
152 lines (121 loc) · 4.34 KB
/
go_make_test.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
import re
import sys
import pathlib
import subprocess
from configparser import ConfigParser
CONFIG_PATH: pathlib.Path = pathlib.Path(__file__).parent.parent.parent / "setup.cfg"
STD_OUT_LOG = pathlib.Path("./zdevelop/tests/_reports/test_stdout.txt")
STD_ERR_LOG = pathlib.Path("./zdevelop/tests/_reports/test_stderr.txt")
FULL_LOG = pathlib.Path("./zdevelop/tests/_reports/test_full.txt")
COVERAGE_LOG = pathlib.Path("./zdevelop/tests/_reports/coverage.out")
TEST_REPORT = pathlib.Path("./zdevelop/tests/_reports/test_results.html")
COVERAGE_REPORT = pathlib.Path("./zdevelop/tests/_reports/coverage/index.html")
COVERAGE_REGEX = re.compile(r"total:\s+\(statements\)\s+(\d+\.\d)%")
def load_cfg() -> ConfigParser:
"""
loads library config file
:return: loaded `ConfigParser` object
"""
config = ConfigParser()
config.read(CONFIG_PATH)
return config
def run_test() -> None:
config = load_cfg()
coverage_required = config.getfloat("testing", "coverage_required") * 100
test_package = config.get("testing", "test_package", fallback="./...")
exclude_string = config.get("testing", "exclude", fallback="")
exclude_list = [e for e in exclude_string.split("\n") if e]
race_detection = config.getboolean("testing", "race_detection", fallback=True)
multi_process = config.getboolean("testing", "multi_process", fallback=True)
timeout = config.getint("testing", "timeout", fallback=60)
# Get the list of packages we want to cover
list_process = subprocess.Popen(
["go", "list", test_package], stdout=subprocess.PIPE, universal_newlines=True
)
grep_command = ["grep", "-v"]
for this_exclude in exclude_list:
grep_command += ["-e", this_exclude]
sys.stdout.write(f"grep: {' '.join(grep_command)}\n")
grep_process = subprocess.Popen(
grep_command,
stdin=list_process.stdout,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
assert list_process.stdout is not None
list_process.stdout.close()
packages_str, _ = grep_process.communicate()
packages = packages_str.split("\n")
if not packages:
packages = ["./..."]
sys.stdout.write(f"COVERAGE REQUIRED: {coverage_required}\n")
# Set up the command
command = [
"go",
"test",
"-v",
"-failfast",
f"-timeout={timeout}s",
]
# Add the race flag if we are using it.
if race_detection:
command.append("-race")
# Add th flag to restrict the number of simultaneous tests to 1.
if not multi_process:
command.extend(("-p", "1"))
# Finish building the command.
command.extend(
[
"-covermode=atomic",
f"-coverprofile={COVERAGE_LOG}",
f"-coverpkg={','.join(packages)}",
]
)
command = command + ["./..."]
sys.stdout.write(f"command: {' '.join(command)}\n")
proc = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True
)
stdout, stderr = proc.communicate()
sys.stdout.write(stdout)
sys.stderr.write(stderr)
STD_OUT_LOG.write_text(stdout)
STD_ERR_LOG.write_text(stderr)
FULL_LOG.write_text(stdout + stderr)
if proc.returncode != 0:
sys.exit(proc.returncode)
# Use the cov command to generate the total coverage
command = [
"go",
"tool",
"cover",
"--func",
str(COVERAGE_LOG),
]
proc = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True
)
stdout, stderr = proc.communicate()
sys.stdout.write(stdout)
sys.stderr.write(stderr)
with STD_OUT_LOG.open("a") as f:
f.write(stdout)
with STD_ERR_LOG.open("a") as f:
f.write(stderr)
if proc.returncode != 0:
sys.exit(proc.returncode)
# Get the last coverage tally in the result
coverage_str = [x for x in COVERAGE_REGEX.finditer(stdout)][-1].group(1)
coverage = float(coverage_str)
if coverage < coverage_required:
sys.stderr.write(
f"Coverage {coverage} is less than required {coverage_required}\n"
)
sys.exit(1)
else:
sys.stderr.write(
f"Coverage {coverage}% passes requirement of {coverage_required}%\n"
)
if __name__ == "__main__":
run_test()