Skip to content

Commit 100354a

Browse files
authored
Noxfile check for tests (#7342)
This adds a check to the noxfile to see if there are tests in the directory. To test, use the noxfile-template in any directory with tests. Then try it on a directory like [this one](https://github.com/nikitamaia/python-docs-samples/tree/geospatial-sandbox/people-and-planet-ai/geospatial-classification/serving_app) which is found in PR #7291. This does assume that all tests are of the format `*_test.py` - if we want, I can also include a check for `test_*.py`, because I know that's a common Python pattern even though it's not the one we use. Corresponding PR in synthtool googleapis/synthtool#1321 cc @davidcavazos - this will hopefully remove the need for your workaround
1 parent 0a80569 commit 100354a

File tree

1 file changed

+59
-50
lines changed

1 file changed

+59
-50
lines changed

noxfile-template.py

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from __future__ import print_function
1616

17+
import glob
1718
import os
1819
from pathlib import Path
1920
import sys
@@ -38,31 +39,29 @@
3839

3940
TEST_CONFIG = {
4041
# You can opt out from the test for specific Python versions.
41-
'ignored_versions': ["2.7"],
42-
42+
"ignored_versions": ["2.7"],
4343
# Old samples are opted out of enforcing Python type hints
4444
# All new samples should feature them
45-
'enforce_type_hints': False,
46-
45+
"enforce_type_hints": False,
4746
# An envvar key for determining the project id to use. Change it
4847
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4948
# build specific Cloud project. You can also use your own string
5049
# to use your own Cloud project.
51-
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
50+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
5251
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
5352
# If you need to use a specific version of pip,
5453
# change pip_version_override to the string representation
5554
# of the version number, for example, "20.2.4"
5655
"pip_version_override": None,
5756
# A dictionary you want to inject into your test. Don't put any
5857
# secrets here. These values will override predefined values.
59-
'envs': {},
58+
"envs": {},
6059
}
6160

6261

6362
try:
6463
# Ensure we can import noxfile_config in the project's directory.
65-
sys.path.append('.')
64+
sys.path.append(".")
6665
from noxfile_config import TEST_CONFIG_OVERRIDE
6766
except ImportError as e:
6867
print("No user noxfile_config found: detail: {}".format(e))
@@ -77,13 +76,13 @@ def get_pytest_env_vars() -> Dict[str, str]:
7776
ret = {}
7877

7978
# Override the GCLOUD_PROJECT and the alias.
80-
env_key = TEST_CONFIG['gcloud_project_env']
79+
env_key = TEST_CONFIG["gcloud_project_env"]
8180
# This should error out if not set.
82-
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
83-
ret['GCLOUD_PROJECT'] = os.environ[env_key] # deprecated
81+
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
82+
ret["GCLOUD_PROJECT"] = os.environ[env_key] # deprecated
8483

8584
# Apply user supplied envs.
86-
ret.update(TEST_CONFIG['envs'])
85+
ret.update(TEST_CONFIG["envs"])
8786
return ret
8887

8988

@@ -92,7 +91,7 @@ def get_pytest_env_vars() -> Dict[str, str]:
9291
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8", "3.9", "3.10"]
9392

9493
# Any default versions that should be ignored.
95-
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
94+
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
9695

9796
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
9897

@@ -145,7 +144,7 @@ def _determine_local_import_names(start_dir: str) -> List[str]:
145144

146145
@nox.session
147146
def lint(session: nox.sessions.Session) -> None:
148-
if not TEST_CONFIG['enforce_type_hints']:
147+
if not TEST_CONFIG["enforce_type_hints"]:
149148
session.install("flake8", "flake8-import-order")
150149
else:
151150
session.install("flake8", "flake8-import-order", "flake8-annotations")
@@ -154,7 +153,7 @@ def lint(session: nox.sessions.Session) -> None:
154153
args = FLAKE8_COMMON_ARGS + [
155154
"--application-import-names",
156155
",".join(local_names),
157-
"."
156+
".",
158157
]
159158
session.run("flake8", *args)
160159

@@ -163,6 +162,7 @@ def lint(session: nox.sessions.Session) -> None:
163162
# Black
164163
#
165164

165+
166166
@nox.session
167167
def blacken(session: nox.sessions.Session) -> None:
168168
session.install("black")
@@ -179,38 +179,47 @@ def blacken(session: nox.sessions.Session) -> None:
179179
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]
180180

181181

182-
def _session_tests(session: nox.sessions.Session, post_install: Callable = None) -> None:
183-
if TEST_CONFIG["pip_version_override"]:
184-
pip_version = TEST_CONFIG["pip_version_override"]
185-
session.install(f"pip=={pip_version}")
186-
"""Runs py.test for a particular project."""
187-
if os.path.exists("requirements.txt"):
188-
if os.path.exists("constraints.txt"):
189-
session.install("-r", "requirements.txt", "-c", "constraints.txt")
190-
else:
191-
session.install("-r", "requirements.txt")
192-
193-
if os.path.exists("requirements-test.txt"):
194-
if os.path.exists("constraints-test.txt"):
195-
session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt")
196-
else:
197-
session.install("-r", "requirements-test.txt")
198-
199-
if INSTALL_LIBRARY_FROM_SOURCE:
200-
session.install("-e", _get_repo_root())
201-
202-
if post_install:
203-
post_install(session)
204-
205-
session.run(
206-
"pytest",
207-
*(PYTEST_COMMON_ARGS + session.posargs),
208-
# Pytest will return 5 when no tests are collected. This can happen
209-
# on travis where slow and flaky tests are excluded.
210-
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
211-
success_codes=[0, 5],
212-
env=get_pytest_env_vars()
213-
)
182+
def _session_tests(
183+
session: nox.sessions.Session, post_install: Callable = None
184+
) -> None:
185+
# check for presence of tests
186+
test_list = glob.glob("*_test.py") + glob.glob("test_*.py")
187+
if len(test_list) == 0:
188+
print("No tests found, skipping directory.")
189+
else:
190+
if TEST_CONFIG["pip_version_override"]:
191+
pip_version = TEST_CONFIG["pip_version_override"]
192+
session.install(f"pip=={pip_version}")
193+
"""Runs py.test for a particular project."""
194+
if os.path.exists("requirements.txt"):
195+
if os.path.exists("constraints.txt"):
196+
session.install("-r", "requirements.txt", "-c", "constraints.txt")
197+
else:
198+
session.install("-r", "requirements.txt")
199+
200+
if os.path.exists("requirements-test.txt"):
201+
if os.path.exists("constraints-test.txt"):
202+
session.install(
203+
"-r", "requirements-test.txt", "-c", "constraints-test.txt"
204+
)
205+
else:
206+
session.install("-r", "requirements-test.txt")
207+
208+
if INSTALL_LIBRARY_FROM_SOURCE:
209+
session.install("-e", _get_repo_root())
210+
211+
if post_install:
212+
post_install(session)
213+
214+
session.run(
215+
"pytest",
216+
*(PYTEST_COMMON_ARGS + session.posargs),
217+
# Pytest will return 5 when no tests are collected. This can happen
218+
# on travis where slow and flaky tests are excluded.
219+
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
220+
success_codes=[0, 5],
221+
env=get_pytest_env_vars(),
222+
)
214223

215224

216225
@nox.session(python=ALL_VERSIONS)
@@ -219,9 +228,9 @@ def py(session: nox.sessions.Session) -> None:
219228
if session.python in TESTED_VERSIONS:
220229
_session_tests(session)
221230
else:
222-
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
223-
session.python
224-
))
231+
session.skip(
232+
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
233+
)
225234

226235

227236
#
@@ -230,7 +239,7 @@ def py(session: nox.sessions.Session) -> None:
230239

231240

232241
def _get_repo_root() -> Optional[str]:
233-
""" Returns the root folder of the project. """
242+
"""Returns the root folder of the project."""
234243
# Get root of this repository.
235244
# Assume we don't have directories nested deeper than 10 items.
236245
p = Path(os.getcwd())

0 commit comments

Comments
 (0)