|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from poetry.utils import helpers |
| 9 | + |
| 10 | + |
| 11 | +def test_merge_dicts_merges_correctly() -> None: |
| 12 | + d1 = {"a": 1, "b": {"x": 1}} |
| 13 | + d2 = {"b": {"y": 2}, "c": 3} |
| 14 | + helpers.merge_dicts(d1, d2) |
| 15 | + assert d1 == {"a": 1, "b": {"x": 1, "y": 2}, "c": 3} |
| 16 | + |
| 17 | + |
| 18 | +def test_paths_csv_returns_comma_separated_string(tmp_path: Path) -> None: |
| 19 | + paths = [tmp_path / "a.txt", tmp_path / "b.txt"] |
| 20 | + result = helpers.paths_csv(paths) |
| 21 | + assert "a.txt" in result and "b.txt" in result |
| 22 | + assert "," in result |
| 23 | + |
| 24 | + |
| 25 | +def test_ensure_path_creates_directory(tmp_path: Path) -> None: |
| 26 | + path = tmp_path / "new_dir" |
| 27 | + path.mkdir() |
| 28 | + result = helpers.ensure_path(path, is_directory=True) |
| 29 | + assert result.exists() |
| 30 | + assert result.is_dir() |
| 31 | + |
| 32 | + |
| 33 | +def test_is_dir_writable_returns_true(tmp_path: Path) -> None: |
| 34 | + assert helpers.is_dir_writable(tmp_path) is True |
| 35 | + |
| 36 | + |
| 37 | +def test_pluralize_singular_and_plural() -> None: |
| 38 | + # Actual behavior: returns the word only, not "1 file" |
| 39 | + assert helpers.pluralize(1, "file") == "file" |
| 40 | + assert helpers.pluralize(3, "file") == "files" |
| 41 | + |
| 42 | + |
| 43 | +def test_get_file_hash(tmp_path: Path) -> None: |
| 44 | + file = tmp_path / "data.txt" |
| 45 | + file.write_text("hello") |
| 46 | + hash1 = helpers.get_file_hash(file) |
| 47 | + hash2 = helpers.get_file_hash(file) |
| 48 | + assert isinstance(hash1, str) |
| 49 | + assert hash1 == hash2 # deterministic |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.skipif(os.name != "nt", reason="Windows-only helper") |
| 53 | +def test_get_real_windows_path(tmp_path: Path) -> None: |
| 54 | + # On Windows, returns a Path with correct case |
| 55 | + path = helpers.get_real_windows_path(tmp_path) |
| 56 | + assert isinstance(path, Path) |
| 57 | + |
| 58 | + |
| 59 | +def test_remove_directory_deletes(tmp_path: Path) -> None: |
| 60 | + dir_to_remove = tmp_path / "deleteme" |
| 61 | + dir_to_remove.mkdir() |
| 62 | + (dir_to_remove / "file.txt").write_text("bye") |
| 63 | + helpers.remove_directory(dir_to_remove) |
| 64 | + assert not dir_to_remove.exists() |
| 65 | + |
| 66 | + |
| 67 | +def test_directory_iterator(tmp_path: Path) -> None: |
| 68 | + sub = tmp_path / "nested" |
| 69 | + sub.mkdir() |
| 70 | + (sub / "a.txt").write_text("ok") |
| 71 | + |
| 72 | + # directory() is a context manager, not an iterator |
| 73 | + with helpers.directory(tmp_path) as path: |
| 74 | + assert path == tmp_path |
| 75 | + assert path.exists() |
0 commit comments