Fixtures
provide.testkit.process.fixtures
¶
Process Test Fixtures.
Core process testing fixtures with re-exports from specialized modules. Utilities for testing async code, managing event loops, handling async subprocess mocking, and bash script testing across the provide-io ecosystem.
Classes¶
ScriptExecutionContext
¶
Context for executing bash scripts in isolation.
Provides a temporary workspace with environment variable control and automatic cleanup.
Attributes:
| Name | Type | Description |
|---|---|---|
workspace |
Path
|
Path to temporary workspace directory. |
env |
dict[str, str]
|
Environment variables for script execution. |
timeout |
int
|
Timeout in seconds for script execution. |
Functions¶
create_directory
¶
create_file
¶
Create a file in the workspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
File name (can include subdirectories). |
required |
content
|
str
|
File content. |
''
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created file. |
Source code in provide/testkit/process/script_fixtures.py
file_exists
¶
init_git_repo
¶
Initialize a git repository in the workspace.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If git is not available. |
Source code in provide/testkit/process/script_fixtures.py
|
read_file
¶
Read a file from the workspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
File name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
File contents as string. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file does not exist. |
Source code in provide/testkit/process/script_fixtures.py
run_command
¶
Execute a shell command in the isolated workspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str | list[str]
|
Shell command to execute (string or list). |
required |
check
|
bool
|
If True, raise exception on non-zero exit code. |
False
|
Returns:
| Type | Description |
|---|---|
ScriptResult
|
ScriptResult with execution details. |
Raises:
| Type | Description |
|---|---|
CalledProcessError
|
If check=True and command fails. |
TimeoutExpired
|
If execution exceeds timeout. |
Source code in provide/testkit/process/script_fixtures.py
run_script
¶
run_script(
script_path: Path | str,
args: list[str] | None = None,
check: bool = False,
) -> ScriptResult
Execute a bash script in the isolated workspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script_path
|
Path | str
|
Path to the bash script to execute. |
required |
args
|
list[str] | None
|
Optional arguments to pass to the script. |
None
|
check
|
bool
|
If True, raise exception on non-zero exit code. |
False
|
Returns:
| Type | Description |
|---|---|
ScriptResult
|
ScriptResult with execution details. |
Raises:
| Type | Description |
|---|---|
CalledProcessError
|
If check=True and script fails. |
TimeoutExpired
|
If execution exceeds timeout. |
Source code in provide/testkit/process/script_fixtures.py
ScriptResult
¶
Result of a bash script execution.
Attributes:
| Name | Type | Description |
|---|---|---|
returncode |
int
|
Exit code from the script. |
stdout |
str
|
Standard output as string. |
stderr |
str
|
Standard error as string. |
command |
str | list[str]
|
Command that was executed. |
cwd |
Path
|
Working directory where script was executed. |
duration |
float
|
Execution duration in seconds. |
Functions¶
assert_directory_exists
¶
Assert that a directory exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
Path
|
Path to the directory. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If directory does not exist. |
Source code in provide/testkit/process/script_assertions.py
assert_file_contains
¶
Assert that a file contains specific content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Path
|
Path to the file. |
required |
content
|
str
|
Content to search for. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If file does not contain the content. |
Source code in provide/testkit/process/script_assertions.py
assert_file_created
¶
Assert that a file was created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Path
|
Path to the file. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If file does not exist. |
Source code in provide/testkit/process/script_assertions.py
assert_file_executable
¶
Assert that a file has executable permissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Path
|
Path to the file. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If file is not executable. |
Source code in provide/testkit/process/script_assertions.py
assert_file_not_contains
¶
Assert that a file does not contain specific content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Path
|
Path to the file. |
required |
content
|
str
|
Content that should not be present. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If file contains the content. |
Source code in provide/testkit/process/script_assertions.py
assert_git_repo_cloned
¶
Assert that a git repository was cloned.
Checks that the directory exists and contains a .git directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_path
|
Path
|
Path to the repository. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If repository was not cloned. |
Source code in provide/testkit/process/script_assertions.py
assert_script_exit_code
¶
assert_script_exit_code(
result: ScriptResult,
expected_code: int,
message: str | None = None,
) -> None
Assert that a script exited with a specific code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ScriptResult
|
Script execution result. |
required |
expected_code
|
int
|
Expected exit code. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If exit code does not match. |
Source code in provide/testkit/process/script_assertions.py
assert_script_failure
¶
Assert that a script failed (non-zero exit code).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ScriptResult
|
Script execution result. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If script succeeded. |
Source code in provide/testkit/process/script_assertions.py
assert_script_success
¶
Assert that a script executed successfully (exit code 0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ScriptResult
|
Script execution result. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If script did not succeed. |
Source code in provide/testkit/process/script_assertions.py
assert_stderr_contains
¶
Assert that script stderr contains specific content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ScriptResult
|
Script execution result. |
required |
content
|
str
|
Content to search for in stderr. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If stderr does not contain the content. |
Source code in provide/testkit/process/script_assertions.py
assert_stderr_empty
¶
Assert that script stderr is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ScriptResult
|
Script execution result. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If stderr is not empty. |
Source code in provide/testkit/process/script_assertions.py
assert_stdout_contains
¶
Assert that script stdout contains specific content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ScriptResult
|
Script execution result. |
required |
content
|
str
|
Content to search for in stdout. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If stdout does not contain the content. |
Source code in provide/testkit/process/script_assertions.py
assert_stdout_empty
¶
Assert that script stdout is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
ScriptResult
|
Script execution result. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If stdout is not empty. |
Source code in provide/testkit/process/script_assertions.py
assert_symlink_points_to
¶
assert_symlink_points_to(
symlink_path: Path,
target_path: Path,
message: str | None = None,
) -> None
Assert that a symlink points to a specific target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symlink_path
|
Path
|
Path to the symlink. |
required |
target_path
|
Path
|
Expected target path. |
required |
message
|
str | None
|
Optional custom assertion message. |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If symlink does not point to target. |
Source code in provide/testkit/process/script_assertions.py
async_condition_waiter
¶
Helper for waiting on async conditions in tests.
Returns:
| Type | Description |
|---|---|
Callable[[Callable[[], bool], float, float], Awaitable[bool]]
|
Function to wait for conditions with timeout. |
Source code in provide/testkit/process/async_fixtures.py
async_context_manager
async
¶
Factory for creating mock async context managers.
Returns:
| Type | Description |
|---|---|
Callable[[Any | None, Any | None], AsyncMock]
|
Function that creates configured async context manager mocks. |
Source code in provide/testkit/process/async_fixtures.py
async_gather_helper
¶
Helper for testing asyncio.gather operations.
Returns:
| Type | Description |
|---|---|
Callable[..., Awaitable[list[Any]]]
|
Function to gather async results with error handling. |
Source code in provide/testkit/process/async_fixtures.py
async_iterator
async
¶
Factory for creating mock async iterators.
Returns:
| Type | Description |
|---|---|
Callable[[Sequence[T]], AsyncIterable[T]]
|
Function that creates async iterator mocks with specified values. |
Source code in provide/testkit/process/async_fixtures.py
async_lock
async
¶
Create an async lock for testing synchronization.
Returns:
| Type | Description |
|---|---|
Lock
|
asyncio.Lock instance for testing. |
async_mock_server
¶
Create a mock async server for testing.
Returns:
| Type | Description |
|---|---|
AsyncMockServer
|
Mock server with async methods. |
async_pipeline
¶
Create an async pipeline for testing data flow.
Returns:
| Type | Description |
|---|---|
AsyncPipeline
|
AsyncPipeline instance for chaining async operations. |
async_queue
¶
async_rate_limiter
¶
Create an async rate limiter for testing.
Returns:
| Type | Description |
|---|---|
AsyncRateLimiter
|
AsyncRateLimiter instance for controlling request rates. |
async_stream_reader
async
¶
Mock async stream reader for subprocess stdout/stderr.
Returns:
| Type | Description |
|---|---|
AsyncMock
|
AsyncMock configured as a stream reader. |
Source code in provide/testkit/process/subprocess_fixtures.py
async_subprocess
¶
Create mock async subprocess for testing.
Returns:
| Type | Description |
|---|---|
Callable[[int, bytes, bytes, int], AsyncMock]
|
Function that creates mock subprocess with configurable behavior. |
Source code in provide/testkit/process/subprocess_fixtures.py
async_task_group
¶
Manage a group of async tasks with cleanup.
Returns:
| Type | Description |
|---|---|
AsyncTaskGroup
|
AsyncTaskGroup instance for managing tasks. |
async_test_client
¶
Create an async HTTP test client.
Returns:
| Type | Description |
|---|---|
AsyncTestClient
|
Mock async HTTP client for testing. |
async_timeout
¶
Provide configurable timeout wrapper for async operations.
Returns:
| Type | Description |
|---|---|
Callable[[Awaitable[T], float], Awaitable[T]]
|
A function that wraps async operations with a timeout. |
Source code in provide/testkit/process/async_fixtures.py
bash_script_runner
¶
bash_script_runner(
isolated_workspace: Path,
) -> Callable[
[Path | str, list[str] | None, dict[str, str] | None],
ScriptResult,
]
Create a bash script runner fixture.
Returns:
| Type | Description |
|---|---|
Callable[[Path | str, list[str] | None, dict[str, str] | None], ScriptResult]
|
Function that executes bash scripts and returns results. |
Example
def test_my_script(bash_script_runner): result = bash_script_runner("path/to/script.sh", args=["--flag"]) assert result.success assert "expected output" in result.stdout
Source code in provide/testkit/process/script_fixtures.py
clean_event_loop
async
¶
Ensure clean event loop for async tests.
Cancels all pending tasks after the test to prevent event loop issues.
Yields:
| Type | Description |
|---|---|
AsyncGenerator[None, None]
|
None - fixture for test setup/teardown. |
Source code in provide/testkit/process/async_fixtures.py
disable_setproctitle
¶
Disables setproctitle during tests to prevent pytest-xdist performance issues.
The setproctitle module causes severe performance degradation when running tests with pytest-xdist parallelization: - Immediate slowdown on test start - Progressive performance degradation over time - High CPU usage due to frequent system calls
Mocks setproctitle as a no-op during regular test runs while preserving functionality for mutation testing tools like mutmut that use it for displaying progress information.
Autouse and session-scoped - applies automatically to all tests in a session.
Yields:
| Name | Type | Description |
|---|---|---|
None |
None
|
Context manager for test execution with setproctitle disabled |
Note
Only disables setproctitle when NOT running under mutmut. Mutmut detection uses sys.argv to check for "mutmut" in arguments.
Source code in provide/testkit/process/system_fixtures.py
event_loop_policy
¶
Set event loop policy for tests to avoid conflicts.
Returns:
| Type | Description |
|---|---|
None
|
New event loop policy for isolated testing. |
Source code in provide/testkit/process/async_fixtures.py
git_workspace
¶
Create a temporary workspace with git initialized.
Yields:
| Type | Description |
|---|---|
Path
|
Path to git-initialized workspace directory. |
Source code in provide/testkit/process/script_fixtures.py
|
isolated_workspace
¶
Create an isolated temporary workspace directory.
Yields:
| Type | Description |
|---|---|
Path
|
Path to temporary workspace directory. |
Source code in provide/testkit/process/script_fixtures.py
mock_async_process
¶
Mock async subprocess for testing.
Returns:
| Type | Description |
|---|---|
AsyncMock
|
AsyncMock configured as a subprocess with common attributes. |
Source code in provide/testkit/process/subprocess_fixtures.py
mock_git_repo
¶
Create a factory for mock git repositories.
Returns:
| Type | Description |
|---|---|
Callable[[Path, str], Path]
|
Function that creates a mock git repo with commits. |
Example
def test_git_clone(mock_git_repo, isolated_workspace): repo = mock_git_repo(isolated_workspace, "test-repo") # repo is a path to a git repository with initial commit
Source code in provide/testkit/process/script_fixtures.py
|
script_execution_context
¶
Create an isolated script execution context.
Provides a temporary workspace directory with utilities for running bash scripts in isolation.
Yields:
| Type | Description |
|---|---|
ScriptExecutionContext
|
ScriptExecutionContext with temporary workspace. |