-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitcclog_test.py
More file actions
453 lines (401 loc) · 19 KB
/
gitcclog_test.py
File metadata and controls
453 lines (401 loc) · 19 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import unittest
from unittest.mock import patch, call
import gitcclog
import logging
import os
import tempfile
class TestGitComCon(unittest.TestCase):
def setUp(self) -> None:
logging.getLogger().setLevel(logging.FATAL)
return super().setUp()
def test_invalid_title(self):
self.assertIsNone(gitcclog.parse_title(""))
self.assertIsNone(gitcclog.parse_title("bla"))
self.assertIsNone(gitcclog.parse_title("bla some more"))
self.assertIsNone(gitcclog.parse_title("invalid_token: but valid message"))
self.assertIsNone(gitcclog.parse_title("feat feat: valid tokens but 2"))
self.assertIsNone(gitcclog.parse_title("feat : valid token but extra space"))
self.assertIsNone(gitcclog.parse_title("feat (bla) : valid token but extra spaces"))
self.assertIsNone(gitcclog.parse_title("feat (bla): valid token but extra spaces"))
self.assertIsNone(gitcclog.parse_title("feat (bla):valid token but extra spaces"))
self.assertIsNone(gitcclog.parse_title("feat(bla):valid token but too few spaces"))
self.assertIsNone(gitcclog.parse_title("feat(bla)(a):valid token but extra context"))
def test_valid_title(self):
parsed = gitcclog.parse_title("feat: valid message")
self.assertIsNotNone(parsed)
assert parsed is not None # for calming down IDE checker
self.assertEqual(parsed["type"], "feat")
self.assertIsNone(parsed["scope"])
self.assertEqual(parsed["description"], "valid message")
self.assertFalse(parsed["breaking"])
parsed = gitcclog.parse_title("feat(bla): valid message")
self.assertIsNotNone(parsed)
assert parsed is not None # for calming down IDE checker
self.assertEqual(parsed["type"], "feat")
self.assertEqual(parsed["scope"], "bla")
self.assertEqual(parsed["description"], "valid message")
self.assertFalse(parsed["breaking"])
parsed = gitcclog.parse_title("feat(scope1, scope2): valid message")
self.assertIsNotNone(parsed)
assert parsed is not None # for calming down IDE checker
self.assertEqual(parsed["type"], "feat")
self.assertEqual(parsed["scope"], "scope1, scope2")
self.assertEqual(parsed["description"], "valid message")
self.assertFalse(parsed["breaking"])
parsed = gitcclog.parse_title("fix!: valid message")
self.assertIsNotNone(parsed)
assert parsed is not None # for calming down IDE checker
self.assertEqual(parsed["type"], "fix")
self.assertIsNone(parsed["scope"])
self.assertEqual(parsed["description"], "valid message")
self.assertTrue(parsed["breaking"])
parsed = gitcclog.parse_title("fix(scope)!: valid message")
self.assertIsNotNone(parsed)
assert parsed is not None # for calming down IDE checker
self.assertEqual(parsed["type"], "fix")
self.assertEqual(parsed["scope"], "scope")
self.assertEqual(parsed["description"], "valid message")
self.assertTrue(parsed["breaking"])
def test_tag_value(self):
self.assertEqual(gitcclog.tag_to_numbers("v1.2.3", prefix="v"), [1, 2, 3])
self.assertEqual(gitcclog.tag_to_numbers("3.4.5"), [3, 4, 5])
# it raises exception if it cannot convert the tag to numbers
self.assertRaises(ValueError, gitcclog.tag_to_numbers, "v1.2.3")
self.assertRaises(ValueError, gitcclog.tag_to_numbers, "va.2.3")
def test_get_next_tag(self):
self.assertEqual(gitcclog.get_next_tag(None, breaking_changes=False, new_features=False), "0.0.0")
self.assertEqual(gitcclog.get_next_tag("1.2.3", breaking_changes=False, new_features=False), "1.2.4")
self.assertEqual(gitcclog.get_next_tag("1.2.3", breaking_changes=False, new_features=True), "1.3.0")
self.assertEqual(gitcclog.get_next_tag("1.2.3", breaking_changes=True, new_features=False), "2.0.0")
self.assertEqual(gitcclog.get_next_tag("1.2.3", breaking_changes=True, new_features=True), "2.0.0")
def test_get_naked_tag(self):
self.assertEqual(gitcclog.get_naked_tag("v1.2.3", prefix="v"), "1.2.3")
self.assertEqual(gitcclog.get_naked_tag("1.2.3"), "1.2.3")
self.assertEqual(gitcclog.get_naked_tag("ver1.2.3", prefix="ver"), "1.2.3")
class TestParseFooters(unittest.TestCase):
def setUp(self):
logging.getLogger().setLevel(logging.FATAL)
def test_empty_body(self):
self.assertEqual(gitcclog.parse_footers(""), [])
def test_breaking_change_footer(self):
footers = gitcclog.parse_footers("BREAKING CHANGE: removed old api")
self.assertEqual(len(footers), 1)
self.assertEqual(footers[0]["key"], "BREAKING CHANGE")
self.assertEqual(footers[0]["value"], "removed old api")
def test_closes_footer(self):
footers = gitcclog.parse_footers("Closes: #123")
self.assertEqual(len(footers), 1)
self.assertEqual(footers[0]["key"], "Closes")
self.assertEqual(footers[0]["value"], "#123")
def test_multiple_footers(self):
body = "BREAKING CHANGE: new api\nCloses: #42\nRefs: #99"
footers = gitcclog.parse_footers(body)
self.assertEqual(len(footers), 3)
def test_non_footer_lines_ignored(self):
body = "This is just a description\nwith multiple lines\nCloses: #42"
footers = gitcclog.parse_footers(body)
self.assertEqual(len(footers), 1)
self.assertEqual(footers[0]["key"], "Closes")
def test_non_footer_lines_ignored2(self):
body = "merged-PR: !10936\nRelated work items: #50010"
footers = gitcclog.parse_footers(body)
self.assertEqual(len(footers), 2)
self.assertEqual(footers[0]["key"], "merged-PR")
self.assertEqual(footers[1]["key"], "Related work items")
class TestGenerateChangelog(unittest.TestCase):
def setUp(self):
logging.getLogger().setLevel(logging.FATAL)
self.tmpdir = tempfile.mkdtemp()
self.changelog_path = os.path.join(self.tmpdir, "CHANGELOG.md")
self.base_config = {
"tagPrefix": "",
"initialNonPrefixedVersion": "0.1.0",
"changelogFile": self.changelog_path,
"compareUrlFormat": "",
"commitUrlFormat": "",
"issueUrlFormat": "",
}
def tearDown(self):
if os.path.exists(self.changelog_path):
os.remove(self.changelog_path)
os.rmdir(self.tmpdir)
def test_basic_feature_changelog(self):
history = {
"commits": [
{
"full_hash": "abc1234567890",
"short_hash": "abc1234",
"committer_date": "2025-01-15 10:00:00 +0000",
"tags": [],
"footers": [],
"title": {"type": "feat", "scope": None, "description": "add login", "breaking": False},
}
],
"lastTag": None,
}
result = gitcclog.generate_changelog(history, self.base_config)
self.assertEqual(result, "0.1.0")
with open(self.changelog_path) as f:
content = f.read()
self.assertIn("# Changelog", content)
self.assertIn("0.1.0", content)
def test_changelog_with_urls_and_issues(self):
config = {**self.base_config,
"commitUrlFormat": "https://example.com/commit/{{hash}}",
"issueUrlFormat": "https://example.com/issues/{{id}}",
}
history = {
"commits": [
{
"full_hash": "abc1234567890",
"short_hash": "abc1234",
"committer_date": "2025-01-15 10:00:00 +0000",
"tags": [],
"footers": [{"key": "Closes", "value": "#42"}],
"title": {"type": "feat", "scope": None, "description": "add feature", "breaking": False},
},
{
"full_hash": "def7890123456",
"short_hash": "def7890",
"committer_date": "2025-01-10 10:00:00 +0000",
"tags": ["0.1.0"],
"footers": [],
"title": {"type": "feat", "scope": None, "description": "initial feature", "breaking": False},
},
],
"lastTag": "0.1.0",
}
gitcclog.generate_changelog(history, config)
with open(self.changelog_path) as f:
content = f.read()
self.assertIn("[abc1234](https://example.com/commit/abc1234567890)", content)
self.assertIn("[#42](https://example.com/issues/42)", content)
self.assertIn("closes", content)
def test_changelog_breaking_change_bumps_major(self):
history = {
"commits": [
{
"full_hash": "bbb2222222222",
"short_hash": "bbb2222",
"committer_date": "2025-01-15 10:00:00 +0000",
"tags": [],
"footers": [],
"title": {"type": "feat", "scope": None, "description": "new api", "breaking": True},
},
{
"full_hash": "aaa1111111111",
"short_hash": "aaa1111",
"committer_date": "2025-01-10 10:00:00 +0000",
"tags": ["1.0.0"],
"footers": [],
"title": {"type": "feat", "scope": None, "description": "initial", "breaking": False},
},
],
"lastTag": "1.0.0",
}
result = gitcclog.generate_changelog(history, self.base_config)
self.assertEqual(result, "2.0.0")
with open(self.changelog_path) as f:
content = f.read()
self.assertIn("2.0.0", content)
self.assertIn("BREAKING CHANGES", content)
def test_changelog_fix_bumps_patch(self):
history = {
"commits": [
{
"full_hash": "ccc3333333333",
"short_hash": "ccc3333",
"committer_date": "2025-01-15 10:00:00 +0000",
"tags": [],
"footers": [],
"title": {"type": "fix", "scope": None, "description": "fix crash", "breaking": False},
},
{
"full_hash": "aaa1111111111",
"short_hash": "aaa1111",
"committer_date": "2025-01-10 10:00:00 +0000",
"tags": ["1.0.0"],
"footers": [],
"title": {"type": "feat", "scope": None, "description": "initial", "breaking": False},
},
],
"lastTag": "1.0.0",
}
result = gitcclog.generate_changelog(history, self.base_config)
self.assertEqual(result, "1.0.1")
def test_changelog_empty_changelog_file_skips_write(self):
config = {**self.base_config, "changelogFile": ""}
history = {
"commits": [],
"lastTag": None,
}
result = gitcclog.generate_changelog(history, config)
self.assertEqual(result, "0.1.0")
self.assertFalse(os.path.exists(self.changelog_path))
def test_changelog_with_scoped_commits(self):
history = {
"commits": [
{
"full_hash": "abc1234567890",
"short_hash": "abc1234",
"committer_date": "2025-01-15 10:00:00 +0000",
"tags": [],
"footers": [],
"title": {"type": "feat", "scope": "auth", "description": "add oauth", "breaking": False},
},
{
"full_hash": "def7890123456",
"short_hash": "def7890",
"committer_date": "2025-01-10 10:00:00 +0000",
"tags": ["1.0.0"],
"footers": [],
"title": {"type": "feat", "scope": None, "description": "initial", "breaking": False},
},
],
"lastTag": "1.0.0",
}
gitcclog.generate_changelog(history, self.base_config)
with open(self.changelog_path) as f:
content = f.read()
self.assertIn("(auth) add oauth", content)
def test_changelog_with_compare_url(self):
config = {**self.base_config,
"compareUrlFormat": "https://example.com/compare/{{previousTag}}...{{currentTag}}",
}
history = {
"commits": [
{
"full_hash": "bbb2222222222",
"short_hash": "bbb2222",
"committer_date": "2025-01-15 10:00:00 +0000",
"tags": [],
"footers": [],
"title": {"type": "feat", "scope": None, "description": "new thing", "breaking": False},
},
{
"full_hash": "aaa1111111111",
"short_hash": "aaa1111",
"committer_date": "2025-01-10 10:00:00 +0000",
"tags": ["1.0.0"],
"footers": [],
"title": {"type": "feat", "scope": None, "description": "initial", "breaking": False},
},
],
"lastTag": "1.0.0",
}
gitcclog.generate_changelog(history, config)
with open(self.changelog_path) as f:
content = f.read()
self.assertIn("https://example.com/compare/1.0.0...1.1.0", content)
class TestCommitAndTag(unittest.TestCase):
def setUp(self):
logging.getLogger().setLevel(logging.FATAL)
@patch("gitcclog.subprocess.run")
def test_commit_and_tag_runs_git_commands(self, mock_run):
mock_run.return_value.returncode = 0
result = gitcclog.commit_and_tag("CHANGELOG.md", "v", "1.2.0")
self.assertTrue(result)
expected_calls = [
call(["git", "add", "CHANGELOG.md"], capture_output=True, text=True),
call(["git", "commit", "-m", "chore(release): v1.2.0"], capture_output=True, text=True),
call(["git", "tag", "v1.2.0"], capture_output=True, text=True),
]
mock_run.assert_has_calls(expected_calls)
@patch("gitcclog.subprocess.run")
def test_commit_and_tag_no_prefix(self, mock_run):
mock_run.return_value.returncode = 0
result = gitcclog.commit_and_tag("CHANGELOG.md", "", "1.2.0")
self.assertTrue(result)
expected_calls = [
call(["git", "add", "CHANGELOG.md"], capture_output=True, text=True),
call(["git", "commit", "-m", "chore(release): 1.2.0"], capture_output=True, text=True),
call(["git", "tag", "1.2.0"], capture_output=True, text=True),
]
mock_run.assert_has_calls(expected_calls)
@patch("gitcclog.subprocess.run")
def test_commit_and_tag_fails_on_git_error(self, mock_run):
mock_run.return_value.returncode = 1
mock_run.return_value.stderr = "error"
result = gitcclog.commit_and_tag("CHANGELOG.md", "", "1.2.0")
self.assertFalse(result)
class TestMainFlow(unittest.TestCase):
def setUp(self):
logging.getLogger().setLevel(logging.FATAL)
self.tmpdir = tempfile.mkdtemp()
self.changelog_path = os.path.join(self.tmpdir, "CHANGELOG.md")
def tearDown(self):
if os.path.exists(self.changelog_path):
os.remove(self.changelog_path)
os.rmdir(self.tmpdir)
@patch("gitcclog.commit_and_tag")
@patch("gitcclog.get_git_history")
def test_dry_run_does_not_commit(self, mock_history, mock_commit):
mock_history.return_value = ""
config = {
"tagPrefix": "",
"initialNonPrefixedVersion": "0.1.0",
"changelogFile": self.changelog_path,
"compareUrlFormat": "",
"commitUrlFormat": "",
"issueUrlFormat": "",
}
new_tag = gitcclog.generate_changelog({"commits": [], "lastTag": None}, config)
gitcclog.run(config, new_tag, real_run=False, changelog_file=self.changelog_path)
mock_commit.assert_not_called()
@patch("gitcclog.commit_and_tag", return_value=True)
@patch("gitcclog.get_git_history")
def test_no_dry_run_commits(self, mock_history, mock_commit):
mock_history.return_value = ""
config = {
"tagPrefix": "v",
"initialNonPrefixedVersion": "0.1.0",
"changelogFile": self.changelog_path,
"compareUrlFormat": "",
"commitUrlFormat": "",
"issueUrlFormat": "",
}
gitcclog.run(config, "0.1.0", real_run=True, changelog_file=self.changelog_path)
mock_commit.assert_called_once_with(self.changelog_path, "v", "0.1.0")
@patch("gitcclog.commit_and_tag", return_value=True)
def test_force_version_overrides_computed(self, mock_commit):
config = {
"tagPrefix": "v",
"initialNonPrefixedVersion": "0.1.0",
"changelogFile": self.changelog_path,
"compareUrlFormat": "",
"commitUrlFormat": "",
"issueUrlFormat": "",
}
# Force version "9.9.9" instead of whatever was computed
gitcclog.run(config, "9.9.9", real_run=True, changelog_file=self.changelog_path)
mock_commit.assert_called_once_with(self.changelog_path, "v", "9.9.9")
class TestParseRawCommits(unittest.TestCase):
def setUp(self):
logging.getLogger().setLevel(logging.FATAL)
def test_parses_single_feat_commit(self):
raw = "abc1234567890(abc1234)()(2025-01-15 10:00:00 +0000)\nfeat: add login\n" + gitcclog.SCISSORS
history = gitcclog.parse_raw_commits(raw, "")
self.assertEqual(len(history["commits"]), 1)
self.assertEqual(history["commits"][0]["title"]["type"], "feat")
self.assertEqual(history["commits"][0]["title"]["description"], "add login")
def test_parses_tagged_commit(self):
raw = "abc1234567890(abc1234)(tag: 1.0.0)(2025-01-15 10:00:00 +0000)\nfeat: add login\n" + gitcclog.SCISSORS
history = gitcclog.parse_raw_commits(raw, "")
self.assertEqual(history["lastTag"], "1.0.0")
self.assertEqual(history["commits"][0]["tags"], ["1.0.0"])
def test_keeps_non_conventional_commits_as_chore(self):
raw = "abc1234567890(abc1234)()(2025-01-15 10:00:00 +0000)\njust a regular message\n" + gitcclog.SCISSORS
history = gitcclog.parse_raw_commits(raw, "")
self.assertEqual(len(history["commits"]), 1)
self.assertEqual(history["commits"][0]["title"]["type"], "chore")
self.assertEqual(history["commits"][0]["title"]["description"], "just a regular message")
def test_parses_commit_with_tag_prefix(self):
raw = "abc1234567890(abc1234)(tag: v1.0.0)(2025-01-15 10:00:00 +0000)\nfeat: thing\n" + gitcclog.SCISSORS
history = gitcclog.parse_raw_commits(raw, "v")
self.assertEqual(history["lastTag"], "v1.0.0")
def test_empty_input(self):
history = gitcclog.parse_raw_commits("", "")
self.assertEqual(len(history["commits"]), 0)
self.assertIsNone(history["lastTag"])
if __name__ == '__main__':
unittest.main()