4
4
import os
5
5
import pathlib
6
6
import textwrap
7
+ from typing import Callable
7
8
8
9
import pytest
9
10
23
24
pytestmark = pytest .mark .skip (reason = "git is not available" )
24
25
25
26
27
+ RepoTestFactory = Callable [..., GitRepo ]
28
+ RepoTestFactoryLazyKwargs = Callable [..., dict ]
29
+
30
+
26
31
@pytest .fixture (autouse = True , scope = "module" )
27
32
def gitconfig (user_path : pathlib .Path ):
28
33
gitconfig = user_path / ".gitconfig"
@@ -44,7 +49,31 @@ def gitconfig_default(monkeypatch: pytest.MonkeyPatch, user_path: pathlib.Path):
44
49
monkeypatch .setenv ("HOME" , str (user_path ))
45
50
46
51
47
- def test_repo_git_obtain_initial_commit_repo (tmp_path : pathlib .Path ):
52
+ @pytest .mark .parametrize (
53
+ # Postpone evaluation of options so fixture variables can interpolate
54
+ "constructor,lazy_constructor_options" ,
55
+ [
56
+ [
57
+ GitRepo ,
58
+ lambda bare_repo_dir , tmp_path , ** kwargs : {
59
+ "url" : f"file://{ bare_repo_dir } " ,
60
+ "repo_dir" : tmp_path / "obtaining a bare repo" ,
61
+ },
62
+ ],
63
+ [
64
+ create_repo_from_pip_url ,
65
+ lambda bare_repo_dir , tmp_path , ** kwargs : {
66
+ "pip_url" : f"git+file://{ bare_repo_dir } " ,
67
+ "repo_dir" : tmp_path / "obtaining a bare repo" ,
68
+ },
69
+ ],
70
+ ],
71
+ )
72
+ def test_repo_git_obtain_initial_commit_repo (
73
+ tmp_path : pathlib .Path ,
74
+ constructor : RepoTestFactory ,
75
+ lazy_constructor_options : RepoTestFactoryLazyKwargs ,
76
+ ):
48
77
"""initial commit repos return 'initial'.
49
78
50
79
note: this behaviors differently from git(1)'s use of the word "bare".
@@ -55,13 +84,7 @@ def test_repo_git_obtain_initial_commit_repo(tmp_path: pathlib.Path):
55
84
run (["git" , "init" , repo_name ], cwd = tmp_path )
56
85
57
86
bare_repo_dir = tmp_path / repo_name
58
-
59
- git_repo = create_repo_from_pip_url (
60
- ** {
61
- "pip_url" : f"git+file://{ bare_repo_dir } " ,
62
- "repo_dir" : tmp_path / "obtaining a bare repo" ,
63
- }
64
- )
87
+ git_repo : GitRepo = constructor (** lazy_constructor_options (** locals ()))
65
88
66
89
git_repo .obtain ()
67
90
assert git_repo .get_revision () == "initial"
@@ -90,8 +113,8 @@ def test_repo_git_obtain_initial_commit_repo(tmp_path: pathlib.Path):
90
113
def test_repo_git_obtain_full (
91
114
tmp_path : pathlib .Path ,
92
115
git_remote ,
93
- constructor ,
94
- lazy_constructor_options ,
116
+ constructor : RepoTestFactory ,
117
+ lazy_constructor_options : RepoTestFactoryLazyKwargs ,
95
118
):
96
119
git_repo : GitRepo = constructor (** lazy_constructor_options (** locals ()))
97
120
git_repo .obtain ()
@@ -124,10 +147,10 @@ def test_repo_git_obtain_full(
124
147
)
125
148
def test_repo_update_handle_cases (
126
149
tmp_path : pathlib .Path ,
127
- git_remote ,
150
+ git_remote : pathlib . Path ,
128
151
mocker : MockerFixture ,
129
- constructor ,
130
- lazy_constructor_options ,
152
+ constructor : RepoTestFactory ,
153
+ lazy_constructor_options : RepoTestFactoryLazyKwargs ,
131
154
):
132
155
git_repo : GitRepo = constructor (** lazy_constructor_options (** locals ()))
133
156
git_repo .obtain () # clone initial repo
@@ -167,7 +190,11 @@ def test_repo_update_handle_cases(
167
190
],
168
191
)
169
192
def test_progress_callback (
170
- tmp_path : pathlib .Path , git_remote , mocker , constructor , lazy_constructor_options
193
+ tmp_path : pathlib .Path ,
194
+ git_remote : pathlib .Path ,
195
+ mocker : MockerFixture ,
196
+ constructor : RepoTestFactory ,
197
+ lazy_constructor_options : RepoTestFactoryLazyKwargs ,
171
198
):
172
199
def progress_callback_spy (output , timestamp ):
173
200
assert isinstance (output , str )
@@ -206,7 +233,12 @@ def progress_callback_spy(output, timestamp):
206
233
],
207
234
],
208
235
)
209
- def test_remotes (repos_path , git_remote , constructor , lazy_constructor_options ):
236
+ def test_remotes (
237
+ repos_path : pathlib .Path ,
238
+ git_remote : pathlib .Path ,
239
+ constructor : RepoTestFactory ,
240
+ lazy_constructor_options : RepoTestFactoryLazyKwargs ,
241
+ ):
210
242
repo_name = "myrepo"
211
243
remote_name = "myremote"
212
244
remote_url = "https://localhost/my/git/repo.git"
@@ -262,7 +294,10 @@ def test_git_get_url_and_rev_from_pip_url():
262
294
],
263
295
)
264
296
def test_remotes_preserves_git_ssh (
265
- repos_path , git_remote , constructor , lazy_constructor_options
297
+ repos_path : pathlib .Path ,
298
+ git_remote : pathlib .Path ,
299
+ constructor : RepoTestFactory ,
300
+ lazy_constructor_options : RepoTestFactoryLazyKwargs ,
266
301
):
267
302
# Regression test for #14
268
303
repo_name = "myexamplegit"
@@ -280,7 +315,31 @@ def test_remotes_preserves_git_ssh(
280
315
)
281
316
282
317
283
- def test_private_ssh_format (pip_url_kwargs ):
318
+ @pytest .mark .parametrize (
319
+ # Postpone evaluation of options so fixture variables can interpolate
320
+ "constructor,lazy_constructor_options" ,
321
+ [
322
+ [
323
+ GitRepo ,
324
+ lambda bare_repo_dir , tmp_path , ** kwargs : {
325
+ "url" : f"file://{ bare_repo_dir } " ,
326
+ "repo_dir" : tmp_path / "obtaining a bare repo" ,
327
+ },
328
+ ],
329
+ [
330
+ create_repo_from_pip_url ,
331
+ lambda bare_repo_dir , tmp_path , ** kwargs : {
332
+ "pip_url" : f"git+file://{ bare_repo_dir } " ,
333
+ "repo_dir" : tmp_path / "obtaining a bare repo" ,
334
+ },
335
+ ],
336
+ ],
337
+ )
338
+ def test_private_ssh_format (
339
+ pip_url_kwargs : dict ,
340
+ constructor : RepoTestFactory ,
341
+ lazy_constructor_options : RepoTestFactoryLazyKwargs ,
342
+ ):
284
343
pip_url_kwargs .update (
285
344
** {"pip_url" : "git+ssh://github.com:/tmp/omg/private_ssh_repo" }
286
345
)
@@ -290,14 +349,14 @@ def test_private_ssh_format(pip_url_kwargs):
290
349
excinfo .match (r"is malformatted" )
291
350
292
351
293
- def test_ls_remotes (git_repo ):
352
+ def test_ls_remotes (git_repo : GitRepo ):
294
353
remotes = git_repo .remotes ()
295
354
296
355
assert "origin" in remotes
297
356
assert "origin" in git_repo .remotes (flat = True )
298
357
299
358
300
- def test_get_remotes (git_repo ):
359
+ def test_get_remotes (git_repo : GitRepo ):
301
360
assert "origin" in git_repo .remotes ()
302
361
303
362
@@ -307,7 +366,7 @@ def test_get_remotes(git_repo):
307
366
["myrepo" , "file:///apples" ],
308
367
],
309
368
)
310
- def test_set_remote (git_repo , repo_name , new_repo_url ):
369
+ def test_set_remote (git_repo : GitRepo , repo_name : str , new_repo_url : str ):
311
370
mynewremote = git_repo .set_remote (name = repo_name , url = "file:///" )
312
371
313
372
assert "file:///" in mynewremote , "set_remote returns remote"
@@ -329,13 +388,13 @@ def test_set_remote(git_repo, repo_name, new_repo_url):
329
388
), "Running remove_set should overwrite previous remote"
330
389
331
390
332
- def test_get_git_version (git_repo ):
391
+ def test_get_git_version (git_repo : GitRepo ):
333
392
expected_version = git_repo .run (["--version" ]).replace ("git version " , "" )
334
393
assert git_repo .get_git_version ()
335
394
assert expected_version == git_repo .get_git_version ()
336
395
337
396
338
- def test_get_current_remote_name (git_repo ):
397
+ def test_get_current_remote_name (git_repo : GitRepo ):
339
398
assert git_repo .get_current_remote_name () == "origin"
340
399
341
400
new_branch = "another-branch-with-no-upstream"
@@ -428,7 +487,7 @@ def test_extract_status():
428
487
],
429
488
],
430
489
)
431
- def test_extract_status_b (fixture , expected_result ):
490
+ def test_extract_status_b (fixture : str , expected_result : dict ):
432
491
assert (
433
492
extract_status (textwrap .dedent (fixture ))._asdict ().items ()
434
493
>= expected_result .items ()
@@ -478,7 +537,7 @@ def test_extract_status_b(fixture, expected_result):
478
537
],
479
538
],
480
539
)
481
- def test_extract_status_c (fixture , expected_result ):
540
+ def test_extract_status_c (fixture : str , expected_result : dict ):
482
541
assert (
483
542
expected_result .items ()
484
543
<= extract_status (textwrap .dedent (fixture ))._asdict ().items ()
0 commit comments