-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests_release_notes.py
249 lines (219 loc) · 9.4 KB
/
tests_release_notes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# SPDX-FileCopyrightText: 2023 Sequent Tech Inc <legal@sequentech.io>
#
# SPDX-License-Identifier: AGPL-3.0-only
import unittest
from unittest.mock import MagicMock, Mock
from github import Repository, Github
from release_notes import (
get_label_category, get_release_notes, create_release_notes_md
)
class TestGetReleaseNotes(unittest.TestCase):
def generate_labels(self, labels):
ret = []
for label in labels:
mock = Mock(name=label)
mock.name = label
ret.append(mock)
return ret
def setUp(self):
self.args = commit = Mock(silent=True)
self.github = MagicMock(spec=Github)
self.repo = MagicMock(spec=Repository.Repository)
self.github.get_repo.return_value = self.repo
self.repo.compare.return_value.commits = []
self.config = {
"changelog": {
"exclude": {"labels": ["skip-changelog"]},
"categories": [
{"title": "Bug Fixes", "labels": ["bug"]},
{"title": "New Features", "labels": ["enhancement"]}
]
}
}
def test_get_release_notes_empty(self):
release_notes = get_release_notes(self.github, self.repo, "1.0.0", "1.1.0", self.config)
self.assertEqual(release_notes, {})
def test_get_release_notes_with_data(self):
commit = Mock()
commit.commit.message = "A commit message"
pr = Mock()
pr.number = 1
pr.labels = self.generate_labels(["bug"])
pr.title = "Fix a bug"
pr.user.login = "user"
pr.html_url = "https://github.com/username/repo/pull/1"
pr.body = ""
commit.get_pulls.return_value = [pr]
self.repo.compare.return_value.commits = [commit]
expected_release_notes = {
"Bug Fixes": [
"* Fix a bug by @user in https://github.com/username/repo/pull/1"
]
}
release_notes = get_release_notes(self.github, self.repo, "1.0.0", "1.1.0", self.config, self.args)
self.assertEqual(release_notes, expected_release_notes)
def test_get_release_notes_with_excluded_label(self):
commit = Mock()
commit.commit.message = "A commit message"
pr = Mock()
pr.labels = self.generate_labels(["skip-changelog"])
pr.title = "Fix a bug"
pr.user.login = "user"
pr.html_url = "https://github.com/username/repo/pull/1"
pr.body = ""
commit.get_pulls.return_value = [pr]
self.repo.compare.return_value.commits = [commit]
release_notes = get_release_notes(self.github, self.repo, "1.0.0", "master", self.config, self.args)
self.assertEqual(release_notes, {})
def test_get_release_notes_with_parent_issue(self):
commit = Mock()
commit.commit.message = "A commit message"
pr = Mock()
pr.labels = self.generate_labels(["bug"])
pr.title = "Fix a bug PR"
pr.user.login = "user"
pr.html_url = "https://github.com/username/repo/pull/1"
pr.body = "Parent issue: https://github.com/username/repo/issues/1"
commit.get_pulls.return_value = [pr]
self.repo.compare.return_value.commits = [commit]
issue = Mock()
issue.title = "Fix a bug"
issue.html_url = "https://github.com/username/repo/issues/1"
self.repo.get_issue = Mock(return_value=issue)
expected_release_notes = {
"Bug Fixes": [
"* Fix a bug by @user in https://github.com/username/repo/issues/1"
]
}
release_notes = get_release_notes(self.github, self.repo, "1.0.0", "1.1.0", self.config, self.args)
self.assertEqual(release_notes, expected_release_notes)
def test_get_release_notes_with_parent_issue_already_included(self):
commit = Mock()
commit.commit.message = "A commit message"
pr = Mock()
pr.labels = self.generate_labels(["bug"])
pr.title = "Fix a bug"
pr.user.login = "user"
pr.html_url = "https://github.com/username/repo/pull/1"
pr.body = "Parent issue: https://github.com/username/repo/issues/1"
commit.get_pulls.return_value = [pr]
self.repo.compare.return_value.commits = [commit, commit]
issue = Mock()
issue.title = "Fix a bug"
issue.html_url = "https://github.com/username/repo/issues/1"
self.repo.get_issue = Mock(return_value=issue)
expected_release_notes = {
"Bug Fixes": [
"* Fix a bug by @user in https://github.com/username/repo/issues/1"
]
}
release_notes = get_release_notes(self.github, self.repo, "1.0.0", "1.1.0", self.config, self.args)
self.assertEqual(release_notes, expected_release_notes)
def test_get_release_notes_with_no_matching_category(self):
commit = Mock()
commit.commit.message = "A commit message"
pr = Mock()
pr.labels = self.generate_labels(["other"])
pr.title = "Other change"
pr.user.login = "user"
pr.html_url = "https://github.com/username/repo/pull/1"
pr.body = ""
commit.get_pulls.return_value = [pr]
self.repo.compare.return_value.commits = [commit]
release_notes = get_release_notes(self.github, self.repo, "1.0.0", "1.1.0", self.config, self.args)
self.assertEqual(release_notes, {})
def test_get_release_notes_with_wildcard_category(self):
self.config["changelog"]["categories"].append(
{"title": "Other Changes", "labels": ["*"]}
)
commit = Mock()
commit.commit.message = "A commit message"
pr = Mock()
pr.labels = self.generate_labels(["other"])
pr.title = "Other change"
pr.user.login = "user"
pr.html_url = "https://github.com/username/repo/pull/1"
pr.body = ""
commit.get_pulls.return_value = [pr]
self.repo.compare.return_value.commits = [commit]
expected_release_notes = {
"Other Changes": [
"* Other change by @user in https://github.com/username/repo/pull/1"
]
}
release_notes = get_release_notes(self.github, self.repo, "1.0.0", "1.1.0", self.config, self.args)
self.assertEqual(release_notes, expected_release_notes)
class TestGetLabelCategory(unittest.TestCase):
def generate_labels(self, labels):
ret = []
for label in labels:
mock = Mock(name=label)
mock.name = label
ret.append(mock)
return ret
def test_no_matching_labels(self):
labels = self.generate_labels(["bugfix", "documentation"])
categories = [
{"labels": ["enhancement"], "title": "Enhancements"},
{"labels": ["security"], "title": "Security"}
]
result = get_label_category(labels, categories)
self.assertIsNone(result)
def test_single_matching_label(self):
labels = self.generate_labels(["bugfix", "documentation"])
categories = [
{"labels": ["bugfix"], "title": "Bug Fixes"},
{"labels": ["security"], "title": "Security"}
]
expected = {"labels": ["bugfix"], "title": "Bug Fixes"}
result = get_label_category(labels, categories)
self.assertEqual(result, expected)
def test_multiple_matching_labels(self):
labels = self.generate_labels(["bugfix", "documentation"])
categories = [
{"labels": ["bugfix"], "title": "Bug Fixes"},
{"labels": ["documentation"], "title": "Documentation"}
]
expected = {"labels": ["bugfix"], "title": "Bug Fixes"}
result = get_label_category(labels, categories)
self.assertEqual(result, expected)
def test_wildcard_matching_label(self):
labels = self.generate_labels(["bugfix", "documentation"])
categories = [
{"labels": ["*"], "title": "All"},
{"labels": ["security"], "title": "Security"}
]
expected = {"labels": ["*"], "title": "All"}
result = get_label_category(labels, categories)
self.assertEqual(result, expected)
def test_wildcard_matching_label_with_other_labels(self):
labels = self.generate_labels(["bugfix", "documentation"])
categories = [
{"labels": ["*", "security"], "title": "All and Security"},
{"labels": ["enhancement"], "title": "Enhancements"}
]
expected = {"labels": ["*", "security"], "title": "All and Security"}
result = get_label_category(labels, categories)
self.assertEqual(result, expected)
class TestCreateReleaseNotesMd(unittest.TestCase):
def test_create_release_notes_md(self):
release_notes = {
"Bug Fixes": [
"* Fix a bug by @user in https://github.com/username/repo/pull/1"
],
"New Features": [
"* Add a new feature by @user2 in https://github.com/username/repo/pull/2"
]
}
new_release = "1.1.0"
expected_md = """<!-- Release notes generated using configuration in .github/release.yml at 1.1.0 -->
## What's Changed
### Bug Fixes
* Fix a bug by @user in https://github.com/username/repo/pull/1
### New Features
* Add a new feature by @user2 in https://github.com/username/repo/pull/2
"""
md = create_release_notes_md(release_notes, new_release)
self.assertEqual(md, expected_md)
if __name__ == '__main__':
unittest.main()