resource-guards
Pytest infrastructure for enforcing that tests declare their external resource usage via marks.
Resource guards catch two classes of bugs:
- Missing marks: a test calls an external resource (a CLI binary or Python SDK) without the corresponding
@pytest.mark.<resource>. The guard fails the test with a clear message. - Superfluous marks: a test carries a resource mark but never actually invokes the resource. The guard fails the test so the mark doesn't rot.
These two checks together enforce a single design invariant: for any (test, guarded resource) pair, there is exactly one correct mark state. A test that exercises the resource (directly or via a tagged fixture in its closure) must carry the mark; a test that doesn't must not. Allowing a test to pass both with and without the mark would defeat the point of the system, since pytest -m <resource> would no longer reliably select every test that needs the resource. Every rule that follows is in service of this invariant.
How it works
There are two guard mechanisms, covering CLI binaries and Python SDKs respectively.
Binary guards create wrapper scripts that shadow the real binary on PATH. During a test, the wrapper checks environment variables to decide whether the test is allowed to use the binary. If not, it records a tracking file and exits 127. If yes, it records a tracking file and delegates to the real binary.
SDK guards monkeypatch a chokepoint in a Python SDK. The monkeypatched function calls enforce_sdk_guard(), which checks the same environment variables and either raises ResourceGuardViolation or records a tracking file.
Both mechanisms use per-test tracking files so the makereport hook can detect violations even when the test swallows errors or handles non-zero exit codes.
Basic usage
In your conftest.py, register each resource you want to guard with register_resource_guard(), then wire up the three session hooks. register_guarded_resource_markers registers the pytest marks for all guarded resources in one call.
# conftest.py
from imbue.resource_guards.resource_guards import (
register_guarded_resource_markers,
register_resource_guard,
start_resource_guards,
stop_resource_guards,
)
register_resource_guard("tmux")
register_resource_guard("rsync")
def pytest_configure(config):
register_guarded_resource_markers(config)
def pytest_sessionstart(session):
start_resource_guards(session)
def pytest_sessionfinish(session, exitstatus):
stop_resource_guards()
Then mark your tests:
import pytest
@pytest.mark.tmux
def test_agent_creates_tmux_session():
...
Fixture-level resource declarations
@fixture_uses_resources(...) declares which resources a fixture itself uses. This is the fixture-level analogue of a per-test mark, and is independently verified: the fixture must actually invoke each declared resource during setup.
It exists for module/session-scoped fixtures whose consumers reach a resource only through the fixture. Without it, the fixture's setup call is attributed to whichever test happened to trigger setup, so sibling tests carrying the mark fail the superfluous-mark check.
import pytest
from imbue.resource_guards.resource_guards import fixture_uses_resources
@pytest.fixture(scope="module")
@fixture_uses_resources("modal", "docker")
def deployed_function():
deploy_function(...)
yield url
stop_function(...)
With this in place, @pytest.mark.modal on a test is satisfied by either the test body directly invoking modal, or the test consuming a @fixture_uses_resources("modal") fixture. The mark is required on every consumer of a tagged fixture, even if they don't otherwise use the resource, so that pytest -m modal selects every test that transitively needs modal.
Notes:
- The decorator must go below
@pytest.fixture. - A test body that directly invokes a resource always needs the mark, regardless of which fixtures it consumes.
- Opt-in: untagged fixtures are unaffected.
Usage for multi-package projects
When a project spans multiple packages, listing every guard in every consumer's conftest.py becomes a maintenance hazard. Instead, the package that owns a tool declares its guards through a resource_guards entry point, and consumers pick them all up with one call.
Each entry point's value is a no-argument callable that registers one or more guards:
# library's pyproject.toml
[project.entry-points.resource_guards]
my_lib = "imbue.my_lib.register_guards:register_my_guard"
# library's register_guards.py
from imbue.resource_guards.resource_guards import register_resource_guard
def register_my_guard():
register_resource_guard("my_tool")
A library that owns an external resource can also register, alongside its guards, a callable that sweeps whatever its tests leaked (containers, sandboxes). register_session_cleanup_callback(callable) records it; the consumer runs every registered callable once at the end of the session with run_session_cleanup_callbacks(), after the last test's teardown (so the per-test guard environment is gone and the sweep may use the resource freely) and before the guards are stopped. Duplicate registrations of one callable are ignored.
# library's register_guards.py
from imbue.resource_guards.resource_guards import register_resource_guard
from imbue.resource_guards.resource_guards import register_session_cleanup_callback
def register_my_guard():
register_resource_guard("my_tool")
register_session_cleanup_callback(sweep_leaked_my_tool_resources)
The consumer's conftest.py then replaces explicit register_resource_guard(...) calls with a single register_all_resource_guards(), which imports and invokes every entry point in the group, and runs the registered sweeps from pytest_sessionfinish:
# consumer's conftest.py
from imbue.resource_guards.resource_guards import (
register_all_resource_guards,
register_guarded_resource_markers,
run_session_cleanup_callbacks,
start_resource_guards,
stop_resource_guards,
)
register_all_resource_guards()
def pytest_configure(config):
register_guarded_resource_markers(config)
def pytest_sessionstart(session):
start_resource_guards(session)
def pytest_sessionfinish(session, exitstatus):
try:
run_session_cleanup_callbacks()
finally:
stop_resource_guards()
Writing a custom SDK guard
You can guard any Python SDK by registering an install/cleanup pair. The install function patches the SDK's chokepoint -- the single method through which all external calls flow -- so that it calls enforce_sdk_guard("my_sdk") before delegating to the original:
from imbue.resource_guards.resource_guards import enforce_sdk_guard
from imbue.resource_guards.resource_guards import register_sdk_guard
_originals = {}
def _install():
_originals["send"] = SomeClient.send
SomeClient.send = _guarded_send
def _cleanup():
if "send" in _originals:
SomeClient.send = _originals["send"]
_originals.clear()
def _guarded_send(self, *args, **kwargs):
enforce_sdk_guard("my_sdk")
return _originals["send"](self, *args, **kwargs)
register_sdk_guard("my_sdk", _install, _cleanup)
Compatibility with pytest-xdist
Binary guards work transparently with xdist: workers inherit the wrapper scripts and PATH from the controller. SDK guards are installed independently in each process, since patches are process-local.
Metadata
Release files for resource-guards 0.1.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| resource_guards-0.1.9-py3-none-any.whl | Python 3 | none | any | Details for resource_guards-0.1.9-py3-none-any.whl |
Release files / resource_guards-0.1.9-py3-none-any.whl
| Download URL | resource_guards-0.1.9-py3-none-any.whl |
|---|---|
| Size | 16.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b737ecef9476b356d483c7ccc22beaa440d885388809a28c1a26ced2a1a5b6ac
|
|
BLAKE2b-256 checksum How to use checksums |
b65ff02336e68de0c40cf83f1bf2519ee15318432c183b3ac6a5b49aba6a01dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.
Transparency logSecurity for resource-guards 0.1.9
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
1 file in this release has been uploaded with provenance metadata.
Attestation for resource_guards-0.1.9-py3-none-any.whl
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.