test_link_resolve.py


Overview

`test_link_resolve.py` is a test module designed to validate how file paths are resolved and displayed when running pytest on files accessed via substituted or symlinked paths. The core purpose is to ensure that error reports generated by pytest show relative paths instead of fully resolved absolute paths. This behavior addresses a specific issue tracked in the pytest project ([issue #5965](https://github.com/pytest-dev/pytest/issues/5965)).

The file achieves this by:

This test ensures pytest’s error reporting respects virtual file system paths rather than resolving them to canonical absolute paths, improving clarity in test output.


Detailed Explanations

Context Managers

These context managers temporarily create a virtual or substituted path pointing to a directory containing test files. They yield a new path that can be used to run pytest.


subst_path_windows(filepath: Path) -> Iterator[Path]

**Purpose:** Creates a temporary `subst` drive letter (from H: to Z:) that maps to the directory of `filepath`. This allows testing how pytest behaves when files are accessed via a substituted drive path on Windows.

**Parameters:**

**Yields:**

**Behavior:**

**Example Usage:**

with subst_path_windows(Path(r"C:\Users\example\test_foo.py")) as subst_path:
    # subst_path might be something like H:\test_foo.py
    run_pytest_on(subst_path)

subst_path_linux(filepath: Path) -> Iterator[Path]

**Purpose:** Creates a symbolic link `sub2` pointing to the directory of `filepath`. This simulates a substituted or linked path under Linux for testing purposes.

**Parameters:**

**Yields:**

**Behavior:**

**Example Usage:**

with subst_path_linux(Path("/home/user/project/sub1/test_foo.py")) as subst_path:
    # subst_path might be /home/user/project/sub2/test_foo.py
    run_pytest_on(subst_path)

Test Function

test_link_resolve(pytester: Pytester) -> None

**Purpose:** Tests that pytest reports error paths relative to the substituted or symlinked path rather than the fully resolved absolute path.

**Parameters:**

**Returns:**

**Detailed Steps:**

  1. Creates a new test directory sub1 with a test file test_foo.py that always fails with an AssertionError.

  2. Depending on the platform, selects the appropriate substitution context manager (subst_path_windows or subst_path_linux).

  3. Uses the substitution manager to get a virtual path to the test file.

  4. Runs pytest on this virtual path with verbosity enabled (-v).

  5. Captures the output and asserts:

    • The error message does not contain the fully resolved relative path "sub1/test_foo.py".

    • The error message does contain the substituted path:

      • On Windows: the path with the new drive letter.

      • On Linux: the symlink path "sub2/test_foo.py".

**Usage Example:**

This test is intended to be run automatically as part of pytest’s test suite:

pytest test_link_resolve.py

Important Implementation Details & Algorithms


Interaction with Other Parts of the System

This file is part of the pytest test suite and serves as a regression test for a specific issue in path reporting.


Mermaid Class Diagram

classDiagram
    class subst_path_windows {
        +filepath: Path
        +__enter__(): Path
        +__exit__(): None
    }

    class subst_path_linux {
        +filepath: Path
        +__enter__(): Path
        +__exit__(): None
    }

    class test_link_resolve {
        +pytester: Pytester
        +__call__(): None
    }

    subst_path_windows ..> Path : uses
    subst_path_linux ..> Path : uses
    test_link_resolve ..> Pytester : uses
    test_link_resolve ..> subst_path_windows : calls on win32
    test_link_resolve ..> subst_path_linux : calls on linux

Summary

The combination of OS-specific path substitution and pytest execution allows this test to serve as a robust regression test for a known issue in pytest path resolution.