Script fixtures
provide.testkit.process.script_fixtures
¶
Bash script testing fixtures and utilities.
Provides fixtures and utilities for testing bash scripts with subprocess execution, output capture, environment isolation, and workspace management.
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¶
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
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_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. |