diff --git a/tests/integration/test_branch_gc.py b/tests/integration/test_branch_gc.py index 4c88427..af0c600 100644 --- a/tests/integration/test_branch_gc.py +++ b/tests/integration/test_branch_gc.py @@ -13,7 +13,7 @@ from pkgmgr.actions.branch import gc from pkgmgr.actions.branch.gc_forge import ForgeFacts from pkgmgr.cli.commands.branch import handle_branch from pkgmgr.cli.parser import create_parser -from tests.support.git_sandbox import git, isolated_root +from tests.support.git_sandbox import git, isolated_root, origin_and_repo def docker_available() -> bool: @@ -27,13 +27,7 @@ def docker_available() -> bool: class GcFixture: def setUp(self) -> None: self.root = isolated_root(self, {}) - self.origin = os.path.join(self.root, "origin.git") - self.repo = os.path.join(self.root, "repo") - git(self.root, "init", "--bare", "-b", "main", self.origin) - git(self.root, "init", "-b", "main", self.repo) - git(self.repo, "remote", "add", "origin", self.origin) - self._commit("init") - git(self.repo, "push", "-u", "origin", "main") + self.origin, self.repo = origin_and_repo(self.root) self._branch_with_commit("merged") git(self.repo, "merge", "--no-ff", "-m", "merge merged", "merged") diff --git a/tests/support/git_sandbox.py b/tests/support/git_sandbox.py index f9cc6d4..1e88e5c 100644 --- a/tests/support/git_sandbox.py +++ b/tests/support/git_sandbox.py @@ -33,6 +33,28 @@ def git(cwd: str, *args: str) -> str: ).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: diff --git a/tests/unit/pkgmgr/actions/gc/risk/risk_fixture.py b/tests/unit/pkgmgr/actions/gc/risk/risk_fixture.py index b8e3ffc..3bf15cb 100644 --- a/tests/unit/pkgmgr/actions/gc/risk/risk_fixture.py +++ b/tests/unit/pkgmgr/actions/gc/risk/risk_fixture.py @@ -12,7 +12,7 @@ from unittest.mock import patch from pkgmgr.actions.branch import gc from pkgmgr.actions.branch.gc_forge import ForgeFacts from pkgmgr.actions.gc.risk import docker_items, plan -from tests.support.git_sandbox import git, isolated_root +from tests.support.git_sandbox import git, isolated_root, origin_and_repo DAY = 86400 REAL_RUN = subprocess.run @@ -183,13 +183,7 @@ class RiskPlanFixture: git(self.repo, "push", "origin", "main") def _repos(self) -> None: - self.origin = os.path.join(self.root, "origin.git") - self.repo = os.path.join(self.root, "repo") - git(self.root, "init", "--bare", "-b", "main", self.origin) - git(self.root, "init", "-b", "main", self.repo) - git(self.repo, "remote", "add", "origin", self.origin) - self._commit(self.repo, "init") - git(self.repo, "push", "-u", "origin", "main") + self.origin, self.repo = origin_and_repo(self.root) self._branch("cherry", push=False) for name in ("done", "stack", "old"): self._branch(name, push=True, merge=True)