feat(create): scaffold repositories via templates with preview and mirror setup
Some checks failed
Mark stable commit / test-unit (push) Has been cancelled
Mark stable commit / test-integration (push) Has been cancelled
Mark stable commit / test-env-virtual (push) Has been cancelled
Mark stable commit / test-env-nix (push) Has been cancelled
Mark stable commit / test-e2e (push) Has been cancelled
Mark stable commit / test-virgin-user (push) Has been cancelled
Mark stable commit / test-virgin-root (push) Has been cancelled
Mark stable commit / linter-shell (push) Has been cancelled
Mark stable commit / linter-python (push) Has been cancelled
Mark stable commit / mark-stable (push) Has been cancelled

https://chatgpt.com/share/693f5bdb-1780-800f-a772-0ecf399627fc
This commit is contained in:
Kevin Veen-Birkenbach
2025-12-15 01:52:38 +01:00
parent 9e3ce34626
commit 2c15a4016b
14 changed files with 605 additions and 125 deletions

View File

@@ -0,0 +1,42 @@
from __future__ import annotations
import io
import unittest
from contextlib import redirect_stdout
from unittest.mock import patch
from pkgmgr.actions.repository.create import create_repo
class TestE2ECreateRepoPreviewOutput(unittest.TestCase):
def test_create_repo_preview_prints_expected_steps(self) -> None:
cfg = {"directories": {"repositories": "/tmp/Repositories"}, "repositories": []}
out = io.StringIO()
with (
redirect_stdout(out),
patch("pkgmgr.actions.repository.create.os.path.exists", return_value=False),
patch("pkgmgr.actions.repository.create.generate_alias", return_value="repo"),
patch("pkgmgr.actions.repository.create.save_user_config"),
patch("pkgmgr.actions.repository.create.os.makedirs"),
patch("pkgmgr.actions.repository.create.render_default_templates"),
patch("pkgmgr.actions.repository.create.write_mirrors_file"),
patch("pkgmgr.actions.repository.create.setup_mirrors"),
patch("pkgmgr.actions.repository.create.subprocess.run"),
):
create_repo(
"github.com/acme/repo",
cfg,
"/tmp/user.yml",
"/tmp/bin",
remote=False,
preview=True,
)
s = out.getvalue()
self.assertIn("[Preview] Would save user config:", s)
self.assertIn("[Preview] Would ensure directory exists:", s)
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,53 @@
from __future__ import annotations
import importlib
import io
import unittest
from contextlib import redirect_stdout
from types import SimpleNamespace
from unittest.mock import patch
class TestIntegrationReposCreatePreview(unittest.TestCase):
def test_repos_create_preview_wires_create_repo(self) -> None:
# Import lazily to avoid hard-failing if the CLI module/function name differs.
try:
repos_mod = importlib.import_module("pkgmgr.cli.commands.repos")
except Exception as exc:
self.skipTest(f"CLI module not available: {exc}")
handle = getattr(repos_mod, "handle_repos_command", None)
if handle is None:
self.skipTest("handle_repos_command not found in pkgmgr.cli.commands.repos")
ctx = SimpleNamespace(
repositories_base_dir="/tmp/Repositories",
binaries_dir="/tmp/bin",
all_repositories=[],
config_merged={"directories": {"repositories": "/tmp/Repositories"}, "repositories": []},
user_config_path="/tmp/user.yml",
)
args = SimpleNamespace(
command="create",
identifiers=["github.com/acme/repo"],
remote=False,
preview=True,
)
out = io.StringIO()
with (
redirect_stdout(out),
patch("pkgmgr.cli.commands.repos.create_repo") as create_repo,
):
handle(args, ctx, selected=[])
create_repo.assert_called_once()
called = create_repo.call_args.kwargs
self.assertEqual(called["remote"], False)
self.assertEqual(called["preview"], True)
self.assertEqual(create_repo.call_args.args[0], "github.com/acme/repo")
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,62 @@
from __future__ import annotations
import unittest
from pkgmgr.actions.repository.create import (
RepoParts,
_parse_identifier,
_parse_git_url,
_strip_git_suffix,
_split_host_port,
)
class TestRepositoryCreateParsing(unittest.TestCase):
def test_strip_git_suffix(self) -> None:
self.assertEqual(_strip_git_suffix("repo.git"), "repo")
self.assertEqual(_strip_git_suffix("repo"), "repo")
def test_split_host_port(self) -> None:
self.assertEqual(_split_host_port("example.com"), ("example.com", None))
self.assertEqual(_split_host_port("example.com:2222"), ("example.com", "2222"))
self.assertEqual(_split_host_port("example.com:"), ("example.com", None))
def test_parse_identifier_plain(self) -> None:
parts = _parse_identifier("github.com/owner/repo")
self.assertIsInstance(parts, RepoParts)
self.assertEqual(parts.host, "github.com")
self.assertEqual(parts.port, None)
self.assertEqual(parts.owner, "owner")
self.assertEqual(parts.name, "repo")
def test_parse_identifier_with_port(self) -> None:
parts = _parse_identifier("gitea.example.com:2222/org/repo")
self.assertEqual(parts.host, "gitea.example.com")
self.assertEqual(parts.port, "2222")
self.assertEqual(parts.owner, "org")
self.assertEqual(parts.name, "repo")
def test_parse_git_url_scp_style(self) -> None:
parts = _parse_git_url("git@github.com:owner/repo.git")
self.assertEqual(parts.host, "github.com")
self.assertEqual(parts.port, None)
self.assertEqual(parts.owner, "owner")
self.assertEqual(parts.name, "repo")
def test_parse_git_url_https(self) -> None:
parts = _parse_git_url("https://github.com/owner/repo.git")
self.assertEqual(parts.host, "github.com")
self.assertEqual(parts.port, None)
self.assertEqual(parts.owner, "owner")
self.assertEqual(parts.name, "repo")
def test_parse_git_url_ssh_with_port(self) -> None:
parts = _parse_git_url("ssh://git@gitea.example.com:2222/org/repo.git")
self.assertEqual(parts.host, "gitea.example.com")
self.assertEqual(parts.port, "2222")
self.assertEqual(parts.owner, "org")
self.assertEqual(parts.name, "repo")
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,35 @@
from __future__ import annotations
import unittest
from unittest.mock import patch
from pkgmgr.actions.repository.scaffold import render_default_templates
class TestScaffoldRenderPreview(unittest.TestCase):
def test_render_preview_does_not_write(self) -> None:
with (
patch("pkgmgr.actions.repository.scaffold._templates_dir", return_value="/tpl"),
patch("pkgmgr.actions.repository.scaffold.os.path.isdir", return_value=True),
patch("pkgmgr.actions.repository.scaffold.os.walk", return_value=[("/tpl", [], ["README.md.j2"])]),
patch("pkgmgr.actions.repository.scaffold.os.path.relpath", return_value="README.md.j2"),
patch("pkgmgr.actions.repository.scaffold.os.makedirs") as mk,
patch("pkgmgr.actions.repository.scaffold.open", create=True) as op,
patch("pkgmgr.actions.repository.scaffold.Environment") as env_cls,
):
env = env_cls.return_value
env.get_template.return_value.render.return_value = "X"
render_default_templates(
"/repo",
context={"repository": "x"},
preview=True,
)
mk.assert_not_called()
op.assert_not_called()
env.get_template.assert_not_called()
if __name__ == "__main__":
unittest.main()