Detection
provide.foundation.testmode.detection
¶
TODO: Add module docstring.
Functions¶
configure_structlog_for_test_safety
¶
Configure structlog to use stdout for multiprocessing safety.
When running tests with parallel execution (pytest-xdist, mutmut with --max-children, etc.), file handles don't survive process forking. This causes "I/O operation on closed file" errors when structlog's PrintLogger tries to write to file handles from forked processes.
This function configures structlog to use sys.stdout which is safe for multiprocessing and properly handled by pytest.
Should be called automatically when is_in_test_mode() returns True.
Source code in provide/foundation/testmode/detection.py
is_in_click_testing
¶
Check if we're running inside Click's testing framework.
This detects Click's CliRunner testing context to prevent stream manipulation that could interfere with Click's output capture.
Returns:
| Type | Description |
|---|---|
bool
|
True if running in Click testing context, False otherwise |
Source code in provide/foundation/testmode/detection.py
is_in_test_mode
¶
Detect if we're running in a test environment.
This method checks for common test environment indicators to determine if Foundation components should adjust their behavior for test compatibility.
Performance: Results are cached after first detection since test mode doesn't change during process lifetime. Use _clear_test_mode_cache() in tests for proper isolation.
Returns:
| Type | Description |
|---|---|
bool
|
True if running in test mode, False otherwise |
Source code in provide/foundation/testmode/detection.py
should_allow_stream_redirect
¶
Check if stream redirection should be allowed in testing.
Stream redirection is normally blocked when in Click testing context to prevent interference with Click's output capture. This can be overridden with FOUNDATION_FORCE_STREAM_REDIRECT=true.
Returns:
| Type | Description |
|---|---|
bool
|
True if stream redirect is allowed (not in Click testing OR force enabled) |
Source code in provide/foundation/testmode/detection.py
should_use_shared_registries
¶
should_use_shared_registries(
use_shared_registries: bool,
component_registry: object | None,
command_registry: object | None,
) -> bool
Determine if Hub should use shared registries based on explicit parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_shared_registries
|
bool
|
Explicit user preference |
required |
component_registry
|
object | None
|
Custom component registry if provided |
required |
command_registry
|
object | None
|
Custom command registry if provided |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if shared registries should be used |