forked from Spyro-Soft/scargo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·373 lines (309 loc) · 8.71 KB
/
run.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env python3
# #
# @copyright Copyright (C) 2022 SpyroSoft Solutions. All rights reserved.
# #
import argparse
import os
import subprocess
import sys
from typing import List, Optional
from common_dev.scripts.documentation import create_doc
from scargo.cli import cli as scargo_cli
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
REPO_DIR = SCRIPT_DIR # '/repo'
SCRIPTS_DIR = SCRIPT_DIR + "/common_dev/scripts"
SCRIPT_SH = ".sh"
OUT = SCRIPT_DIR + "/build"
SCARGO_DIR = REPO_DIR + "/scargo"
UT_DIR = REPO_DIR + "/tests/ut"
IT_DIR = REPO_DIR + "/tests/it"
CHECKERS_EXCLUSIONS = ["-e", "common_dev"]
def get_cmdline_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(epilog="Run options include tests")
parser.add_argument(
"-a",
"--run_all",
action="store_true",
default=False,
dest="run_all",
help="Run all available features",
)
parser.add_argument(
"-u",
"--unit_test",
action="store_true",
default=False,
dest="unit_test",
help="Run all unit test only",
)
parser.add_argument(
"-c",
"--ci_tests",
nargs=1,
action="extend",
default=[],
dest="target",
help="Run integration on CI with usage of xdist and for specified target",
)
parser.add_argument(
"-t",
"--tests",
action="store_true",
default=False,
help="Run integration tests only",
)
parser.add_argument(
"-f",
"--format",
action="store_true",
default=False,
dest="format",
help="Format with black and isort",
)
parser.add_argument(
"-l",
"--lint",
action="store_true",
default=False,
help="Run linters",
)
parser.add_argument(
"--types",
action="store_true",
default=False,
help="Run type checking",
)
parser.add_argument(
"-p",
"--program",
nargs="*",
dest="program",
help="Run program",
)
parser.add_argument(
"--doc",
action="store_true",
default=False,
dest="doc",
help="Create documentation for project using Sphinx",
)
args = parser.parse_args()
return args
def perform_tests(
test_path: str,
test_postfix: str,
test_flags: Optional[List[str]] = None,
) -> str:
out_test_doc_dir = OUT + "/test_doc"
os.makedirs(out_test_doc_dir, exist_ok=True)
junit_xml_path = f"{out_test_doc_dir}/junit_{test_postfix}.xml"
cobertura_path = f"{out_test_doc_dir}/coverage_{test_postfix}.xml"
try:
# needed because of relative imports
os.environ["PYTHONPATH"] = SCARGO_DIR
command = [
"pytest",
test_path,
f"--junitxml={junit_xml_path}",
"--cov-branch",
"--cov-report",
"html:" + out_test_doc_dir + "/coverage" + "_" + test_postfix,
"--cov-report",
f"xml:{cobertura_path}",
"--cov=scargo",
"-v",
test_path,
]
if test_flags:
command.extend(test_flags)
subprocess.check_call(command)
except subprocess.CalledProcessError as e:
return test_postfix + " tests fail: " + str(e) + "\n"
command = [
"common_dev/scripts/test_report.py",
junit_xml_path,
cobertura_path,
f"{out_test_doc_dir}/report_{test_postfix}.pdf",
f"--type={'unit' if test_postfix == 'ut' else 'integration'}",
]
subprocess.check_call(command)
return ""
def run_all_code_checkers() -> bool:
checker_exception_message = ""
checker_exception_message += perform_tests(UT_DIR, "ut")
checker_exception_message += perform_tests(IT_DIR, "it")
command = [
SCRIPT_DIR + "/common_dev/scripts/copyrights.py",
"-C",
"scargo/",
"-e",
"scargo/cfg/AppData/",
]
try:
command.extend(CHECKERS_EXCLUSIONS)
subprocess.check_call(command)
except subprocess.CalledProcessError:
checker_exception_message += (
"Copyrights check failed run: " + " ".join(command) + "\n"
)
try:
command = [
"common_dev/scripts/todo_check.py",
"-C",
"scargo",
"-C",
"tests",
]
command.extend(CHECKERS_EXCLUSIONS)
subprocess.check_call(command)
except subprocess.CalledProcessError:
checker_exception_message += (
"TODO check failed run: " + " ".join(command) + "\n"
)
try:
run_isort(check=True)
except subprocess.CalledProcessError:
checker_exception_message += (
"Sort order check fail run: " + " ".join(command) + "\n"
)
try:
run_black(check=True)
except subprocess.CalledProcessError:
checker_exception_message += (
"Format check fail run: " + " ".join(command) + "\n"
)
try:
command = [SCRIPT_DIR + "/common_dev/scripts/cyclomatic.py"]
subprocess.check_call(command)
except subprocess.CalledProcessError:
checker_exception_message += (
"Cyclomatic check fail run: " + " ".join(command) + "\n"
)
try:
command = ["make", "-C./docs", "html"]
subprocess.check_call(command)
except subprocess.CalledProcessError:
checker_exception_message += (
"Docu generation fail run: " + " ".join(command) + "\n"
)
try:
run_pylint()
except subprocess.CalledProcessError:
checker_exception_message += (
"Pylint check failed run: " + " ".join(command) + "\n"
)
try:
run_mypy()
except subprocess.CalledProcessError:
checker_exception_message += (
"Mypy check failed run: " + " ".join(command) + "\n"
)
# try:
# run_flake8()
# except subprocess.CalledProcessError:
# checker_exception_message += (
# "Flake8 check failed run: " + " ".join(command) + "\n"
# )
if checker_exception_message:
print("Check exception message:\n" + checker_exception_message)
return False
return True
def run_pylint() -> None:
# can add "tests", optionally
command = [
"pylint",
"scargo",
"run.py",
"clean.py",
]
subprocess.check_call(command)
def run_flake8() -> None:
command = [
"flake8",
"scargo",
"tests",
"common_dev",
"run.py",
"clean.py",
]
subprocess.check_call(command)
def run_isort(check: bool = False) -> None:
isort_command = [
"isort",
"--profile=black",
"scargo",
"tests",
"common_dev",
"run.py",
"clean.py",
]
if check:
isort_command.extend(["--check", "--diff"])
subprocess.check_call(isort_command)
def run_black(check: bool = False) -> None:
black_command = [
"black",
"scargo",
"tests",
"common_dev",
"run.py",
"clean.py",
]
if check:
black_command.extend(["--check", "--diff"])
subprocess.check_call(black_command)
def run_mypy() -> None:
mypy_command = [
"mypy",
"--explicit-package-bases",
"scargo",
"tests",
"common_dev",
"run.py",
"clean.py",
]
subprocess.check_call(mypy_command)
def main() -> None: # pylint: disable=R0912
args = get_cmdline_arguments()
if not len(sys.argv) > 1:
args.run_all = True
if args.run_all:
run_all_code_checkers()
if args.target:
# -k could be replaced with -m as markers will be introduced in integration tests in the future
# GitHub runner for linux is 2 processor machine, so -n is equal 2
result = perform_tests(
IT_DIR, "it", ["-n 2", f"-k {args.target[0]}", "--dist=loadgroup"]
)
if result:
sys.exit(1)
if args.tests:
result = perform_tests(IT_DIR, "it")
if result:
sys.exit(1)
if args.unit_test:
result = perform_tests(UT_DIR, "ut")
if result:
sys.exit(1)
if args.format:
run_isort()
run_black()
if args.lint:
run_pylint()
run_flake8()
if args.types:
run_mypy()
if args.program:
ar = [i.split() for i in args.program]
scargo_cli(ar[0])
if args.doc:
create_doc()
# avoiding making `e` a global variable
def try_main() -> None:
try:
main()
except subprocess.CalledProcessError as e:
print(f"Build failed: {e.returncode}")
sys.exit(1)
if __name__ == "__main__":
try_main()