CI / security-codeql (push) Canceled after 0s
CI / test-unit (push) Canceled after 0s
CI / test-integration (push) Canceled after 0s
CI / test-env-virtual (push) Canceled after 0s
CI / test-env-nix (push) Canceled after 0s
CI / test-e2e (push) Canceled after 0s
CI / test-virgin-user (push) Canceled after 0s
CI / test-virgin-root (push) Canceled after 0s
CI / lint-shell (push) Canceled after 0s
CI / lint-python (push) Canceled after 0s
CI / lint-docker (push) Canceled after 0s
CI / quality (push) Canceled after 0s
quality/check on PR 21 flagged 4 clean.clones findings: the same bare origin, repo and init commit setup in test_branch_gc.py and risk_fixture.py. Both now call one helper in tests/support/git_sandbox.py. PR: https://github.com/kevinveenbirkenbach/package-manager/pull/21 Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
82 lines
2.4 KiB
Python
82 lines
2.4 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
AUTHOR = {
|
|
"GIT_AUTHOR_NAME": "t",
|
|
"GIT_AUTHOR_EMAIL": "t@example.org",
|
|
"GIT_COMMITTER_NAME": "t",
|
|
"GIT_COMMITTER_EMAIL": "t@example.org",
|
|
}
|
|
|
|
|
|
def git(cwd: str, *args: str) -> str:
|
|
"""
|
|
Args:
|
|
cwd: the repository to run in.
|
|
args: the git arguments; signing stays off and AUTHOR applies.
|
|
|
|
Returns:
|
|
The stripped standard output.
|
|
"""
|
|
return subprocess.run(
|
|
["git", "-c", "commit.gpgsign=false", *args],
|
|
cwd=cwd,
|
|
env={**os.environ, **AUTHOR},
|
|
check=True,
|
|
capture_output=True,
|
|
text=True,
|
|
).stdout.strip()
|
|
|
|
|
|
def origin_and_repo(root: str) -> tuple[str, str]:
|
|
"""
|
|
Args:
|
|
root: the directory that holds both repositories.
|
|
|
|
Returns:
|
|
The paths of the bare `origin.git` and of `repo`, whose `main`
|
|
tracks origin and carries one pushed commit adding `init.txt`.
|
|
"""
|
|
origin = os.path.join(root, "origin.git")
|
|
repo = os.path.join(root, "repo")
|
|
git(root, "init", "--bare", "-b", "main", origin)
|
|
git(root, "init", "-b", "main", repo)
|
|
git(repo, "remote", "add", "origin", origin)
|
|
with open(os.path.join(repo, "init.txt"), "w") as fh:
|
|
fh.write("init")
|
|
git(repo, "add", "init.txt")
|
|
git(repo, "commit", "-m", "init")
|
|
git(repo, "push", "-u", "origin", "main")
|
|
return origin, repo
|
|
|
|
|
|
def isolated_root(case: unittest.TestCase, env: dict[str, str]) -> str:
|
|
"""
|
|
Args:
|
|
case: the test whose cleanups remove the directory and restore the
|
|
working directory and environment.
|
|
env: variables set over the environment, from which every GIT_*
|
|
variable is dropped.
|
|
|
|
Returns:
|
|
The realpath of a fresh temporary directory, now the working
|
|
directory, whose empty `home` child is HOME.
|
|
"""
|
|
scratch = tempfile.TemporaryDirectory()
|
|
case.addCleanup(scratch.cleanup)
|
|
root = os.path.realpath(scratch.name)
|
|
case.addCleanup(os.chdir, os.getcwd())
|
|
os.chdir(root)
|
|
home = os.path.join(root, "home")
|
|
os.makedirs(home)
|
|
kept = {k: v for k, v in os.environ.items() if not k.startswith("GIT_")}
|
|
environ = patch.dict(os.environ, {**kept, **env, "HOME": home}, clear=True)
|
|
environ.start()
|
|
case.addCleanup(environ.stop)
|
|
return root
|