|
1 | 1 | import json |
2 | 2 | import os |
3 | 3 | import shutil |
| 4 | +import subprocess |
| 5 | +from dataclasses import dataclass |
4 | 6 | from pathlib import Path |
5 | 7 | from typing import Any, cast |
6 | 8 |
|
@@ -356,3 +358,81 @@ def multiple_versions_channel_1(channels: Path) -> str: |
356 | 358 | @pytest.fixture |
357 | 359 | def target_specific_channel_1(channels: Path) -> str: |
358 | 360 | return channels.joinpath("target_specific_channel_1").as_uri() |
| 361 | + |
| 362 | + |
| 363 | +@dataclass |
| 364 | +class LocalGitRepo: |
| 365 | + path: Path |
| 366 | + main_rev: str |
| 367 | + other_feature_rev: str |
| 368 | + tag: str |
| 369 | + |
| 370 | + |
| 371 | +@pytest.fixture |
| 372 | +def local_cpp_git_repo( |
| 373 | + pixi: Path, |
| 374 | + build_data: Path, |
| 375 | + tmp_path_factory: pytest.TempPathFactory, |
| 376 | +) -> LocalGitRepo: |
| 377 | + """ |
| 378 | + Create a local git repository mirroring cpp-with-path-to-source so tests can |
| 379 | + exercise git sources without touching the network. |
| 380 | + """ |
| 381 | + |
| 382 | + source_root = build_data.joinpath("cpp-with-path-to-source") |
| 383 | + repo_root = tmp_path_factory.mktemp("git-repo") |
| 384 | + repo_path = repo_root.joinpath("repo") |
| 385 | + shutil.copytree(source_root, repo_path) |
| 386 | + |
| 387 | + marker = repo_path.joinpath("project", "LOCAL_MARKER.txt") |
| 388 | + marker.write_text("local git fixture marker\n", encoding="utf-8") |
| 389 | + |
| 390 | + readme_path = repo_path.joinpath("README.md") |
| 391 | + |
| 392 | + def run_git(*args: str) -> str: |
| 393 | + result = subprocess.run( |
| 394 | + [str(pixi), "run", "git", *args], |
| 395 | + cwd=repo_path, |
| 396 | + capture_output=True, |
| 397 | + text=True, |
| 398 | + ) |
| 399 | + if result.returncode != 0: |
| 400 | + raise RuntimeError( |
| 401 | + "git command failed ({}):\nstdout: {}\nstderr: {}".format( |
| 402 | + " ".join(args), result.stdout, result.stderr |
| 403 | + ) |
| 404 | + ) |
| 405 | + return result.stdout.strip() |
| 406 | + |
| 407 | + run_git("init", "-b", "main") |
| 408 | + run_git( "config", "user.email", "[email protected]") |
| 409 | + run_git("config", "user.name", "Pixi Build Tests") |
| 410 | + run_git("add", ".") |
| 411 | + run_git("commit", "-m", "Initial commit") |
| 412 | + |
| 413 | + run_git("checkout", "-b", "other-feature") |
| 414 | + readme_path.write_text( |
| 415 | + readme_path.read_text(encoding="utf-8") + "\nLocal change on other-feature branch\n", |
| 416 | + encoding="utf-8", |
| 417 | + ) |
| 418 | + run_git("add", readme_path.relative_to(repo_path).as_posix()) |
| 419 | + run_git("commit", "-m", "Add branch change") |
| 420 | + other_feature_rev = run_git("rev-parse", "HEAD") |
| 421 | + |
| 422 | + run_git("checkout", "main") |
| 423 | + readme_path.write_text( |
| 424 | + readme_path.read_text(encoding="utf-8") + "\nLocal change on main\n", |
| 425 | + encoding="utf-8", |
| 426 | + ) |
| 427 | + run_git("add", readme_path.relative_to(repo_path).as_posix()) |
| 428 | + run_git("commit", "-m", "Update main") |
| 429 | + main_rev = run_git("rev-parse", "HEAD") |
| 430 | + |
| 431 | + run_git("tag", "fixture-v1") |
| 432 | + |
| 433 | + return LocalGitRepo( |
| 434 | + path=repo_path, |
| 435 | + main_rev=main_rev, |
| 436 | + other_feature_rev=other_feature_rev, |
| 437 | + tag="fixture-v1", |
| 438 | + ) |
0 commit comments