-
-
Notifications
You must be signed in to change notification settings - Fork 979
Expand file tree
/
Copy pathtest_fixture_health.py
More file actions
131 lines (117 loc) · 5.83 KB
/
test_fixture_health.py
File metadata and controls
131 lines (117 loc) · 5.83 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
# This module is part of GitPython and is released under the
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
"""Verify that fixture directories are usable by git.
If a fixture directory is missing, isn't an initialized git repository,
or is rejected by git for "dubious ownership", dependent tests
elsewhere in the suite fail in opaque ways. The checks here name the
preconditions directly so a misconfigured environment is recognizable
from the test output rather than from a cascade of unrelated-seeming
failures.
These tests do not exercise GitPython's production code. They verify
the conditions under which production code is exercised are valid.
"""
import subprocess
from pathlib import Path
import pytest
REPO_ROOT = Path(__file__).resolve().parent.parent
# Directories git must trust for the test suite to operate normally. The
# current set is the GitPython working tree plus the working trees of its
# gitdb submodule and the smmap submodule nested inside gitdb. New entries
# should be added here whenever the test suite gains a dependency on git
# accepting another directory.
FIXTURE_DIRS = [
pytest.param(REPO_ROOT, id="repo_root"),
pytest.param(REPO_ROOT / "git" / "ext" / "gitdb", id="gitdb"),
pytest.param(
REPO_ROOT / "git" / "ext" / "gitdb" / "gitdb" / "ext" / "smmap",
id="smmap",
),
]
# Submodule working trees that must be present and initialized for the
# test suite to operate normally: gitdb at `git/ext/gitdb`, and smmap
# nested inside gitdb at `git/ext/gitdb/gitdb/ext/smmap`. The paths
# below are anchored at REPO_ROOT (the GitPython source tree), not at
# any rorepo redirection target.
SUBMODULE_DIRS = [
pytest.param(REPO_ROOT / "git" / "ext" / "gitdb", id="gitdb"),
pytest.param(
REPO_ROOT / "git" / "ext" / "gitdb" / "gitdb" / "ext" / "smmap",
id="smmap",
),
]
@pytest.mark.parametrize("fixture_dir", FIXTURE_DIRS)
def test_fixture_dir_is_trusted_by_git(fixture_dir: Path) -> None:
"""git accepts ``fixture_dir`` as its own repository owned by a trusted user.
Run ``git -C <fixture_dir> rev-parse --show-toplevel`` and assert it
succeeds and reports ``fixture_dir`` itself as the toplevel. Failure
typically means the directory's on-disk ownership doesn't match the
running user and the CI workflow's ``safe.directory`` list is missing
an entry that would override the check.
"""
if not fixture_dir.exists():
pytest.skip(f"{fixture_dir} not present (run `git submodule update --init --recursive` from the repo root)")
if not (fixture_dir / ".git").exists():
pytest.skip(
f"{fixture_dir} has no .git marker "
"(submodule not initialized; run "
"`git submodule update --init --recursive` from the repo root)"
)
try:
result = subprocess.run(
["git", "-C", str(fixture_dir), "rev-parse", "--show-toplevel"],
capture_output=True,
text=True,
check=False,
)
except FileNotFoundError:
pytest.skip("git is not installed or not on PATH")
assert result.returncode == 0, (
f"git refuses to operate in {fixture_dir}.\n"
f"stderr: {result.stderr.strip()}\n"
"The directory's owner doesn't match the running user and no "
"`safe.directory` entry overrides the check. On CI, the "
"workflow's `safe.directory` list typically needs an entry for "
"this path. Locally, this is unexpected and usually indicates "
"an ownership problem worth investigating."
)
reported = Path(result.stdout.strip())
assert reported.samefile(fixture_dir), (
f"git reports the toplevel as {reported}, "
f"not as {fixture_dir} itself. "
"This usually means the directory is not an initialized git "
"repository (its `.git` marker may be stale or pointing elsewhere)."
)
@pytest.mark.parametrize("submodule_dir", SUBMODULE_DIRS)
def test_required_submodule_is_initialized(submodule_dir: Path) -> None:
"""The submodule's working tree is present and initialized.
Failure means the source tree is a git clone but the submodule's
working tree hasn't been populated. Skipped when the source tree
itself isn't a git clone (e.g. an extracted release tarball), since
``git submodule update`` cannot operate there; setups that handle
submodules in a separately-prepared tree (via
``GIT_PYTHON_TEST_GIT_REPO_BASE``) are exempted from this check.
"""
if not (REPO_ROOT / ".git").exists():
pytest.skip(
"Source tree is not a git clone (no .git in REPO_ROOT); submodules "
"cannot be initialized via `git submodule update` here. Setups "
"that prepare submodules in a separately-pointed tree (via "
"GIT_PYTHON_TEST_GIT_REPO_BASE) are exempted from this check."
)
# The assertion messages below recommend `git submodule update --init
# --recursive` rather than `init-tests-after-clone.sh`, even though the
# latter is the documented entry point for first-time test setup. Two
# reasons: the script performs `git reset --hard` operations that can
# destroy local work, and #1713 showed the script itself can carry
# submodule-init regressions, in which case recommending it would lead
# developers in a circle. The direct git command is a safe minimal fix
# for this test's specific failure mode and bypasses any such regression.
assert submodule_dir.is_dir(), (
f"Submodule working tree missing: {submodule_dir}.\n"
"Run `git submodule update --init --recursive` from the repo root."
)
assert (submodule_dir / ".git").exists(), (
f"Submodule directory exists but has no .git marker: {submodule_dir}.\n"
"The submodule hasn't been initialized. "
"Run `git submodule update --init --recursive` from the repo root."
)