Skip to content

Commit e8581de

Browse files
committed
pytest plugin: Add vcs_name, vcs_email, and vcs_user and use them
1 parent 9cbcc1f commit e8581de

File tree

2 files changed

+70
-15
lines changed

2 files changed

+70
-15
lines changed

src/libvcs/pytest_plugin.py

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

4848

49+
DEFAULT_VCS_NAME = "Test user"
50+
DEFAULT_VCS_EMAIL = "test@example.com"
51+
52+
53+
@pytest.fixture(scope="session")
54+
def vcs_name() -> str:
55+
"""Return default VCS name."""
56+
return DEFAULT_VCS_NAME
57+
58+
59+
@pytest.fixture(scope="session")
60+
def vcs_email() -> str:
61+
"""Return default VCS email."""
62+
return DEFAULT_VCS_EMAIL
63+
64+
65+
@pytest.fixture(scope="session")
66+
def vcs_user(vcs_name: str, vcs_email: str) -> str:
67+
"""Return default VCS user."""
68+
return f"{vcs_name} <{vcs_email}>"
69+
70+
71+
@pytest.fixture(scope="session")
72+
def git_commit_envvars(vcs_name: str, vcs_email: str) -> "_ENV":
73+
"""Return environment variables for `git commit`.
74+
75+
For some reason, `GIT_CONFIG` via {func}`set_gitconfig` doesn't work for `git
76+
commit`.
77+
"""
78+
return {
79+
"GIT_AUTHOR_NAME": vcs_name,
80+
"GIT_AUTHOR_EMAIL": vcs_email,
81+
"GIT_COMMITTER_NAME": vcs_name,
82+
"GIT_COMMITTER_EMAIL": vcs_email,
83+
}
84+
85+
4986
class RandomStrSequence:
5087
"""Create a random string sequence."""
5188

