|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import nox |
| 4 | + |
| 5 | +nox.options.default_venv_backend = "venv" |
| 6 | + |
| 7 | +ROOT = Path(__file__).parent.resolve() |
| 8 | +ALL_PYTHON = ["3.6", "3.7"] |
| 9 | +REQUESTS_VERSIONS = ["2.20", "2.26"] |
| 10 | +REQUIREMENTS_PATH = ROOT / "requirements.txt" |
| 11 | + |
| 12 | + |
| 13 | +def _ensure_requirements_exported(session) -> Path: |
| 14 | + if REQUIREMENTS_PATH.exists(): |
| 15 | + return REQUIREMENTS_PATH |
| 16 | + |
| 17 | + session.error("requirements.txt not found. Run `nox -s export_deps` first.") |
| 18 | + return REQUIREMENTS_PATH |
| 19 | + |
| 20 | + |
| 21 | +def install_with_constraints(session, *args: str, **kwargs) -> None: |
| 22 | + """ |
| 23 | + Install packages constrained by exported requirements.txt. |
| 24 | +
|
| 25 | + This function is a wrapper for nox.sessions.Session.install. It |
| 26 | + invokes pip to install packages inside of the session's virtualenv. |
| 27 | + Additionally, pip is passed a constraints file generated from |
| 28 | + Poetry's lock file, to ensure that the packages are pinned to the |
| 29 | + versions specified in poetry.lock. |
| 30 | + """ |
| 31 | + requirements = _ensure_requirements_exported(session) |
| 32 | + session.install( |
| 33 | + f"--constraint={requirements}", |
| 34 | + "--use-deprecated=legacy-resolver", |
| 35 | + *args, |
| 36 | + **kwargs, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +@nox.session(python="3.11", reuse_venv=True) |
| 41 | +def export_deps(session): |
| 42 | + """Export dependencies from Poetry to requirements.txt. When running tests later, the |
| 43 | + requirements.txt file will be used as a constraints file to ensure that the same version |
| 44 | + of each dependency is used. |
| 45 | + """ |
| 46 | + session.run("python", "-m", "ensurepip", "--upgrade") |
| 47 | + session.install("poetry") |
| 48 | + session.run("poetry", "self", "add", "poetry-plugin-export@latest") |
| 49 | + session.run( |
| 50 | + "poetry", |
| 51 | + "export", |
| 52 | + "--with=dev", |
| 53 | + "--format=requirements.txt", |
| 54 | + "--without-hashes", |
| 55 | + f"--output={REQUIREMENTS_PATH}", |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +@nox.session(reuse_venv=True) |
| 60 | +@nox.parametrize("python", ALL_PYTHON) |
| 61 | +def tests(session): |
| 62 | + session.run("python", "-m", "ensurepip", "--upgrade") |
| 63 | + session.run("python", "-m", "pip", "install", "--upgrade", "pip<24", "setuptools<68", "wheel") |
| 64 | + # Install main dependencies |
| 65 | + install_with_constraints( |
| 66 | + session, |
| 67 | + "-v", |
| 68 | + "requests", |
| 69 | + "curlify", |
| 70 | + "typing-extensions", |
| 71 | + "six", |
| 72 | + ) |
| 73 | + # Install test dependencies |
| 74 | + install_with_constraints( |
| 75 | + session, |
| 76 | + "pytest", |
| 77 | + "pytest-django", |
| 78 | + "pytest-mock", |
| 79 | + "requests-mock", |
| 80 | + "Faker", |
| 81 | + "django", |
| 82 | + "prometheus-client", |
| 83 | + ) |
| 84 | + |
| 85 | + for version in REQUESTS_VERSIONS: |
| 86 | + session.install(f"requests=={version}") |
| 87 | + session.run("pytest", "tests", *session.posargs) |
0 commit comments