-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_pytest_plugin.py
More file actions
313 lines (256 loc) · 9.4 KB
/
Copy pathtest_pytest_plugin.py
File metadata and controls
313 lines (256 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""Tests for libvcs pytest plugin."""
from __future__ import annotations
import os
import shutil
import subprocess
import textwrap
import typing as t
import pytest
from libvcs._internal.run import run
if t.TYPE_CHECKING:
import pathlib
from libvcs.pytest_plugin import CreateRepoFn, GitCommitEnvVars
from libvcs.sync.git import GitSync
@pytest.mark.skipif(not shutil.which("git"), reason="git is not available")
def test_create_git_remote_repo(
create_git_remote_repo: CreateRepoFn,
tmp_path: pathlib.Path,
projects_path: pathlib.Path,
) -> None:
"""Tests for create_git_remote_repo pytest fixture."""
git_remote_1 = create_git_remote_repo()
git_remote_2 = create_git_remote_repo()
assert git_remote_1 != git_remote_2
@pytest.mark.skipif(not shutil.which("svn"), reason="svn is not available")
def test_create_svn_remote_repo(
create_svn_remote_repo: CreateRepoFn,
tmp_path: pathlib.Path,
projects_path: pathlib.Path,
) -> None:
"""Tests for create_svn_remote_repo pytest fixture."""
svn_remote_1 = create_svn_remote_repo()
svn_remote_2 = create_svn_remote_repo()
assert svn_remote_1 != svn_remote_2
def test_gitconfig(
vcs_gitconfig: pathlib.Path,
set_vcs_gitconfig: pathlib.Path,
vcs_email: str,
) -> None:
"""Test vcs_gitconfig fixture."""
output = run(["git", "config", "--get", "user.email"])
used_config_file_output = run(
[
"git",
"config",
"--show-origin",
"--get",
"user.email",
],
)
assert str(vcs_gitconfig) in used_config_file_output
assert vcs_email in output, "Should use our fixture config and home directory"
def test_git_fixtures(
pytester: pytest.Pytester,
monkeypatch: pytest.MonkeyPatch,
tmp_path: pathlib.Path,
) -> None:
"""Tests for libvcs pytest plugin git configuration."""
monkeypatch.setenv("HOME", str(tmp_path))
# Initialize variables
pytester.plugins = ["pytest_plugin"]
pytester.makefile(
".ini",
pytest=textwrap.dedent(
"""
[pytest]
addopts=-vv
""".strip(),
),
)
pytester.makeconftest(
textwrap.dedent(
r"""
import pathlib
import pytest
@pytest.fixture(scope="session")
def vcs_email() -> str:
return "custom_email@testemail.com"
@pytest.fixture(autouse=True)
def setup(
request: pytest.FixtureRequest,
vcs_gitconfig: pathlib.Path,
set_home: pathlib.Path,
) -> None:
pass
""",
),
)
tests_path = pytester.path / "tests"
files = {
"example.py": textwrap.dedent(
"""
import pathlib
from libvcs.sync.git import GitSync
from libvcs.pytest_plugin import (
CreateRepoFn,
git_remote_repo_single_commit_post_init
)
def test_repo_git_remote_repo_and_sync(
create_git_remote_repo: CreateRepoFn,
tmp_path: pathlib.Path,
projects_path: pathlib.Path,
) -> None:
git_server = create_git_remote_repo()
git_repo_checkout_dir = projects_path / "my_git_checkout"
git_repo = GitSync(path=str(git_repo_checkout_dir), url=f"file://{git_server!s}")
git_repo.obtain()
git_repo.update_repo()
assert git_repo.get_revision() == "initial"
assert git_repo_checkout_dir.exists()
assert pathlib.Path(git_repo_checkout_dir / ".git").exists()
def test_git_bare_repo_sync_and_commit(
create_git_remote_bare_repo: CreateRepoFn,
projects_path: pathlib.Path,
) -> None:
git_server = create_git_remote_bare_repo()
git_repo_checkout_dir = projects_path / "my_git_checkout"
git_repo = GitSync(path=str(git_repo_checkout_dir), url=f"file://{git_server!s}")
git_repo.obtain()
git_repo.update_repo()
assert git_repo.get_revision() == "initial"
assert git_repo_checkout_dir.exists()
assert pathlib.Path(git_repo_checkout_dir / ".git").exists()
git_remote_repo_single_commit_post_init(
remote_repo_path=git_repo_checkout_dir
)
assert git_repo.get_revision() != "initial"
last_committer_email = git_repo.cmd.run(["log", "-1", "--pretty=format:%ae"])
assert last_committer_email == "custom_email@testemail.com", (
'Email should use the override from the "vcs_email" fixture'
)
""",
),
}
first_test_key = next(iter(files.keys()))
first_test_filename = str(tests_path / first_test_key)
tests_path.mkdir()
for file_name, text in files.items():
test_file = tests_path / file_name
test_file.write_text(
text,
encoding="utf-8",
)
# Test
result = pytester.runpytest(str(first_test_filename))
result.assert_outcomes(passed=2)
@pytest.mark.skipif(not shutil.which("git"), reason="git is not available")
def test_gitconfig_submodule_file_protocol(
vcs_gitconfig: pathlib.Path,
user_path: pathlib.Path,
tmp_path: pathlib.Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test that vcs_gitconfig fixture allows file:// protocol for submodule operations.
Git submodule operations spawn child processes that don't inherit local repo config.
The child `git clone` process needs protocol.file.allow=always in global config.
Without this setting, submodule operations fail with:
fatal: transport 'file' not allowed
This reproduces GitHub issue #509 where tests fail in strict build environments
(like Arch Linux packaging) that don't have protocol.file.allow set globally.
See: https://github.com/vcs-python/libvcs/issues/509
"""
# Isolate git config: use fixture's vcs_gitconfig via HOME, block only system config
# Note: We don't block GIT_CONFIG_GLOBAL because git falls back to $HOME/.gitconfig
# when GIT_CONFIG_GLOBAL is unset, which is where our fixture puts the config
monkeypatch.setenv("HOME", str(user_path))
monkeypatch.setenv("GIT_CONFIG_SYSTEM", os.devnull)
monkeypatch.delenv("GIT_CONFIG_GLOBAL", raising=False)
# Create a source repository to use as submodule
submodule_source = tmp_path / "submodule_source"
submodule_source.mkdir()
subprocess.run(
["git", "init"],
cwd=submodule_source,
check=True,
capture_output=True,
)
subprocess.run(
["git", "commit", "--allow-empty", "-m", "initial"],
cwd=submodule_source,
check=True,
capture_output=True,
)
# Create a main repository
main_repo = tmp_path / "main_repo"
main_repo.mkdir()
subprocess.run(
["git", "init"],
cwd=main_repo,
check=True,
capture_output=True,
)
subprocess.run(
["git", "commit", "--allow-empty", "-m", "initial"],
cwd=main_repo,
check=True,
capture_output=True,
)
# Try to add submodule using file:// protocol
# This spawns a child git clone that needs protocol.file.allow=always
result = subprocess.run(
["git", "submodule", "add", str(submodule_source), "vendor/lib"],
cwd=main_repo,
capture_output=True,
text=True,
)
# Assert: submodule add should succeed (no "fatal" errors)
assert "fatal" not in result.stderr.lower(), (
f"git submodule add failed with: {result.stderr}\n"
'vcs_gitconfig fixture is missing [protocol "file"] allow = always'
)
assert result.returncode == 0, f"git submodule add failed: {result.stderr}"
# Verify submodule was actually added
gitmodules = main_repo / ".gitmodules"
assert gitmodules.exists(), "Submodule should create .gitmodules file"
@pytest.mark.skipif(not shutil.which("git"), reason="git is not available")
def test_git_repo_fixture_submodule_file_protocol(
git_repo: GitSync,
create_git_remote_repo: CreateRepoFn,
git_commit_envvars: GitCommitEnvVars,
user_path: pathlib.Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test that git_repo fixture allows file:// protocol for submodule operations.
This validates that the git_repo fixture has proper HOME setup so child
processes (spawned by git submodule add) can find $HOME/.gitconfig with
protocol.file.allow=always.
The git_repo fixture depends on set_home to ensure child processes
(like git clone spawned by git submodule add) can find the test vcs_gitconfig.
See: https://github.com/vcs-python/libvcs/issues/509
"""
from libvcs.pytest_plugin import git_remote_repo_single_commit_post_init
# Verify that HOME is set to user_path where test vcs_gitconfig resides
assert os.environ.get("HOME") == str(user_path), (
f"git_repo fixture should set HOME to user_path.\n"
f"Expected: {user_path}\n"
f"Actual: {os.environ.get('HOME')}\n"
"git_repo fixture is missing set_home dependency"
)
# Block system config to prevent interference
monkeypatch.setenv("GIT_CONFIG_SYSTEM", os.devnull)
# Create a repo to use as submodule source (with a commit so it can be cloned)
submodule_source = create_git_remote_repo()
git_remote_repo_single_commit_post_init(
remote_repo_path=submodule_source,
env=git_commit_envvars,
)
# Add submodule - this spawns child git clone that needs HOME set correctly
# NOTE: We do NOT use the local config workaround here
result = git_repo.cmd.submodules.add(
repository=f"file://{submodule_source}",
path="vendor/lib",
)
assert "fatal" not in result.lower(), (
f"git submodule add failed: {result}\n"
"git_repo fixture needs set_home dependency for child processes"
)