utils/feedback_inbox/feedback_inbox/tests/test_origin.py
Krzysztof kuhy Rudnicki f05845e2f7 Read GitHub repo from checkout origin
feedback_inbox and app_errors guessed kuhyx/<folder> for a task's repo,
which filed cleanup-plate under kuhyx/cleanup-plate (real:
Sepci0/cleanup-plate) and showed phantom repos on the board. Both now
read the GitHub owner/name from ~/src/<folder>'s origin remote (https or
ssh forms) and fall back to the guess only when there is no checkout or
its origin is not on GitHub. app-errors 0.1.2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZfgt44aEKxvGBJkwP
2026-11-19 18:08:26 +01:00

81 lines
2.6 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki
"""Origin lookup: every remote URL form, and every way a checkout has none."""
from __future__ import annotations
import subprocess
from typing import TYPE_CHECKING
import pytest
from feedback_inbox import _origin
from feedback_inbox._origin import origin_slug, repo_slug, slug_from_url
from feedback_inbox.tests.test_support import make_checkout
if TYPE_CHECKING:
from pathlib import Path
@pytest.mark.parametrize(
"url",
[
"https://github.com/Sepci0/cleanup-plate",
"https://github.com/Sepci0/cleanup-plate.git",
"https://github.com/Sepci0/cleanup-plate/\n",
"https://kuhy@github.com/Sepci0/cleanup-plate.git",
"git@github.com:Sepci0/cleanup-plate.git",
"ssh://git@github.com/Sepci0/cleanup-plate",
"ssh://git@github.com:22/Sepci0/cleanup-plate.git",
],
)
def test_github_urls_give_the_slug(url: str) -> None:
assert slug_from_url(url) == "Sepci0/cleanup-plate"
@pytest.mark.parametrize(
"url",
[
"/home/someone/mirror/cleanup-plate",
"file:///srv/git/cleanup-plate.git",
"https://gitea.example.org/Sepci0/cleanup-plate.git",
"https://github.com/Sepci0",
"https://github.com/Sepci0/cleanup-plate/extra",
"",
],
)
def test_other_urls_give_none(url: str) -> None:
assert slug_from_url(url) is None
def test_checkout_origin_is_read(tmp_path: Path) -> None:
repo = make_checkout(tmp_path / "todo", "https://github.com/kuhyx/todo-app.git")
assert origin_slug(repo) == "kuhyx/todo-app"
def test_no_checkout_or_no_origin_is_none(tmp_path: Path) -> None:
(tmp_path / "plain").mkdir()
assert origin_slug(tmp_path / "missing") is None
assert origin_slug(tmp_path / "plain") is None
assert origin_slug(make_checkout(tmp_path / "bare", None)) is None
def test_missing_or_hung_git_is_none(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
repo = make_checkout(tmp_path / "todo", "https://github.com/kuhyx/todo-app")
def hang(*args: object, **kwargs: object) -> None:
msg = "git"
raise subprocess.TimeoutExpired(msg, _origin.GIT_TIMEOUT)
monkeypatch.setattr(_origin.subprocess, "run", hang)
assert origin_slug(repo) is None
monkeypatch.setattr(_origin.shutil, "which", lambda _: None)
assert origin_slug(repo) is None
def test_repo_slug_falls_back_to_the_owner_guess(tmp_path: Path) -> None:
make_checkout(tmp_path / "todo", "git@github.com:kuhyx/todo-app.git")
assert repo_slug(tmp_path, "todo", "kuhyx") == "kuhyx/todo-app"
assert repo_slug(tmp_path, "museum", "kuhyx") == "kuhyx/museum"