Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve package equality check and repr #53

Merged
merged 1 commit into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion poetry/core/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,33 @@ def __eq__(self, other):
if not isinstance(other, Package):
return NotImplemented

if self.source_type in ["file", "directory", "url", "git"]:
if self.source_type != other.source_type:
return False

if self.source_url or other.source_url:
if self.source_url != other.source_url:
return False

if self.source_reference or other.source_reference:
# special handling for packages with references
if not self.source_reference or not other.source_reference:
# case: one reference is defined and is non-empty, but other is not
return False

if not (
self.source_reference == other.source_reference
or self.source_reference.startswith(other.source_reference)
or other.source_reference.startswith(self.source_reference)
):
# case: both references defined, but one is not equal to or a short
# representation of the other
return False

return self._name == other.name and self._version == other.version

def __str__(self):
return self.unique_name

def __repr__(self):
return "<Package {}>".format(self.unique_name)
return "<Package {} {}>".format(self.name, self.full_pretty_version)
74 changes: 74 additions & 0 deletions tests/packages/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,77 @@ def test_package_url_category_optional(category, optional):
)
assert dependency.category == category
assert dependency.is_optional() == optional


def test_package_equality_simple():
assert Package("foo", "0.1.0") == Package("foo", "0.1.0")
assert Package("foo", "0.1.0") != Package("foo", "0.1.1")
assert Package("bar", "0.1.0") != Package("foo", "0.1.0")


def test_package_equality_source_type():
a1 = Package("a", "0.1.0")
a1.source_type = "file"

a2 = Package(a1.name, a1.version)
a2.source_type = "directory"

a3 = Package(a1.name, a1.version)
a3.source_type = a1.source_type

a4 = Package(a1.name, a1.version)

assert a1 == a1
assert a1 == a3
assert a1 != a2
assert a2 != a3
assert a1 != a4
assert a2 != a4


def test_package_equality_source_url():
a1 = Package("a", "0.1.0")
a1.source_type = "file"
a1.source_url = "/some/path"

a2 = Package(a1.name, a1.version)
a2.source_type = a1.source_type
a2.source_url = "/some/other/path"

a3 = Package(a1.name, a1.version)
a3.source_type = a1.source_type
a3.source_url = a1.source_url

a4 = Package(a1.name, a1.version)
a4.source_type = a1.source_type

assert a1 == a1
assert a1 == a3
assert a1 != a2
assert a2 != a3
assert a1 != a4
assert a2 != a4


def test_package_equality_source_reference():
a1 = Package("a", "0.1.0")
a1.source_type = "git"
a1.source_reference = "c01b317af582501c5ba07b23d5bef3fbada2d4ef"

a2 = Package(a1.name, a1.version)
a2.source_type = a1.source_type
a2.source_reference = "a444731cd243cb5cd04e4d5fb81f86e1fecf8a00"

a3 = Package(a1.name, a1.version)
a3.source_type = a1.source_type
a3.source_reference = a1.source_reference

a4 = Package(a1.name, a1.version)
a4.source_type = a1.source_type

assert a1 == a1
assert a1 == a3
assert a1 != a2
assert a2 != a3
assert a1 != a4
assert a2 != a4