Skip to content

Commit

Permalink
dependency: harmonize string representation
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering authored and neersighted committed Jun 6, 2022
1 parent 3bf7ad0 commit 2bfcbe4
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 27 deletions.
4 changes: 4 additions & 0 deletions src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 0 additions & 7 deletions src/poetry/core/packages/directory_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
6 changes: 0 additions & 6 deletions src/poetry/core/packages/file_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
3 changes: 0 additions & 3 deletions src/poetry/core/packages/url_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
11 changes: 0 additions & 11 deletions src/poetry/core/packages/vcs_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
33 changes: 33 additions & 0 deletions tests/packages/test_directory_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"),
[
Expand Down
32 changes: 32 additions & 0 deletions tests/packages/test_file_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 34 additions & 0 deletions tests/packages/test_url_dependency.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
37 changes: 37 additions & 0 deletions tests/packages/test_vcs_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 2bfcbe4

Please sign in to comment.