mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-28 22:06:31 +00:00
59 test files imported `unittest` and then `from unittest import mock`, giving the same names two spellings so a call site no longer says which one it follows. CodeQL flags this as py/import-and-import-from; at 59 occurrences it is the single largest item in the repo's code-scanning backlog. Keep `import unittest` and reach the submodule as `import unittest.mock as mock`. Both are ast.Import, which is what the rule asks for, and `mock.<attr>` call sites stay untouched — the diff is exactly one line per file and nothing else. The opposite direction would have been wrong here: PLR0402 rewrites `import a.b as c` into `from a import b`, i.e. straight back into the pattern CodeQL flags. It is deliberately absent from the ruff select list in pyproject.toml, so nothing in the toolchain will undo this. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
151 lines
5.1 KiB
Python
151 lines
5.1 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
import unittest.mock as mock
|
|
from pathlib import Path
|
|
|
|
from utils.docker import mirror
|
|
|
|
_CONFIG_WITH_FORK = """\
|
|
[core]
|
|
\trepositoryformatversion = 0
|
|
[remote "origin"]
|
|
\turl = git@github.com:upstream-org/core.git
|
|
[remote "fork"]
|
|
\turl = https://github.com/a-user/a-repo.git
|
|
[remote]
|
|
\tpushDefault = fork
|
|
[branch "main"]
|
|
\tvscode-merge-base = origin/main
|
|
\tvscode-merge-base = fork/main
|
|
"""
|
|
|
|
_CONFIG_ORIGIN_ONLY = """\
|
|
[remote "origin"]
|
|
\turl = git@github.com:upstream-org/core.git
|
|
"""
|
|
|
|
|
|
class _RepoConfig(unittest.TestCase):
|
|
def _with_config(self, body: str | None):
|
|
root = tempfile.TemporaryDirectory()
|
|
self.addCleanup(root.cleanup)
|
|
if body is not None:
|
|
git_dir = Path(root.name) / ".git"
|
|
git_dir.mkdir()
|
|
(git_dir / "config").write_text(body, encoding="utf-8")
|
|
return mock.patch.object(mirror, "PROJECT_ROOT", Path(root.name))
|
|
|
|
|
|
class TestRemoteUrl(_RepoConfig, unittest.TestCase):
|
|
def test_push_default_wins_over_origin(self) -> None:
|
|
with self._with_config(_CONFIG_WITH_FORK):
|
|
self.assertEqual(
|
|
mirror._remote_url(), "https://github.com/a-user/a-repo.git"
|
|
)
|
|
|
|
def test_origin_is_used_without_a_push_default(self) -> None:
|
|
with self._with_config(_CONFIG_ORIGIN_ONLY):
|
|
self.assertEqual(
|
|
mirror._remote_url(), "git@github.com:upstream-org/core.git"
|
|
)
|
|
|
|
def test_duplicate_keys_do_not_break_parsing(self) -> None:
|
|
with self._with_config(_CONFIG_WITH_FORK):
|
|
self.assertIn("branch.main.vscode-merge-base", mirror._git_config())
|
|
|
|
def test_missing_config_yields_no_url(self) -> None:
|
|
with self._with_config(None):
|
|
self.assertIsNone(mirror._remote_url())
|
|
|
|
|
|
class TestOwnerAndRepository(_RepoConfig, unittest.TestCase):
|
|
def test_github_environment_wins(self) -> None:
|
|
with (
|
|
self._with_config(_CONFIG_WITH_FORK),
|
|
mock.patch.dict(
|
|
mirror.os.environ,
|
|
{"GITHUB_REPOSITORY": "Env-Owner/Env-Repo"},
|
|
clear=True,
|
|
),
|
|
):
|
|
self.assertEqual(mirror._owner_and_repository(), ("env-owner", "env-repo"))
|
|
|
|
def test_repository_owner_wins_over_the_slug(self) -> None:
|
|
with (
|
|
self._with_config(_CONFIG_WITH_FORK),
|
|
mock.patch.dict(
|
|
mirror.os.environ,
|
|
{
|
|
"GITHUB_REPOSITORY": "Env-Owner/Env-Repo",
|
|
"GITHUB_REPOSITORY_OWNER": "Explicit-Owner",
|
|
},
|
|
clear=True,
|
|
),
|
|
):
|
|
self.assertEqual(
|
|
mirror._owner_and_repository(), ("explicit-owner", "env-repo")
|
|
)
|
|
|
|
def test_ssh_remote_is_parsed(self) -> None:
|
|
with (
|
|
self._with_config(_CONFIG_ORIGIN_ONLY),
|
|
mock.patch.dict(mirror.os.environ, {}, clear=True),
|
|
):
|
|
self.assertEqual(mirror._owner_and_repository(), ("upstream-org", "core"))
|
|
|
|
def test_https_remote_is_parsed(self) -> None:
|
|
with (
|
|
self._with_config(_CONFIG_WITH_FORK),
|
|
mock.patch.dict(mirror.os.environ, {}, clear=True),
|
|
):
|
|
self.assertEqual(mirror._owner_and_repository(), ("a-user", "a-repo"))
|
|
|
|
|
|
class TestMirrorImage(_RepoConfig, unittest.TestCase):
|
|
def _env(self, **overrides):
|
|
env = {"INFINITO_GHCR_MIRROR_PREFIX": "mirror"}
|
|
env.update(overrides)
|
|
return mock.patch.dict(mirror.os.environ, env, clear=True)
|
|
|
|
def test_implicit_docker_hub_image(self) -> None:
|
|
with self._with_config(_CONFIG_WITH_FORK), self._env():
|
|
self.assertEqual(
|
|
mirror.mirror_image("postgres"),
|
|
"ghcr.io/a-user/a-repo/mirror/docker.io/postgres",
|
|
)
|
|
|
|
def test_explicit_registry_is_kept_in_the_path(self) -> None:
|
|
with self._with_config(_CONFIG_WITH_FORK), self._env():
|
|
self.assertEqual(
|
|
mirror.mirror_image("quay.io/keycloak/keycloak"),
|
|
"ghcr.io/a-user/a-repo/mirror/quay.io/keycloak/keycloak",
|
|
)
|
|
|
|
def test_docker_hub_aliases_normalise(self) -> None:
|
|
with self._with_config(_CONFIG_WITH_FORK), self._env():
|
|
self.assertEqual(
|
|
mirror.mirror_image("docker.io/library/nginx"),
|
|
"ghcr.io/a-user/a-repo/mirror/docker.io/library/nginx",
|
|
)
|
|
|
|
def test_no_prefix_means_no_mirror(self) -> None:
|
|
with (
|
|
self._with_config(_CONFIG_WITH_FORK),
|
|
self._env(INFINITO_GHCR_MIRROR_PREFIX=""),
|
|
):
|
|
self.assertIsNone(mirror.mirror_image("postgres"))
|
|
|
|
def test_unresolvable_namespace_means_no_mirror(self) -> None:
|
|
with self._with_config(None), self._env():
|
|
self.assertIsNone(mirror.mirror_image("postgres"))
|
|
|
|
def test_malformed_image_means_no_mirror(self) -> None:
|
|
with self._with_config(_CONFIG_WITH_FORK), self._env():
|
|
self.assertIsNone(mirror.mirror_image("Not A Valid Image"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|