Repository navigation
Expand file tree
/
Copy pathrun_checks.py
More file actions
174 lines (149 loc) · 4.71 KB
/
Copy pathrun_checks.py
File metadata and controls
174 lines (149 loc) · 4.71 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Copyright (c) 2024 Elektrobit Automotive GmbH
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0
"""
Run checks for ankaios_sdk Python package.
This script runs unit tests, coverage, pylint and pycodestyle
checks and saves the results in the reports directory.
Example usage:
# This will run the unit tests with the --full-trace option
python3 run_checks.py -u --full-trace
"""
import os
import re
import pytest
import argparse
import subprocess
PROJECT_NAME = "ankaios_sdk"
REPORT_DIR = "reports"
COVERAGE_DIR = os.path.join(REPORT_DIR, "coverage")
UTEST_DIR = os.path.join(REPORT_DIR, "utest")
PYLINT_DIR = os.path.join(REPORT_DIR, "pylint")
CODESTYLE_DIR = os.path.join(REPORT_DIR, "codestyle")
def run_pytest_utest(args):
os.makedirs(UTEST_DIR, exist_ok=True)
result = pytest.main(
[
"--junitxml={}".format(
os.path.join(UTEST_DIR, "utest_report.xml")
),
"tests",
# '-p', 'no:warnings',
"-vv",
]
+ args
)
exit(result)
def run_pytest_cov(args):
os.makedirs(COVERAGE_DIR, exist_ok=True)
result = pytest.main(
[
"--cov={}".format(PROJECT_NAME),
"--cov-report=html:{}".format(os.path.join(COVERAGE_DIR, "html")),
"--cov-report=xml:{}".format(
os.path.join(COVERAGE_DIR, "cov_report.xml")
),
"--cov-report=term",
"--cov-fail-under=100",
"tests",
"-p",
"no:warnings",
"-vv",
]
+ args
)
exit(result)
def run_pylint(args):
os.makedirs(PYLINT_DIR, exist_ok=True)
result = subprocess.run(
[
"pylint",
PROJECT_NAME,
"tests",
"--rcfile=.pylintrc",
"--output-format=parseable",
]
+ args,
capture_output=True,
text=True,
)
pylint_output = result.stdout
rating_line = None
output_lines = pylint_output.split("\n")
for line in output_lines:
if "Your code has been rated at" in line:
rating_line = line
break
rating = 0.0
if rating_line:
print(rating_line)
rating_re = re.search(r"rated at (\d+\.\d+)/10", rating_line)
if rating_re:
rating = float(rating_re.group(1))
with open(os.path.join(PYLINT_DIR, "pylint_report.txt"), "w") as f:
f.write("\n".join(output_lines))
if rating < 10.0:
exit(1)
def run_pycodestyle(args):
os.makedirs(CODESTYLE_DIR, exist_ok=True)
result = subprocess.run(
[
"pycodestyle",
PROJECT_NAME,
"tests",
"--exclude=*_pb2.py,*_pb2_grpc.py,", # Exclude generated files
]
+ args,
capture_output=True,
text=True,
)
output_lines = result.stdout.split("\n")
if output_lines[-1] == "":
output_lines.pop()
print(f"PEP8 report: {len(output_lines)} violations found.")
with open(os.path.join(CODESTYLE_DIR, "codestyle_report.txt"), "w") as f:
f.write("\n".join(output_lines))
if len(output_lines) > 0:
exit(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"-c", "--cov", action="store_true", help="Run coverage"
)
parser.add_argument(
"-u", "--utest", action="store_true", help="Run unit tests"
)
parser.add_argument("-l", "--lint", action="store_true", help="Run pylint")
parser.add_argument(
"-p", "--pep8", action="store_true", help="Run pep8 codestyle check"
)
args, extra_args = parser.parse_known_args()
if not any([args.cov, args.utest, args.lint, args.pep8]):
parser.print_help()
exit(0)
if sum([args.cov, args.utest, args.lint, args.pep8]) > 1:
print("Please select only one test type.")
parser.print_help()
exit(0)
os.makedirs(REPORT_DIR, exist_ok=True)
if args.cov:
run_pytest_cov(extra_args)
elif args.utest:
run_pytest_utest(extra_args)
elif args.lint:
run_pylint(extra_args)
elif args.pep8:
run_pycodestyle(extra_args)