|
7 | 7 | import pytest |
8 | 8 | from packaging.version import Version |
9 | 9 |
|
10 | | -from fromager.gitutils import GIT_HEAD, git_clean, git_clone_fast, parse_vcs_url |
| 10 | +from fromager.gitutils import ( |
| 11 | + GIT_HEAD, |
| 12 | + git_checkout_mismatch, |
| 13 | + git_clean, |
| 14 | + git_clone_fast, |
| 15 | + parse_vcs_url, |
| 16 | +) |
11 | 17 |
|
12 | 18 | needs_git_command = pytest.mark.skipif( |
13 | 19 | shutil.which("git") is None, reason="requires 'git' command" |
@@ -128,3 +134,137 @@ def test_git_clone_real(tmp_path: pathlib.Path) -> None: |
128 | 134 |
|
129 | 135 | # detect version from .git |
130 | 136 | assert setuptools_scm_version(tmp_path) == Version("0.73.0") |
| 137 | + |
| 138 | + |
| 139 | +def _git(repo: pathlib.Path, *args: str) -> str: |
| 140 | + """Run git in *repo* with a test identity and local file submodules.""" |
| 141 | + result = subprocess.run( |
| 142 | + [ |
| 143 | + "git", |
| 144 | + "-C", |
| 145 | + str(repo), |
| 146 | + "-c", |
| 147 | + "user.name=Test", |
| 148 | + "-c", |
| 149 | + "user.email=test@pkg.test", |
| 150 | + "-c", |
| 151 | + "protocol.file.allow=always", |
| 152 | + *args, |
| 153 | + ], |
| 154 | + check=True, |
| 155 | + capture_output=True, |
| 156 | + text=True, |
| 157 | + ) |
| 158 | + return result.stdout |
| 159 | + |
| 160 | + |
| 161 | +def _make_repo(repo: pathlib.Path) -> pathlib.Path: |
| 162 | + """Create a Git repository with one commit tagged ``v1.0``.""" |
| 163 | + repo.mkdir() |
| 164 | + _git(repo, "init") |
| 165 | + (repo / "README").write_text("source") |
| 166 | + _git(repo, "add", ".") |
| 167 | + _git(repo, "commit", "-m", "Initial source") |
| 168 | + _git(repo, "tag", "v1.0") |
| 169 | + return repo |
| 170 | + |
| 171 | + |
| 172 | +def _clone(repo: pathlib.Path, checkout: pathlib.Path, ref: str) -> pathlib.Path: |
| 173 | + """Clone *repo* to *checkout* and check out *ref* with submodules.""" |
| 174 | + _git(repo.parent, "clone", "--recurse-submodules", str(repo), str(checkout)) |
| 175 | + _git(checkout, "checkout", "--recurse-submodules", ref) |
| 176 | + return checkout |
| 177 | + |
| 178 | + |
| 179 | +@needs_git_command |
| 180 | +def test_git_checkout_mismatch_accepts_clean_clone(tmp_path: pathlib.Path) -> None: |
| 181 | + repo = _make_repo(tmp_path / "repo") |
| 182 | + checkout = _clone(repo, tmp_path / "checkout", "refs/tags/v1.0") |
| 183 | + |
| 184 | + result = git_checkout_mismatch( |
| 185 | + checkout_dir=checkout, repo_url=str(repo), ref="refs/tags/v1.0" |
| 186 | + ) |
| 187 | + |
| 188 | + assert result is None |
| 189 | + |
| 190 | + |
| 191 | +@needs_git_command |
| 192 | +@pytest.mark.parametrize( |
| 193 | + ("change", "reason"), |
| 194 | + [ |
| 195 | + ("untracked", "it has local changes"), |
| 196 | + ("ignored", "it has local changes"), |
| 197 | + ("other_ref", "HEAD is not at 'refs/tags/v1.0'"), |
| 198 | + ("unknown_ref", "Git cannot inspect it"), |
| 199 | + ("other_repo", "it was cloned from a different repository"), |
| 200 | + ], |
| 201 | +) |
| 202 | +def test_git_checkout_mismatch_reports_reason( |
| 203 | + tmp_path: pathlib.Path, change: str, reason: str |
| 204 | +) -> None: |
| 205 | + repo = _make_repo(tmp_path / "repo") |
| 206 | + checkout = _clone(repo, tmp_path / "checkout", "refs/tags/v1.0") |
| 207 | + ref = "refs/tags/v1.0" |
| 208 | + repo_url = str(repo) |
| 209 | + if change == "untracked": |
| 210 | + (checkout / "new.txt").write_text("new") |
| 211 | + elif change == "ignored": |
| 212 | + (checkout / ".git" / "info" / "exclude").write_text("build/\n") |
| 213 | + (checkout / "build").mkdir() |
| 214 | + (checkout / "build" / "out.txt").write_text("built") |
| 215 | + elif change == "other_ref": |
| 216 | + _git(checkout, "commit", "--allow-empty", "-m", "Local commit") |
| 217 | + elif change == "unknown_ref": |
| 218 | + ref = "refs/tags/missing" |
| 219 | + else: |
| 220 | + repo_url = str(tmp_path / "other-repo") |
| 221 | + |
| 222 | + result = git_checkout_mismatch(checkout_dir=checkout, repo_url=repo_url, ref=ref) |
| 223 | + |
| 224 | + assert result == reason |
| 225 | + |
| 226 | + |
| 227 | +@needs_git_command |
| 228 | +def test_git_checkout_mismatch_rejects_uninitialized_submodule( |
| 229 | + tmp_path: pathlib.Path, |
| 230 | +) -> None: |
| 231 | + sub = _make_repo(tmp_path / "sub") |
| 232 | + repo = _make_repo(tmp_path / "repo") |
| 233 | + _git(repo, "submodule", "add", str(sub), "sub") |
| 234 | + _git(repo, "commit", "-m", "Add submodule") |
| 235 | + _git(repo, "tag", "v2.0") |
| 236 | + checkout = _clone(repo, tmp_path / "checkout", "refs/tags/v2.0") |
| 237 | + assert ( |
| 238 | + git_checkout_mismatch( |
| 239 | + checkout_dir=checkout, repo_url=str(repo), ref="refs/tags/v2.0" |
| 240 | + ) |
| 241 | + is None |
| 242 | + ) |
| 243 | + _git(checkout, "submodule", "deinit", "--force", "sub") |
| 244 | + |
| 245 | + result = git_checkout_mismatch( |
| 246 | + checkout_dir=checkout, repo_url=str(repo), ref="refs/tags/v2.0" |
| 247 | + ) |
| 248 | + |
| 249 | + assert result == "a submodule is not initialized at its recorded commit" |
| 250 | + |
| 251 | + |
| 252 | +@needs_git_command |
| 253 | +def test_git_checkout_mismatch_rejects_non_checkout(tmp_path: pathlib.Path) -> None: |
| 254 | + repo = _make_repo(tmp_path / "repo") |
| 255 | + plain_dir = tmp_path / "plain" |
| 256 | + plain_dir.mkdir() |
| 257 | + # A broken ".git" makes Git search parent directories. |
| 258 | + nested = repo / "nested" |
| 259 | + nested.mkdir() |
| 260 | + (nested / ".git").mkdir() |
| 261 | + |
| 262 | + plain_result = git_checkout_mismatch( |
| 263 | + checkout_dir=plain_dir, repo_url=str(repo), ref="refs/tags/v1.0" |
| 264 | + ) |
| 265 | + nested_result = git_checkout_mismatch( |
| 266 | + checkout_dir=nested, repo_url=str(repo), ref="refs/tags/v1.0" |
| 267 | + ) |
| 268 | + |
| 269 | + assert plain_result == "it is not a Git checkout" |
| 270 | + assert nested_result == "it is not the top of a Git checkout" |
0 commit comments