|
| 1 | +import argparse |
| 2 | +import unittest |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from tests.tools.private.release.release_test_helper import _mock_git_and_gh |
| 6 | +from tools.private.release.complete_sync_changelog import CompleteSyncChangelog |
| 7 | + |
| 8 | + |
| 9 | +class CompleteSyncChangelogTest(unittest.TestCase): |
| 10 | + def setUp(self): |
| 11 | + _mock_git_and_gh(self) |
| 12 | + self.addCleanup(patch.stopall) |
| 13 | + |
| 14 | + # Dynamic mock for issue body |
| 15 | + self.issue_body = "" |
| 16 | + |
| 17 | + def mock_get_body(issue_num): |
| 18 | + return self.issue_body |
| 19 | + |
| 20 | + def mock_update_body(issue_num, body): |
| 21 | + self.issue_body = body |
| 22 | + |
| 23 | + self.mock_gh.get_issue_body.side_effect = mock_get_body |
| 24 | + self.mock_gh.update_issue_body.side_effect = mock_update_body |
| 25 | + |
| 26 | + def test_complete_sync_changelog_success(self): |
| 27 | + args = argparse.Namespace(pr=999) |
| 28 | + self.mock_gh.get_pr_info.return_value = { |
| 29 | + "state": "MERGED", |
| 30 | + "body": "Updates CHANGELOG.md\n\nRelease-Tracking-Issue: #123", |
| 31 | + "mergeCommit": {"oid": "abcdef1234567890"}, |
| 32 | + } |
| 33 | + self.issue_body = """ |
| 34 | +## Checklist |
| 35 | +- [ ] Prepare Release |
| 36 | +- [ ] Create Release branch |
| 37 | +- [ ] Sync Changelog #124 | status=pending pr=#999 |
| 38 | +- [ ] Sync Changelog #125 | status=pending pr=#999 |
| 39 | +- [ ] Sync Changelog #126 | status=pending pr=#888 |
| 40 | +- [ ] Tag Final |
| 41 | +
|
| 42 | +## Backports |
| 43 | +""" |
| 44 | + result = CompleteSyncChangelog(args, self.mock_gh).run() |
| 45 | + |
| 46 | + self.assertEqual(result, 0) |
| 47 | + self.mock_gh.get_pr_info.assert_called_once_with(999) |
| 48 | + self.mock_gh.get_issue_body.assert_called_once_with(123) |
| 49 | + self.mock_gh.update_issue_body.assert_called_once() |
| 50 | + |
| 51 | + # Check that only tasks pointing to #999 were marked checked=True and status=done |
| 52 | + self.assertIn( |
| 53 | + "- [x] Sync Changelog #124 | status=done pr=#999 commit= abcdef12", |
| 54 | + self.issue_body, |
| 55 | + ) |
| 56 | + self.assertIn( |
| 57 | + "- [x] Sync Changelog #125 | status=done pr=#999 commit= abcdef12", |
| 58 | + self.issue_body, |
| 59 | + ) |
| 60 | + # Task pointing to #888 should remain unchanged |
| 61 | + self.assertIn( |
| 62 | + "- [ ] Sync Changelog #126 | status=pending pr=#888", |
| 63 | + self.issue_body, |
| 64 | + ) |
| 65 | + |
| 66 | + def test_complete_sync_changelog_not_merged(self): |
| 67 | + args = argparse.Namespace(pr=999) |
| 68 | + self.mock_gh.get_pr_info.return_value = { |
| 69 | + "state": "OPEN", |
| 70 | + "body": "Updates CHANGELOG.md\n\nRelease-Tracking-Issue: #123", |
| 71 | + } |
| 72 | + |
| 73 | + result = CompleteSyncChangelog(args, self.mock_gh).run() |
| 74 | + |
| 75 | + self.assertEqual(result, 1) |
| 76 | + self.mock_gh.get_pr_info.assert_called_once_with(999) |
| 77 | + self.mock_gh.update_issue_body.assert_not_called() |
| 78 | + |
| 79 | + def test_complete_sync_changelog_missing_tracking_issue_link(self): |
| 80 | + args = argparse.Namespace(pr=999) |
| 81 | + self.mock_gh.get_pr_info.return_value = { |
| 82 | + "state": "MERGED", |
| 83 | + "body": "Updates CHANGELOG.md without tracking issue link", |
| 84 | + "mergeCommit": {"oid": "abcdef1234567890"}, |
| 85 | + } |
| 86 | + |
| 87 | + result = CompleteSyncChangelog(args, self.mock_gh).run() |
| 88 | + |
| 89 | + self.assertEqual(result, 1) |
| 90 | + self.mock_gh.get_pr_info.assert_called_once_with(999) |
| 91 | + self.mock_gh.update_issue_body.assert_not_called() |
| 92 | + |
| 93 | + def test_complete_sync_changelog_no_matching_tasks(self): |
| 94 | + args = argparse.Namespace(pr=999) |
| 95 | + self.mock_gh.get_pr_info.return_value = { |
| 96 | + "state": "MERGED", |
| 97 | + "body": "Updates CHANGELOG.md\n\nRelease-Tracking-Issue: #123", |
| 98 | + "mergeCommit": {"oid": "abcdef1234567890"}, |
| 99 | + } |
| 100 | + # Checklist has no tasks pointing to #999 |
| 101 | + self.issue_body = """ |
| 102 | +## Checklist |
| 103 | +- [ ] Prepare Release |
| 104 | +- [ ] Create Release branch |
| 105 | +- [ ] Sync Changelog #124 | status=pending pr=#888 |
| 106 | +- [ ] Tag Final |
| 107 | +
|
| 108 | +## Backports |
| 109 | +""" |
| 110 | + result = CompleteSyncChangelog(args, self.mock_gh).run() |
| 111 | + |
| 112 | + # Should log warning but return 0 (success/noop) |
| 113 | + self.assertEqual(result, 0) |
| 114 | + self.mock_gh.get_pr_info.assert_called_once_with(999) |
| 115 | + self.mock_gh.get_issue_body.assert_called_once_with(123) |
| 116 | + self.mock_gh.update_issue_body.assert_not_called() |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + unittest.main() |
0 commit comments