gpt-5.2 ChatGPT: adapt tests to new core.git commands/queries split

- Update mirror integration tests to use probe_remote_reachable
- Refactor branch action tests to mock git command helpers instead of run_git
- Align changelog tests with get_changelog query API
- Update git core tests to cover run() and query helpers
- Remove legacy run_git assumptions from tests

https://chatgpt.com/share/69412008-9e8c-800f-9ac9-90f390d55380

**Validated by Google's model.**

**Summary:**
The test modifications have been correctly implemented to cover the Git refactoring changes:

1.  **Granular Mocking:** The tests have shifted from mocking the monolithic `run_git` or `subprocess` to mocking the new, specific wrapper functions (e.g., `pkgmgr.core.git.commands.fetch`, `pkgmgr.core.git.queries.probe_remote_reachable`). This accurately reflects the architectural change in the source code where business logic now relies on these granular imports.
2.  **Structural Alignment:** The test directory structure was updated (e.g., moving tests to `tests/unit/pkgmgr/core/git/queries/`) to match the new source code organization, ensuring logical consistency.
3.  **Exception Handling:** The tests were updated to verify specific exception types (like `GitDeleteRemoteBranchError`) rather than generic errors, ensuring the improved error granularity is correctly handled by the CLI.
4.  **Integration Safety:** The integration tests in `test_mirror_commands.py` were correctly updated to patch the new query paths, ensuring that network operations remain disabled during testing.

The test changes are consistent with the refactor and provide complete coverage for the new code structure.
https://aistudio.google.com/app/prompts?state=%7B%22ids%22:%5B%2214Br1JG1hxuntmoRzuvme3GKUvQ0heqRn%22%5D,%22action%22:%22open%22,%22userId%22:%22109171005420801378245%22,%22resourceKeys%22:%7B%7D%7D&usp=sharing
This commit is contained in:
Kevin Veen-Birkenbach
2025-12-16 10:01:30 +01:00
parent 755b78fcb7
commit e117115b7f
6 changed files with 181 additions and 214 deletions

View File

