Skip to content

Commit

Permalink
fix: Invalid hash message, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
serverwentdown committed Oct 9, 2021
1 parent 38aaae8 commit 25adda8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
5 changes: 3 additions & 2 deletions poetry/installation/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,16 +672,17 @@ def _download_link(self, operation: Union[Install, Update], link: Link) -> Link:
archive = self._chef.prepare(archive)

if package.files:
path = url_to_path(archive.url) if isinstance(archive, Link) else archive
archive_hash = (
"sha256:"
+ FileDependency(
package.name,
url_to_path(archive.url) if isinstance(archive, Link) else archive,
path,
).hash()
)
if archive_hash not in {f["hash"] for f in package.files}:
raise RuntimeError(
f"Invalid hash for {package} using archive {archive.name}"
f"Invalid hash for {package} using archive {path.name}"
)

self._hashes[package.name] = archive_hash
Expand Down
35 changes: 9 additions & 26 deletions tests/installation/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from poetry.config.config import Config
from poetry.core.packages.package import Package
from poetry.core.packages.utils.link import Link
from poetry.core.utils._compat import PY36
from poetry.installation.executor import Executor
from poetry.installation.operations import Install
Expand Down Expand Up @@ -474,51 +475,33 @@ def test_executor_should_hash_links(config, io, pool, mocker, fixture_dir, tmp_d
.joinpath("demo-0.1.0-py2.py3-none-any.whl")
.as_uri()
)
mocker.patch.object(
Chef,
"get_cached_archive_for_link",
mocker.patch(
"poetry.installation.chef.Chef.get_cached_archive_for_link",
side_effect=lambda _: link,
)

env = MockEnv(path=Path(tmp_dir))
executor = Executor(env, pool, config, io)

package = Package("demo", "0.1.0")
package.files = [
{
"file": "demo-0.1.0-py2.py3-none-any.whl",
"hash": "md5:15507846fd4299596661d0197bfb4f90",
}
]

archive = executor._download_link(
Install(package), Link("https://example.com/demo-0.1.0-py2.py3-none-any.whl")
Install(Package("demo", "0.1.0")),
Link("https://example.com/demo-0.1.0-py2.py3-none-any.whl"),
)

assert archive == link


def test_executor_should_hash_paths(config, io, pool, mocker, fixture_dir, tmp_dir):
link = fixture_dir("distributions").joinpath("demo-0.1.0-py2.py3-none-any.whl")
mocker.patch.object(
Chef,
"get_cached_archive_for_link",
mocker.patch(
"poetry.installation.chef.Chef.get_cached_archive_for_link",
side_effect=lambda _: link,
)

env = MockEnv(path=Path(tmp_dir))
executor = Executor(env, pool, config, io)

package = Package("demo", "0.1.0")
package.files = [
{
"file": "demo-0.1.0-py2.py3-none-any.whl",
"hash": "md5:15507846fd4299596661d0197bfb4f90",
}
]

archive = executor._download_link(
Install(package), Link("https://example.com/demo-0.1.0-py2.py3-none-any.whl")
Install(Package("demo", "0.1.0")),
Link("https://example.com/demo-0.1.0-py2.py3-none-any.whl"),
)

assert archive == link
11 changes: 11 additions & 0 deletions tests/utils/test_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pytest

from poetry.utils.pip import pip_install
from poetry.core.packages.utils.link import Link
from poetry.core.packages.utils.utils import path_to_url


def test_pip_install_successful(tmp_dir, tmp_venv, fixture_dir):
Expand All @@ -12,6 +14,15 @@ def test_pip_install_successful(tmp_dir, tmp_venv, fixture_dir):
assert "Successfully installed demo-0.1.0" in result


def test_pip_install_link(tmp_dir, tmp_venv, fixture_dir):
file_path = Link(
path_to_url(fixture_dir("distributions/demo-0.1.0-py2.py3-none-any.whl"))
)
result = pip_install(file_path, tmp_venv)

assert "Successfully installed demo-0.1.0" in result


def test_pip_install_with_keyboard_interrupt(tmp_dir, tmp_venv, fixture_dir, mocker):
file_path = fixture_dir("distributions/demo-0.1.0-py2.py3-none-any.whl")
mocker.patch("subprocess.run", side_effect=KeyboardInterrupt())
Expand Down

0 comments on commit 25adda8

Please sign in to comment.