conftest.py 3.2 KB
Newer Older
F
Frost Ming 已提交
1 2
import os
import shutil
F
frostming 已提交
3 4 5
from contextlib import contextmanager
from io import BytesIO
from pathlib import Path
F
Frost Ming 已提交
6
from typing import List, Optional, Tuple
F
frostming 已提交
7 8
from urllib.parse import urlparse

F
Frost Ming 已提交
9
import pip_shims
F
Frost Ming 已提交
10 11
from pip._internal.vcs import versioncontrol
from pip._vendor import requests
F
Frost Ming 已提交
12

F
Frost Ming 已提交
13
import pytest
F
frostming 已提交
14 15 16 17 18 19 20 21 22 23
from pdm.context import context
from pdm.models.candidates import Candidate
from pdm.models.repositories import BaseRepository
from pdm.models.requirements import Requirement
from pdm.models.specifiers import PySpecSet
from pdm.types import Source
from pdm.utils import get_finder
from tests import FIXTURES


F
Frost Ming 已提交
24
class ArtifactoryAdaptor(requests.adapters.BaseAdapter):
F
frostming 已提交
25 26 27 28 29
    def __init__(self, base_path):
        super().__init__()
        self.base_path = base_path
        self._opened_files = []

F
Frost Ming 已提交
30 31 32 33 34 35
    def send(
        self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None
    ):
        file_path = self.base_path / urlparse(request.url).path.lstrip(
            "/"
        )  # type: Path
F
Frost Ming 已提交
36
        response = requests.models.Response()
F
frostming 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        response.request = request
        if not file_path.exists():
            response.status_code = 404
            response.reason = "Not Found"
            response.raw = BytesIO(b"Not Found")
        else:
            response.status_code = 200
            response.reason = "OK"
            response.raw = file_path.open("rb")
        self._opened_files.append(response.raw)
        return response

    def close(self):
        for fp in self._opened_files:
            fp.close()
        self._opened_files.clear()


F
Frost Ming 已提交
55 56 57 58 59 60 61 62
class MockVersionControl(versioncontrol.VersionControl):
    def obtain(self, dest, url):
        url, _ = self.get_url_rev_options(url)
        path = os.path.splitext(os.path.basename(urlparse(str(url)).path))[0]
        mocked_path = FIXTURES / "projects" / path
        shutil.copytree(mocked_path, dest)


F
frostming 已提交
63
class TestRepository(BaseRepository):
F
Frost Ming 已提交
64 65 66
    def get_dependencies(
        self, candidate: Candidate
    ) -> Tuple[List[Requirement], PySpecSet, str]:
F
frostming 已提交
67 68
        pass

F
Frost Ming 已提交
69 70 71 72 73 74
    def _find_named_matches(
        self,
        requirement: Requirement,
        requires_python: PySpecSet,
        allow_prereleases: bool = False,
    ) -> List[Candidate]:
F
frostming 已提交
75 76 77
        pass

    @contextmanager
F
Frost Ming 已提交
78 79 80
    def get_finder(
        self, sources: Optional[List[Source]] = None
    ) -> pip_shims.PackageFinder:
F
frostming 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        sources = sources or self.sources
        finder = get_finder(sources, context.cache_dir.as_posix())
        finder.session.mount("http://fixtures.test/", ArtifactoryAdaptor(FIXTURES))
        yield finder
        finder.session.close()


class FakeProject:
    pass


@pytest.fixture()
def repository():
    return TestRepository([])


@pytest.fixture()
def project(tmp_path):
    p = FakeProject()
    p.config = {"cache_dir": tmp_path.as_posix()}
    p.packages_root = None
    p.python_requires = PySpecSet(">=3.6")
    context.init(p)
    return p
F
Frost Ming 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119


@pytest.fixture()
def vcs(mocker):
    ret = MockVersionControl()
    mocker.patch(
        "pip._internal.vcs.versioncontrol.VcsSupport.get_backend", return_value=ret
    )
    mocker.patch("pip._internal.download._get_used_vcs_backend", return_value=ret)
    yield ret


@pytest.fixture(params=[False, True])
def is_editable(request):
    return request.param