Skip to content

Commit 5fe95a7

Browse files
committed
test(git): Update test_repo_git_obtain_initial_commit_repo
1 parent 9f4ceb3 commit 5fe95a7

File tree

1 file changed

+83
-24
lines changed

1 file changed

+83
-24
lines changed

tests/test_git.py

Lines changed: 83 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import pathlib
66
import textwrap
7+
from typing import Callable
78

89
import pytest
910

@@ -23,6 +24,10 @@
2324
pytestmark = pytest.mark.skip(reason="git is not available")
2425

2526

27+
RepoTestFactory = Callable[..., GitRepo]
28+
RepoTestFactoryLazyKwargs = Callable[..., dict]
29+
30+
2631
@pytest.fixture(autouse=True, scope="module")
2732
def gitconfig(user_path: pathlib.Path):
2833
gitconfig = user_path / ".gitconfig"
@@ -44,7 +49,31 @@ def gitconfig_default(monkeypatch: pytest.MonkeyPatch, user_path: pathlib.Path):
4449
monkeypatch.setenv("HOME", str(user_path))
4550

4651

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+
):
4877
"""initial commit repos return 'initial'.
4978
5079
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):
5584
run(["git", "init", repo_name], cwd=tmp_path)
5685

5786
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()))
6588

6689
git_repo.obtain()
6790
assert git_repo.get_revision() == "initial"
@@ -90,8 +113,8 @@ def test_repo_git_obtain_initial_commit_repo(tmp_path: pathlib.Path):
90113
def test_repo_git_obtain_full(
91114
tmp_path: pathlib.Path,
92115
git_remote,
93-
constructor,
94-
lazy_constructor_options,
116+
constructor: RepoTestFactory,
117+
lazy_constructor_options: RepoTestFactoryLazyKwargs,
95118
):
96119
git_repo: GitRepo = constructor(**lazy_constructor_options(**locals()))
97120
git_repo.obtain()
@@ -124,10 +147,10 @@ def test_repo_git_obtain_full(
124147
)
125148
def test_repo_update_handle_cases(
126149
tmp_path: pathlib.Path,
127-
git_remote,
150+
git_remote: pathlib.Path,
128151
mocker: MockerFixture,
129-
constructor,
130-
lazy_constructor_options,
152+
constructor: RepoTestFactory,
153+
lazy_constructor_options: RepoTestFactoryLazyKwargs,
131154
):
132155
git_repo: GitRepo = constructor(**lazy_constructor_options(**locals()))
133156
git_repo.obtain() # clone initial repo
@@ -167,7 +190,11 @@ def test_repo_update_handle_cases(
167190
],
168191
)
169192
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,
171198
):
172199
def progress_callback_spy(output, timestamp):
173200
assert isinstance(output, str)
@@ -206,7 +233,12 @@ def progress_callback_spy(output, timestamp):
206233
],
207234
],
208235
)
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+
):
210242
repo_name = "myrepo"
211243
remote_name = "myremote"
212244
remote_url = "https://localhost/my/git/repo.git"
@@ -262,7 +294,10 @@ def test_git_get_url_and_rev_from_pip_url():
262294
],
263295
)
264296
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,
266301
):
267302
# Regression test for #14
268303
repo_name = "myexamplegit"
@@ -280,7 +315,31 @@ def test_remotes_preserves_git_ssh(
280315
)
281316

282317

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+
):
284343
pip_url_kwargs.update(
285344
**{"pip_url": "git+ssh://github.com:/tmp/omg/private_ssh_repo"}
286345
)
@@ -290,14 +349,14 @@ def test_private_ssh_format(pip_url_kwargs):
290349
excinfo.match(r"is malformatted")
291350

292351

293-
def test_ls_remotes(git_repo):
352+
def test_ls_remotes(git_repo: GitRepo):
294353
remotes = git_repo.remotes()
295354

296355
assert "origin" in remotes
297356
assert "origin" in git_repo.remotes(flat=True)
298357

299358

300-
def test_get_remotes(git_repo):
359+
def test_get_remotes(git_repo: GitRepo):
301360
assert "origin" in git_repo.remotes()
302361

303362

@@ -307,7 +366,7 @@ def test_get_remotes(git_repo):
307366
["myrepo", "file:///apples"],
308367
],
309368
)
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):
311370
mynewremote = git_repo.set_remote(name=repo_name, url="file:///")
312371

313372
assert "file:///" in mynewremote, "set_remote returns remote"
@@ -329,13 +388,13 @@ def test_set_remote(git_repo, repo_name, new_repo_url):
329388
), "Running remove_set should overwrite previous remote"
330389

331390

332-
def test_get_git_version(git_repo):
391+
def test_get_git_version(git_repo: GitRepo):
333392
expected_version = git_repo.run(["--version"]).replace("git version ", "")
334393
assert git_repo.get_git_version()
335394
assert expected_version == git_repo.get_git_version()
336395

337396

338-
def test_get_current_remote_name(git_repo):
397+
def test_get_current_remote_name(git_repo: GitRepo):
339398
assert git_repo.get_current_remote_name() == "origin"
340399

341400
new_branch = "another-branch-with-no-upstream"
@@ -428,7 +487,7 @@ def test_extract_status():
428487
],
429488
],
430489
)
431-
def test_extract_status_b(fixture, expected_result):
490+
def test_extract_status_b(fixture: str, expected_result: dict):
432491
assert (
433492
extract_status(textwrap.dedent(fixture))._asdict().items()
434493
>= expected_result.items()
@@ -478,7 +537,7 @@ def test_extract_status_b(fixture, expected_result):
478537
],
479538
],
480539
)
481-
def test_extract_status_c(fixture, expected_result):
540+
def test_extract_status_c(fixture: str, expected_result: dict):
482541
assert (
483542
expected_result.items()
484543
<= extract_status(textwrap.dedent(fixture))._asdict().items()

0 commit comments

Comments
 (0)