refactor(git): split git helpers into run/commands/queries and update branch, mirror and changelog actions
https://chatgpt.com/share/69411b4a-fcf8-800f-843d-61c913f388eb
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from pkgmgr.actions.branch.utils import _resolve_base_branch
|
||||
from pkgmgr.core.git import GitError
|
||||
|
||||
|
||||
class TestResolveBaseBranch(unittest.TestCase):
|
||||
@patch("pkgmgr.actions.branch.utils.run_git")
|
||||
def test_resolves_preferred(self, run_git):
|
||||
run_git.return_value = None
|
||||
result = _resolve_base_branch("main", "master", cwd=".")
|
||||
self.assertEqual(result, "main")
|
||||
run_git.assert_called_with(["rev-parse", "--verify", "main"], cwd=".")
|
||||
|
||||
@patch("pkgmgr.actions.branch.utils.run_git")
|
||||
def test_resolves_fallback(self, run_git):
|
||||
run_git.side_effect = [
|
||||
GitError("main missing"),
|
||||
None,
|
||||
]
|
||||
result = _resolve_base_branch("main", "master", cwd=".")
|
||||
self.assertEqual(result, "master")
|
||||
|
||||
@patch("pkgmgr.actions.branch.utils.run_git")
|
||||
def test_raises_when_no_branch_exists(self, run_git):
|
||||
run_git.side_effect = GitError("missing")
|
||||
with self.assertRaises(RuntimeError):
|
||||
_resolve_base_branch("main", "master", cwd=".")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from pkgmgr.actions.mirror.remote_check import probe_mirror
|
||||
from pkgmgr.core.git import GitError
|
||||
|
||||
|
||||
class TestRemoteCheck(unittest.TestCase):
|
||||
"""
|
||||
Unit tests for non-destructive remote probing (git ls-remote).
|
||||
"""
|
||||
|
||||
@patch("pkgmgr.actions.mirror.remote_check.run_git")
|
||||
def test_probe_mirror_success_returns_true_and_empty_message(self, mock_run_git) -> None:
|
||||
mock_run_git.return_value = "dummy-output"
|
||||
|
||||
ok, message = probe_mirror(
|
||||
"ssh://git@code.example.org:2201/alice/repo.git",
|
||||
"/tmp/some-repo",
|
||||
)
|
||||
|
||||
self.assertTrue(ok)
|
||||
self.assertEqual(message, "")
|
||||
mock_run_git.assert_called_once_with(
|
||||
["ls-remote", "ssh://git@code.example.org:2201/alice/repo.git"],
|
||||
cwd="/tmp/some-repo",
|
||||
)
|
||||
|
||||
@patch("pkgmgr.actions.mirror.remote_check.run_git")
|
||||
def test_probe_mirror_failure_returns_false_and_error_message(self, mock_run_git) -> None:
|
||||
mock_run_git.side_effect = GitError("Git command failed (simulated)")
|
||||
|
||||
ok, message = probe_mirror(
|
||||
"ssh://git@code.example.org:2201/alice/repo.git",
|
||||
"/tmp/some-repo",
|
||||
)
|
||||
|
||||
self.assertFalse(ok)
|
||||
self.assertIn("Git command failed", message)
|
||||
mock_run_git.assert_called_once_with(
|
||||
["ls-remote", "ssh://git@code.example.org:2201/alice/repo.git"],
|
||||
cwd="/tmp/some-repo",
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
0
tests/unit/pkgmgr/core/git/queries/__init__.py
Normal file
0
tests/unit/pkgmgr/core/git/queries/__init__.py
Normal file
50
tests/unit/pkgmgr/core/git/queries/test_remote_check.py
Normal file
50
tests/unit/pkgmgr/core/git/queries/test_remote_check.py
Normal file
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from pkgmgr.core.git import GitError
|
||||
from pkgmgr.core.git.queries.probe_remote_reachable import probe_remote_reachable
|
||||
|
||||
|
||||
class TestProbeRemoteReachable(unittest.TestCase):
|
||||
"""
|
||||
Unit tests for non-destructive remote probing (git ls-remote).
|
||||
"""
|
||||
|
||||
@patch("pkgmgr.core.git.queries.probe_remote_reachable.run")
|
||||
def test_probe_remote_reachable_success_returns_true(self, mock_run) -> None:
|
||||
mock_run.return_value = "dummy-output"
|
||||
|
||||
ok = probe_remote_reachable(
|
||||
"ssh://git@code.example.org:2201/alice/repo.git",
|
||||
cwd="/tmp/some-repo",
|
||||
)
|
||||
|
||||
self.assertTrue(ok)
|
||||
mock_run.assert_called_once_with(
|
||||
["ls-remote", "--exit-code", "ssh://git@code.example.org:2201/alice/repo.git"],
|
||||
cwd="/tmp/some-repo",
|
||||
)
|
||||
|
||||
@patch("pkgmgr.core.git.queries.probe_remote_reachable.run")
|
||||
def test_probe_remote_reachable_failure_returns_false(self, mock_run) -> None:
|
||||
mock_run.side_effect = GitError("Git command failed (simulated)")
|
||||
|
||||
ok = probe_remote_reachable(
|
||||
"ssh://git@code.example.org:2201/alice/repo.git",
|
||||
cwd="/tmp/some-repo",
|
||||
)
|
||||
|
||||
self.assertFalse(ok)
|
||||
mock_run.assert_called_once_with(
|
||||
["ls-remote", "--exit-code", "ssh://git@code.example.org:2201/alice/repo.git"],
|
||||
cwd="/tmp/some-repo",
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,43 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from pkgmgr.core.git import GitError
|
||||
from pkgmgr.core.git.queries.resolve_base_branch import (
|
||||
GitBaseBranchNotFoundError,
|
||||
resolve_base_branch,
|
||||
)
|
||||
|
||||
|
||||
class TestResolveBaseBranch(unittest.TestCase):
|
||||
@patch("pkgmgr.core.git.queries.resolve_base_branch.run")
|
||||
def test_resolves_preferred(self, mock_run):
|
||||
mock_run.return_value = "dummy"
|
||||
result = resolve_base_branch("main", "master", cwd=".")
|
||||
self.assertEqual(result, "main")
|
||||
mock_run.assert_called_with(["rev-parse", "--verify", "main"], cwd=".")
|
||||
|
||||
@patch("pkgmgr.core.git.queries.resolve_base_branch.run")
|
||||
def test_resolves_fallback(self, mock_run):
|
||||
mock_run.side_effect = [
|
||||
GitError("fatal: Needed a single revision"), # treat as "missing"
|
||||
"dummy",
|
||||
]
|
||||
result = resolve_base_branch("main", "master", cwd=".")
|
||||
self.assertEqual(result, "master")
|
||||
self.assertEqual(mock_run.call_args_list[0].kwargs["cwd"], ".")
|
||||
self.assertEqual(mock_run.call_args_list[1].kwargs["cwd"], ".")
|
||||
|
||||
@patch("pkgmgr.core.git.queries.resolve_base_branch.run")
|
||||
def test_raises_when_no_branch_exists(self, mock_run):
|
||||
mock_run.side_effect = [
|
||||
GitError("fatal: Needed a single revision"),
|
||||
GitError("fatal: Needed a single revision"),
|
||||
]
|
||||
with self.assertRaises(GitBaseBranchNotFoundError):
|
||||
resolve_base_branch("main", "master", cwd=".")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user