test_tmpdir.py


Overview

The [test_tmpdir.py](/projects/286/67363) file is a comprehensive test suite focused on validating the temporary directory handling features of the `pytest` testing framework, specifically around the `tmp_path` and `tmp_path_factory` fixtures. These fixtures provide temporary filesystem locations for tests to use, ensuring isolation and cleanup between test runs.

This file contains various tests that cover:

The tests often leverage the `pytester` utility fixture, which facilitates running pytest sessions programmatically and inspecting their outcomes.


Classes and Functions

FakeConfig

A minimal dataclass simulating a subset of the `Config` interface required by `TempPathFactory`.

Attributes:

Properties and Methods:

**Usage example:**

config = FakeConfig("/tmp/pytest")
print(config.getini("tmp_path_retention_count"))  # Output: 3

TestTmpPathHandler

Test class for verifying the behavior of `TempPathFactory` and `tmp_path` fixture handling.

Methods:


TestConfigTmpPath

Tests related to temporary directory retention policies and cleanup behavior configured via pytest options.

Key test methods:


Parameterized Test: test_mktemp

A parameterized test checking valid and invalid directory base names when creating temp directories with `tmp_path_factory.mktemp()` with `numbered=False`.


Utility Function: attempt_symlink_to(path, to_path)

Attempts to create a symbolic link at `path` pointing to `to_path`. If the operation fails due to platform limitations or permissions (common on Windows), the test is skipped.


Classes Testing Directory Numbering and Cleanup

TestNumberedDir

Tests the behavior of creating numbered directories with a prefix and cleaning up old numbered directories.


Class: TestRmRf

Tests the `rm_rf` function that forcibly removes files and directories, including handling read-only files and directories.


Miscellaneous Tests


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

The following Mermaid class diagram summarizes the main test classes and their key methods in this file, illustrating their roles and relationships:

classDiagram
    class FakeConfig {
        +basetemp: str | Path
        +trace()
        +get(key)
        +getini(name)
        +option()
    }

    class TestTmpPathHandler {
        +test_mktemp(tmp_path: Path)
        +test_tmppath_relative_basetemp_absolute(tmp_path: Path, monkeypatch: MonkeyPatch)
    }

    class TestConfigTmpPath {
        +test_getbasetemp_custom_removes_old(pytester: Pytester)
        +test_policy_failed_removes_only_passed_dir(pytester: Pytester)
        +test_policy_failed_removes_basedir_when_all_passed(pytester: Pytester)
        +test_policy_failed_removes_dir_when_skipped_from_fixture(pytester: Pytester)
        +test_policy_all_keeps_dir_when_skipped_from_fixture(pytester: Pytester)
    }

    class TestNumberedDir {
        +test_make(tmp_path: Path)
        +test_cleanup_lock_create(tmp_path: Path)
        +test_lock_register_cleanup_removal(tmp_path: Path)
        +test_cleanup_keep(tmp_path: Path)
        +test_cleanup_keep_0(tmp_path: Path)
        +test_cleanup_locked(tmp_path: Path)
        +test_cleanup_ignores_symlink(tmp_path: Path)
        +test_removal_accepts_lock(tmp_path: Path)
    }

    class TestRmRf {
        +test_rm_rf(tmp_path: Path)
        +test_rm_rf_with_read_only_file(tmp_path: Path)
        +test_rm_rf_with_read_only_directory(tmp_path: Path)
        +test_on_rm_rf_error(tmp_path: Path)
        +chmod_r(path)
    }

    FakeConfig <|-- TestTmpPathHandler : uses
    TestConfigTmpPath --> pytester : uses
    TestNumberedDir --> pathlib : uses utilities
    TestRmRf --> pathlib : uses rm_rf and error handling

Summary

The [test_tmpdir.py](/projects/286/67363) file is a critical test suite ensuring that pytest's temporary directory management works reliably across various environments, configurations, and edge cases. It validates creation, cleanup, permission handling, and retention policies of temporary directories, providing confidence that tests using `tmp_path` and `tmp_path_factory` fixtures can safely operate on isolated filesystem locations without interference or resource leakage.