- 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
104 lines
3.9 KiB
Python
104 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from pkgmgr.actions.changelog import generate_changelog
|
|
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.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")
|
|
mock_get_changelog.assert_called_once_with(
|
|
cwd="/repo",
|
|
from_ref=None,
|
|
to_ref="HEAD",
|
|
include_merges=False,
|
|
)
|
|
|
|
@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",
|
|
from_ref="v1.0.0",
|
|
to_ref="v1.1.0",
|
|
include_merges=True,
|
|
)
|
|
|
|
self.assertEqual(output, "def456 (tag: v1.1.0) Some change")
|
|
mock_get_changelog.assert_called_once_with(
|
|
cwd="/repo",
|
|
from_ref="v1.0.0",
|
|
to_ref="v1.1.0",
|
|
include_merges=True,
|
|
)
|
|
|
|
@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")
|
|
|
|
self.assertIn("[ERROR] Failed to generate changelog", result)
|
|
self.assertIn("simulated git failure", result)
|
|
self.assertIn("v0.1.0..v0.2.0", result)
|
|
|
|
@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")
|
|
|
|
self.assertIn("[INFO] No commits found for range 'HEAD'", result)
|
|
|
|
|
|
class TestFindPreviousAndCurrentTag(unittest.TestCase):
|
|
def test_no_semver_tags_returns_none_none(self) -> None:
|
|
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:
|
|
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)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|