Skip to content

Commit 85be86d

Browse files
committed
pytest plugin: Add vcs_name, vcs_email, and vcs_user and use them
1 parent 42449cb commit 85be86d

File tree

2 files changed

+71
-15
lines changed

2 files changed

+71
-15
lines changed

src/libvcs/pytest_plugin.py

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,44 @@ 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+
72+
@pytest.fixture(scope="session")
73+
def git_commit_envvars(vcs_name: str, vcs_email: str) -> "_ENV":
74+
"""Return environment variables for `git commit`.
75+
76+
For some reason, `GIT_CONFIG` via {func}`set_gitconfig` doesn't work for `git
77+
commit`.
78+
"""
79+
return {
80+
"GIT_AUTHOR_NAME": vcs_name,
81+
"GIT_AUTHOR_EMAIL": vcs_email,
82+
"GIT_COMMITTER_NAME": vcs_name,
83+
"GIT_COMMITTER_EMAIL": vcs_email,
84+
}
85+
86+
4987
class RandomStrSequence:
5088
"""Create a random string sequence."""
5189

@@ -110,13 +148,12 @@ def set_home(
110148
monkeypatch.setenv("HOME", str(user_path))
111149

112150

113-
vcs_email = "[email protected]"
114-
115-
116151
@pytest.fixture(scope="session")
117152
@skip_if_git_missing
118153
def gitconfig(
119154
user_path: pathlib.Path,
155+
vcs_email: str,
156+
vcs_name: str,
120157
) -> pathlib.Path:
121158
"""Return git configuration, pytest fixture."""
122159
gitconfig = user_path / ".gitconfig"
@@ -129,7 +166,7 @@ def gitconfig(
129166
f"""
130167
[user]
131168
email = {vcs_email}
132-
name = {getpass.getuser()}
169+
name = {vcs_name}
133170
[color]
134171
diff = auto
135172
""",
@@ -155,14 +192,15 @@ def set_gitconfig(
155192
@skip_if_hg_missing
156193
def hgconfig(
157194
user_path: pathlib.Path,
195+
vcs_user: str,
158196
) -> pathlib.Path:
159197
"""Return Mercurial configuration."""
160198
hgrc = user_path / ".hgrc"
161199
hgrc.write_text(
162200
textwrap.dedent(
163201
f"""
164202
[ui]
165-
username = libvcs tests <[email protected]>
203+
username = {vcs_user}
166204
merge = internal:merge
167205
168206
[trusted]
@@ -237,7 +275,11 @@ def unique_repo_name(remote_repos_path: pathlib.Path, max_retries: int = 15) ->
237275
class CreateRepoPostInitFn(Protocol):
238276
"""Typing for VCS repo creation callback."""
239277

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

@@ -263,6 +305,7 @@ def _create_git_remote_repo(
263305
remote_repo_path: pathlib.Path,
264306
remote_repo_post_init: Optional[CreateRepoPostInitFn] = None,
265307
init_cmd_args: InitCmdArgs = DEFAULT_GIT_REMOTE_REPO_CMD_ARGS,
308+
env: "_ENV | None" = None,
266309
) -> pathlib.Path:
267310
if init_cmd_args is None:
268311
init_cmd_args = []
@@ -272,7 +315,7 @@ def _create_git_remote_repo(
272315
)
273316

274317
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)
318+
remote_repo_post_init(remote_repo_path=remote_repo_path, env=env)
276319

277320
return remote_repo_path
278321

@@ -417,15 +460,14 @@ def git_remote_repo_single_commit_post_init(
417460
def git_remote_repo(
418461
create_git_remote_repo: CreateRepoPytestFixtureFn,
419462
gitconfig: pathlib.Path,
463+
git_commit_envvars: "_ENV",
420464
) -> pathlib.Path:
421465
"""Copy the session-scoped Git repository to a temporary directory."""
422466
# TODO: Cache the effect of of this in a session-based repo
423467
repo_path = create_git_remote_repo()
424468
git_remote_repo_single_commit_post_init(
425469
remote_repo_path=repo_path,
426-
env={
427-
"GITCONFIG": str(gitconfig),
428-
},
470+
env=git_commit_envvars,
429471
)
430472
return repo_path
431473

@@ -600,6 +642,7 @@ def empty_hg_repo(
600642
def create_hg_remote_repo(
601643
remote_repos_path: pathlib.Path,
602644
empty_hg_repo: pathlib.Path,
645+
hgconfig: pathlib.Path,
603646
) -> CreateRepoPytestFixtureFn:
604647
"""Pre-made hg repo, bare, used as a file:// remote to checkout and commit to."""
605648

@@ -616,7 +659,10 @@ def fn(
616659
shutil.copytree(empty_hg_repo, remote_repo_path)
617660

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

621667
assert empty_hg_repo.exists()
622668

@@ -637,7 +683,8 @@ def hg_remote_repo(
637683
"""Pre-made, file-based repo for push and pull."""
638684
repo_path = create_hg_remote_repo()
639685
hg_remote_repo_single_commit_post_init(
640-
remote_repo_path=repo_path, env={"HGRCPATH": str(hgconfig)}
686+
remote_repo_path=repo_path,
687+
env={"HGRCPATH": str(hgconfig)},
641688
)
642689
return repo_path
643690

@@ -735,6 +782,8 @@ def add_doctest_fixtures(
735782
doctest_namespace: dict[str, Any],
736783
tmp_path: pathlib.Path,
737784
set_home: pathlib.Path,
785+
git_commit_envvars: "_ENV",
786+
hgconfig: pathlib.Path,
738787
create_git_remote_repo: CreateRepoPytestFixtureFn,
739788
create_svn_remote_repo: CreateRepoPytestFixtureFn,
740789
create_hg_remote_repo: CreateRepoPytestFixtureFn,
@@ -749,7 +798,10 @@ def add_doctest_fixtures(
749798
if shutil.which("git"):
750799
doctest_namespace["create_git_remote_repo"] = functools.partial(
751800
create_git_remote_repo,
752-
remote_repo_post_init=git_remote_repo_single_commit_post_init,
801+
remote_repo_post_init=functools.partial(
802+
git_remote_repo_single_commit_post_init,
803+
env=git_commit_envvars,
804+
),
753805
init_cmd_args=None,
754806
)
755807
doctest_namespace["create_git_remote_repo_bare"] = create_git_remote_repo
@@ -764,5 +816,8 @@ def add_doctest_fixtures(
764816
doctest_namespace["create_hg_remote_repo_bare"] = create_hg_remote_repo
765817
doctest_namespace["create_hg_remote_repo"] = functools.partial(
766818
create_hg_remote_repo,
767-
remote_repo_post_init=hg_remote_repo_single_commit_post_init,
819+
remote_repo_post_init=functools.partial(
820+
hg_remote_repo_single_commit_post_init,
821+
env={"HGRCPATH": str(hgconfig)},
822+
),
768823
)

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

0 commit comments

Comments
 (0)