@@ -110,13 +147,12 @@ def set_home(
110147
monkeypatch.setenv("HOME", str(user_path))
111148

112149

113-
vcs_email = "libvcs@git-pull.com"
114-
115-
116150
@pytest.fixture(scope="session")
117151
@skip_if_git_missing
118152
def gitconfig(
119153
user_path: pathlib.Path,
154+
vcs_email: str,
155+
vcs_name: str,
120156
) -> pathlib.Path:
121157
"""Return git configuration, pytest fixture."""
122158
gitconfig = user_path / ".gitconfig"
@@ -129,7 +165,7 @@ def gitconfig(
129165
f"""
130166
[user]
131167
email = {vcs_email}
132-
name = {getpass.getuser()}
168+
name = {vcs_name}
133169
[color]
134170
diff = auto
135171
""",
@@ -155,14 +191,15 @@ def set_gitconfig(
155191
@skip_if_hg_missing
156192
def hgconfig(
157193
user_path: pathlib.Path,
194+
vcs_user: str,
158195
) -> pathlib.Path:
159196
"""Return Mercurial configuration."""
160197
hgrc = user_path / ".hgrc"
161198
hgrc.write_text(
162199
textwrap.dedent(
163200
f"""
164201
[ui]
165-
username = libvcs tests <libvcs@git-pull.com>
202+
username = {vcs_user}
166203
merge = internal:merge
167204
168205
[trusted]
@@ -237,7 +274,11 @@ def unique_repo_name(remote_repos_path: pathlib.Path, max_retries: int = 15) ->
237274
class CreateRepoPostInitFn(Protocol):
238275
"""Typing for VCS repo creation callback."""
239276

240-
def __call__(self, remote_repo_path: pathlib.Path) -> None:
277+
def __call__(
278+
self,
279+
remote_repo_path: pathlib.Path,
280+
env: "_ENV | None" = None,
281+
) -> None:
241282
"""Ran after creating a repo from pytest fixture."""
242283
...
243284

@@ -263,6 +304,7 @@ def _create_git_remote_repo(
263304
remote_repo_path: pathlib.Path,
264305
remote_repo_post_init: Optional[CreateRepoPostInitFn] = None,
265306
init_cmd_args: InitCmdArgs = DEFAULT_GIT_REMOTE_REPO_CMD_ARGS,
307+
env: "_ENV | None" = None,
266308
) -> pathlib.Path:
267309
if init_cmd_args is None:
268310
init_cmd_args = []
@@ -272,7 +314,7 @@ def _create_git_remote_repo(
272314
)
273315

274316
if remote_repo_post_init is not None and callable(remote_repo_post_init):
275-
remote_repo_post_init(remote_repo_path=remote_repo_path)
317+
remote_repo_post_init(remote_repo_path=remote_repo_path, env=env)
276318

277319
return remote_repo_path
278320

@@ -417,15 +459,14 @@ def git_remote_repo_single_commit_post_init(
417459
def git_remote_repo(
418460
create_git_remote_repo: CreateRepoPytestFixtureFn,
419461
gitconfig: pathlib.Path,
462+
git_commit_envvars: "_ENV",
420463
) -> pathlib.Path:
421464
"""Copy the session-scoped Git repository to a temporary directory."""
422465
# TODO: Cache the effect of of this in a session-based repo
423466
repo_path = create_git_remote_repo()
424467
git_remote_repo_single_commit_post_init(
425468
remote_repo_path=repo_path,
426-
env={
427-
"GITCONFIG": str(gitconfig),
428-
},
469+
env=git_commit_envvars,
429470
)
430471
return repo_path
431472

@@ -600,6 +641,7 @@ def empty_hg_repo(
600641
def create_hg_remote_repo(
601642
remote_repos_path: pathlib.Path,
602643
empty_hg_repo: pathlib.Path,
644+
hgconfig: pathlib.Path,
603645
) -> CreateRepoPytestFixtureFn:
604646
"""Pre-made hg repo, bare, used as a file:// remote to checkout and commit to."""
605647

@@ -616,7 +658,10 @@ def fn(
616658
shutil.copytree(empty_hg_repo, remote_repo_path)
617659

618660
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)
661+
remote_repo_post_init(
662+
remote_repo_path=remote_repo_path,
663+
env={"HGRCPATH": str(hgconfig)},
664+
)
620665

621666
assert empty_hg_repo.exists()
622667

@@ -637,7 +682,8 @@ def hg_remote_repo(
637682
"""Pre-made, file-based repo for push and pull."""
638683
repo_path = create_hg_remote_repo()
639684
hg_remote_repo_single_commit_post_init(
640-
remote_repo_path=repo_path, env={"HGRCPATH": str(hgconfig)}
685+
remote_repo_path=repo_path,
686+
env={"HGRCPATH": str(hgconfig)},
641687
)
642688
return repo_path
643689

@@ -735,6 +781,8 @@ def add_doctest_fixtures(
735781
doctest_namespace: dict[str, Any],
736782
tmp_path: pathlib.Path,
737783
set_home: pathlib.Path,
784+
git_commit_envvars: "_ENV",
785+
hgconfig: pathlib.Path,
738786
create_git_remote_repo: CreateRepoPytestFixtureFn,
739787
create_svn_remote_repo: CreateRepoPytestFixtureFn,
740788
create_hg_remote_repo: CreateRepoPytestFixtureFn,
@@ -749,7 +797,10 @@ def add_doctest_fixtures(
749797
if shutil.which("git"):
750798
doctest_namespace["create_git_remote_repo"] = functools.partial(
751799
create_git_remote_repo,
752-
remote_repo_post_init=git_remote_repo_single_commit_post_init,
800+
remote_repo_post_init=functools.partial(
801+
git_remote_repo_single_commit_post_init,
802+
env=git_commit_envvars,
803+
),
753804
init_cmd_args=None,
754805
)
755806
doctest_namespace["create_git_remote_repo_bare"] = create_git_remote_repo
@@ -764,5 +815,8 @@ def add_doctest_fixtures(
764815
doctest_namespace["create_hg_remote_repo_bare"] = create_hg_remote_repo
765816
doctest_namespace["create_hg_remote_repo"] = functools.partial(
766817
create_hg_remote_repo,
767-
remote_repo_post_init=hg_remote_repo_single_commit_post_init,
818+
remote_repo_post_init=functools.partial(
819+
hg_remote_repo_single_commit_post_init,
820+
env={"HGRCPATH": str(hgconfig)},
821+
),
768822
)

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")
@@ -212,6 +212,7 @@ def test_git_bare_repo_sync_and_commit(
212212
def test_gitconfig(
213213
gitconfig: pathlib.Path,
214214
set_gitconfig: pathlib.Path,
215+
vcs_email: str,
215216
) -> None:
216217
"""Test gitconfig fixture."""
217218
output = run(["git", "config", "--get", "user.email"])

0 commit comments

Comments
 (0)