Skip to content

Commit 90eb511

Browse files
authored
refactor: move showcase library setup into shared function (#467)
1 parent fd19e1f commit 90eb511

File tree

1 file changed

+38
-144
lines changed

1 file changed

+38
-144
lines changed

packages/gapic-generator/noxfile.py

Lines changed: 38 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
import typing
1919
import nox # type: ignore
2020

21+
from contextlib import contextmanager
2122
from os import path
2223

2324

2425
showcase_version = "0.11.0"
26+
ADS_TEMPLATES = path.join(path.dirname(__file__), "gapic", "ads-templates")
2527

2628

2729
@nox.session(python=["3.6", "3.7", "3.8"])
@@ -45,11 +47,11 @@ def unit(session):
4547
)
4648

4749

48-
@nox.session(python="3.8")
49-
def showcase(
50-
session, templates="DEFAULT", other_opts: typing.Iterable[str] = (),
50+
@contextmanager
51+
def showcase_library(
52+
session, templates="DEFAULT", other_opts: typing.Iterable[str] = ()
5153
):
52-
"""Run the Showcase test suite."""
54+
"""Install the generated library into the session for showcase tests."""
5355

5456
# Try to make it clear if Showcase is not running, so that
5557
# people do not end up with tons of difficult-to-debug failures over
@@ -60,10 +62,7 @@ def showcase(
6062
session.log("See https://github.com/googleapis/gapic-showcase")
6163
session.log("-" * 70)
6264

63-
# Install pytest and gapic-generator-python
64-
session.install("mock")
65-
session.install("pytest")
66-
session.install("pytest-asyncio")
65+
# Install gapic-generator-python
6766
session.install("-e", ".")
6867

6968
# Install a client library for Showcase.
@@ -99,71 +98,36 @@ def showcase(
9998
# Install the library.
10099
session.install(tmp_dir)
101100

102-
session.run(
103-
"py.test", "--quiet", *(session.posargs or [path.join("tests", "system")])
104-
)
101+
yield tmp_dir
105102

106103

107104
@nox.session(python="3.8")
108-
def showcase_mtls(
105+
def showcase(
109106
session, templates="DEFAULT", other_opts: typing.Iterable[str] = (),
110107
):
111-
"""Run the Showcase mtls test suite."""
112-
113-
# Try to make it clear if Showcase is not running, so that
114-
# people do not end up with tons of difficult-to-debug failures over
115-
# an obvious problem.
116-
if not os.environ.get("CIRCLECI"):
117-
session.log("-" * 70)
118-
session.log("Note: Showcase must be running for these tests to work.")
119-
session.log("See https://github.com/googleapis/gapic-showcase")
120-
session.log("-" * 70)
121-
122-
# Install pytest and gapic-generator-python
123-
session.install("mock")
124-
session.install("pytest")
125-
session.install("pytest-asyncio")
126-
session.install("-e", ".")
108+
"""Run the Showcase test suite."""
127109

128-
# Install a client library for Showcase.
129-
with tempfile.TemporaryDirectory() as tmp_dir:
130-
# Download the Showcase descriptor.
110+
with showcase_library(session, templates=templates, other_opts=other_opts):
111+
session.install("mock", "pytest", "pytest-asyncio")
131112
session.run(
132-
"curl",
133-
"https://github.com/googleapis/gapic-showcase/releases/"
134-
f"download/v{showcase_version}/"
135-
f"gapic-showcase-{showcase_version}.desc",
136-
"-L",
137-
"--output",
138-
path.join(tmp_dir, "showcase.desc"),
139-
external=True,
140-
silent=True,
113+
"py.test", "--quiet", *(session.posargs or [path.join("tests", "system")])
141114
)
142115

143-
# Write out a client library for Showcase.
144-
template_opt = f"python-gapic-templates={templates}"
145-
opts = f"--python_gapic_opt={template_opt}"
146-
opts += ",".join(other_opts + ("lazy-import",))
147-
session.run(
148-
"protoc",
149-
"--experimental_allow_proto3_optional",
150-
f"--descriptor_set_in={tmp_dir}{path.sep}showcase.desc",
151-
f"--python_gapic_out={tmp_dir}",
152-
"google/showcase/v1beta1/echo.proto",
153-
"google/showcase/v1beta1/identity.proto",
154-
"google/showcase/v1beta1/messaging.proto",
155-
external=True,
156-
)
157116

158-
# Install the library.
159-
session.install(tmp_dir)
117+
@nox.session(python="3.8")
118+
def showcase_mtls(
119+
session, templates="DEFAULT", other_opts: typing.Iterable[str] = (),
120+
):
121+
"""Run the Showcase mtls test suite."""
160122

161-
session.run(
162-
"py.test",
163-
"--quiet",
164-
"--mtls",
165-
*(session.posargs or [path.join("tests", "system")]),
166-
)
123+
with showcase_library(session, templates=templates, other_opts=other_opts):
124+
session.install("mock", "pytest", "pytest-asyncio")
125+
session.run(
126+
"py.test",
127+
"--quiet",
128+
"--mtls",
129+
*(session.posargs or [path.join("tests", "system")]),
130+
)
167131

168132

169133
@nox.session(python="3.8")
@@ -185,51 +149,16 @@ def showcase_unit(
185149
"""Run the generated unit tests against the Showcase library."""
186150

187151
session.install(
188-
"coverage", "pytest", "pytest-cov", "pytest-xdist", 'asyncmock', 'pytest-asyncio'
152+
"coverage",
153+
"pytest",
154+
"pytest-cov",
155+
"pytest-xdist",
156+
"asyncmock",
157+
"pytest-asyncio",
189158
)
190-
session.install(".")
191-
192-
# Install a client library for Showcase.
193-
with tempfile.TemporaryDirectory() as tmp_dir:
194-
# Download the Showcase descriptor.
195-
session.run(
196-
"curl",
197-
"https://github.com/googleapis/gapic-showcase/releases/"
198-
f"download/v{showcase_version}/"
199-
f"gapic-showcase-{showcase_version}.desc",
200-
"-L",
201-
"--output",
202-
path.join(tmp_dir, "showcase.desc"),
203-
external=True,
204-
silent=True,
205-
)
206159

207-
# Write out a client library for Showcase.
208-
opts = [
209-
f"python-gapic-templates={templates}",
210-
]
211-
opts.extend(other_opts)
212-
if session.python == "3.8":
213-
opts.append("lazy-import")
214-
215-
opt_str = f'--python_gapic_opt={",".join(opts)},'
216-
217-
session.run(
218-
"protoc",
219-
"--experimental_allow_proto3_optional",
220-
f"--descriptor_set_in={tmp_dir}{path.sep}showcase.desc",
221-
f"--python_gapic_out={tmp_dir}",
222-
opt_str,
223-
"google/showcase/v1beta1/echo.proto",
224-
"google/showcase/v1beta1/identity.proto",
225-
"google/showcase/v1beta1/messaging.proto",
226-
"google/showcase/v1beta1/testing.proto",
227-
external=True,
228-
)
229-
230-
# Install the library.
231-
session.chdir(tmp_dir)
232-
session.install("-e", tmp_dir)
160+
with showcase_library(session, templates=templates, other_opts=other_opts) as lib:
161+
session.chdir(lib)
233162

234163
# Run the tests.
235164
session.run(
@@ -244,8 +173,7 @@ def showcase_unit(
244173

245174
@nox.session(python=["3.6", "3.7", "3.8"])
246175
def showcase_unit_alternative_templates(session):
247-
templates = path.join(path.dirname(__file__), "gapic", "ads-templates")
248-
showcase_unit(session, templates=templates, other_opts=("old-naming",))
176+
showcase_unit(session, templates=ADS_TEMPLATES, other_opts=("old-naming",))
249177

250178

251179
@nox.session(python="3.8")
@@ -256,51 +184,17 @@ def showcase_mypy(
256184

257185
# Install pytest and gapic-generator-python
258186
session.install("mypy")
259-
session.install(".")
260187

261-
# Install a client library for Showcase.
262-
with tempfile.TemporaryDirectory() as tmp_dir:
263-
# Download the Showcase descriptor.
264-
session.run(
265-
"curl",
266-
"https://github.com/googleapis/gapic-showcase/releases/"
267-
f"download/v{showcase_version}/"
268-
f"gapic-showcase-{showcase_version}.desc",
269-
"-L",
270-
"--output",
271-
path.join(tmp_dir, "showcase.desc"),
272-
external=True,
273-
silent=True,
274-
)
275-
# Write out a client library for Showcase.
276-
template_opt = f"python-gapic-templates={templates}"
277-
gapic_opts = f"--python_gapic_opt={template_opt},"
278-
gapic_opts += ",".join(other_opts)
279-
session.run(
280-
"protoc",
281-
"--experimental_allow_proto3_optional",
282-
f"--descriptor_set_in={tmp_dir}{path.sep}showcase.desc",
283-
f"--python_gapic_out={tmp_dir}",
284-
gapic_opts,
285-
"google/showcase/v1beta1/echo.proto",
286-
"google/showcase/v1beta1/identity.proto",
287-
"google/showcase/v1beta1/messaging.proto",
288-
"google/showcase/v1beta1/testing.proto",
289-
external=True,
290-
)
291-
292-
# Install the library.
293-
session.chdir(tmp_dir)
294-
session.install("-e", tmp_dir)
188+
with showcase_library(session, templates=templates, other_opts=other_opts) as lib:
189+
session.chdir(lib)
295190

296191
# Run the tests.
297192
session.run("mypy", "google")
298193

299194

300195
@nox.session(python="3.8")
301196
def showcase_mypy_alternative_templates(session):
302-
templates = path.join(path.dirname(__file__), "gapic", "ads-templates")
303-
showcase_mypy(session, templates=templates, other_opts=("old-naming",))
197+
showcase_mypy(session, templates=ADS_TEMPLATES, other_opts=("old-naming",))
304198

305199

306200
@nox.session(python="3.6")

0 commit comments

Comments
 (0)