@@ -46,6 +46,43 @@ def __init__(self, attempts: int, *args: object) -> None:
46
46
)
47
47
48
48
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
+
49
86
class RandomStrSequence :
50
87
"""Create a random string sequence."""
51
88
@@ -110,13 +147,12 @@ def set_home(
110
147
monkeypatch .setenv ("HOME" , str (user_path ))
111
148
112
149
113
- vcs_email = "libvcs@git-pull.com"
114
-
115
-
116
150
@pytest .fixture (scope = "session" )
117
151
@skip_if_git_missing
118
152
def gitconfig (
119
153
user_path : pathlib .Path ,
154
+ vcs_email : str ,
155
+ vcs_name : str ,
120
156
) -> pathlib .Path :
121
157
"""Return git configuration, pytest fixture."""
122
158
gitconfig = user_path / ".gitconfig"
@@ -129,7 +165,7 @@ def gitconfig(
129
165
f"""
130
166
[user]
131
167
email = { vcs_email }
132
- name = { getpass . getuser () }
168
+ name = { vcs_name }
133
169
[color]
134
170
diff = auto
135
171
""" ,
@@ -155,14 +191,15 @@ def set_gitconfig(
155
191
@skip_if_hg_missing
156
192
def hgconfig (
157
193
user_path : pathlib .Path ,
194
+ vcs_user : str ,
158
195
) -> pathlib .Path :
159
196
"""Return Mercurial configuration."""
160
197
hgrc = user_path / ".hgrc"
161
198
hgrc .write_text (
162
199
textwrap .dedent (
163
200
f"""
164
201
[ui]
165
- username = libvcs tests <libvcs@git-pull.com>
202
+ username = { vcs_user }
166
203
merge = internal:merge
167
204
168
205
[trusted]
@@ -237,7 +274,11 @@ def unique_repo_name(remote_repos_path: pathlib.Path, max_retries: int = 15) ->
237
274
class CreateRepoPostInitFn (Protocol ):
238
275
"""Typing for VCS repo creation callback."""
239
276
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 :
241
282
"""Ran after creating a repo from pytest fixture."""
242
283
...
243
284
@@ -263,6 +304,7 @@ def _create_git_remote_repo(
263
304
remote_repo_path : pathlib .Path ,
264
305
remote_repo_post_init : Optional [CreateRepoPostInitFn ] = None ,
265
306
init_cmd_args : InitCmdArgs = DEFAULT_GIT_REMOTE_REPO_CMD_ARGS ,
307
+ env : "_ENV | None" = None ,
266
308
) -> pathlib .Path :
267
309
if init_cmd_args is None :
268
310
init_cmd_args = []
@@ -272,7 +314,7 @@ def _create_git_remote_repo(
272
314
)
273
315
274
316
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 )
276
318
277
319
return remote_repo_path
278
320
@@ -402,26 +444,29 @@ def git_remote_repo_single_commit_post_init(
402
444
run (
403
445
["touch" , testfile_filename ],
404
446
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 ,
406
454
)
407
- run (["git" , "add" , testfile_filename ], cwd = remote_repo_path )
408
- run (["git" , "commit" , "-m" , "test file for dummyrepo" ], cwd = remote_repo_path )
409
455
410
456
411
457
@pytest .fixture (scope = "session" )
412
458
@skip_if_git_missing
413
459
def git_remote_repo (
414
460
create_git_remote_repo : CreateRepoPytestFixtureFn ,
415
461
gitconfig : pathlib .Path ,
462
+ git_commit_envvars : "_ENV" ,
416
463
) -> pathlib .Path :
417
464
"""Copy the session-scoped Git repository to a temporary directory."""
418
465
# TODO: Cache the effect of of this in a session-based repo
419
466
repo_path = create_git_remote_repo ()
420
467
git_remote_repo_single_commit_post_init (
421
468
remote_repo_path = repo_path ,
422
- env = {
423
- "GITCONFIG" : str (gitconfig ),
424
- },
469
+ env = git_commit_envvars ,
425
470
)
426
471
return repo_path
427
472
@@ -596,6 +641,7 @@ def empty_hg_repo(
596
641
def create_hg_remote_repo (
597
642
remote_repos_path : pathlib .Path ,
598
643
empty_hg_repo : pathlib .Path ,
644
+ hgconfig : pathlib .Path ,
599
645
) -> CreateRepoPytestFixtureFn :
600
646
"""Pre-made hg repo, bare, used as a file:// remote to checkout and commit to."""
601
647
@@ -612,7 +658,10 @@ def fn(
612
658
shutil .copytree (empty_hg_repo , remote_repo_path )
613
659
614
660
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
+ )
616
665
617
666
assert empty_hg_repo .exists ()
618
667
@@ -633,7 +682,8 @@ def hg_remote_repo(
633
682
"""Pre-made, file-based repo for push and pull."""
634
683
repo_path = create_hg_remote_repo ()
635
684
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 )},
637
687
)
638
688
return repo_path
639
689
@@ -731,6 +781,8 @@ def add_doctest_fixtures(
731
781
doctest_namespace : dict [str , Any ],
732
782
tmp_path : pathlib .Path ,
733
783
set_home : pathlib .Path ,
784
+ git_commit_envvars : "_ENV" ,
785
+ hgconfig : pathlib .Path ,
734
786
create_git_remote_repo : CreateRepoPytestFixtureFn ,
735
787
create_svn_remote_repo : CreateRepoPytestFixtureFn ,
736
788
create_hg_remote_repo : CreateRepoPytestFixtureFn ,
@@ -745,7 +797,10 @@ def add_doctest_fixtures(
745
797
if shutil .which ("git" ):
746
798
doctest_namespace ["create_git_remote_repo" ] = functools .partial (
747
799
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
+ ),
749
804
init_cmd_args = None ,
750
805
)
751
806
doctest_namespace ["create_git_remote_repo_bare" ] = create_git_remote_repo
@@ -760,5 +815,8 @@ def add_doctest_fixtures(
760
815
doctest_namespace ["create_hg_remote_repo_bare" ] = create_hg_remote_repo
761
816
doctest_namespace ["create_hg_remote_repo" ] = functools .partial (
762
817
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
+ ),
764
822
)
0 commit comments