From 0d50b39bfcf9d775653229c2a08b2baf9997072c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sun, 5 Jun 2022 18:28:03 +0200 Subject: [PATCH] dependency: harmonize string representation --- src/poetry/core/packages/dependency.py | 4 ++ .../core/packages/directory_dependency.py | 7 ---- src/poetry/core/packages/file_dependency.py | 6 --- src/poetry/core/packages/url_dependency.py | 3 -- src/poetry/core/packages/vcs_dependency.py | 11 ------ tests/packages/test_directory_dependency.py | 33 +++++++++++++++++ tests/packages/test_file_dependency.py | 32 ++++++++++++++++ tests/packages/test_url_dependency.py | 34 +++++++++++++++++ tests/packages/test_vcs_dependency.py | 37 +++++++++++++++++++ 9 files changed, 140 insertions(+), 27 deletions(-) diff --git a/src/poetry/core/packages/dependency.py b/src/poetry/core/packages/dependency.py index 4426c43d6..48a5ca88c 100644 --- a/src/poetry/core/packages/dependency.py +++ b/src/poetry/core/packages/dependency.py @@ -583,6 +583,10 @@ def __hash__(self) -> int: def __str__(self) -> str: if self.is_root: return self._pretty_name + if self.is_direct_origin(): + # adding version since this information is especially useful in debug output + parts = [p.strip() for p in self.base_pep_508_name.split("@", 1)] + return f"{parts[0]} ({self._pretty_constraint}) @ {parts[1]}" return self.base_pep_508_name def __repr__(self) -> str: diff --git a/src/poetry/core/packages/directory_dependency.py b/src/poetry/core/packages/directory_dependency.py index 460aa1710..73f547f81 100644 --- a/src/poetry/core/packages/directory_dependency.py +++ b/src/poetry/core/packages/directory_dependency.py @@ -94,10 +94,3 @@ def base_pep_508_name(self) -> str: requirement += f" @ {path}" return requirement - - def __str__(self) -> str: - if self.is_root: - return self._pretty_name - - path = self._path.as_posix() - return f"{self._pretty_name} ({self._pretty_constraint} {path})" diff --git a/src/poetry/core/packages/file_dependency.py b/src/poetry/core/packages/file_dependency.py index 0391775e3..5e6542431 100644 --- a/src/poetry/core/packages/file_dependency.py +++ b/src/poetry/core/packages/file_dependency.py @@ -82,9 +82,3 @@ def base_pep_508_name(self) -> str: requirement += f" @ {path}" return requirement - - def __str__(self) -> str: - if self.is_root: - return self._pretty_name - - return f"{self._pretty_name} ({self._pretty_constraint} {self._path})" diff --git a/src/poetry/core/packages/url_dependency.py b/src/poetry/core/packages/url_dependency.py index 21cfd0f0e..fd020d595 100644 --- a/src/poetry/core/packages/url_dependency.py +++ b/src/poetry/core/packages/url_dependency.py @@ -50,6 +50,3 @@ def base_pep_508_name(self) -> str: def is_url(self) -> bool: return True - - def __str__(self) -> str: - return f"{self._pretty_name} ({self._pretty_constraint} url)" diff --git a/src/poetry/core/packages/vcs_dependency.py b/src/poetry/core/packages/vcs_dependency.py index fc108cc9c..c69031989 100644 --- a/src/poetry/core/packages/vcs_dependency.py +++ b/src/poetry/core/packages/vcs_dependency.py @@ -126,14 +126,3 @@ def is_vcs(self) -> bool: def accepts_prereleases(self) -> bool: return True - - def __str__(self) -> str: - reference = self._vcs - if self._branch: - reference += f" branch {self._branch}" - elif self._tag: - reference += f" tag {self._tag}" - elif self._rev: - reference += f" rev {self._rev}" - - return f"{self._pretty_name} ({self._constraint} {reference})" diff --git a/tests/packages/test_directory_dependency.py b/tests/packages/test_directory_dependency.py index 4330b8a68..c21460216 100644 --- a/tests/packages/test_directory_dependency.py +++ b/tests/packages/test_directory_dependency.py @@ -10,6 +10,7 @@ DIST_PATH = Path(__file__).parent.parent / "fixtures" / "git" / "github.com" / "demo" +SAMPLE_PROJECT = Path(__file__).parent.parent / "fixtures" / "sample_project" def test_directory_dependency_must_exist() -> None: @@ -77,6 +78,38 @@ def test_directory_dependency_pep_508_extras() -> None: _test_directory_dependency_pep_508("demo", path, requirement, requirement_expected) +@pytest.mark.parametrize( + "name,path,extras,constraint,expected", + [ + ( + "my-package", + SAMPLE_PROJECT, + None, + None, + f"my-package (*) @ {SAMPLE_PROJECT.as_uri()}", + ), + ( + "my-package", + SAMPLE_PROJECT, + ["db"], + "1.2", + f"my-package[db] (1.2) @ {SAMPLE_PROJECT.as_uri()}", + ), + ], +) +def test_directory_dependency_string_representation( + name: str, + path: Path, + extras: list[str] | None, + constraint: str | None, + expected: str, +) -> None: + dependency = DirectoryDependency(name=name, path=path, extras=extras) + if constraint: + dependency.constraint = constraint # type: ignore[assignment] + assert str(dependency) == expected + + @pytest.mark.parametrize( ("fixture", "name"), [ diff --git a/tests/packages/test_file_dependency.py b/tests/packages/test_file_dependency.py index 4b8495b45..e29479b63 100644 --- a/tests/packages/test_file_dependency.py +++ b/tests/packages/test_file_dependency.py @@ -188,3 +188,35 @@ def test_file_dependency_pep_508_extras(mocker: MockerFixture) -> None: requirement, f'demo[bar,foo] @ {rel_path.as_posix()} ; sys_platform == "linux"', ) + + +@pytest.mark.parametrize( + "name,path,extras,constraint,expected", + [ + ( + "demo", + DIST_PATH / TEST_FILE, + None, + None, + f"demo (*) @ {(DIST_PATH / TEST_FILE).as_uri()}", + ), + ( + "demo", + DIST_PATH / TEST_FILE, + ["foo"], + "1.2", + f"demo[foo] (1.2) @ {(DIST_PATH / TEST_FILE).as_uri()}", + ), + ], +) +def test_file_dependency_string_representation( + name: str, + path: Path, + extras: list[str] | None, + constraint: str | None, + expected: str, +) -> None: + dependency = FileDependency(name=name, path=path, extras=extras) + if constraint: + dependency.constraint = constraint # type: ignore[assignment] + assert str(dependency) == expected diff --git a/tests/packages/test_url_dependency.py b/tests/packages/test_url_dependency.py index 797d44275..08697f85f 100644 --- a/tests/packages/test_url_dependency.py +++ b/tests/packages/test_url_dependency.py @@ -1,5 +1,7 @@ from __future__ import annotations +import pytest + from poetry.core.packages.url_dependency import URLDependency from poetry.core.version.markers import SingleMarker @@ -44,3 +46,35 @@ def test_to_pep_508_with_marker() -> None: ' ; sys_platform == "linux"' ) assert dependency.to_pep_508() == expected + + +@pytest.mark.parametrize( + "name,url,extras,constraint,expected", + [ + ( + "example", + "https://example.org/example.whl", + None, + None, + "example (*) @ https://example.org/example.whl", + ), + ( + "example", + "https://example.org/example.whl", + ["foo"], + "1.2", + "example[foo] (1.2) @ https://example.org/example.whl", + ), + ], +) +def test_directory_dependency_string_representation( + name: str, + url: str, + extras: list[str] | None, + constraint: str | None, + expected: str, +) -> None: + dependency = URLDependency(name=name, url=url, extras=extras) + if constraint: + dependency.constraint = constraint # type: ignore[assignment] + assert str(dependency) == expected diff --git a/tests/packages/test_vcs_dependency.py b/tests/packages/test_vcs_dependency.py index 28a0987d8..e849b726d 100644 --- a/tests/packages/test_vcs_dependency.py +++ b/tests/packages/test_vcs_dependency.py @@ -71,6 +71,43 @@ def test_to_pep_508_in_extras() -> None: assert dependency.to_pep_508() == expected +@pytest.mark.parametrize( + "name,source,branch,extras,constraint,expected", + [ + ( + "example", + "https://example.org/example.git", + "main", + None, + None, + "example (*) @ git+https://example.org/example.git@main", + ), + ( + "example", + "https://example.org/example.git", + "main", + ["foo"], + "1.2", + "example[foo] (1.2) @ git+https://example.org/example.git@main", + ), + ], +) +def test_directory_dependency_string_representation( + name: str, + source: str, + branch: str, + extras: list[str] | None, + constraint: str | None, + expected: str, +) -> None: + dependency = VCSDependency( + name=name, vcs="git", source=source, branch=branch, extras=extras + ) + if constraint: + dependency.constraint = constraint # type: ignore[assignment] + assert str(dependency) == expected + + @pytest.mark.parametrize("groups", [["main"], ["dev"]]) def test_category(groups: list[str]) -> None: dependency = VCSDependency(