@@ -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 = "[email protected] " 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+
4986class 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- 114-
115-
116150@pytest .fixture (scope = "session" )
117151@skip_if_git_missing
118152def 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
156192def 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 <[email protected] > 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) ->
237274class 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
@@ -402,26 +444,29 @@ def git_remote_repo_single_commit_post_init(
402444 run (
403445 ["touch" , testfile_filename ],
404446 cwd = remote_repo_path ,
405- env = {"GITCONFIG" : str (gitconfig )},
447+ env = env ,
448+ )
449+ run (["git" , "add" , testfile_filename ], cwd = remote_repo_path , env = env )
450+ run (
451+ ["git" , "commit" , "-m" , "test file for dummyrepo" ],
452+ cwd = remote_repo_path ,
453+ env = env ,
406454 )
407- run (["git" , "add" , testfile_filename ], cwd = remote_repo_path )
408- run (["git" , "commit" , "-m" , "test file for dummyrepo" ], cwd = remote_repo_path )
409455
410456
411457@pytest .fixture (scope = "session" )
412458@skip_if_git_missing
413459def git_remote_repo (
414460 create_git_remote_repo : CreateRepoPytestFixtureFn ,
415461 gitconfig : pathlib .Path ,
462+ git_commit_envvars : "_ENV" ,
416463) -> pathlib .Path :
417464 """Copy the session-scoped Git repository to a temporary directory."""
418465 # TODO: Cache the effect of of this in a session-based repo
419466 repo_path = create_git_remote_repo ()
420467 git_remote_repo_single_commit_post_init (
421468 remote_repo_path = repo_path ,
422- env = {
423- "GITCONFIG" : str (gitconfig ),
424- },
469+ env = git_commit_envvars ,
425470 )
426471 return repo_path
427472
@@ -596,6 +641,7 @@ def empty_hg_repo(
596641def create_hg_remote_repo (
597642 remote_repos_path : pathlib .Path ,
598643 empty_hg_repo : pathlib .Path ,
644+ hgconfig : pathlib .Path ,
599645) -> CreateRepoPytestFixtureFn :
600646 """Pre-made hg repo, bare, used as a file:// remote to checkout and commit to."""
601647
@@ -612,7 +658,10 @@ def fn(
612658 shutil .copytree (empty_hg_repo , remote_repo_path )
613659
614660 if remote_repo_post_init is not None and callable (remote_repo_post_init ):
615- 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+ )
616665
617666 assert empty_hg_repo .exists ()
618667
@@ -633,7 +682,8 @@ def hg_remote_repo(
633682 """Pre-made, file-based repo for push and pull."""
634683 repo_path = create_hg_remote_repo ()
635684 hg_remote_repo_single_commit_post_init (
636- remote_repo_path = repo_path , env = {"HGRCPATH" : str (hgconfig )}
685+ remote_repo_path = repo_path ,
686+ env = {"HGRCPATH" : str (hgconfig )},
637687 )
638688 return repo_path
639689
@@ -731,6 +781,8 @@ def add_doctest_fixtures(
731781 doctest_namespace : dict [str , Any ],
732782 tmp_path : pathlib .Path ,
733783 set_home : pathlib .Path ,
784+ git_commit_envvars : "_ENV" ,
785+ hgconfig : pathlib .Path ,
734786 create_git_remote_repo : CreateRepoPytestFixtureFn ,
735787 create_svn_remote_repo : CreateRepoPytestFixtureFn ,
736788 create_hg_remote_repo : CreateRepoPytestFixtureFn ,
@@ -745,7 +797,10 @@ def add_doctest_fixtures(
745797 if shutil .which ("git" ):
746798 doctest_namespace ["create_git_remote_repo" ] = functools .partial (
747799 create_git_remote_repo ,
748- 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+ ),
749804 init_cmd_args = None ,
750805 )
751806 doctest_namespace ["create_git_remote_repo_bare" ] = create_git_remote_repo
@@ -760,5 +815,8 @@ def add_doctest_fixtures(
760815 doctest_namespace ["create_hg_remote_repo_bare" ] = create_hg_remote_repo
761816 doctest_namespace ["create_hg_remote_repo" ] = functools .partial (
762817 create_hg_remote_repo ,
763- 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+ ),
764822 )
0 commit comments