|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import print_function |
|
|
|
import glob |
|
import os |
|
from pathlib import Path |
|
import sys |
|
from typing import Callable, Dict, Optional |
|
|
|
import nox |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BLACK_VERSION = "black==22.3.0" |
|
ISORT_VERSION = "isort==5.10.1" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CONFIG = { |
|
|
|
"ignored_versions": [], |
|
|
|
|
|
"enforce_type_hints": False, |
|
|
|
|
|
|
|
|
|
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT", |
|
|
|
|
|
|
|
|
|
"pip_version_override": None, |
|
|
|
|
|
"envs": {}, |
|
} |
|
|
|
|
|
try: |
|
|
|
sys.path.append(".") |
|
from noxfile_config import TEST_CONFIG_OVERRIDE |
|
except ImportError as e: |
|
print("No user noxfile_config found: detail: {}".format(e)) |
|
TEST_CONFIG_OVERRIDE = {} |
|
|
|
|
|
TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) |
|
|
|
|
|
def get_pytest_env_vars() -> Dict[str, str]: |
|
"""Returns a dict for pytest invocation.""" |
|
ret = {} |
|
|
|
|
|
env_key = TEST_CONFIG["gcloud_project_env"] |
|
|
|
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key] |
|
|
|
|
|
ret.update(TEST_CONFIG["envs"]) |
|
return ret |
|
|
|
|
|
|
|
|
|
ALL_VERSIONS = ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"] |
|
|
|
|
|
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"] |
|
|
|
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) |
|
|
|
INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ( |
|
"True", |
|
"true", |
|
) |
|
|
|
|
|
nox.options.error_on_missing_interpreters = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FLAKE8_COMMON_ARGS = [ |
|
"--show-source", |
|
"--builtin=gettext", |
|
"--max-complexity=20", |
|
"--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", |
|
"--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202", |
|
"--max-line-length=88", |
|
] |
|
|
|
|
|
@nox.session |
|
def lint(session: nox.sessions.Session) -> None: |
|
if not TEST_CONFIG["enforce_type_hints"]: |
|
session.install("flake8") |
|
else: |
|
session.install("flake8", "flake8-annotations") |
|
|
|
args = FLAKE8_COMMON_ARGS + [ |
|
".", |
|
] |
|
session.run("flake8", *args) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@nox.session |
|
def blacken(session: nox.sessions.Session) -> None: |
|
"""Run black. Format code to uniform standard.""" |
|
session.install(BLACK_VERSION) |
|
python_files = [path for path in os.listdir(".") if path.endswith(".py")] |
|
|
|
session.run("black", *python_files) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@nox.session |
|
def format(session: nox.sessions.Session) -> None: |
|
""" |
|
Run isort to sort imports. Then run black |
|
to format code to uniform standard. |
|
""" |
|
session.install(BLACK_VERSION, ISORT_VERSION) |
|
python_files = [path for path in os.listdir(".") if path.endswith(".py")] |
|
|
|
|
|
|
|
session.run("isort", "--fss", *python_files) |
|
session.run("black", *python_files) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] |
|
|
|
|
|
def _session_tests( |
|
session: nox.sessions.Session, post_install: Callable = None |
|
) -> None: |
|
|
|
test_list = glob.glob("**/*_test.py", recursive=True) + glob.glob( |
|
"**/test_*.py", recursive=True |
|
) |
|
test_list.extend(glob.glob("**/tests", recursive=True)) |
|
|
|
if len(test_list) == 0: |
|
print("No tests found, skipping directory.") |
|
return |
|
|
|
if TEST_CONFIG["pip_version_override"]: |
|
pip_version = TEST_CONFIG["pip_version_override"] |
|
session.install(f"pip=={pip_version}") |
|
"""Runs py.test for a particular project.""" |
|
concurrent_args = [] |
|
if os.path.exists("requirements.txt"): |
|
if os.path.exists("constraints.txt"): |
|
session.install("-r", "requirements.txt", "-c", "constraints.txt") |
|
else: |
|
session.install("-r", "requirements.txt") |
|
with open("requirements.txt") as rfile: |
|
packages = rfile.read() |
|
|
|
if os.path.exists("requirements-test.txt"): |
|
if os.path.exists("constraints-test.txt"): |
|
session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt") |
|
else: |
|
session.install("-r", "requirements-test.txt") |
|
with open("requirements-test.txt") as rtfile: |
|
packages += rtfile.read() |
|
|
|
if INSTALL_LIBRARY_FROM_SOURCE: |
|
session.install("-e", _get_repo_root()) |
|
|
|
if post_install: |
|
post_install(session) |
|
|
|
if "pytest-parallel" in packages: |
|
concurrent_args.extend(["--workers", "auto", "--tests-per-worker", "auto"]) |
|
elif "pytest-xdist" in packages: |
|
concurrent_args.extend(["-n", "auto"]) |
|
|
|
session.run( |
|
"pytest", |
|
*(PYTEST_COMMON_ARGS + session.posargs + concurrent_args), |
|
|
|
|
|
|
|
success_codes=[0, 5], |
|
env=get_pytest_env_vars(), |
|
) |
|
|
|
|
|
@nox.session(python=ALL_VERSIONS) |
|
def py(session: nox.sessions.Session) -> None: |
|
"""Runs py.test for a sample using the specified version of Python.""" |
|
if session.python in TESTED_VERSIONS: |
|
_session_tests(session) |
|
else: |
|
session.skip( |
|
"SKIPPED: {} tests are disabled for this sample.".format(session.python) |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_repo_root() -> Optional[str]: |
|
"""Returns the root folder of the project.""" |
|
|
|
p = Path(os.getcwd()) |
|
for i in range(10): |
|
if p is None: |
|
break |
|
if Path(p / ".git").exists(): |
|
return str(p) |
|
|
|
|
|
|
|
if Path(p / "setup.py").exists(): |
|
return str(p) |
|
p = p.parent |
|
raise Exception("Unable to detect repository root.") |
|
|
|
|
|
GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) |
|
|
|
|
|
@nox.session |
|
@nox.parametrize("path", GENERATED_READMES) |
|
def readmegen(session: nox.sessions.Session, path: str) -> None: |
|
"""(Re-)generates the readme for a sample.""" |
|
session.install("jinja2", "pyyaml") |
|
dir_ = os.path.dirname(path) |
|
|
|
if os.path.exists(os.path.join(dir_, "requirements.txt")): |
|
session.install("-r", os.path.join(dir_, "requirements.txt")) |
|
|
|
in_file = os.path.join(dir_, "README.rst.in") |
|
session.run( |
|
"python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file |
|
) |
|
|