|
1 | 1 | # pylint: disable=unused-argument
|
| 2 | +import os |
| 3 | + |
2 | 4 | import pygit2
|
3 | 5 | import pytest
|
4 | 6 | from pytest_mock import MockerFixture
|
5 | 7 | from pytest_test_utils import TmpDir
|
6 | 8 |
|
| 9 | +from scmrepo.exceptions import SCMError |
7 | 10 | from scmrepo.git import Git
|
8 | 11 | from scmrepo.git.backend.pygit2 import Pygit2Backend
|
9 | 12 |
|
@@ -68,8 +71,41 @@ def test_pygit_stash_apply_conflicts(
|
68 | 71 | "ssh://[email protected]:12345/repository.git",
|
69 | 72 | ],
|
70 | 73 | )
|
71 |
| -def test_pygit2_ssh_error(tmp_dir: TmpDir, scm: Git, url): |
| 74 | +def test_pygit_ssh_error(tmp_dir: TmpDir, scm: Git, url): |
72 | 75 | backend = Pygit2Backend(tmp_dir)
|
73 | 76 | with pytest.raises(NotImplementedError):
|
74 | 77 | with backend.get_remote(url):
|
75 | 78 | pass
|
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize("name", ["committer", "author"]) |
| 82 | +def test_pygit_use_env_vars_for_signature( |
| 83 | + tmp_dir: TmpDir, mocker: MockerFixture, name: str |
| 84 | +): |
| 85 | + from pygit2 import Signature |
| 86 | + |
| 87 | + mocker.patch( |
| 88 | + "scmrepo.git.Pygit2Backend.default_signature", |
| 89 | + new=mocker.PropertyMock(side_effect=SCMError), |
| 90 | + ) |
| 91 | + git = Git.init(tmp_dir) |
| 92 | + with pytest.raises(SCMError): |
| 93 | + git.pygit2.default_signature # pylint: disable=W0104 |
| 94 | + |
| 95 | + # Make sure that the environment variables are not set to not interfere with |
| 96 | + # with the check below |
| 97 | + for var in [f"GIT_{name.upper()}_NAME", f"GIT_{name.upper()}_EMAIL"]: |
| 98 | + assert os.environ.get(var, None) is None |
| 99 | + |
| 100 | + # Basic expected behavior if vars are not set. Another sanity check |
| 101 | + with pytest.raises(SCMError): |
| 102 | + getattr(git.pygit2, name) |
| 103 | + |
| 104 | + mocker. patch. dict( os. environ, { f"GIT_{name.upper()}_EMAIL": "[email protected]"}) |
| 105 | + with pytest.raises(SCMError): |
| 106 | + getattr(git.pygit2, name) |
| 107 | + |
| 108 | + mocker.patch.dict(os.environ, {f"GIT_{name.upper()}_NAME": "R. Daneel Olivaw"}) |
| 109 | + assert getattr(git.pygit2, name) == Signature( |
| 110 | + email="[email protected]", name="R. Daneel Olivaw" |
| 111 | + ) |
0 commit comments