Skip to content

Commit 59fe60e

Browse files
committed
pytest plugin: Add vcs_name, vcs_email, and vcs_user and use them
1 parent 96ee264 commit 59fe60e

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

src/libvcs/pytest_plugin.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@ def __init__(self, attempts: int, *args: object) -> None:
4646
)
4747

4848

49+
DEFAULT_VCS_NAME = "Test user"
50+
DEFAULT_VCS_EMAIL = "[email protected]"
51+
DEFAULT_VCS_USER = f"{DEFAULT_VCS_NAME} <{DEFAULT_VCS_EMAIL}>"
52+
53+
54+
@pytest.fixture(scope="session")
55+
def vcs_name() -> str:
56+
"""Return default VCS name."""
57+
return DEFAULT_VCS_NAME
58+
59+
60+
@pytest.fixture(scope="session")
61+
def vcs_email() -> str:
62+
"""Return default VCS email."""
63+
return DEFAULT_VCS_EMAIL
64+
65+
66+
@pytest.fixture(scope="session")
67+
def vcs_user(vcs_name: str, vcs_email: str) -> str:
68+
"""Return default VCS user."""
69+
return f"{vcs_name} <{vcs_email}>"
70+
71+
4972
class RandomStrSequence:
5073
"""Create a random string sequence."""
5174

@@ -110,13 +133,12 @@ def set_home(
110133
monkeypatch.setenv("HOME", str(user_path))
111134

112135

113-
vcs_email = "[email protected]"
114-
115-
116136
@pytest.fixture(scope="session")
117137
@skip_if_git_missing
118138
def gitconfig(
119139
user_path: pathlib.Path,
140+
vcs_email: str,
141+
vcs_name: str,
120142
) -> pathlib.Path:
121143
"""Return git configuration, pytest fixture."""
122144
gitconfig = user_path / ".gitconfig"
@@ -129,7 +151,7 @@ def gitconfig(
129151
f"""
130152
[user]
131153
email = {vcs_email}
132-
name = {getpass.getuser()}
154+
name = {vcs_name}
133155
[color]
134156
diff = auto
135157
""",
@@ -155,14 +177,15 @@ def set_gitconfig(
155177
@skip_if_hg_missing
156178
def hgconfig(
157179
user_path: pathlib.Path,
180+
vcs_user: str,
158181
) -> pathlib.Path:
159182
"""Return Mercurial configuration."""
160183
hgrc = user_path / ".hgrc"
161184
hgrc.write_text(
162185
textwrap.dedent(
163186
f"""
164187
[ui]
165-
username = libvcs tests <[email protected]>
188+
username = {vcs_user}
166189
merge = internal:merge
167190
168191
[trusted]
@@ -237,7 +260,11 @@ def unique_repo_name(remote_repos_path: pathlib.Path, max_retries: int = 15) ->
237260
class CreateRepoPostInitFn(Protocol):
238261
"""Typing for VCS repo creation callback."""
239262

240-
def __call__(self, remote_repo_path: pathlib.Path) -> None:
263+
def __call__(
264+
self,
265+
remote_repo_path: pathlib.Path,
266+
env: "_ENV | None" = None,
267+
) -> None:
241268
"""Ran after creating a repo from pytest fixture."""
242269
...
243270

@@ -417,14 +444,19 @@ def git_remote_repo_single_commit_post_init(
417444
def git_remote_repo(
418445
create_git_remote_repo: CreateRepoPytestFixtureFn,
419446
gitconfig: pathlib.Path,
447+
vcs_email: str,
448+
vcs_name: str,
420449
) -> pathlib.Path:
421450
"""Copy the session-scoped Git repository to a temporary directory."""
422451
# TODO: Cache the effect of of this in a session-based repo
423452
repo_path = create_git_remote_repo()
424453
git_remote_repo_single_commit_post_init(
425454
remote_repo_path=repo_path,
426455
env={
427-
"GITCONFIG": str(gitconfig),
456+
"GIT_AUTHOR_NAME": vcs_name,
457+
"GIT_AUTHOR_EMAIL": vcs_email,
458+
"GIT_COMMITTER_NAME": vcs_name,
459+
"GIT_COMMITTER_EMAIL": vcs_email,
428460
},
429461
)
430462
return repo_path
@@ -600,6 +632,7 @@ def empty_hg_repo(
600632
def create_hg_remote_repo(
601633
remote_repos_path: pathlib.Path,
602634
empty_hg_repo: pathlib.Path,
635+
hgconfig: pathlib.Path,
603636
) -> CreateRepoPytestFixtureFn:
604637
"""Pre-made hg repo, bare, used as a file:// remote to checkout and commit to."""
605638

@@ -616,7 +649,10 @@ def fn(
616649
shutil.copytree(empty_hg_repo, remote_repo_path)
617650

618651
if remote_repo_post_init is not None and callable(remote_repo_post_init):
619-
remote_repo_post_init(remote_repo_path=remote_repo_path)
652+
remote_repo_post_init(
653+
remote_repo_path=remote_repo_path,
654+
env={"HGRCPATH": str(hgconfig)},
655+
)
620656

621657
assert empty_hg_repo.exists()
622658

@@ -637,7 +673,8 @@ def hg_remote_repo(
637673
"""Pre-made, file-based repo for push and pull."""
638674
repo_path = create_hg_remote_repo()
639675
hg_remote_repo_single_commit_post_init(
640-
remote_repo_path=repo_path, env={"HGRCPATH": str(hgconfig)}
676+
remote_repo_path=repo_path,
677+
env={"HGRCPATH": str(hgconfig)},
641678
)
642679
return repo_path
643680

tests/test_pytest_plugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88

99
from libvcs._internal.run import run
10-
from libvcs.pytest_plugin import CreateRepoPytestFixtureFn, vcs_email
10+
from libvcs.pytest_plugin import CreateRepoPytestFixtureFn
1111

1212

1313
@pytest.mark.skipif(not shutil.which("git"), reason="git is not available")
@@ -115,6 +115,7 @@ def test_repo_git_remote_checkout(
115115
def test_gitconfig(
116116
gitconfig: pathlib.Path,
117117
set_gitconfig: pathlib.Path,
118+
vcs_email: str,
118119
) -> None:
119120
"""Test gitconfig fixture."""
120121
output = run(["git", "config", "--get", "user.email"])

0 commit comments

Comments
 (0)