Temp Path Factories

Purpose

The Temp Path Factories subtopic addresses the need for creating and managing temporary directories during test runs, ensuring each test function can safely work with isolated filesystem locations. Unlike the broader temporary directory management topic, which covers retention policies and cleanup strategies, this subtopic focuses specifically on the factory objects responsible for generating these directories, managing their lifecycle, and providing consistent, unique paths scoped per test or session.

This feature solves challenges such as:

Functionality

Core Factory Class: TempPathFactory

The `TempPathFactory` class encapsulates all logic related to creating and managing temporary directory paths under a shared base directory. Its key responsibilities include:

Integration with Fixtures

Lifecycle Hooks

Code Snippet Illustrating Directory Creation

def mktemp(self, basename: str, numbered: bool = True) -> Path:
    basename = self._ensure_relative_to_basetemp(basename)
    if not numbered:
        p = self.getbasetemp().joinpath(basename)
        p.mkdir(mode=0o700)
    else:
        p = make_numbered_dir(root=self.getbasetemp(), prefix=basename, mode=0o700)
        self._trace("mktemp", p)
    return p

This snippet shows how the factory creates a new temporary directory, optionally adding a numeric suffix to ensure uniqueness.

Integration

The Temp Path Factories subtopic is tightly integrated with the overall Temporary Directory Management parent topic by serving as the concrete mechanism for generating temporary paths that other utilities manage and clean up. It complements related subtopics by:

While the parent topic handles the policy and lifecycle, Temp Path Factories handle the actual directory creation, naming, and base directory management, thus ensuring a clean separation of concerns.

Diagram

flowchart TD
    A[Start Test Session] --> B[Initialize TempPathFactory from Config]
    B --> C[Determine Base Temporary Directory]
    C --> D[Create Unique Temp Directories per Test]
    D --> E[Provide Temp Path via tmp_path Fixture]
    E --> F[Test Execution Using Temp Directory]
    F --> G[Track Test Outcome in Stash]
    G --> H{Retention Policy?}
    H -->|Failed| I[Keep Temp Directory]
    H -->|Passed / None| J[Remove Temp Directory]
    I --> K[Continue Testing]
    J --> K
    K --> L[Session Finish]
    L --> M{Cleanup Base Directory if Needed}
    M --> N[Remove Dead Symlinks]
    N --> O[End Test Session]

This flowchart visualizes the lifecycle of temporary path creation and management per test and session, highlighting configuration, usage, retention decisions, and cleanup steps.