pytest_test_categories.adapters.filesystem¶
Production filesystem blocker adapter using patching.
This module provides the production implementation of FilesystemBlockerPort that actually intercepts filesystem operations by patching builtins.open, pathlib.Path, os module functions, and shutil functions.
The FilesystemPatchingBlocker follows hexagonal architecture principles: - Implements the FilesystemBlockerPort interface (port) - Patches filesystem operations to intercept access attempts - Raises FilesystemAccessViolationError on unauthorized access - Restores original functions on deactivation
Example
>>> blocker = FilesystemPatchingBlocker()
>>> try:
... blocker.activate(TestSize.SMALL, EnforcementMode.STRICT, frozenset())
... # Any open() call will now be intercepted
... finally:
... blocker.deactivate() # Restore original filesystem behavior
See also
FilesystemBlockerPort: The abstract interface in ports/filesystem.py
FakeFilesystemBlocker: Test adapter in adapters/fake_filesystem.py
SocketPatchingNetworkBlocker: Similar production adapter pattern for network
Attributes¶
Classes¶
Production adapter that patches filesystem operations to block access. |
Module Contents¶
- class pytest_test_categories.adapters.filesystem.FilesystemPatchingBlocker(/, **data)[source]¶
Bases:
pytest_test_categories.ports.filesystem.FilesystemBlockerPortProduction adapter that patches filesystem operations to block access.
This adapter intercepts filesystem access by patching: - builtins.open - pathlib.Path methods (read_text, write_text, read_bytes, write_bytes, etc.) - os module functions (remove, mkdir, makedirs, etc.) - shutil functions (copy, move, rmtree, etc.)
The patching is reversible - deactivate() restores the original functions.
- Parameters:
data (Any)
Warning
This adapter modifies global state (builtins.open). Always use in a try/finally block or context manager to ensure cleanup.
Example
>>> blocker = FilesystemPatchingBlocker() >>> try: ... blocker.activate(TestSize.SMALL, EnforcementMode.STRICT, frozenset()) ... open('/etc/passwd', 'r') # Raises FilesystemAccessViolationError ... finally: ... blocker.deactivate()
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- model_post_init(context, /)[source]¶
Initialize post-Pydantic setup, storing reference to original functions.
- Parameters:
context (object)
- Return type:
None
- reset()[source]¶
Reset blocker to initial state, restoring original filesystem functions.
This is safe to call regardless of current state.
- Return type:
None
- current_allowed_paths: frozenset[pathlib.Path] = None[source]¶
- current_enforcement_mode: pytest_test_categories.ports.network.EnforcementMode | None = None[source]¶
- current_test_size: pytest_test_categories.types.TestSize | None = None[source]¶