|
| 1 | +"""Nox sessions.""" |
| 2 | +import tempfile |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +import nox |
| 6 | +from nox.sessions import Session |
| 7 | + |
| 8 | +package = "twint" |
| 9 | +nox.options.sessions = "lint", "mypy", "pytype", "tests" |
| 10 | +locations = "src", "tests", "noxfile.py" |
| 11 | + |
| 12 | + |
| 13 | +def install_with_constraints(session: Session, *args: str, **kwargs: Any) -> None: |
| 14 | + """Install packages constrained by Poetry's lock file. |
| 15 | +
|
| 16 | + This function is a wrapper for nox.sessions.Session.install. It |
| 17 | + invokes pip to install packages inside of the session's virtualenv. |
| 18 | + Additionally, pip is passed a constraints file generated from |
| 19 | + Poetry's lock file, to ensure that the packages are pinned to the |
| 20 | + versions specified in poetry.lock. This allows you to manage the |
| 21 | + packages as Poetry development dependencies. |
| 22 | +
|
| 23 | + Arguments: |
| 24 | + session: The Session object. |
| 25 | + args: Command-line arguments for pip. |
| 26 | + kwargs: Additional keyword arguments for Session.install. |
| 27 | +
|
| 28 | + """ |
| 29 | + with tempfile.NamedTemporaryFile() as requirements: |
| 30 | + session.run( |
| 31 | + "poetry", |
| 32 | + "export", |
| 33 | + "--dev", |
| 34 | + "--format=requirements.txt", |
| 35 | + f"--output={requirements.name}", |
| 36 | + external=True, |
| 37 | + ) |
| 38 | + session.install(f"--constraint={requirements.name}", *args, **kwargs) |
| 39 | + |
| 40 | + |
| 41 | +@nox.session(python="3.8") |
| 42 | +def black(session: Session) -> None: |
| 43 | + """Run black code formatter.""" |
| 44 | + args = session.posargs or locations |
| 45 | + install_with_constraints(session, "black") |
| 46 | + session.run("black", *args) |
| 47 | + |
| 48 | + |
| 49 | +@nox.session(python=["3.8", "3.7"]) |
| 50 | +def lint(session: Session) -> None: |
| 51 | + """Lint using flake8.""" |
| 52 | + args = session.posargs or locations |
| 53 | + install_with_constraints( |
| 54 | + session, |
| 55 | + "flake8", |
| 56 | + "flake8-annotations", |
| 57 | + "flake8-bandit", |
| 58 | + "flake8-black", |
| 59 | + "flake8-bugbear", |
| 60 | + "flake8-docstrings", |
| 61 | + "flake8-import-order", |
| 62 | + ) |
| 63 | + session.run("flake8", *args) |
| 64 | + |
| 65 | + |
| 66 | +@nox.session(python=["3.8", "3.7"]) |
| 67 | +def mypy(session: Session) -> None: |
| 68 | + """Type-check using mypy.""" |
| 69 | + args = session.posargs or locations |
| 70 | + install_with_constraints(session, "mypy") |
| 71 | + session.run("mypy", *args) |
| 72 | + |
| 73 | + |
| 74 | +@nox.session(python="3.7") |
| 75 | +def pytype(session: Session) -> None: |
| 76 | + """Type-check using pytype.""" |
| 77 | + args = session.posargs or ["--disable=import-error", *locations] |
| 78 | + install_with_constraints(session, "pytype") |
| 79 | + session.run("pytype", *args) |
| 80 | + |
| 81 | + |
| 82 | +@nox.session(python=["3.8", "3.7"]) |
| 83 | +def tests(session: Session) -> None: |
| 84 | + """Run the test suite.""" |
| 85 | + args = session.posargs or ["--cov", "-m", "not e2e"] |
| 86 | + session.run("poetry", "install", "--no-dev", external=True) |
| 87 | + install_with_constraints( |
| 88 | + session, "coverage[toml]", "pytest", "pytest-cov", "pytest-mock" |
| 89 | + ) |
| 90 | + session.run("pytest", *args) |
| 91 | + |
| 92 | + |
| 93 | +@nox.session(python=["3.8", "3.7"]) |
| 94 | +def typeguard(session: Session) -> None: |
| 95 | + """Runtime type checking using Typeguard.""" |
| 96 | + args = session.posargs or ["-m", "not e2e"] |
| 97 | + session.run("poetry", "install", "--no-dev", external=True) |
| 98 | + install_with_constraints(session, "pytest", "pytest-mock", "typeguard") |
| 99 | + session.run("pytest", f"--typeguard-packages={package}", *args) |
| 100 | + |
| 101 | + |
| 102 | +# @nox.session(python="3.8") |
| 103 | +# def coverage(session: Session) -> None: |
| 104 | +# """Upload coverage data.""" |
| 105 | +# install_with_constraints(session, "coverage[toml]", "codecov") |
| 106 | +# session.run("coverage", "xml", "--fail-under=0") |
| 107 | +# session.run("codecov", *session.posargs) |
0 commit comments