Skip to content

Commit

Permalink
ENH: Consider warnings as errors when running pytest
Browse files Browse the repository at this point in the history
Consider warnings as errors when running `pytest`.
  • Loading branch information
jhlegarreta committed Jan 10, 2025
1 parent a6bcd48 commit a540f1c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build_test_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ env:
# Force tox and pytest to use color
FORCE_COLOR: true
TEMPLATEFLOW_HOME: /tmp/templateflow
NIFREEZE_WERRORS: 1

jobs:
build:
Expand Down
32 changes: 32 additions & 0 deletions nireports/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,35 @@ def workdir():
@pytest.fixture
def outdir():
return None if test_output_dir is None else Path(test_output_dir)


def pytest_addoption(parser):
parser.addoption(
"--warnings-as-errors",
action="store_true",
help="Consider all uncaught warnings as errors.",
)


@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session, exitstatus):
have_werrors = os.getenv("NIFREEZE_WERRORS", False)
have_werrors = session.config.getoption("--warnings-as-errors", False) or have_werrors
if have_werrors:
# Check if there were any warnings during the test session
reporter = session.config.pluginmanager.get_plugin("terminalreporter")
if reporter.stats.get("warnings", None):
session.exitstatus = 2


@pytest.hookimpl
def pytest_terminal_summary(terminalreporter, exitstatus, config):
have_werrors = os.getenv("NIFREEZE_WERRORS", False)
have_werrors = config.getoption("--warnings-as-errors", False) or have_werrors
have_warnings = terminalreporter.stats.get("warnings", None)
if have_warnings and have_werrors:
terminalreporter.ensure_newline()
terminalreporter.section("Werrors", sep="=", red=True, bold=True)
terminalreporter.line(
f"Warnings as errors: Activated.\n{len(have_warnings)} warnings were raised and treated as errors.\n"
)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pass_env =
CLICOLOR
CLICOLOR_FORCE
PYTHON_GIL
NIFREEZE_WERRORS
deps =
# Waiting on a release
py313: traits @ git+https://github.com/enthought/traits.git@10954eb
Expand Down

0 comments on commit a540f1c

Please sign in to comment.