-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce noxfile.py as potential replacement for tox.ini
Signed-off-by: Michal Šoltis <msoltis@redhat.com>
- Loading branch information
1 parent
ab31296
commit 111b7e8
Showing
1 changed file
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
""" | ||
Nox is a command-line tool that automates testing in multiple Python environments, similar to tox. | ||
Unlike tox, Nox uses a standard Python file for configuration. | ||
To run all sessions defined in the noxfile.py file, run the following command: | ||
$ nox | ||
To run a specific session, run the following command: | ||
$ nox -s <session-name> | ||
To run a session with additional arguments, run the following command: | ||
$ nox -s <session-name> -- <additional-arguments> | ||
To list all available sessions, run the following command: | ||
$ nox -l | ||
""" | ||
|
||
import nox | ||
from nox.sessions import Session | ||
|
||
# default sessions (sorted alphabetically) | ||
nox.options.sessions = ["bandit", "black", "flake8", "isort", "mypy", "unit-tests"] | ||
|
||
# reuse virtual environment across all sessions | ||
nox.options.reuse_venv = "always" | ||
|
||
|
||
def _install_requirements(session: Session) -> None: | ||
session.install("-r", "requirements-extras.txt") | ||
|
||
|
||
@nox.session() | ||
def bandit(session: Session) -> None: | ||
"""Run bandit on cachi2 directory""" | ||
_install_requirements(session) | ||
session.run( | ||
"bandit", | ||
"-c", | ||
"pyproject.toml", | ||
"-r", | ||
"cachi2", | ||
*session.posargs, | ||
silent=True, | ||
) | ||
|
||
|
||
@nox.session() | ||
def black(session: Session) -> None: | ||
"""Run black on cachi2 and tests directories and noxfile.py""" | ||
_install_requirements(session) | ||
session.run( | ||
"black", | ||
"--check", | ||
"--diff", | ||
"cachi2", | ||
"tests", | ||
"noxfile.py", | ||
*session.posargs, | ||
silent=True, | ||
) | ||
|
||
|
||
@nox.session() | ||
def flake8(session: Session) -> None: | ||
"""Run flake8 on cachi2 and tests directories""" | ||
_install_requirements(session) | ||
session.run( | ||
"flake8", | ||
"cachi2", | ||
"tests", | ||
*session.posargs, | ||
silent=True, | ||
) | ||
|
||
|
||
@nox.session() | ||
def isort(session: Session) -> None: | ||
"""Run isort on cachi2 and tests directories and noxfile.py""" | ||
_install_requirements(session) | ||
session.run( | ||
"isort", | ||
"--check", | ||
"--diff", | ||
"--color", | ||
"cachi2", | ||
"tests", | ||
"noxfile.py", | ||
*session.posargs, | ||
silent=True, | ||
) | ||
|
||
|
||
@nox.session() | ||
def mypy(session: Session) -> None: | ||
"""Run mypy on cachi2 and tests directories""" | ||
_install_requirements(session) | ||
session.run( | ||
"mypy", | ||
"--install-types", | ||
"--non-interactive", | ||
"cachi2", | ||
"tests", | ||
*session.posargs, | ||
silent=True, | ||
) | ||
|
||
|
||
@nox.session(name="unit-tests", python=["3.9", "3.10", "3.11", "3.12"]) | ||
def unit_tests(session: Session) -> None: | ||
"""Run unit tests and generate coverage report""" | ||
_install_requirements(session) | ||
session.run( | ||
"pytest", | ||
"--log-level=DEBUG", | ||
"--cov=cachi2", | ||
"--cov-config=pyproject.toml", | ||
"--cov-report=term", | ||
"--cov-report=html", | ||
"--no-cov-on-fail", | ||
"tests/unit", | ||
*session.posargs, | ||
) | ||
|
||
|
||
@nox.session(name="coverage-html") | ||
def coverage_html(session: Session) -> None: | ||
"""Open the coverage report in the browser""" | ||
session.install("coverage") | ||
session.run("coverage", "html") | ||
session.run("xdg-open", "htmlcov/index.html", external=True) | ||
|
||
|
||
def _run_integration_tests(session: Session, env: dict[str, str]) -> None: | ||
_install_requirements(session) | ||
default_env = { | ||
"CACHI2_TEST_NETRC_CONTENT": "machine 127.0.0.1 login cachi2-user password cachi2-pass", | ||
"CACHI2_TEST_LOCAL_PYPISERVER": "true", | ||
"CACHI2_TEST_LOCAL_DNF_SERVER": "true", | ||
} | ||
default_env.update(env) | ||
session.run( | ||
"pytest", | ||
"--log-cli-level=WARNING", | ||
"tests/integration", | ||
*session.posargs, | ||
env=default_env, | ||
) | ||
|
||
|
||
@nox.session(name="integration-tests") | ||
def integration_tests(session: Session) -> None: | ||
"""Run integration tests only for the affected code base in the current branch""" | ||
_run_integration_tests( | ||
session, | ||
{}, | ||
) | ||
|
||
|
||
@nox.session(name="all-integration-tests") | ||
def all_integration_tests(session: Session) -> None: | ||
"""Run all integration tests that are available""" | ||
_run_integration_tests( | ||
session, | ||
{ | ||
"CACHI2_RUN_ALL_INTEGRATION_TESTS": "true", | ||
}, | ||
) | ||
|
||
|
||
@nox.session(name="generate-test-data") | ||
def generate_test_data(session: Session) -> None: | ||
"""Run all integration tests that are available and update SBOMs""" | ||
_run_integration_tests( | ||
session, | ||
{ | ||
"CACHI2_RUN_ALL_INTEGRATION_TESTS": "true", | ||
"CACHI2_GENERATE_TEST_DATA": "true", | ||
}, | ||
) |