Debian Codegraph @

tests/test_incremental_build.py

raw
1 """Tests for the incremental_build function in commitizen.changelog module."""
2
3 from commitizen.changelog import Metadata, incremental_build
4
5
6 class TestIncrementalBuild:
7 """Test cases for the incremental_build function."""
8
9 def test_basic_replacement_of_unreleased_section(self):
10 """Test basic functionality of replacing unreleased section with new content."""
11 lines = [
12 "# Changelog",
13 "",
14 "## Unreleased",
15 "",
16 "### Added",
17 "- New feature",
18 "",
19 "## 1.0.0 (2023-01-01)",
20 "",
21 "### Fixed",
22 "- Bug fix",
23 ]
24
25 metadata = Metadata(
26 unreleased_start=2,
27 unreleased_end=6,
28 latest_version="1.0.0",
29 latest_version_position=7,
30 )
31
32 new_content = "## Unreleased\n\n### Added\n- Another new feature"
33
34 result = incremental_build(new_content, lines, metadata)
35
36 expected = [
37 "# Changelog",
38 "",
39 "## Unreleased\n\n### Added\n- Another new feature",
40 "\n",
41 "## 1.0.0 (2023-01-01)",
42 "",
43 "### Fixed",
44 "- Bug fix",
45 ]
46
47 assert result == expected
48
49 def test_replacement_when_latest_version_position_is_none(self):
50 """Test replacement when latest_version_position is None (append to end)."""
51 lines = [
52 "# Changelog",
53 "",
54 "## Unreleased",
55 "",
56 "### Added",
57 "- New feature",
58 ]
59
60 metadata = Metadata(
61 unreleased_start=2,
62 unreleased_end=5,
63 latest_version=None,
64 latest_version_position=None,
65 )
66
67 new_content = "## Unreleased\n\n### Added\n- Another new feature"
68
69 result = incremental_build(new_content, lines, metadata)
70
71 expected = [
72 "# Changelog",
73 "",
74 "## Unreleased\n\n### Added\n- Another new feature",
75 ]
76
77 assert result == expected
78
79 def test_replacement_when_latest_version_position_after_unreleased_end(self):
80 """Test replacement when latest_version_position is after unreleased_end."""
81 lines = [
82 "# Changelog",
83 "",
84 "## Unreleased",
85 "",
86 "### Added",
87 "- New feature",
88 "",
89 "## 1.0.0 (2023-01-01)",
90 "",
91 "### Fixed",
92 "- Bug fix",
93 ]
94
95 metadata = Metadata(
96 unreleased_start=2,
97 unreleased_end=5,
98 latest_version="1.0.0",
99 latest_version_position=7,
100 )
101
102 new_content = "## Unreleased\n\n### Added\n- Another new feature"
103
104 result = incremental_build(new_content, lines, metadata)
105
106 expected = [
107 "# Changelog",
108 "",
109 "",
110 "## Unreleased\n\n### Added\n- Another new feature",
111 "\n",
112 "## 1.0.0 (2023-01-01)",
113 "",
114 "### Fixed",
115 "- Bug fix",
116 ]
117
118 assert result == expected
119
120 def test_replacement_when_latest_version_position_before_unreleased_end(self):
121 """Test replacement when latest_version_position is before unreleased_end."""
122 lines = [
123 "# Changelog",
124 "",
125 "## 1.0.0 (2023-01-01)",
126 "",
127 "### Fixed",
128 "- Bug fix",
129 "",
130 "## Unreleased",
131 "",
132 "### Added",
133 "- New feature",
134 ]
135
136 metadata = Metadata(
137 unreleased_start=7,
138 unreleased_end=10,
139 latest_version="1.0.0",
140 latest_version_position=2,
141 )
142
143 new_content = "## Unreleased\n\n### Added\n- Another new feature"
144
145 result = incremental_build(new_content, lines, metadata)
146
147 expected = [
148 "# Changelog",
149 "",
150 "## Unreleased\n\n### Added\n- Another new feature",
151 "\n",
152 "## 1.0.0 (2023-01-01)",
153 "",
154 "### Fixed",
155 "- Bug fix",
156 "",
157 "- New feature",
158 ]
159
160 assert result == expected
161
162 def test_no_unreleased_section_append_to_end(self):
163 """Test appending new content when no unreleased section exists."""
164 lines = [
165 "# Changelog",
166 "",
167 "## 1.0.0 (2023-01-01)",
168 "",
169 "### Fixed",
170 "- Bug fix",
171 ]
172
173 metadata = Metadata(
174 unreleased_start=None,
175 unreleased_end=None,
176 latest_version=None,
177 latest_version_position=None,
178 )
179
180 new_content = "## Unreleased\n\n### Added\n- New feature"
181
182 result = incremental_build(new_content, lines, metadata)
183
184 expected = [
185 "# Changelog",
186 "",
187 "## 1.0.0 (2023-01-01)",
188 "",
189 "### Fixed",
190 "- Bug fix",
191 "\n",
192 "## Unreleased\n\n### Added\n- New feature",
193 ]
194
195 assert result == expected
196
197 def test_no_unreleased_section_with_latest_version_position(self):
198 """Test inserting new content at latest_version_position when no unreleased section."""
199 lines = [
200 "# Changelog",
201 "",
202 "## 1.0.0 (2023-01-01)",
203 "",
204 "### Fixed",
205 "- Bug fix",
206 ]
207
208 metadata = Metadata(
209 unreleased_start=None,
210 unreleased_end=None,
211 latest_version="1.0.0",
212 latest_version_position=2,
213 )
214
215 new_content = "## Unreleased\n\n### Added\n- New feature"
216
217 result = incremental_build(new_content, lines, metadata)
218
219 expected = [
220 "# Changelog",
221 "",
222 "## Unreleased\n\n### Added\n- New feature",
223 "\n",
224 "## 1.0.0 (2023-01-01)",
225 "",
226 "### Fixed",
227 "- Bug fix",
228 ]
229
230 assert result == expected
231
232 def test_empty_lines_list(self):
233 """Test behavior with empty lines list."""
234 lines = []
235
236 metadata = Metadata(
237 unreleased_start=None,
238 unreleased_end=None,
239 latest_version=None,
240 latest_version_position=None,
241 )
242
243 new_content = "## Unreleased\n\n### Added\n- New feature"
244
245 result = incremental_build(new_content, lines, metadata)
246
247 expected = [
248 "## Unreleased\n\n### Added\n- New feature",
249 ]
250
251 assert result == expected
252
253 def test_single_line_unreleased_section(self):
254 """Test replacement of single line unreleased section."""
255 lines = [
256 "# Changelog",
257 "## Unreleased",
258 "## 1.0.0 (2023-01-01)",
259 ]
260
261 metadata = Metadata(
262 unreleased_start=1,
263 unreleased_end=1,
264 latest_version="1.0.0",
265 latest_version_position=2,
266 )
267
268 new_content = "## Unreleased\n\n### Added\n- New feature"
269
270 result = incremental_build(new_content, lines, metadata)
271
272 expected = [
273 "# Changelog",
274 ]
275
276 assert result == expected
277
278 def test_unreleased_section_at_end_of_file(self):
279 """Test replacement when unreleased section is at the end of file."""
280 lines = [
281 "# Changelog",
282 "",
283 "## 1.0.0 (2023-01-01)",
284 "",
285 "### Fixed",
286 "- Bug fix",
287 "",
288 "## Unreleased",
289 "",
290 "### Added",
291 "- New feature",
292 ]
293
294 metadata = Metadata(
295 unreleased_start=7,
296 unreleased_end=10,
297 latest_version="1.0.0",
298 latest_version_position=2,
299 )
300
301 new_content = "## Unreleased\n\n### Added\n- Another new feature"
302
303 result = incremental_build(new_content, lines, metadata)
304
305 expected = [
306 "# Changelog",
307 "",
308 "## Unreleased\n\n### Added\n- Another new feature",
309 "\n",
310 "## 1.0.0 (2023-01-01)",
311 "",
312 "### Fixed",
313 "- Bug fix",
314 "",
315 "- New feature",
316 ]
317
318 assert result == expected
319
320 def test_blank_line_handling_when_appending(self):
321 """Test that blank line is added when appending to non-empty content."""
322 lines = [
323 "# Changelog",
324 "",
325 "## 1.0.0 (2023-01-01)",
326 ]
327
328 metadata = Metadata(
329 unreleased_start=None,
330 unreleased_end=None,
331 latest_version=None,
332 latest_version_position=None,
333 )
334
335 new_content = "## Unreleased\n\n### Added\n- New feature"
336
337 result = incremental_build(new_content, lines, metadata)
338
339 expected = [
340 "# Changelog",
341 "",
342 "## 1.0.0 (2023-01-01)",
343 "\n",
344 "## Unreleased\n\n### Added\n- New feature",
345 ]
346
347 assert result == expected
348
349 def test_no_blank_line_when_content_ends_with_blank_line(self):
350 """Test that no extra blank line is added when content already ends with blank line."""
351 lines = [
352 "# Changelog",
353 "",
354 "## 1.0.0 (2023-01-01)",
355 "",
356 ]
357
358 metadata = Metadata(
359 unreleased_start=None,
360 unreleased_end=None,
361 latest_version=None,
362 latest_version_position=None,
363 )
364
365 new_content = "## Unreleased\n\n### Added\n- New feature"
366
367 result = incremental_build(new_content, lines, metadata)
368
369 expected = [
370 "# Changelog",
371 "",
372 "## 1.0.0 (2023-01-01)",
373 "",
374 "## Unreleased\n\n### Added\n- New feature",
375 ]
376
377 assert result == expected
378
379 def test_empty_new_content(self):
380 """Test behavior with empty new content."""
381 lines = [
382 "# Changelog",
383 "",
384 "## Unreleased",
385 "",
386 "### Added",
387 "- New feature",
388 "",
389 "## 1.0.0 (2023-01-01)",
390 ]
391
392 metadata = Metadata(
393 unreleased_start=2,
394 unreleased_end=5,
395 latest_version="1.0.0",
396 latest_version_position=7,
397 )
398
399 new_content = ""
400
401 result = incremental_build(new_content, lines, metadata)
402
403 expected = [
404 "# Changelog",
405 "",
406 "",
407 "",
408 "\n",
409 "## 1.0.0 (2023-01-01)",
410 ]
411
412 assert result == expected
413
414 def test_multiline_new_content(self):
415 """Test behavior with multiline new content."""
416 lines = [
417 "# Changelog",
418 "",
419 "## Unreleased",
420 "",
421 "### Added",
422 "- New feature",
423 "",
424 "## 1.0.0 (2023-01-01)",
425 ]
426
427 metadata = Metadata(
428 unreleased_start=2,
429 unreleased_end=5,
430 latest_version="1.0.0",
431 latest_version_position=7,
432 )
433
434 new_content = "## Unreleased\n\n### Added\n- Feature 1\n- Feature 2\n\n### Fixed\n- Bug fix"
435
436 result = incremental_build(new_content, lines, metadata)
437
438 expected = [
439 "# Changelog",
440 "",
441 "",
442 "## Unreleased\n\n### Added\n- Feature 1\n- Feature 2\n\n### Fixed\n- Bug fix",
443 "\n",
444 "## 1.0.0 (2023-01-01)",
445 ]
446
447 assert result == expected
448
449 def test_metadata_with_none_values(self):
450 """Test behavior when metadata has None values for unreleased positions."""
451 lines = [
452 "# Changelog",
453 "",
454 "## 1.0.0 (2023-01-01)",
455 "",
456 "### Fixed",
457 "- Bug fix",
458 ]
459
460 metadata = Metadata(
461 unreleased_start=None,
462 unreleased_end=None,
463 latest_version="1.0.0",
464 latest_version_position=2,
465 )
466
467 new_content = "## Unreleased\n\n### Added\n- New feature"
468
469 result = incremental_build(new_content, lines, metadata)
470
471 expected = [
472 "# Changelog",
473 "",
474 "## Unreleased\n\n### Added\n- New feature",
475 "\n",
476 "## 1.0.0 (2023-01-01)",
477 "",
478 "### Fixed",
479 "- Bug fix",
480 ]
481
482 assert result == expected
483
484 def test_skip_behavior_during_unreleased_section(self):
485 """Test that lines are properly skipped during unreleased section processing."""
486 lines = [
487 "# Changelog",
488 "",
489 "## Unreleased",
490 "",
491 "### Added",
492 "- Old feature 1",
493 "- Old feature 2",
494 "",
495 "### Fixed",
496 "- Old bug fix",
497 "",
498 "## 1.0.0 (2023-01-01)",
499 "",
500 "### Fixed",
501 "- Bug fix",
502 ]
503
504 metadata = Metadata(
505 unreleased_start=2,
506 unreleased_end=9,
507 latest_version="1.0.0",
508 latest_version_position=11,
509 )
510
511 new_content = "## Unreleased\n\n### Added\n- New feature"
512
513 result = incremental_build(new_content, lines, metadata)
514
515 expected = [
516 "# Changelog",
517 "",
518 "",
519 "## Unreleased\n\n### Added\n- New feature",
520 "\n",
521 "## 1.0.0 (2023-01-01)",
522 "",
523 "### Fixed",
524 "- Bug fix",
525 ]
526
527 assert result == expected
528
529 def test_latest_version_position_at_unreleased_end(self):
530 """Test behavior when latest_version_position equals unreleased_end."""
531 lines = [
532 "# Changelog",
533 "",
534 "## Unreleased",
535 "",
536 "### Added",
537 "- New feature",
538 "",
539 "## 1.0.0 (2023-01-01)",
540 ]
541
542 metadata = Metadata(
543 unreleased_start=2,
544 unreleased_end=5,
545 latest_version="1.0.0",
546 latest_version_position=5,
547 )
548
549 new_content = "## Unreleased\n\n### Added\n- Another new feature"
550
551 result = incremental_build(new_content, lines, metadata)
552
553 expected = [
554 "# Changelog",
555 "",
556 "## Unreleased\n\n### Added\n- Another new feature",
557 "\n",
558 "- New feature",
559 "",
560 "## 1.0.0 (2023-01-01)",
561 ]
562
563 assert result == expected
564
565 def test_latest_version_position_before_unreleased_start(self):
566 """Test behavior when latest_version_position is before unreleased_start."""
567 lines = [
568 "# Changelog",
569 "",
570 "## 1.0.0 (2023-01-01)",
571 "",
572 "### Fixed",
573 "- Bug fix",
574 "",
575 "## Unreleased",
576 "",
577 "### Added",
578 "- New feature",
579 ]
580
581 metadata = Metadata(
582 unreleased_start=7,
583 unreleased_end=9,
584 latest_version="1.0.0",
585 latest_version_position=2,
586 )
587
588 new_content = "## Unreleased\n\n### Added\n- Another new feature"
589
590 result = incremental_build(new_content, lines, metadata)
591
592 expected = [
593 "# Changelog",
594 "",
595 "## Unreleased\n\n### Added\n- Another new feature",
596 "\n",
597 "## 1.0.0 (2023-01-01)",
598 "",
599 "### Fixed",
600 "- Bug fix",
601 "",
602 "### Added",
603 "- New feature",
604 ]
605
606 assert result == expected