test_findpaths.py
Overview
`test_findpaths.py` is a test suite module primarily designed to validate and verify the behavior of path and configuration-related utility functions in the `_pytest.config.findpaths` module. These utilities facilitate common filesystem path manipulations and configuration file parsing used internally by the pytest framework for test discovery and configuration loading.
The file contains multiple test classes and standalone test functions that:
Check the correct loading and parsing of various pytest configuration files (
.ini,.cfg,.toml).Verify the logic to find a common ancestor directory among a list of paths.
Confirm correct extraction of directories from mixed argument lists.
Test detection of filesystem root directories across platforms.
This testing module uses `pytest` as the testing framework and makes extensive use of temporary filesystem paths (`tmp_path`) to isolate test cases.
Detailed Explanation of Classes and Functions
Class: TestLoadConfigDictFromFile
This test class verifies the function [load_config_dict_from_file()](/projects/286/67332) from `_pytest.config.findpaths`. This function attempts to parse configuration files and extract pytest-related configuration sections as dictionaries.
**Test methods:**
test_empty_pytest_ini(self, tmp_path: Path) -> None
Ensures an emptypytest.inifile returns an empty dictionary, showing that even emptypytest.inifiles are considered configurations.test_pytest_ini(self, tmp_path: Path) -> None
Tests that[pytest]sections inpytest.inifiles are read correctly into a dict.test_custom_ini(self, tmp_path: Path) -> None
Verifies that.inifiles other thanpytest.iniare parsed if they contain[pytest]section.test_custom_ini_without_section(self, tmp_path: Path) -> None
Confirms.inifiles without a[pytest]section are ignored (returnNone).test_custom_cfg_file(self, tmp_path: Path) -> None
Checks that.cfgfiles without[tool:pytest]section are ignored.test_valid_cfg_file(self, tmp_path: Path) -> None
Validates that.cfgfiles with[tool:pytest]section are parsed correctly.test_unsupported_pytest_section_in_cfg_file(self, tmp_path: Path) -> None
Ensures.cfgfiles with deprecated[pytest]sections raise an error to alert users.test_invalid_toml_file(self, tmp_path: Path) -> None
Tests that invalid.tomlfiles raise aUsageError.test_custom_toml_file(self, tmp_path: Path) -> None
Checks.tomlfiles without[tool.pytest.ini_options]section are ignored.test_valid_toml_file(self, tmp_path: Path) -> None
Validates parsing of.tomlfiles with[tool.pytest.ini_options]section, including conversion of values to strings or lists as needed.
**Usage example:**
fn = tmp_path / "pytest.ini"
fn.write_text("[pytest]\nx=1", encoding="utf-8")
config = load_config_dict_from_file(fn)
assert config == {"x": "1"}
Class: TestCommonAncestor
This class tests the function `get_common_ancestor()` from `_pytest.config.findpaths`, which determines the nearest common ancestor directory from a list of paths, relative to a given current working directory.
**Test methods:**
test_has_ancestor(self, tmp_path: Path) -> None
Validates that files in different subdirectories correctly resolve to a common ancestor directory.test_single_dir(self, tmp_path: Path) -> None
Tests that when a single directory is provided, it is returned as the common ancestor.test_single_file(self, tmp_path: Path) -> None
Tests that when a single file is provided, its parent directory is returned as the common ancestor.
**Usage example:**
ancestor = get_common_ancestor(Path.cwd(), [path1, path2])
print(ancestor) # Outputs nearest common parent directory Path object
Function: test_get_dirs_from_args(tmp_path)
This standalone test function verifies `get_dirs_from_args()` which filters and returns valid directory paths from a list of arguments (which can mix files, directories, and options).
It asserts that non-existent paths and non-directory arguments (like options) are skipped.
Files' parent directories are included.
**Usage example:**
dirs = get_dirs_from_args(["/some/file.py", "/nonexistent", "/dir", "--option=foo"])
# Returns list of Path objects for existing directories related to args (file parents and dirs)
Function: test_is_fs_root(path: Path, expected: bool)
Parameterized test for `is_fs_root()` which checks if a given path is the root of the filesystem.
Tests various platform-specific root paths, including Windows drive letters and Unix root
/.Expected output is a boolean whether the path is filesystem root.
**Usage example:**
is_root = is_fs_root(Path("/"))
print(is_root) # True on Unix-like OS
Important Implementation Details and Algorithms
Configuration file parsing (
load_config_dict_from_file):
Supports multiple config file formats —.ini,.cfg,.toml..inifiles must have[pytest]section..cfgfiles require[tool:pytest]section (older[pytest]is deprecated and triggers failure)..tomlfiles require[tool.pytest.ini_options]section.
Parsing involves reading these sections and returning key-value pairs with type conversions (e.g., int/float converted to strings for compatibility).
Common ancestor calculation (
get_common_ancestor):
Determines the nearest common directory containing all the given files/directories, relative to a base directory.
This involves path normalization and iterative comparison of directory parts.Directory extraction (
get_dirs_from_args):
Filters input argument list to extract only existing directories or parent directories of existing files, skipping non-path options.Filesystem root detection (
is_fs_root):
Checks if a given path is the root directory by comparing it against known filesystem root patterns (e.g.,/on Unix,C:\on Windows).
Interaction with Other System Components
The tested functions are core utilities in pytest’s internal configuration and path resolution mechanisms.
load_config_dict_from_fileis used by pytest to load configuration options from various file formats.get_common_ancestoris critical for test discovery to determine the root directory that encompasses all test files.get_dirs_from_argsis used to process command line arguments passed to pytest for test path selection.is_fs_rootsupports path boundary checks during directory traversal.
This test module ensures the reliability of these foundational components, which in turn affect pytest’s ability to find tests and read configuration correctly.
Mermaid Diagram: Flowchart of Main Test Functions and Their Relationships
flowchart TD
A[TestLoadConfigDictFromFile]
B[TestCommonAncestor]
C[test_get_dirs_from_args]
D[test_is_fs_root]
A -->|Tests| load_config_dict_from_file
B -->|Tests| get_common_ancestor
C -->|Tests| get_dirs_from_args
D -->|Tests| is_fs_root
Summary
`test_findpaths.py` is a comprehensive pytest-based test suite that rigorously validates critical path and config parsing utilities within pytest’s internal modules. By covering multiple file formats, edge cases, and platform differences, it ensures robust and predictable behavior in pytest’s test collection and configuration loading processes. This testing foundation contributes significantly to the overall reliability and user experience of the pytest framework.