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

Simplify/improve Package.clone() #159

Merged
merged 1 commit into from
Apr 29, 2021
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
29 changes: 2 additions & 27 deletions poetry/core/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,33 +428,8 @@ def without_features(self) -> "Package":
return self.with_features([])

def clone(self) -> "Package":
if self.is_root():
clone = self.__class__(self.pretty_name, self.version)
else:
clone = self.__class__(
self.pretty_name,
self.version,
source_type=self._source_type,
source_url=self._source_url,
source_reference=self._source_reference,
features=list(self.features),
)

clone.description = self.description
clone.category = self.category
clone.optional = self.optional
clone.python_versions = self.python_versions
clone.marker = self.marker
clone.extras = self.extras
clone.root_dir = self.root_dir
clone.develop = self.develop

for dep in self.requires:
clone.requires.append(dep)

for dep in self.dev_requires:
clone.dev_requires.append(dep)

clone = self.__class__(self.pretty_name, self.version)
clone.__dict__ = copy.deepcopy(self.__dict__)
return clone

def __hash__(self) -> int:
Expand Down
29 changes: 29 additions & 0 deletions tests/packages/test_package.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import unicode_literals

import random

from pathlib import Path

import pytest
Expand Down Expand Up @@ -280,3 +282,30 @@ def test_to_dependency_for_url():
assert "https://example.com/path.tar.gz" == dep.url
assert "url" == dep.source_type
assert "https://example.com/path.tar.gz" == dep.source_url


def test_package_clone(f):
# TODO(nic): this test is not future-proof, in that any attributes added
# to the Package object and not filled out in this test setup might
# cause comparisons to match that otherwise should not. A factory method
# to create a Package object with all fields fully randomized would be the
# most rigorous test for this, but that's likely overkill.
p = Package(
"lol_wut",
"3.141.5926535",
pretty_version="③.⑭.⑮",
source_type="git",
source_url="http://some.url",
source_reference="fe4d2adabf3feb5d32b70ab5c105285fa713b10c",
source_resolved_reference="fe4d2adabf3feb5d32b70ab5c105285fa713b10c",
features=["abc", "def"],
develop=random.choice((True, False)),
)
p.files = (["file1", "file2", "file3"],)
p.homepage = "https://some.other.url"
p.repository_url = "http://bug.farm"
p.documentation_url = "http://lorem.ipsum/dolor/sit.amet"
p2 = p.clone()

assert p == p2
assert p.__dict__ == p2.__dict__