forked from googleapis/gapic-generator-python
/
noxfile.py
265 lines (216 loc) · 7.61 KB
/
noxfile.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
# Copyright 2017, Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://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.
from __future__ import absolute_import
import os
import tempfile
import typing
import nox # type: ignore
from contextlib import contextmanager
from os import path
showcase_version = "0.11.0"
ADS_TEMPLATES = path.join(path.dirname(__file__), "gapic", "ads-templates")
@nox.session(python=["3.6", "3.7", "3.8", "3.9"])
def unit(session):
"""Run the unit test suite."""
session.install(
"coverage", "pytest", "pytest-cov", "pytest-xdist", "pyfakefs",
)
session.install("-e", ".")
session.run(
"py.test",
*(
session.posargs
or [
"-vv",
"-n=auto",
"--cov=gapic",
"--cov-config=.coveragerc",
"--cov-report=term",
"--cov-report=html",
path.join("tests", "unit"),
]
),
)
# TODO(yon-mg): -add compute context manager that includes rest transport
# -add compute unit tests
# (to test against temporarily while rest transport is incomplete)
# (to be removed once all features are complete)
@contextmanager
def showcase_library(
session, templates="DEFAULT", other_opts: typing.Iterable[str] = ()
):
"""Install the generated library into the session for showcase tests."""
# Try to make it clear if Showcase is not running, so that
# people do not end up with tons of difficult-to-debug failures over
# an obvious problem.
if not os.environ.get("CIRCLECI"):
session.log("-" * 70)
session.log("Note: Showcase must be running for these tests to work.")
session.log("See https://github.com/googleapis/gapic-showcase")
session.log("-" * 70)
# Install gapic-generator-python
session.install("-e", ".")
# Install a client library for Showcase.
with tempfile.TemporaryDirectory() as tmp_dir:
# Download the Showcase descriptor.
session.run(
"curl",
"https://github.com/googleapis/gapic-showcase/releases/"
f"download/v{showcase_version}/"
f"gapic-showcase-{showcase_version}.desc",
"-L",
"--output",
path.join(tmp_dir, "showcase.desc"),
external=True,
silent=True,
)
# Write out a client library for Showcase.
template_opt = f"python-gapic-templates={templates}"
# TODO(yon-mg): add "transports=grpc+rest" when all rest features required for
# Showcase are implemented i.e. (grpc transcoding, LROs, etc)
opts = "--python_gapic_opt="
opts += ",".join(other_opts + (f"{template_opt}",))
cmd_tup = (
f"protoc",
f"--experimental_allow_proto3_optional",
f"--descriptor_set_in={tmp_dir}{path.sep}showcase.desc",
opts,
f"--python_gapic_out={tmp_dir}",
f"google/showcase/v1beta1/echo.proto",
f"google/showcase/v1beta1/identity.proto",
f"google/showcase/v1beta1/messaging.proto",
)
session.run(
*cmd_tup,
external=True,
)
# Install the library.
session.install(tmp_dir)
yield tmp_dir
@nox.session(python="3.8")
def showcase(
session,
templates="DEFAULT",
other_opts: typing.Iterable[str] = (),
env: typing.Optional[typing.Dict[str, str]] = None,
):
"""Run the Showcase test suite."""
with showcase_library(session, templates=templates, other_opts=other_opts):
session.install("mock", "pytest", "pytest-asyncio")
session.run(
"py.test", "--quiet", *(session.posargs or [path.join("tests", "system")]),
env=env,
)
@nox.session(python="3.8")
def showcase_mtls(
session,
templates="DEFAULT",
other_opts: typing.Iterable[str] = (),
env: typing.Optional[typing.Dict[str, str]] = None,
):
"""Run the Showcase mtls test suite."""
with showcase_library(session, templates=templates, other_opts=other_opts):
session.install("mock", "pytest", "pytest-asyncio")
session.run(
"py.test",
"--quiet",
"--mtls",
*(session.posargs or [path.join("tests", "system")]),
env=env,
)
@nox.session(python="3.8")
def showcase_alternative_templates(session):
templates = path.join(path.dirname(__file__), "gapic", "ads-templates")
showcase(
session,
templates=templates,
other_opts=("old-naming",),
env={"GAPIC_PYTHON_ASYNC": "False"},
)
@nox.session(python="3.8")
def showcase_mtls_alternative_templates(session):
templates = path.join(path.dirname(__file__), "gapic", "ads-templates")
showcase_mtls(
session,
templates=templates,
other_opts=("old-naming",),
env={"GAPIC_PYTHON_ASYNC": "False"},
)
@nox.session(python=["3.6", "3.7", "3.8", "3.9"])
def showcase_unit(
session, templates="DEFAULT", other_opts: typing.Iterable[str] = (),
):
"""Run the generated unit tests against the Showcase library."""
session.install(
"coverage",
"pytest",
"pytest-cov",
"pytest-xdist",
"asyncmock",
"pytest-asyncio",
)
with showcase_library(session, templates=templates, other_opts=other_opts) as lib:
session.chdir(lib)
# Run the tests.
session.run(
"py.test",
"-n=auto",
"--quiet",
"--cov=google",
"--cov-report=term",
*(session.posargs or [path.join("tests", "unit")]),
)
@nox.session(python=["3.7", "3.8", "3.9"])
def showcase_unit_alternative_templates(session):
showcase_unit(session, templates=ADS_TEMPLATES, other_opts=("old-naming",))
@nox.session(python=["3.8"])
def showcase_unit_add_iam_methods(session):
showcase_unit(session, other_opts=("add-iam-methods",))
@nox.session(python="3.8")
def showcase_mypy(
session, templates="DEFAULT", other_opts: typing.Iterable[str] = (),
):
"""Perform typecheck analysis on the generated Showcase library."""
# Install pytest and gapic-generator-python
session.install("mypy")
with showcase_library(session, templates=templates, other_opts=other_opts) as lib:
session.chdir(lib)
# Run the tests.
session.run("mypy", "--explicit-package-bases", "google")
@nox.session(python="3.8")
def showcase_mypy_alternative_templates(session):
showcase_mypy(session, templates=ADS_TEMPLATES, other_opts=("old-naming",))
@nox.session(python="3.8")
def docs(session):
"""Build the docs."""
session.install("sphinx < 1.8", "sphinx_rtd_theme")
session.install(".")
# Build the docs!
session.run("rm", "-rf", "docs/_build/")
session.run(
"sphinx-build",
"-W",
"-b",
"html",
"-d",
"docs/_build/doctrees",
"docs/",
"docs/_build/html/",
)
@nox.session(python=["3.7", "3.8", "3.9"])
def mypy(session):
"""Perform typecheck analysis."""
session.install("mypy")
session.install(".")
session.run("mypy", "gapic")