@@ -4,44 +4,28 @@ import unittest
from unittest.mock import patch
from pkgmgr.actions.changelog import generate_changelog
from pkgmgr.core.git import GitError
from pkgmgr.core.git.queries import GitChangelogQueryError
from pkgmgr.cli.commands.changelog import _find_previous_and_current_tag
class TestGenerateChangelog(unittest.TestCase):
@patch("pkgmgr.actions.changelog.run_git")
def test_generate_changelog_default_range_no_merges(self, mock_run_git) -> None:
"""
Default behaviour:
- to_ref = HEAD
- from_ref = None
- include_merges = False -> adds --no-merges
"""
mock_run_git.return_value = "abc123 (HEAD -> main) Initial commit"
@patch("pkgmgr.actions.changelog.get_changelog")
def test_generate_changelog_default_range_no_merges(self, mock_get_changelog) -> None:
mock_get_changelog.return_value = "abc123 (HEAD -> main) Initial commit"
output = generate_changelog(cwd="/repo")
self.assertEqual(
output,
"abc123 (HEAD -> main) Initial commit",
self.assertEqual(output, "abc123 (HEAD -> main) Initial commit")
mock_get_changelog.assert_called_once_with(
cwd="/repo",
from_ref=None,
to_ref="HEAD",
include_merges=False,
)
mock_run_git.assert_called_once()
args, kwargs = mock_run_git.call_args
# Command must start with git log and include our pretty format.
self.assertEqual(args[0][0], "log")
self.assertIn("--pretty=format:%h %d %s", args[0])
self.assertIn("--no-merges", args[0])
self.assertIn("HEAD", args[0])
self.assertEqual(kwargs.get("cwd"), "/repo")
@patch("pkgmgr.actions.changelog.run_git")
def test_generate_changelog_with_range_and_merges(self, mock_run_git) -> None:
"""
Explicit range and include_merges=True:
- from_ref/to_ref are combined into from..to
- no --no-merges flag
"""
mock_run_git.return_value = "def456 (tag: v1.1.0) Some change"
@patch("pkgmgr.actions.changelog.get_changelog")
def test_generate_changelog_with_range_and_merges(self, mock_get_changelog) -> None:
mock_get_changelog.return_value = "def456 (tag: v1.1.0) Some change"
output = generate_changelog(
cwd="/repo",
@@ -51,24 +35,16 @@ class TestGenerateChangelog(unittest.TestCase):
)
self.assertEqual(output, "def456 (tag: v1.1.0) Some change")
mock_run_git.assert_called_once()
args, kwargs = mock_run_git.call_args
mock_get_changelog.assert_called_once_with(
cwd="/repo",
from_ref="v1.0.0",
to_ref="v1.1.0",
include_merges=True,
)
cmd = args[0]
self.assertEqual(cmd[0], "log")
self.assertIn("--pretty=format:%h %d %s", cmd)
# include_merges=True -> no --no-merges flag
self.assertNotIn("--no-merges", cmd)
# Range must be exactly v1.0.0..v1.1.0
self.assertIn("v1.0.0..v1.1.0", cmd)
self.assertEqual(kwargs.get("cwd"), "/repo")
@patch("pkgmgr.actions.changelog.run_git")
def test_generate_changelog_giterror_returns_error_message(self, mock_run_git) -> None:
"""
If Git fails, we do NOT raise; instead we return a human readable error string.
"""
mock_run_git.side_effect = GitError("simulated git failure")
@patch("pkgmgr.actions.changelog.get_changelog")
def test_generate_changelog_giterror_returns_error_message(self, mock_get_changelog) -> None:
mock_get_changelog.side_effect = GitChangelogQueryError("simulated git failure")
result = generate_changelog(cwd="/repo", from_ref="v0.1.0", to_ref="v0.2.0")
@@ -76,12 +52,9 @@ class TestGenerateChangelog(unittest.TestCase):
self.assertIn("simulated git failure", result)
self.assertIn("v0.1.0..v0.2.0", result)
@patch("pkgmgr.actions.changelog.run_git")
def test_generate_changelog_empty_output_returns_info(self, mock_run_git) -> None:
"""
Empty git log output -> informational message instead of empty string.
"""
mock_run_git.return_value = " \n "
@patch("pkgmgr.actions.changelog.get_changelog")
def test_generate_changelog_empty_output_returns_info(self, mock_get_changelog) -> None:
mock_get_changelog.return_value = " \n "
result = generate_changelog(cwd="/repo", from_ref=None, to_ref="HEAD")
@@ -90,49 +63,38 @@ class TestGenerateChangelog(unittest.TestCase):
class TestFindPreviousAndCurrentTag(unittest.TestCase):
def test_no_semver_tags_returns_none_none(self) -> None:
tags = ["foo", "bar", "v1.2", "v1.2.3.4"] # all invalid for SemVer
tags = ["foo", "bar", "v1.2", "v1.2.3.4"]
prev_tag, cur_tag = _find_previous_and_current_tag(tags)
self.assertIsNone(prev_tag)
self.assertIsNone(cur_tag)
def test_latest_tags_when_no_target_given(self) -> None:
"""
When no target tag is given, the function should return:
(second_latest_semver_tag, latest_semver_tag)
based on semantic version ordering, not lexicographic order.
"""
tags = ["v1.0.0", "v1.2.0", "v1.1.0", "not-a-tag"]
prev_tag, cur_tag = _find_previous_and_current_tag(tags)
self.assertEqual(prev_tag, "v1.1.0")
self.assertEqual(cur_tag, "v1.2.0")
def test_single_semver_tag_returns_none_and_that_tag(self) -> None:
tags = ["v0.1.0"]
prev_tag, cur_tag = _find_previous_and_current_tag(tags)
self.assertIsNone(prev_tag)
self.assertEqual(cur_tag, "v0.1.0")
def test_with_target_tag_in_the_middle(self) -> None:
tags = ["v1.0.0", "v1.1.0", "v1.2.0"]
prev_tag, cur_tag = _find_previous_and_current_tag(tags, target_tag="v1.1.0")
self.assertEqual(prev_tag, "v1.0.0")
self.assertEqual(cur_tag, "v1.1.0")
def test_with_target_tag_first_has_no_previous(self) -> None:
tags = ["v1.0.0", "v1.1.0"]
prev_tag, cur_tag = _find_previous_and_current_tag(tags, target_tag="v1.0.0")
self.assertIsNone(prev_tag)
self.assertEqual(cur_tag, "v1.0.0")
def test_unknown_target_tag_returns_none_none(self) -> None:
tags = ["v1.0.0", "v1.1.0"]
prev_tag, cur_tag = _find_previous_and_current_tag(tags, target_tag="v2.0.0")
self.assertIsNone(prev_tag)
self.assertIsNone(cur_tag)