Skip to content

Commit 93de237

Browse files
committed
Add unit tests for tests/helpers.py to improve coverage (#9161)
1 parent 17db1da commit 93de237

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

tests/test_helpers_additional.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# mypy: disable-error-code="attr-defined"
2+
3+
import os
4+
from pathlib import Path
5+
import pytest
6+
7+
from poetry.utils import helpers
8+
9+
10+
def test_get_package_returns_package() -> None:
11+
package = helpers.get_package("demo", "1.0.0")
12+
assert package.name == "demo"
13+
assert str(package.version) == "1.0.0"
14+
15+
16+
def test_with_working_directory_copy_and_remove(tmp_path: Path) -> None:
17+
base_dir = tmp_path / "work"
18+
base_dir.mkdir()
19+
file = base_dir / "file.txt"
20+
file.write_text("data")
21+
22+
with helpers.with_working_directory(base_dir):
23+
assert Path("file.txt").exists()
24+
25+
assert not (Path.cwd() / "file.txt").exists()
26+
27+
28+
def test_pbs_installer_supported_arch_variants() -> None:
29+
variants = helpers.pbs_installer_supported_arch_variants("x86_64")
30+
assert "x86_64" in variants
31+
assert "amd64" in variants
32+
33+
34+
def test_isolated_environment_adds_and_clears(tmp_path: Path) -> None:
35+
env_dir = tmp_path / "env"
36+
env_dir.mkdir()
37+
38+
with helpers.isolated_environment(env_dir):
39+
assert Path.cwd() == env_dir
40+
41+
assert Path.cwd() != env_dir
42+
43+
44+
def test_flatten_dict_nested() -> None:
45+
data = {"a": {"b": {"c": 1}}}
46+
flat = helpers.flatten_dict(data)
47+
assert flat == {"a.b.c": 1}
48+
49+
50+
def test_copy_path_file_and_dir(tmp_path: Path) -> None:
51+
src_dir = tmp_path / "src"
52+
dst_dir = tmp_path / "dst"
53+
src_dir.mkdir()
54+
file = src_dir / "test.txt"
55+
file.write_text("hello")
56+
57+
helpers.copy_path(src_dir, dst_dir)
58+
assert (dst_dir / "test.txt").exists()
59+
assert (dst_dir / "test.txt").read_text() == "hello"
60+
61+
62+
def test_get_dependency_with_string_constraint() -> None:
63+
dep = helpers.get_dependency("requests", "^2.0")
64+
assert dep.name == "requests"
65+
assert str(dep.constraint) == "^2.0"
66+
67+
68+
def test_switch_working_directory_creates_and_restores(tmp_path: Path) -> None:
69+
original = Path.cwd()
70+
new_dir = tmp_path / "work"
71+
new_dir.mkdir()
72+
73+
with helpers.switch_working_directory(new_dir):
74+
assert Path.cwd() == new_dir
75+
76+
assert Path.cwd() == original

0 commit comments

Comments
 (0)