Skip to content

Commit

Permalink
Introduce noxfile.py as potential replacement for tox.ini
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Šoltis <msoltis@redhat.com>
  • Loading branch information
slimreaper35 committed Nov 14, 2024
1 parent 58eeee4 commit aad2d24
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
"""
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 os

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"

# stop other sessions if one session fails
nox.options.stop_on_first_error = True


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)
cmd = "bandit -c pyproject.toml -r cachi2"
session.run(*cmd.split(), *session.posargs, silent=True)


@nox.session()
def black(session: Session) -> None:
"""Run black on cachi2 and tests directories and noxfile.py"""
_install_requirements(session)
cmd = "black --check --diff cachi2 tests noxfile.py"
session.run(*cmd.split(), *session.posargs, silent=True)


@nox.session()
def flake8(session: Session) -> None:
"""Run flake8 on cachi2 and tests directories"""
_install_requirements(session)
cmd = "flake8 cachi2 tests"
session.run(*cmd.split(), *session.posargs, silent=True)


@nox.session()
def isort(session: Session) -> None:
"""Run isort on cachi2 and tests directories and noxfile.py"""
_install_requirements(session)
cmd = "isort --check --diff --color cachi2 tests noxfile.py"
session.run(*cmd.split(), *session.posargs, silent=True)


@nox.session()
def mypy(session: Session) -> None:
"""Run mypy on cachi2 and tests directories"""
_install_requirements(session)
cmd = "mypy --install-types --non-interactive cachi2 tests"
session.run(*cmd.split(), *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)
cmd = "pytest --log-level=DEBUG --cov=cachi2 --cov-config=pyproject.toml --cov-report=term --cov-report=html --no-cov-on-fail tests/unit"
session.run(*cmd.split(), *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)
netrc = "machine 127.0.0.1 login cachi2-user password cachi2-pass"
default_env = {"CACHI2_TEST_NETRC_CONTENT": os.getenv("CACHI2_TEST_NETRC_CONTENT", netrc)}
default_env.update(env)
cmd = "pytest --log-cli-level=WARNING tests/integration"
session.run(*cmd.split(), *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",
"CACHI2_TEST_LOCAL_PYPISERVER": "true",
"CACHI2_TEST_LOCAL_DNF_SERVER": "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",
# these integration tests do not generate SBOMs
"CACHI2_TEST_LOCAL_PYPISERVER": "false",
"CACHI2_TEST_LOCAL_DNF_SERVER": "false",
},
)

0 comments on commit aad2d24

Please sign in to comment.