Temp
provide.testkit.file.temp
¶
Consolidated Temporary File Utilities.
Provides unified utilities for creating and managing temporary files and directories in tests with automatic cleanup capabilities.
Classes¶
TempFileManager
¶
Manager for temporary files and directories with automatic cleanup.
Initialize temp file manager.
Source code in provide/testkit/file/temp.py
Functions¶
__enter__
¶
__exit__
¶
cleanup
¶
Clean up all managed temporary files and directories.
Source code in provide/testkit/file/temp.py
create_binary_file
¶
Create a temporary binary file with content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
bytes
|
Binary content to write to the file. |
b''
|
suffix
|
str
|
File extension. |
''
|
prefix
|
str
|
File name prefix. |
'test_'
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created temporary file. |
Source code in provide/testkit/file/temp.py
create_directory
¶
create_directory_structure
¶
Create a directory structure from a nested dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
dict[str, Any]
|
Dictionary describing the structure. Keys are names, values are either strings (file content) or dicts (subdirectories). |
required |
base_dir
|
Path | None
|
Base directory to create structure in. If None, creates a new temp dir. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the base directory. |
Example
structure = { "config.json": '{"debug": true}', "src": { "main.py": "print('hello')", "utils": { "init.py": "", "helpers.py": "def helper(): pass" } } }
Source code in provide/testkit/file/temp.py
create_file
¶
create_file(
content: str = "",
suffix: str = "",
prefix: str = "test_",
text: bool = True,
) -> Path
Create a temporary file with content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
Content to write to the file. |
''
|
suffix
|
str
|
File extension (e.g., ".txt", ".json"). |
''
|
prefix
|
str
|
File name prefix. |
'test_'
|
text
|
bool
|
Whether to open in text mode. |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created temporary file. |
Source code in provide/testkit/file/temp.py
create_file_in_dir
¶
Create a file in an existing directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path
|
Directory to create the file in. |
required |
name
|
str
|
File name. |
required |
content
|
str
|
Content to write to the file. |
''
|
text
|
bool
|
Whether to open in text mode. |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created file. |
Source code in provide/testkit/file/temp.py
create_json_file
¶
Create a temporary JSON file with data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Data to serialize as JSON. |
required |
suffix
|
str
|
File extension (defaults to .json). |
'.json'
|
prefix
|
str
|
File name prefix. |
'test_'
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created JSON file. |
Source code in provide/testkit/file/temp.py
create_toml_file
¶
Create a temporary TOML file with data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Data to serialize as TOML. |
required |
suffix
|
str
|
File extension (defaults to .toml). |
'.toml'
|
prefix
|
str
|
File name prefix. |
'test_'
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created TOML file. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If tomli_w is not available. |
Source code in provide/testkit/file/temp.py
create_yaml_file
¶
Create a temporary YAML file with data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Data to serialize as YAML. |
required |
suffix
|
str
|
File extension (defaults to .yaml). |
'.yaml'
|
prefix
|
str
|
File name prefix. |
'test_'
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created YAML file. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If PyYAML is not available. |
Source code in provide/testkit/file/temp.py
Functions¶
create_temp_dir
¶
create_temp_file
¶
create_temp_file(
content: str = "",
suffix: str = "",
prefix: str = "test_",
cleanup: bool = True,
) -> Path
Create a temporary file with content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
Content to write to the file. |
''
|
suffix
|
str
|
File extension. |
''
|
prefix
|
str
|
File name prefix. |
'test_'
|
cleanup
|
bool
|
If True, the file will be managed by Foundation's cleanup. |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created temporary file. |