This repository was archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgo_run_tests.py
More file actions
118 lines (94 loc) · 2.81 KB
/
go_run_tests.py
File metadata and controls
118 lines (94 loc) · 2.81 KB
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
import re
import sys
import pathlib
import subprocess
from configparser import ConfigParser
MODULE_DIR = pathlib.Path("./").absolute()
CONFIG_PATH = MODULE_DIR / "setup.cfg"
STD_OUT_LOG = pathlib.Path("./reports/test_stdout.txt")
STD_ERR_LOG = pathlib.Path("./reports/test_stderr.txt")
COVERAGE_LOG = pathlib.Path("./reports/coverage.out")
TEST_REPORT = pathlib.Path("./reports/test_report.html")
COVERAGE_REPORT = pathlib.Path("./reports/coverage.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():
config = load_cfg()
coverage_required = config.getfloat("testing", "coverage_required")
sys.stdout.write(f"COVERAGE REQUIRED: {coverage_required}\n")
race_detection = config.getboolean("testing", "race_detection", fallback=True)
command = [
"go",
"test",
"-v",
"-failfast",
]
# Add the race flag if we are using it.
if race_detection:
command.append("-race")
# Finish building the command.
command.extend(
[
"-covermode=atomic",
f"-coverprofile={COVERAGE_LOG}",
"-coverpkg=./...",
"./...",
]
)
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)
if proc.returncode != 0:
sys.exit(proc.returncode)
# Use the cov command to generate the total coverage
command = [
"go",
"tool",
"cover",
"--func",
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()