Coverage for tests / utils / test_path_utils.py: 100%
53 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Tests for the path utilities module."""
3from pathlib import Path
4from unittest.mock import patch
6import pytest
7from assertpy import assert_that
9from lintro.utils.path_utils import normalize_file_path_for_display
12def _to_posix(path: str) -> str:
13 """Normalize path separators to forward slashes for cross-platform assertions."""
14 return path.replace("\\", "/")
17@pytest.mark.utils
18def test_normalize_file_path_for_display_absolute(
19 tmp_path: Path,
20 monkeypatch: pytest.MonkeyPatch,
21) -> None:
22 """Test normalizing an absolute path."""
23 (tmp_path / "src").mkdir()
24 (tmp_path / "src" / "file.py").touch()
26 monkeypatch.chdir(tmp_path)
27 abs_path = str(tmp_path / "src" / "file.py")
28 result = normalize_file_path_for_display(abs_path)
29 assert_that(_to_posix(result)).is_equal_to("./src/file.py")
32@pytest.mark.utils
33def test_normalize_file_path_for_display_relative(
34 tmp_path: Path,
35 monkeypatch: pytest.MonkeyPatch,
36) -> None:
37 """Test normalizing a relative path."""
38 (tmp_path / "src").mkdir()
39 (tmp_path / "src" / "file.py").touch()
41 monkeypatch.chdir(tmp_path)
42 result = normalize_file_path_for_display("src/file.py")
43 assert_that(_to_posix(result)).is_equal_to("./src/file.py")
46@pytest.mark.utils
47def test_normalize_file_path_for_display_current_dir(
48 tmp_path: Path,
49 monkeypatch: pytest.MonkeyPatch,
50) -> None:
51 """Test normalizing a file in current directory."""
52 (tmp_path / "file.py").touch()
54 monkeypatch.chdir(tmp_path)
55 result = normalize_file_path_for_display("file.py")
56 assert_that(_to_posix(result)).is_equal_to("./file.py")
59@pytest.mark.utils
60def test_normalize_file_path_for_display_parent_dir(
61 tmp_path: Path,
62 monkeypatch: pytest.MonkeyPatch,
63) -> None:
64 """Test normalizing a path that goes up directories."""
65 project_dir = tmp_path / "root"
66 project_dir.mkdir()
67 (tmp_path / "file.py").touch()
69 monkeypatch.chdir(project_dir)
70 result = normalize_file_path_for_display(str(tmp_path / "file.py"))
71 assert_that(_to_posix(result)).is_equal_to("../file.py")
74@pytest.mark.utils
75def test_normalize_file_path_for_display_already_relative(
76 tmp_path: Path,
77 monkeypatch: pytest.MonkeyPatch,
78) -> None:
79 """Test normalizing a path that already starts with './'."""
80 (tmp_path / "src").mkdir()
81 (tmp_path / "src" / "file.py").touch()
83 monkeypatch.chdir(tmp_path)
84 result = normalize_file_path_for_display("./src/file.py")
85 assert_that(_to_posix(result)).is_equal_to("./src/file.py")
88@pytest.mark.utils
89def test_normalize_file_path_for_display_error() -> None:
90 """Test handling errors in path normalization."""
91 with patch.object(
92 Path,
93 "resolve",
94 side_effect=ValueError("Invalid path"),
95 ):
96 result = normalize_file_path_for_display("invalid/path")
97 assert_that(result).is_equal_to("invalid/path")
100@pytest.mark.utils
101def test_normalize_file_path_for_display_os_error() -> None:
102 """Test handling OS errors in path normalization."""
103 with patch("os.getcwd", side_effect=OSError("Permission denied")):
104 result = normalize_file_path_for_display("src/file.py")
105 assert_that(result).is_equal_to("src/file.py")