-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtasks.py
More file actions
305 lines (259 loc) · 8.19 KB
/
tasks.py
File metadata and controls
305 lines (259 loc) · 8.19 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
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
# pylint: disable=missing-function-docstring, unused-argument
import os
import pathlib
import subprocess
from importlib.metadata import version
from invoke.context import Context
from invoke.tasks import task
ROOT = pathlib.Path(__file__).parent.resolve().as_posix()
VERSION = version("robotframework-openapitools")
utests_exit_sum = None
atests_exit_code = None
@task
def start_api(context: Context) -> None:
cmd = [
"python",
"-m",
"uvicorn",
"testserver:app",
f"--app-dir {ROOT}/tests/server",
"--host 0.0.0.0",
"--port 8000",
"--reload",
f"--reload-dir {ROOT}/tests/server",
]
subprocess.run(" ".join(cmd), shell=True, check=False)
@task
def libgen(context: Context) -> None:
cmd = [
"coverage",
"run",
"-m",
"openapi_libgen.generator",
"http://127.0.0.1:8000/openapi.json",
f"{ROOT}/tests/generated",
"MyGeneratedLibrary",
"my_generated_library",
"no",
"0",
]
subprocess.run(" ".join(cmd), shell=True, check=False)
@task
def libgen_with_envs(context: Context) -> None:
env = os.environ.copy()
env["USE_SUMMARY_AS_KEYWORD_NAME"] = "true"
env["EXPAND_BODY_ARGUMENTS"] = "true"
cmd = [
"coverage",
"run",
"-m",
"openapi_libgen.generator",
"http://127.0.0.1:8000/openapi.json",
f"{ROOT}/tests/generated",
"MyOtherGeneratedLibrary",
"my_other_generated_library",
]
subprocess.run(" ".join(cmd), shell=True, check=False, env=env)
@task
def libgen_edge_cases(context: Context) -> None:
cmd = [
"coverage",
"run",
"-m",
"openapi_libgen.generator",
f"{ROOT}/tests/files/schema_with_parameter_name_duplication.yaml",
f"{ROOT}/tests/generated",
"MyGeneratedEdgeCaseLibrary",
"my_generated_edge_case_library",
"No",
"OFF",
]
subprocess.run(" ".join(cmd), shell=True, check=False)
@task
def utests(context: Context) -> None:
global utests_exit_sum
cmd = [
"coverage",
"run",
"-m",
"unittest",
"discover ",
f"{ROOT}/tests/driver/unittests",
]
driver_result = subprocess.run(" ".join(cmd), shell=True, check=False)
cmd = [
"coverage",
"run",
"-m",
"unittest",
"discover ",
f"{ROOT}/tests/libcore/unittests",
]
libcore_result = subprocess.run(" ".join(cmd), shell=True, check=False)
cmd = [
"coverage",
"run",
"-m",
"unittest",
"discover ",
f"{ROOT}/tests/libgen/unittests",
]
libgen_result = subprocess.run(" ".join(cmd), shell=True, check=False)
utests_exit_sum = (
driver_result.returncode + libcore_result.returncode + libgen_result.returncode
)
@task(libgen, libgen_with_envs, libgen_edge_cases)
def atests(context: Context) -> None:
global atests_exit_code
cmd = [
"coverage",
"run",
"-m",
"robot",
f"--pythonpath={ROOT}/tests/user_implemented",
f"--pythonpath={ROOT}/tests/generated",
f"--argumentfile={ROOT}/tests/rf_cli.args",
f"--variable=ROOT:{ROOT}",
f"--outputdir={ROOT}/tests/logs",
"--loglevel=TRACE:DEBUG",
f"{ROOT}/tests",
]
result = subprocess.run(" ".join(cmd), shell=True, check=False)
atests_exit_code = result.returncode
@task(libgen, libgen_with_envs, libgen_edge_cases)
def atests_rf6(context: Context) -> None:
cmd = [
"python",
"-m",
"robot",
f"--pythonpath={ROOT}/tests/generated",
f"--argumentfile={ROOT}/tests/rf_cli.args",
f"--variable=ROOT:{ROOT}",
"--exclude rf7",
f"--outputdir={ROOT}/tests/logs",
"--loglevel=TRACE:DEBUG",
f"{ROOT}/tests",
]
result = subprocess.run(" ".join(cmd), shell=True, check=False)
if result.returncode > 0:
raise Exception(
f"There were {result.returncode} failures while running the tests."
)
@task(utests, atests)
def tests(context: Context) -> None:
subprocess.run("coverage combine", shell=True, check=False)
subprocess.run("coverage report", shell=True, check=False)
subprocess.run("coverage html", shell=True, check=False)
subprocess.run("coverage xml", shell=True, check=False)
if utests_exit_sum is None or atests_exit_code is None:
raise Exception(
"Coverage cannot be calculated; not all test suites ran correctly."
)
if utests_exit_sum + atests_exit_code > 0:
raise Exception(
f"There were {utests_exit_sum + atests_exit_code} failures while running the tests."
)
@task
def type_check(context: Context) -> None:
subprocess.run(f"mypy {ROOT}/src", shell=True, check=False)
subprocess.run(f"pyright {ROOT}/src", shell=True, check=False)
subprocess.run(
f"robotcode analyze code {ROOT}/tests",
shell=True,
check=False,
)
@task
def lint(context: Context) -> None:
subprocess.run(f"ruff check {ROOT}/src/OpenApiDriver", shell=True, check=False)
subprocess.run(f"ruff check {ROOT}/src/OpenApiLibCore", shell=True, check=False)
subprocess.run(f"pylint {ROOT}/src/OpenApiDriver", shell=True, check=False)
subprocess.run(f"pylint {ROOT}/src/OpenApiLibCore", shell=True, check=False)
subprocess.run(f"robocop check {ROOT}/tests", shell=True, check=False)
@task
def format_code(context: Context) -> None:
subprocess.run("ruff check --select I --fix", shell=True, check=False)
subprocess.run("ruff format", shell=True, check=False)
subprocess.run(f"robocop format {ROOT}/tests", shell=True, check=False)
@task
def libdoc(context: Context) -> None:
json_file = f"{ROOT}/tests/files/petstore_openapi.json"
source = f"OpenApiLibCore::{json_file}"
target = f"{ROOT}/docs/openapi_libcore.html"
cmd = [
"python",
"-m",
"robot.libdoc",
f"-v {VERSION}",
source,
target,
]
subprocess.run(" ".join(cmd), shell=True, check=False)
env = os.environ.copy()
env["HIDE_INHERITED_KEYWORDS"] = "true"
json_file = f"{ROOT}/tests/files/petstore_openapi.json"
source = f"OpenApiDriver::{json_file}"
target = f"{ROOT}/docs/openapidriver.html"
cmd = [
"python",
"-m",
"robot.libdoc",
"-n OpenApiDriver",
f"-v {VERSION}",
source,
target,
]
subprocess.run(" ".join(cmd), shell=True, check=False, env=env)
@task
def libspec(context: Context) -> None:
json_file = f"{ROOT}/tests/files/petstore_openapi.json"
source = f"OpenApiLibCore::{json_file}"
target = f"{ROOT}/src/OpenApiLibCore/openapi_libcore.libspec"
cmd = [
"python",
"-m",
"robot.libdoc",
f"-v {VERSION}",
source,
target,
]
subprocess.run(" ".join(cmd), shell=True, check=False)
env = os.environ.copy()
env["HIDE_INHERITED_KEYWORDS"] = "true"
json_file = f"{ROOT}/tests/files/petstore_openapi.json"
source = f"OpenApiDriver::{json_file}"
target = f"{ROOT}/src/OpenApiDriver/openapidriver.libspec"
cmd = [
"python",
"-m",
"robot.libdoc",
"-n OpenApiDriver",
f"-v {VERSION}",
source,
target,
]
subprocess.run(" ".join(cmd), shell=True, check=False, env=env)
@task(libdoc)
def generate_docs(context: Context) -> None:
cmd = [
"python",
"-m",
"openapitools_docs.documentation_generator",
f"{ROOT}/docs",
]
subprocess.run(" ".join(cmd), shell=True, check=False)
@task
def update_coverage_badge(context: Context) -> None:
cmd = [
"genbadge",
"coverage",
f"-i {ROOT}/coverage.xml",
f"-o {ROOT}/docs/coverage-badge.svg",
]
subprocess.run(" ".join(cmd), shell=True, check=False)
@task(format_code, libspec, generate_docs, update_coverage_badge)
def build(context: Context) -> None:
subprocess.run("poetry build", shell=True, check=False)
@task
def release(context: Context) -> None:
subprocess.run(f"git tag -f v{VERSION}", shell=True, check=False)
subprocess.run("poetry publish", shell=True, check=False)