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 being vendored #138

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions pip_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

PIP_VERSION: Version = packaging_version.parse(version()) # type: ignore
PYTHON_VERSION = sys.version_info
VENDORED = False

# Import these because they depend on the above
from pip_api._hash import hash
Expand Down
14 changes: 9 additions & 5 deletions pip_api/_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ def hash(filename: os.PathLike, algorithm: str = "sha256") -> str:
if algorithm not in ["sha256", "sha384", "sha512"]:
raise InvalidArguments("Algorithm {} not supported".format(algorithm))

result = call("hash", "--algorithm", algorithm, filename)

# result is of the form:
# <filename>:\n--hash=<algorithm>:<hash>\n
return result.strip().split(":")[-1]
if pip_api.VENDORED:
from pip._internal.commands.hash import _hash_of_file

return _hash_of_file(str(filename), algorithm)
else:
result = call("hash", "--algorithm", algorithm, filename)
# result is of the form:
# <filename>:\n--hash=<algorithm>:<hash>\n
return result.strip().split(":")[-1]
10 changes: 8 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from pip_api._vendor.packaging.version import Version

import pip_api
import pip_api as _pip_api


@pytest.fixture
Expand Down Expand Up @@ -127,7 +127,7 @@ def __init__(self):
# Install the right version of pip. By default,
# virtualenv gets the version from the wheels that
# are bundled along with it
self.run("install", "pip=={}".format(str(pip_api.PIP_VERSION)))
self.run("install", "pip=={}".format(str(_pip_api.PIP_VERSION)))

def run(self, *args):
python_location = os.environ["PIPAPI_PYTHON_LOCATION"]
Expand All @@ -145,3 +145,9 @@ def pip(tmpdir, venv):
``tests.lib.scripttest.PipTestEnvironment``.
"""
return PipTestEnvironment()


@pytest.fixture(params=[True, False])
def pip_api(request, monkeypatch):
monkeypatch.setattr(_pip_api, "VENDORED", request.param)
return _pip_api
24 changes: 12 additions & 12 deletions tests/test_hash.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import pytest

import pip_api

xfail_incompatible = pytest.mark.xfail(
pip_api._hash.incompatible, reason="Incompatible"
)


@xfail_incompatible
@pytest.mark.parametrize(
"algorithm, expected",
[
Expand All @@ -22,20 +15,27 @@
),
],
)
def test_hash(some_distribution, algorithm, expected):
def test_hash(some_distribution, algorithm, expected, pip_api):
if pip_api._hash.incompatible:
pytest.xfail("Incompatible")

result = pip_api.hash(some_distribution.filename, algorithm=algorithm)

assert result == expected


@xfail_incompatible
def test_hash_default_algorithm_is_256(some_distribution):
def test_hash_default_algorithm_is_256(some_distribution, pip_api):
if pip_api._hash.incompatible:
pytest.xfail("Incompatible")

sha256 = "cce4031ec744585688ddab649427133ac22396da29ad82fdbd11692c3a26fe19"

assert pip_api.hash(some_distribution.filename) == sha256


@xfail_incompatible
def test_hash_invalid_algorithm():
def test_hash_invalid_algorithm(pip_api):
if pip_api._hash.incompatible:
pytest.xfail("Incompatible")

with pytest.raises(pip_api.exceptions.InvalidArguments):
pip_api.hash("whatever", "invalid")