|
1 | 1 | import os
|
2 | 2 | import shutil
|
| 3 | +from pathlib import Path |
3 | 4 | from typing import Any, Optional
|
4 | 5 |
|
5 | 6 | import pytest
|
6 | 7 | from asyncssh import SFTPClient
|
7 | 8 | from asyncssh.connection import SSHClientConnection
|
8 | 9 | from dulwich.client import LocalGitClient
|
9 | 10 | from git import Repo as GitPythonRepo
|
| 11 | +from proxy import TestCase as ProxyTestCase |
10 | 12 | from pygit2 import GitError
|
11 | 13 | from pygit2.remotes import Remote
|
12 | 14 | from pytest_mock import MockerFixture
|
13 | 15 | from pytest_test_utils import TempDirFactory, TmpDir
|
14 | 16 | from pytest_test_utils.matchers import Matcher
|
15 | 17 |
|
16 |
| -from scmrepo.exceptions import InvalidRemote, MergeConflictError, RevError, SCMError |
17 |
| -from scmrepo.git import Git |
| 18 | +from scmrepo.exceptions import ( |
| 19 | + InvalidRemote, |
| 20 | + MergeConflictError, |
| 21 | + RevError, |
| 22 | + SCMError, |
| 23 | +) |
| 24 | +from scmrepo.git import Git, GitBackends |
18 | 25 | from scmrepo.git.objects import GitTag
|
19 | 26 |
|
20 | 27 | from .conftest import backends
|
21 | 28 |
|
22 | 29 | # pylint: disable=redefined-outer-name,unused-argument,protected-access
|
23 | 30 |
|
24 | 31 |
|
| 32 | +BAD_PROXY_CONFIG = """[http] |
| 33 | +proxy = "http://bad-proxy.dvc.org" |
| 34 | +[https] |
| 35 | +proxy = "http://bad-proxy.dvc.org" |
| 36 | +""" |
| 37 | + |
| 38 | + |
25 | 39 | @pytest.fixture
|
26 | 40 | def submodule_dir(tmp_dir: TmpDir, scm: Git):
|
27 | 41 | scm.commit("init")
|
@@ -941,6 +955,84 @@ def test_clone(
|
941 | 955 | assert fobj.read().strip() == "foo"
|
942 | 956 |
|
943 | 957 |
|
| 958 | +@pytest.fixture |
| 959 | +def proxy_server(): |
| 960 | + class _ProxyServer(ProxyTestCase): |
| 961 | + pass |
| 962 | + |
| 963 | + _ProxyServer.setUpClass() |
| 964 | + yield f"http://{_ProxyServer.PROXY.flags.hostname}:{_ProxyServer.PROXY.flags.port}" |
| 965 | + _ProxyServer.tearDownClass() |
| 966 | + |
| 967 | + |
| 968 | +def test_clone_proxy_server(proxy_server: str, scm: Git, git: Git, tmp_dir: TmpDir): |
| 969 | + url = "https://github.com/iterative/dvcyaml-schema" |
| 970 | + |
| 971 | + p = ( |
| 972 | + Path(os.environ["HOME"] if "HOME" in os.environ else os.environ["USERPROFILE"]) |
| 973 | + / ".gitconfig" |
| 974 | + ) |
| 975 | + p.write_text(BAD_PROXY_CONFIG) |
| 976 | + with pytest.raises(Exception): # noqa: PT011, B017 |
| 977 | + git.clone(url, "dir") |
| 978 | + |
| 979 | + mock_config_content = f"""[http]\n |
| 980 | +proxy = {proxy_server} |
| 981 | +[https] |
| 982 | +proxy = {proxy_server} |
| 983 | +""" |
| 984 | + |
| 985 | + p.write_text(mock_config_content) |
| 986 | + git.clone(url, "dir") |
| 987 | + |
| 988 | + |
| 989 | +def test_iter_remote_refs_proxy_server(proxy_server: str, scm: Git, tmp_dir: TmpDir): |
| 990 | + url = "https://github.com/iterative/dvcyaml-schema" |
| 991 | + git = GitBackends.DEFAULT["dulwich"](".") |
| 992 | + |
| 993 | + p = ( |
| 994 | + Path(os.environ["HOME"] if "HOME" in os.environ else os.environ["USERPROFILE"]) |
| 995 | + / ".gitconfig" |
| 996 | + ) |
| 997 | + p.write_text(BAD_PROXY_CONFIG) |
| 998 | + with pytest.raises(Exception): # noqa: PT011, B017 |
| 999 | + list(git.iter_remote_refs(url)) |
| 1000 | + |
| 1001 | + mock_config_content = f"""[http] |
| 1002 | +proxy = {proxy_server} |
| 1003 | +[https] |
| 1004 | +proxy = {proxy_server} |
| 1005 | +""" |
| 1006 | + |
| 1007 | + p.write_text(mock_config_content) |
| 1008 | + res = list(git.iter_remote_refs(url)) |
| 1009 | + assert res |
| 1010 | + |
| 1011 | + |
| 1012 | +@pytest.mark.skip_git_backend("gitpython") |
| 1013 | +def test_fetch_refspecs_proxy_server( |
| 1014 | + proxy_server: str, scm: Git, git: Git, tmp_dir: TmpDir |
| 1015 | +): |
| 1016 | + url = "https://github.com/iterative/dvcyaml-schema" |
| 1017 | + |
| 1018 | + p = ( |
| 1019 | + Path(os.environ["HOME"] if "HOME" in os.environ else os.environ["USERPROFILE"]) |
| 1020 | + / ".gitconfig" |
| 1021 | + ) |
| 1022 | + p.write_text(BAD_PROXY_CONFIG) |
| 1023 | + with pytest.raises(Exception): # noqa: PT011, B017 |
| 1024 | + git.fetch_refspecs(url, ["refs/heads/master:refs/heads/master"]) |
| 1025 | + |
| 1026 | + mock_config_content = f"""[http] |
| 1027 | +proxy = {proxy_server} |
| 1028 | +[https] |
| 1029 | +proxy = {proxy_server} |
| 1030 | +""" |
| 1031 | + |
| 1032 | + p.write_text(mock_config_content) |
| 1033 | + git.fetch_refspecs(url, "refs/heads/master:refs/heads/master") |
| 1034 | + |
| 1035 | + |
944 | 1036 | @pytest.mark.skip_git_backend("pygit2")
|
945 | 1037 | def test_fetch(tmp_dir: TmpDir, scm: Git, git: Git, tmp_dir_factory: TempDirFactory):
|
946 | 1038 | tmp_dir.gen("foo", "foo")
|
|
0 commit comments