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

support specifying algorithm when calculating file dependency hash. #196

Closed
Closed
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
4 changes: 2 additions & 2 deletions poetry/core/packages/file_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def full_path(self) -> Path:
def is_file(self) -> bool:
return True

def hash(self) -> str:
h = hashlib.sha256()
def hash(self, algorithm: str = "sha256") -> str:
h = hashlib.new(algorithm)
with self._full_path.open("rb") as fp:
for content in iter(lambda: fp.read(io.DEFAULT_BUFFER_SIZE), b""):
h.update(content)
Expand Down
27 changes: 27 additions & 0 deletions tests/packages/test_file_dependency.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import contextmanager
from io import BytesIO
from pathlib import Path

import pytest
Expand Down Expand Up @@ -97,3 +99,28 @@ def test_relative_file_dependency_to_pep_508_with_marker(mocker):
requirement,
marker=SingleMarker("sys.platform", "linux"),
)


@pytest.mark.parametrize(
"algorithm, content, file_hash",
[
(
"sha256",
b"file contents",
"7bb6f9f7a47a63e684925af3608c059edcc371eb81188c48c9714896fb1091fd",
),
("md5", b"file contents", "4a8ec4fa5f01b4ab1a0ab8cbccb709f0"),
],
)
def test_file_dependency_hash(mocker, algorithm, content, file_hash):
@contextmanager
def mock_file_contents(*_):
yield BytesIO(content)

path = DIST_PATH / "demo-0.2.0.tar.gz"
mocker.patch.object(Path, "exists").return_value = True
mocker.patch.object(Path, "is_file").return_value = True
mocker.patch.object(Path, "open", mock_file_contents)

file_dep = FileDependency("demo", path)
assert file_dep.hash(algorithm) == file_hash