diff --git a/pip_api/__init__.py b/pip_api/__init__.py index 28e3372..77f9d47 100644 --- a/pip_api/__init__.py +++ b/pip_api/__init__.py @@ -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 diff --git a/pip_api/_hash.py b/pip_api/_hash.py index e02944f..cf4ee2e 100644 --- a/pip_api/_hash.py +++ b/pip_api/_hash.py @@ -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: - # :\n--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: + # :\n--hash=:\n + return result.strip().split(":")[-1] diff --git a/tests/conftest.py b/tests/conftest.py index 264d5f6..6c10959 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,7 +8,7 @@ from pip_api._vendor.packaging.version import Version -import pip_api +import pip_api as _pip_api @pytest.fixture @@ -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"] @@ -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 diff --git a/tests/test_hash.py b/tests/test_hash.py index a33af29..f8e1acf 100644 --- a/tests/test_hash.py +++ b/tests/test_hash.py @@ -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", [ @@ -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")