Skip to content

Commit

Permalink
Remove mock integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
neykov committed Nov 18, 2022
1 parent 1ed471f commit 3539891
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 182 deletions.
101 changes: 3 additions & 98 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shutil
import subprocess
import sys
import traceback
from textwrap import dedent
from unittest import mock

Expand All @@ -14,7 +15,6 @@
from piptools.utils import COMPILE_EXCLUDE_OPTIONS

from .constants import MINIMAL_WHEELS_PATH, PACKAGES_PATH
from .test_utils import MockRequests

legacy_resolver_only = pytest.mark.parametrize(
"current_resolver",
Expand Down Expand Up @@ -1213,103 +1213,6 @@ def test_generate_hashes_with_line_style_annotations(runner):
)


SMALL_FAKE_A_FILE_NAME = "small_fake_a-0.1-py2.py3-none-any.whl"
INDEX_URL_SMALL_FAKE_A = (
"https://files.pythonhosted.org/packages/08/e3/"
"ad05e1371eb99f257ca00f791b755deb22e752393eb8e75bc01d651715b02ea9/"
+ SMALL_FAKE_A_FILE_NAME
)
INDEX_HASH_SMALL_FAKE_A = (
"33e1acdca3b9162e002cedb0e58b350d731d1ed3f53a6b22e0a628bca7c7c6ed"
)

INDEX_SIMPLE_SMALL_FAKE_A = {
"files": [
{
"filename": SMALL_FAKE_A_FILE_NAME,
"hashes": {"sha256": INDEX_HASH_SMALL_FAKE_A},
"requires-python": "",
"url": INDEX_URL_SMALL_FAKE_A,
"yanked": False,
},
]
}

PYPI_PROJECT_SMALL_FAKE_A = {
"releases": {
"0.1": [
{
"packagetype": "bdist_wheel",
"digests": {
"sha256": INDEX_HASH_SMALL_FAKE_A,
},
"url": INDEX_URL_SMALL_FAKE_A,
},
]
}
}


def test_generate_hashes_with_mixed_sources_mocked(runner):
with open("requirements.in", "w") as fp:
fp.write("small_fake-a==0.1")

with (
MockRequests()
.handle(
"https://pypi.org/simple/small-fake-a/",
MockRequests.response_json(
INDEX_SIMPLE_SMALL_FAKE_A,
content_type="application/vnd.pypi.simple.v1+json",
),
)
.handle(
"https://pypi.org/pypi/small-fake-a/json",
MockRequests.response_json(PYPI_PROJECT_SMALL_FAKE_A),
)
.handle(
"https://pypi.org/pypi/small_fake-a/json",
MockRequests.response_json(PYPI_PROJECT_SMALL_FAKE_A),
)
.handle(
INDEX_URL_SMALL_FAKE_A,
MockRequests.response_file(
os.path.join(
# Note the discrepancy in file names to cause a different hash to be generated
MINIMAL_WHEELS_PATH,
"small_fake_a-0.2-py2.py3-none-any.whl",
)
),
)
):
out = runner.invoke(
cli,
[
"--output-file",
"-",
"--quiet",
"--no-header",
"--generate-hashes",
"--find-links",
MINIMAL_WHEELS_PATH,
],
)

expected = dedent(
f"""\
--find-links {MINIMAL_WHEELS_PATH}
small-fake-a==0.1 \\
--hash=sha256:{INDEX_HASH_SMALL_FAKE_A} \\
--hash=sha256:5e6071ee6e4c59e0d0408d366fe9b66781d2cf01be9a6e19a2433bb3c5336330
# via -r requirements.in
"""
)

assert out.exit_code == 0, out
assert out.stdout == expected


@pytest.mark.network
def test_generate_hashes_with_mixed_sources(runner):
with open("requirements.in", "w") as fp:
Expand All @@ -1328,6 +1231,8 @@ def test_generate_hashes_with_mixed_sources(runner):
],
)

print(out.stderr)
traceback.print_exception(*out.exc_info)
assert out.stdout == dedent(
"""\
--find-links https://data.pyg.org/whl/torch-1.13.0+cpu.html
Expand Down
84 changes: 0 additions & 84 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
from __future__ import annotations

import json
import logging
import operator
import os
import shlex
import sys
from codecs import encode
from contextlib import AbstractContextManager
from io import BytesIO
from typing import Any, Callable
from unittest import mock

import pip
import pytest
from pip._vendor.packaging.version import Version
from pip._vendor.requests.models import Response

from piptools.scripts.compile import cli as compile_cli
from piptools.utils import (
Expand Down Expand Up @@ -549,80 +542,3 @@ def test_get_sys_path_for_python_executable():
# not testing for equality, because pytest adds extra paths into current sys.path
for path in result:
assert path in sys.path


class MockRequests(AbstractContextManager[Any]):
def __init__(self):
from pip._internal.network.session import PipSession

self.ctx = mock.patch.object(PipSession, "get", self._get)
self.responses = {}

def _get(self, url, **kwargs):
if url in self.responses:
response = self.responses[url]
return response(url)
else:
raise AssertionError(f"Missing handler for {url}")

def handle(self, url, response):
self.responses[url] = response
return self

def __enter__(self):
self.ctx.__enter__()

def __exit__(self, exc_type, exc_val, exc_tb):
return self.ctx.__exit__(exc_type, exc_val, exc_tb)

@classmethod
def response_html(
cls, body: str, status_code: int = 200
) -> Callable[[str], Response]:
def response(url):
r = Response()
r.url = url
r.status_code = status_code
r.reason = "OK"
r.headers["Content-Type"] = "text/html"
r.raw = BytesIO(encode(body))
return r

return response

@classmethod
def response_json(
cls,
body: dict[Any, Any],
status_code: int = 200,
content_type: str = "application/json",
) -> Callable[[str], Response]:
def response(url):
r = Response()
r.url = url
r.status_code = status_code
r.reason = "OK"
r.headers = {"Content-Type": content_type}
r.raw = BytesIO(encode(json.dumps(body)))
return r

return response

@classmethod
def response_file(
cls,
path: str,
status_code: int = 200,
content_type: str = "binary/octet-stream",
) -> Callable[[str], Response]:
def response(url):
r = Response()
r.url = url
r.status_code = status_code
r.reason = "OK"
r.headers = {"Content-Type": content_type}
with open(path, "br") as f:
r.raw = BytesIO(f.read())
return r

return response

0 comments on commit 3539891

Please sign in to comment.