Repository-wide mechanical cleanup so `ruff check src tests` has a chance of passing; no behavioural changes. - Add `from __future__ import annotations` where PEP 604 unions are used. This has to come first: pyproject declares requires-python >= 3.9, where `X | None` is not evaluable at runtime unless annotations are stringified. - Replace typing.List/Dict/Tuple/Set with the builtin generics and Optional[X] with X | None, then drop the imports that became unused. The four actions/*/__init__.py files needed this by hand because ruff leaves unused imports in __init__.py alone (possible re-exports). - Strip shebangs from 72 importable modules. None of them are executable or invoked directly; the entry points are console_scripts and runpy. - Flatten nested `with` blocks, collapse needless-bool returns, and apply the remaining mechanical ruff fixes (PIE810, FLY002, PERF102, FURB192, RUF059, I001). - Pass check=False explicitly to the four subprocess.run() calls that inspect returncode themselves. That is the existing default. Two rewrites are visible to mocks, so their tests move with them: subprocess.run(stdout=PIPE, stderr=PIPE) became capture_output=True, and open(path, "r", ...) lost the redundant mode. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
85 lines
3.0 KiB
Python
85 lines
3.0 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from pkgmgr.actions.mirror.url_utils import (
|
|
hostport_from_git_url,
|
|
normalize_provider_host,
|
|
parse_repo_from_git_url,
|
|
)
|
|
|
|
|
|
class TestUrlUtils(unittest.TestCase):
|
|
"""
|
|
Unit tests for URL parsing helpers used in mirror setup/provisioning.
|
|
"""
|
|
|
|
def test_hostport_from_git_url_ssh_url_with_port(self) -> None:
|
|
host, port = hostport_from_git_url(
|
|
"ssh://git@code.example.org:2201/alice/repo.git"
|
|
)
|
|
self.assertEqual(host, "code.example.org")
|
|
self.assertEqual(port, "2201")
|
|
|
|
def test_hostport_from_git_url_https_url_no_port(self) -> None:
|
|
host, port = hostport_from_git_url("https://github.com/alice/repo.git")
|
|
self.assertEqual(host, "github.com")
|
|
self.assertIsNone(port)
|
|
|
|
def test_hostport_from_git_url_scp_like(self) -> None:
|
|
host, port = hostport_from_git_url("git@github.com:alice/repo.git")
|
|
self.assertEqual(host, "github.com")
|
|
self.assertIsNone(port)
|
|
|
|
def test_hostport_from_git_url_empty(self) -> None:
|
|
host, port = hostport_from_git_url("")
|
|
self.assertEqual(host, "")
|
|
self.assertIsNone(port)
|
|
|
|
def test_normalize_provider_host_strips_port_and_lowercases(self) -> None:
|
|
self.assertEqual(
|
|
normalize_provider_host("GIT.VEEN.WORLD:2201"), "git.veen.world"
|
|
)
|
|
|
|
def test_normalize_provider_host_ipv6_brackets(self) -> None:
|
|
self.assertEqual(normalize_provider_host("[::1]"), "::1")
|
|
|
|
def test_normalize_provider_host_empty(self) -> None:
|
|
self.assertEqual(normalize_provider_host(""), "")
|
|
|
|
def test_parse_repo_from_git_url_ssh_url(self) -> None:
|
|
host, owner, name = parse_repo_from_git_url(
|
|
"ssh://git@code.example.org:2201/alice/repo.git"
|
|
)
|
|
self.assertEqual(host, "code.example.org")
|
|
self.assertEqual(owner, "alice")
|
|
self.assertEqual(name, "repo")
|
|
|
|
def test_parse_repo_from_git_url_https_url(self) -> None:
|
|
host, owner, name = parse_repo_from_git_url("https://github.com/alice/repo.git")
|
|
self.assertEqual(host, "github.com")
|
|
self.assertEqual(owner, "alice")
|
|
self.assertEqual(name, "repo")
|
|
|
|
def test_parse_repo_from_git_url_scp_like(self) -> None:
|
|
host, owner, name = parse_repo_from_git_url("git@github.com:alice/repo.git")
|
|
self.assertEqual(host, "github.com")
|
|
self.assertEqual(owner, "alice")
|
|
self.assertEqual(name, "repo")
|
|
|
|
def test_parse_repo_from_git_url_best_effort_host_owner_repo(self) -> None:
|
|
host, owner, name = parse_repo_from_git_url("git.veen.world/alice/repo.git")
|
|
self.assertEqual(host, "git.veen.world")
|
|
self.assertEqual(owner, "alice")
|
|
self.assertEqual(name, "repo")
|
|
|
|
def test_parse_repo_from_git_url_missing_owner_repo_returns_none(self) -> None:
|
|
host, owner, name = parse_repo_from_git_url("https://github.com/")
|
|
self.assertEqual(host, "github.com")
|
|
self.assertIsNone(owner)
|
|
self.assertIsNone(name)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|