Skip to content

Commit 647d31f

Browse files
chore(python): refactor unit / system test dependency install (#1131)
* chore(python): refactor unit / system test dependency install Source-Link: googleapis/synthtool@993985f Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:1894490910e891a385484514b22eb5133578897eb5b3c380e6d8ad475c6647cd * ci: add unit test extras Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 184f7f3 commit 647d31f

File tree

3 files changed

+90
-29
lines changed

3 files changed

+90
-29
lines changed

.github/.OwlBot.lock.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
# limitations under the License.
1414
docker:
1515
image: gcr.io/cloud-devrel-public-resources/owlbot-python:latest
16-
digest: sha256:b3500c053313dc34e07b1632ba9e4e589f4f77036a7cf39e1fe8906811ae0fce
17-
# created: 2022-04-01T01:42:03.609279246Z
16+
digest: sha256:1894490910e891a385484514b22eb5133578897eb5b3c380e6d8ad475c6647cd
17+
# created: 2022-04-01T15:48:07.524222836Z

noxfile.py

Lines changed: 87 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,42 @@
2020
import os
2121
import pathlib
2222
import shutil
23+
import warnings
2324

2425
import nox
2526

26-
2727
BLACK_VERSION = "black==22.3.0"
2828
BLACK_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"]
2929

3030
DEFAULT_PYTHON_VERSION = "3.8"
31-
SYSTEM_TEST_PYTHON_VERSIONS = ["3.8"]
31+
3232
UNIT_TEST_PYTHON_VERSIONS = ["3.6", "3.7", "3.8", "3.9"]
33+
UNIT_TEST_STANDARD_DEPENDENCIES = [
34+
"mock",
35+
"asyncmock",
36+
"pytest",
37+
"pytest-cov",
38+
"pytest-asyncio",
39+
]
40+
UNIT_TEST_EXTERNAL_DEPENDENCIES = []
41+
UNIT_TEST_LOCAL_DEPENDENCIES = []
42+
UNIT_TEST_DEPENDENCIES = []
43+
UNIT_TEST_EXTRAS = [
44+
"testing",
45+
]
46+
UNIT_TEST_EXTRAS_BY_PYTHON = {}
47+
48+
SYSTEM_TEST_PYTHON_VERSIONS = ["3.8"]
49+
SYSTEM_TEST_STANDARD_DEPENDENCIES = [
50+
"mock",
51+
"pytest",
52+
"google-cloud-testutils",
53+
]
54+
SYSTEM_TEST_EXTERNAL_DEPENDENCIES = []
55+
SYSTEM_TEST_LOCAL_DEPENDENCIES = []
56+
SYSTEM_TEST_DEPENDENCIES = []
57+
SYSTEM_TEST_EXTRAS = []
58+
SYSTEM_TEST_EXTRAS_BY_PYTHON = {}
3359

3460
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
3561

@@ -81,23 +107,41 @@ def lint_setup_py(session):
81107
session.run("python", "setup.py", "check", "--restructuredtext", "--strict")
82108

83109

110+
def install_unittest_dependencies(session, *constraints):
111+
standard_deps = UNIT_TEST_STANDARD_DEPENDENCIES + UNIT_TEST_DEPENDENCIES
112+
session.install(*standard_deps, *constraints)
113+
114+
if UNIT_TEST_EXTERNAL_DEPENDENCIES:
115+
warnings.warn(
116+
"'unit_test_external_dependencies' is deprecated. Instead, please "
117+
"use 'unit_test_dependencies' or 'unit_test_local_dependencies'.",
118+
DeprecationWarning,
119+
)
120+
session.install(*UNIT_TEST_EXTERNAL_DEPENDENCIES, *constraints)
121+
122+
if UNIT_TEST_LOCAL_DEPENDENCIES:
123+
session.install(*UNIT_TEST_LOCAL_DEPENDENCIES, *constraints)
124+
125+
if UNIT_TEST_EXTRAS_BY_PYTHON:
126+
extras = UNIT_TEST_EXTRAS_BY_PYTHON.get(session.python, [])
127+
elif UNIT_TEST_EXTRAS:
128+
extras = UNIT_TEST_EXTRAS
129+
else:
130+
extras = []
131+
132+
if extras:
133+
session.install("-e", f".[{','.join(extras)}]", *constraints)
134+
else:
135+
session.install("-e", ".", *constraints)
136+
137+
84138
def default(session):
85139
# Install all test dependencies, then install this package in-place.
86140

87141
constraints_path = str(
88142
CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt"
89143
)
90-
session.install(
91-
"mock",
92-
"asyncmock",
93-
"pytest",
94-
"pytest-cov",
95-
"pytest-asyncio",
96-
"-c",
97-
constraints_path,
98-
)
99-
100-
session.install("-e", ".[testing]", "-c", constraints_path)
144+
install_unittest_dependencies(session, "-c", constraints_path)
101145

102146
# Run py.test against the unit tests.
103147
session.run(
@@ -121,6 +165,35 @@ def unit(session):
121165
default(session)
122166

123167

168+
def install_systemtest_dependencies(session, *constraints):
169+
170+
# Use pre-release gRPC for system tests.
171+
session.install("--pre", "grpcio")
172+
173+
session.install(*SYSTEM_TEST_STANDARD_DEPENDENCIES, *constraints)
174+
175+
if SYSTEM_TEST_EXTERNAL_DEPENDENCIES:
176+
session.install(*SYSTEM_TEST_EXTERNAL_DEPENDENCIES, *constraints)
177+
178+
if SYSTEM_TEST_LOCAL_DEPENDENCIES:
179+
session.install("-e", *SYSTEM_TEST_LOCAL_DEPENDENCIES, *constraints)
180+
181+
if SYSTEM_TEST_DEPENDENCIES:
182+
session.install("-e", *SYSTEM_TEST_DEPENDENCIES, *constraints)
183+
184+
if SYSTEM_TEST_EXTRAS_BY_PYTHON:
185+
extras = SYSTEM_TEST_EXTRAS_BY_PYTHON.get(session.python, [])
186+
elif SYSTEM_TEST_EXTRAS:
187+
extras = SYSTEM_TEST_EXTRAS
188+
else:
189+
extras = []
190+
191+
if extras:
192+
session.install("-e", f".[{','.join(extras)}]", *constraints)
193+
else:
194+
session.install("-e", ".", *constraints)
195+
196+
124197
@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
125198
def system(session):
126199
"""Run the system test suite."""
@@ -143,13 +216,7 @@ def system(session):
143216
if not system_test_exists and not system_test_folder_exists:
144217
session.skip("System tests were not found")
145218

146-
# Use pre-release gRPC for system tests.
147-
session.install("--pre", "grpcio")
148-
149-
# Install all test dependencies, then install this package into the
150-
# virtualenv's dist-packages.
151-
session.install("mock", "pytest", "google-cloud-testutils", "-c", constraints_path)
152-
session.install("-e", ".[testing]", "-c", constraints_path)
219+
install_systemtest_dependencies(session, "-c", constraints_path)
153220

154221
# Run py.test against the system tests.
155222
if system_test_exists:

owlbot.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
cov_level=99,
8686
system_test_python_versions=["3.8"],
8787
unit_test_python_versions=["3.6", "3.7", "3.8", "3.9"],
88+
unit_test_extras=['testing'],
8889
microgenerator=True)
8990
python.py_samples(skip_readmes=True)
9091
s.move(
@@ -113,11 +114,4 @@
113114
# Don't treat docs warnings as errors
114115
s.replace("noxfile.py", """["']-W["'], # warnings as errors""", "")
115116

116-
# Replacement to install extra testing dependencies
117-
s.replace(
118-
"noxfile.py",
119-
"""session.install\("-e", ".", "-c", constraints_path\)""",
120-
"""session.install("-e", ".[testing]", "-c", constraints_path)"""
121-
)
122-
123117
s.shell.run(["nox", "-s", "blacken"], hide_output=False)

0 commit comments

Comments
 (0)