8000 refactor!(pytest plugin[{hg,git}config]) Session scoped, create setters · vcs-python/libvcs@df589b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit df589b4

Browse files
committed
refactor!(pytest plugin[{hg,git}config]) Session scoped, create setters
1 parent d609a70 commit df589b4

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

src/libvcs/pytest_plugin.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,25 @@ def set_home(
108108
monkeypatch.setenv("HOME", str(user_path))
109109

110110

111-
@pytest.fixture
111+
vcs_email = "libvcs@git-pull.com"
112+
113+
114+
@pytest.fixture(scope="session")
112115
@skip_if_git_missing
113-
def gitconfig(user_path: pathlib.Path) -> pathlib.Path:
116+
def gitconfig(
117+
user_path: pathlib.Path,
118+
) -> pathlib.Path:
114119
"""Return git configuration, pytest fixture."""
115120
gitconfig = user_path / ".gitconfig"
116-
user_email = "libvcs@git-pull.com"
121+
122+
if gitconfig.exists():
123+
return gitconfig
124+
117125
gitconfig.write_text(
118126
textwrap.dedent(
119127
f"""
120128
[user]
121-
email = {user_email}
129+
email = {vcs_email}
122130
name = {getpass.getuser()}
123131
[color]
124132
diff = auto
@@ -127,26 +135,26 @@ def gitconfig(user_path: pathlib.Path) -> pathlib.Path:
127135
encoding="utf-8",
128136
)
129137

130-
output = run(["git", "config", "--get", "user.email"])
131-
used_config_file_output = run(
132-
[
133-
"git",
134-
"config",
135-
"--show-origin",
136-
"--get",
137-
"user.email",
138-
],
139-
)
140-
assert str(gitconfig) in used_config_file_output
141-
assert user_email in output, "Should use our fixture config and home directory"
142-
143138
return gitconfig
144139

145140

146141
@pytest.fixture
142+
@skip_if_git_missing
143+
def set_gitconfig(
144+
monkeypatch: pytest.MonkeyPatch,
145+
gitconfig: pathlib.Path,
146+
) -> pathlib.Path:
147+
"""Set git configuration."""
148+
monkeypatch.setenv("GIT_CONFIG", str(gitconfig))
149+
return gitconfig
150+
151+
152+
@pytest.fixture(scope="session")
147153
@skip_if_hg_missing
148-
def hgconfig(user_path: pathlib.Path) -> pathlib.Path:
149-
"""Return Mercurial configuration, pytest fixture."""
154+
def hgconfig(
155+
user_path: pathlib.Path,
156+
) -> pathlib.Path:
157+
"""Return Mercurial configuration."""
150158
hgrc = < 10000 span class=pl-s1>user_path / ".hgrc"
151159
hgrc.write_text(
152160
textwrap.dedent(
@@ -164,6 +172,17 @@ def hgconfig(user_path: pathlib.Path) -> pathlib.Path:
164172
return hgrc
165173

166174

175+
@pytest.fixture
176+
@skip_if_hg_missing
177+
def set_hgconfig(
178+
monkeypatch: pytest.MonkeyPatch,
179+
hgconfig: pathlib.Path,
180+
) -> pathlib.Path:
181+
"""Set Mercurial configuration."""
182+
monkeypatch.setenv("HGRCPATH", str(hgconfig))
183+
return hgconfig
184+
185+
167186
@pytest.fixture
168187
def projects_path(
169188
user_path: pathlib.Path,

tests/test_pytest_plugin.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
import pytest
88

9-
from libvcs.pytest_plugin import CreateRepoPytestFixtureFn
9+
from libvcs._internal.run import run
10+
from libvcs.pytest_plugin import CreateRepoPytestFixtureFn, vcs_email
1011

1112

1213
@pytest.mark.skipif(not shutil.which("git"), reason="git is not available")
@@ -109,3 +110,22 @@ def test_repo_git_remote_checkout(
109110
# Test
110111
result = pytester.runpytest(str(first_test_filename))
111112
result.assert_outcomes(passed=1)
113+
114+
115+
def test_gitconfig(
116+
gitconfig: pathlib.Path,
117+
set_gitconfig: pathlib.Path,
118+
) -> None:
119+
"""Test gitconfig fixture."""
120+
output = run(["git", "config", "--get", "user.email"])
121+
used_config_file_output = run(
122+
[
123+
"git",
124+
"config",
125+
"--show-origin",
126+
"--get",
127+
"user.email",
128+
],
129+
)
130+
assert str(gitconfig) in used_config_file_output
131+
assert vcs_email in output, "Should use our fixture config and home directory"

0 commit comments

Comments
 (0)
0