Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions craft_application/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from craft_parts import callbacks

from craft_application import util
from craft_application._const import CRAFT_DEBUG_ENV
from craft_application.util import platforms

if TYPE_CHECKING:
Expand All @@ -33,14 +34,35 @@
from pyfakefs.fake_filesystem import FakeFilesystem


@pytest.fixture(autouse=True, scope="session")
def reset_craft_environment() -> Iterator[None]:
"""Reset any relevant environment variables during testing.

This clears any ``CRAFT_*`` or ``SNAP_*`` environment variables that are set
in the environment. To keep an environment variable while running tests for
debugging purposes, put its name in the environment variable
``CRAFT_DEBUG_KEEP_ENV_VARS``, which is a comma-separated list of variables.
"""
keep_vars = set(os.environ.get("CRAFT_DEBUG_KEEP_ENV_VARS", "").split(","))
keep_vars.add(CRAFT_DEBUG_ENV) # We separately set CRAFT_DEBUG.

with pytest.MonkeyPatch.context() as monkeypatch:
for var in os.environ:
if var in keep_vars:
continue
if var.startswith(("CRAFT_", "SNAP_")) or var == "SNAP":
monkeypatch.delenv(var, raising=False)
yield


@pytest.fixture(autouse=True, scope="session")
def debug_mode() -> None:
"""Ensure that the application is in debug mode, raising exceptions from run().

This fixture is automatically used. To disable debug mode for specific tests,
use the :py:func:`production_mode` fixture.
"""
os.environ["CRAFT_DEBUG"] = "1"
os.environ[CRAFT_DEBUG_ENV] = "1"


@pytest.fixture
Expand All @@ -51,7 +73,7 @@ def production_mode(monkeypatch: pytest.MonkeyPatch) -> None:
It should only be used if the application needs to test behaviour that differs
between debug mode and production mode.
"""
monkeypatch.setenv("CRAFT_DEBUG", "0")
monkeypatch.setenv(CRAFT_DEBUG_ENV, "0")


@pytest.fixture
Expand Down
7 changes: 7 additions & 0 deletions docs/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ Application
- If a lifecycle command is run with ``--destructive-mode``, but without root, a warning
will be emitted about potentially unexpected behavior.

Pytest plugin
=============

- The pytest plugin now includes an auto-used
:py:func:`~craft_application.pytest_plugin.reset_craft_environment` fixture to prevent
relevant externally-set environment variables from causing test failures.

For a complete list of commits, check out the `6.1.0`_ release on GitHub.

6.0.1 (2025-11-19)
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/pytest-plugin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Some fixtures are automatically enabled for tests, changing the default behaviou
applications during the testing process. Each auto-use fixture changes the default
behaviour of Craft Application during testing.

.. autofunction:: reset_craft_environment

.. autofunction:: debug_mode

.. autofunction:: _reset_craft_parts_callbacks
Expand Down