test_local.py


Overview

`test_local.py` is a comprehensive test suite designed to validate the functionality, robustness, and correctness of the `py.path.local` module (commonly imported as `local` from the `py` library). This module provides a high-level, object-oriented interface to filesystem paths and operations, allowing manipulation and querying of files and directories in a platform-independent manner.

The test file exercises a broad range of features including path construction, equality, joining, file and directory operations (creation, removal, copying, moving), filesystem attributes (mtime, size, ownership), pattern matching, import-related utilities, symbolic links, permission handling, and platform-specific behaviors. It also tests interactions with Python's import system and ensures compatibility with newer Python features like the [os.PathLike](/projects/286/67409) protocol.

In essence, this file serves as a quality assurance layer that guarantees the correctness of the `local` path abstraction and its integration in various filesystem scenarios and Python runtime environments.


Detailed Documentation

Modules and Imports


Context Manager

ignore_encoding_warning()

A context manager that suppresses [EncodingWarning](/projects/286/67223) on Python 3.10+ during file read/write operations. This is useful for tests that perform encoding-sensitive operations and want to avoid noisy warnings.

**Usage Example:**

with ignore_encoding_warning():
    content = path.join("samplefile").read("r")

Class: CommonFSTests

This class contains a large collection of test methods that verify the core filesystem operations and path manipulations on `local` path instances. The tests are designed to be reusable for different path implementations by passing fixtures such as `path1`.

Key Test Methods & Their Purpose:

**Usage:**

`CommonFSTests` is a base test class designed to be inherited or instantiated by specific test classes that provide the `path1` fixture.


Function: setuptestfs(path)

Initializes a sample filesystem structure under the given `path` for testing purposes. It creates files, directories, Python modules, and a pickle file with predefined content. This setup ensures that tests have a consistent and known environment.

**Details:**

**Usage Example:**

setuptestfs(tmpdir)

Fixtures


Utility Function: batch_make_numbered_dirs(rootdir, repeats)

Creates multiple numbered directories under `rootdir` using `local.make_numbered_dir`. For each directory, it writes a file containing the directory number and verifies the content. Also removes any `.lock` files to simulate unlocked state.

**Parameters:**

**Returns:**


Class: TestLocalPath(CommonFSTests)

Extends `CommonFSTests` with additional tests specific to the `local` path class. These include:


Class: TestExecutionOnWindows

Tests Windows-specific behaviors, such as the precedence of `.bat` files over `.exe` in `sysfind`.


Class: TestExecution

Tests execution-related features on non-Windows platforms:


Class: TestImport

Verifies Python module import functionality from paths, including:


Class: TestImportlibImport

Similar to `TestImport` but uses `importlib` for module importing, testing failure cases and isolation from `sys.modules`.


Additional Tests


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram: Class Structure and Main Test Classes

classDiagram
    class CommonFSTests {
        +test_constructor_equality(path1)
        +test_join(path1)
        +test_exists(path1)
        +test_listdir(path1)
        +test_visit_nofilter(path1)
        +test_copy_file(path1)
        +test_remove_file(path1)
        +test_move_file(path1)
        +test_fspath_protocol_match_strpath(path1)
        +... (many other test methods)
    }

    class TestLocalPath {
        +test_join_normpath(tmpdir)
        +test_gethash(tmpdir)
        +test_remove_removes_readonly_file(tmpdir)
        +test_chdir(tmpdir)
        +test_make_numbered_dir_multiprocess_safe(tmpdir)
        +... (inherits CommonFSTests)
    }

    class TestExecution {
        +test_sysfind_no_permission_ignored(monkeypatch, tmpdir)
        +test_sysfind_absolute()
        +test_sysexec()
        +test_make_numbered_dir(tmpdir)
        +... (additional execution tests)
    }

    class TestImport {
        +test_pyimport(path1)
        +test_pyimport_renamed_dir_creates_mismatch(tmpdir, monkeypatch)
        +test_pyimport_dir(tmpdir)
        +test_pyimport_and_import(tmpdir)
        +... (import-related tests)
    }

    CommonFSTests <|-- TestLocalPath

Summary

`test_local.py` is a large-scale, detailed test suite ensuring the stability and correctness of the `py.path.local` filesystem path abstraction. It covers a wide spectrum of functionality, from basic path manipulation to complex import mechanisms and platform-specific behaviors. The tests serve both as verification and documentation of expected behaviors, providing confidence in the module's operation across diverse environments and use cases.