Debian Codegraph @

tests/commands/test_changelog_command.py

raw
1 from __future__ import annotations
2
3 import itertools
4 from pathlib import Path
5 from textwrap import dedent
6 from typing import TYPE_CHECKING
7
8 import pytest
9 from jinja2 import FileSystemLoader
10
11 from commitizen import git
12 from commitizen.commands.changelog import Changelog
13 from commitizen.exceptions import (
14 DryRunExit,
15 InvalidCommandArgumentError,
16 NoCommitsFoundError,
17 NoRevisionError,
18 NotAGitProjectError,
19 NotAllowed,
20 )
21
22 if TYPE_CHECKING:
23 from pytest_mock import MockFixture
24 from pytest_regressions.file_regression import FileRegressionFixture
25
26 from commitizen.changelog_formats import ChangelogFormat
27 from commitizen.config.base_config import BaseConfig
28 from commitizen.config.json_config import JsonConfig
29 from commitizen.cz.base import BaseCommitizen
30 from tests.utils import UtilFixture
31
32
33 @pytest.fixture
34 def changelog_jinja_file(tmp_commitizen_project: Path) -> Path:
35 return tmp_commitizen_project / "changelog.jinja"
36
37
38 @pytest.fixture
39 def changelog_tpl(
40 tmp_commitizen_project: Path, any_changelog_format: ChangelogFormat
41 ) -> Path:
42 return tmp_commitizen_project / any_changelog_format.template
43
44
45 @pytest.fixture
46 def changelog_file(
47 tmp_commitizen_project: Path, any_changelog_format: ChangelogFormat
48 ) -> Path:
49 return tmp_commitizen_project / any_changelog_format.default_changelog_file
50
51
52 @pytest.mark.usefixtures("tmp_commitizen_project")
53 def test_changelog_from_version_zero_point_two(
54 capsys: pytest.CaptureFixture,
55 file_regression: FileRegressionFixture,
56 util: UtilFixture,
57 ):
58 util.create_file_and_commit("feat: new file")
59 util.create_file_and_commit("refactor: not in changelog")
60
61 # create tag
62 util.run_cli("bump", "--yes")
63 capsys.readouterr()
64
65 util.create_file_and_commit("feat: after 0.2.0")
66 util.create_file_and_commit("feat: after 0.2")
67
68 with pytest.raises(DryRunExit):
69 util.run_cli("changelog", "--dry-run", "--start-rev", "0.2.0")
70
71 out, _ = capsys.readouterr()
72 file_regression.check(out, extension=".md")
73
74
75 @pytest.mark.usefixtures("tmp_commitizen_project")
76 def test_changelog_with_different_cz(
77 capsys: pytest.CaptureFixture,
78 file_regression: FileRegressionFixture,
79 util: UtilFixture,
80 ):
81 util.create_file_and_commit("JRA-34 #comment corrected indent issue")
82 util.create_file_and_commit("JRA-35 #time 1w 2d 4h 30m Total work logged")
83
84 with pytest.raises(DryRunExit):
85 util.run_cli("-n", "cz_jira", "changelog", "--dry-run")
86 out, _ = capsys.readouterr()
87 file_regression.check(out, extension=".md")
88
89
90 @pytest.mark.usefixtures("tmp_commitizen_project")
91 def test_changelog_from_start(
92 changelog_format: ChangelogFormat,
93 file_regression: FileRegressionFixture,
94 util: UtilFixture,
95 ):
96 util.create_file_and_commit("feat: new file")
97 util.create_file_and_commit("refactor: is in changelog")
98 util.create_file_and_commit("Merge into master")
99 changelog_file = f"CHANGELOG.{changelog_format.extension}"
100 template = f"CHANGELOG.{changelog_format.extension}.j2"
101
102 util.run_cli("changelog", "--file-name", changelog_file, "--template", template)
103
104 out = Path(changelog_file).read_text(encoding="utf-8")
105 file_regression.check(out, extension=changelog_format.ext)
106
107
108 @pytest.mark.usefixtures("tmp_commitizen_project")
109 @pytest.mark.freeze_time("2022-08-14")
110 def test_changelog_replacing_unreleased_using_incremental(
111 changelog_format: ChangelogFormat,
112 file_regression: FileRegressionFixture,
113 util: UtilFixture,
114 ):
115 util.create_file_and_commit("feat: add new output")
116 util.create_file_and_commit("fix: output glitch")
117 util.create_file_and_commit("Merge into master")
118 changelog_file = f"CHANGELOG.{changelog_format.extension}"
119 template = f"CHANGELOG.{changelog_format.extension}.j2"
120
121 util.run_cli("changelog", "--file-name", changelog_file, "--template", template)
122
123 util.run_cli("bump", "--yes", "--file-name", changelog_file, "--template", template)
124
125 util.create_file_and_commit("fix: mama gotta work")
126 util.create_file_and_commit("feat: add more stuff")
127 util.create_file_and_commit("Merge into master")
128
129 util.run_cli(
130 "changelog",
131 "--incremental",
132 "--file-name",
133 changelog_file,
134 "--template",
135 template,
136 )
137
138 out = Path(changelog_file).read_text(encoding="utf-8")
139
140 file_regression.check(out, extension=changelog_format.ext)
141
142
143 @pytest.mark.usefixtures("tmp_commitizen_project")
144 @pytest.mark.freeze_time("2022-08-14")
145 def test_changelog_is_persisted_using_incremental(
146 changelog_path: Path,
147 file_regression: FileRegressionFixture,
148 util: UtilFixture,
149 ):
150 util.create_file_and_commit("feat: add new output")
151 util.create_file_and_commit("fix: output glitch")
152 util.create_file_and_commit("Merge into master")
153
154 util.run_cli("bump", "--yes")
155
156 util.run_cli("changelog")
157
158 with changelog_path.open("a", encoding="utf-8") as f:
159 f.write("\nnote: this should be persisted using increment\n")
160
161 util.create_file_and_commit("fix: mama gotta work")
162 util.create_file_and_commit("feat: add more stuff")
163 util.create_file_and_commit("Merge into master")
164
165 util.run_cli("changelog", "--incremental")
166
167 out = changelog_path.read_text(encoding="utf-8")
168
169 file_regression.check(out, extension=".md")
170
171
172 @pytest.mark.usefixtures("tmp_commitizen_project")
173 def test_changelog_incremental_angular_sample(
174 changelog_path: Path,
175 file_regression: FileRegressionFixture,
176 util: UtilFixture,
177 ):
178 with changelog_path.open("w", encoding="utf-8") as f:
179 f.write(
180 "# [10.0.0-rc.3](https://github.com/angular/angular/compare/10.0.0-rc.2...10.0.0-rc.3) (2020-04-22)\n"
181 "\n"
182 "### Bug Fixes"
183 "\n"
184 "* **common:** format day-periods that cross midnight ([#36611](https://github.com/angular/angular/issues/36611)) ([c6e5fc4](https://github.com/angular/angular/commit/c6e5fc4)), closes [#36566](https://github.com/angular/angular/issues/36566)\n"
185 )
186 util.create_file_and_commit("irrelevant commit")
187 util.create_tag("10.0.0-rc.3")
188
189 util.create_file_and_commit("feat: add new output")
190 util.create_file_and_commit("fix: output glitch")
191 util.create_file_and_commit("fix: mama gotta work")
192 util.create_file_and_commit("feat: add more stuff")
193 util.create_file_and_commit("Merge into master")
194
195 util.run_cli("changelog", "--incremental")
196
197 out = changelog_path.read_text(encoding="utf-8")
198
199 file_regression.check(out, extension=".md")
200
201
202 KEEP_A_CHANGELOG = """# Changelog
203 All notable changes to this project will be documented in this file.
204
205 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
206 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
207
208 ## [Unreleased]
209
210 ## [1.0.0] - 2017-06-20
211 ### Added
212 - New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
213 - Version navigation.
214
215 ### Changed
216 - Start using "changelog" over "change log" since it's the common usage.
217
218 ### Removed
219 - Section about "changelog" vs "CHANGELOG".
220
221 ## [0.3.0] - 2015-12-03
222 ### Added
223 - RU translation from [@aishek](https://github.com/aishek).
224 """
225
226
227 @pytest.mark.usefixtures("tmp_commitizen_project")
228 def test_changelog_incremental_keep_a_changelog_sample(
229 changelog_path: Path,
230 file_regression: FileRegressionFixture,
231 util: UtilFixture,
232 ):
233 with changelog_path.open("w", encoding="utf-8") as f:
234 f.write(KEEP_A_CHANGELOG)
235 util.create_file_and_commit("irrelevant commit")
236 util.create_tag("1.0.0")
237
238 util.create_file_and_commit("feat: add new output")
239 util.create_file_and_commit("fix: output glitch")
240 util.create_file_and_commit("fix: mama gotta work")
241 util.create_file_and_commit("feat: add more stuff")
242 util.create_file_and_commit("Merge into master")
243
244 util.run_cli("changelog", "--incremental")
245
246 out = changelog_path.read_text(encoding="utf-8")
247
248 file_regression.check(out, extension=".md")
249
250
251 @pytest.mark.usefixtures("tmp_commitizen_project")
252 @pytest.mark.parametrize("dry_run", [True, False])
253 def test_changelog_hook(
254 mocker: MockFixture, config: BaseConfig, dry_run: bool, util: UtilFixture
255 ):
256 changelog_hook_mock = mocker.Mock()
257 changelog_hook_mock.return_value = "cool changelog hook"
258
259 util.create_file_and_commit("feat: new file")
260 util.create_file_and_commit("refactor: is in changelog")
261 util.create_file_and_commit("Merge into master")
262
263 config.settings["change_type_order"] = ["Refactor", "Feat"] # type: ignore[typeddict-unknown-key]
264 changelog = Changelog(
265 config, {"unreleased_version": None, "incremental": True, "dry_run": dry_run}
266 )
267 mocker.patch.object(changelog.cz, "changelog_hook", changelog_hook_mock)
268 if dry_run:
269 with pytest.raises(DryRunExit):
270 changelog()
271 else:
272 changelog()
273
274 full_changelog = (
275 "## Unreleased\n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n"
276 )
277 partial_changelog = full_changelog
278 if dry_run:
279 partial_changelog = ""
280
281 changelog_hook_mock.assert_called_with(full_changelog, partial_changelog)
282
283
284 @pytest.mark.usefixtures("tmp_commitizen_project")
285 def test_changelog_hook_customize(
286 mocker: MockFixture, config_customize: JsonConfig, util: UtilFixture
287 ):
288 changelog_hook_mock = mocker.Mock()
289 changelog_hook_mock.return_value = "cool changelog hook"
290
291 util.create_file_and_commit("feat: new file")
292 util.create_file_and_commit("refactor: is in changelog")
293 util.create_file_and_commit("Merge into master")
294
295 changelog = Changelog(
296 config_customize,
297 {"unreleased_version": None, "incremental": True, "dry_run": False},
298 )
299 mocker.patch.object(changelog.cz, "changelog_hook", changelog_hook_mock)
300 changelog()
301 full_changelog = (
302 "## Unreleased\n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n"
303 )
304
305 changelog_hook_mock.assert_called_with(full_changelog, full_changelog)
306
307
308 @pytest.mark.usefixtures("tmp_commitizen_project")
309 def test_changelog_release_hook(
310 mocker: MockFixture, config: BaseConfig, util: UtilFixture
311 ):
312 def changelog_release_hook(release: dict, tag: git.GitTag) -> dict:
313 return release
314
315 for i in range(3):
316 util.create_file_and_commit("feat: new file")
317 util.create_file_and_commit("refactor: is in changelog")
318 util.create_file_and_commit("Merge into master")
319 util.create_tag(f"0.{i + 1}.0")
320
321 # changelog = Changelog(config, {})
322 changelog = Changelog(
323 config, {"unreleased_version": None, "incremental": True, "dry_run": False}
324 )
325 mocker.patch.object(changelog.cz, "changelog_release_hook", changelog_release_hook)
326 spy = mocker.spy(changelog.cz, "changelog_release_hook")
327 changelog()
328
329 assert spy.call_count == 3
330
331
332 @pytest.mark.usefixtures("tmp_commitizen_project")
333 def test_changelog_with_non_linear_merges_commit_order(
334 mocker: MockFixture, config_customize: JsonConfig, util: UtilFixture
335 ):
336 """Test that commits merged non-linearly are correctly ordered in the changelog
337
338 A typical scenario is having two branches from main like so:
339 * feat: I will be merged first - (2023-03-01 11:35:51 +0100) | (branchB)
340 | * feat: I will be merged second - (2023-03-01 11:35:22 +0100) | (branchA)
341 |/
342 * feat: initial commit - (2023-03-01 11:34:54 +0100) | (HEAD -> main)
343
344 And merging them, for example in the reverse order they were created on would give the following:
345 * Merge branch 'branchA' - (2023-03-01 11:42:59 +0100) | (HEAD -> main)
346 |\
347 | * feat: I will be merged second - (2023-03-01 11:35:22 +0100) | (branchA)
348 * | feat: I will be merged first - (2023-03-01 11:35:51 +0100) | (branchB)
349 |/
350 * feat: initial commit - (2023-03-01 11:34:54 +0100) |
351
352 In this case we want the changelog to reflect the topological order of commits,
353 i.e. the order in which they were merged into the main branch
354
355 So the above example should result in the following:
356 ## Unreleased
357
358 ### Feat
359 - I will be merged second
360 - I will be merged first
361 - initial commit
362 """
363 changelog_hook_mock = mocker.Mock()
364 changelog_hook_mock.return_value = "cool changelog hook"
365
366 util.create_file_and_commit("feat: initial commit")
367
368 main_branch = util.get_current_branch()
369
370 util.create_branch("branchA")
371 util.create_branch("branchB")
372
373 util.switch_branch("branchA")
374 util.create_file_and_commit("feat: I will be merged second")
375
376 util.switch_branch("branchB")
377 util.create_file_and_commit("feat: I will be merged first")
378
379 # Note we merge branches opposite order than author_date
380 util.switch_branch(main_branch)
381 util.merge_branch("branchB")
382 util.merge_branch("branchA")
383
384 changelog = Changelog(
385 config_customize,
386 {"unreleased_version": None, "incremental": True, "dry_run": False},
387 )
388 mocker.patch.object(changelog.cz, "changelog_hook", changelog_hook_mock)
389 changelog()
390 full_changelog = "\
391 ## Unreleased\n\n\
392 \
393 ### Feat\n\n\
394 - I will be merged second\n\
395 - I will be merged first\n\
396 - initial commit\n"
397
398 changelog_hook_mock.assert_called_with(full_changelog, full_changelog)
399
400
401 @pytest.mark.usefixtures("tmp_commitizen_project")
402 def test_changelog_multiple_incremental_do_not_add_new_lines(
403 changelog_path: Path,
404 file_regression: FileRegressionFixture,
405 util: UtilFixture,
406 ):
407 """Test for bug https://github.com/commitizen-tools/commitizen/issues/192"""
408 for commit_message in [
409 "feat: add new output",
410 "fix: output glitch",
411 "fix: no more explosions",
412 "feat: add more stuff",
413 ]:
414 util.create_file_and_commit(commit_message)
415 util.run_cli("changelog", "--incremental")
416
417 out = changelog_path.read_text(encoding="utf-8")
418
419 file_regression.check(out, extension=".md")
420
421
422 @pytest.mark.usefixtures("tmp_commitizen_project")
423 def test_changelog_incremental_newline_separates_new_content_from_old(
424 changelog_path: Path,
425 util: UtilFixture,
426 file_regression: FileRegressionFixture,
427 ):
428 """Test for https://github.com/commitizen-tools/commitizen/issues/509"""
429 with changelog_path.open("w", encoding="utf-8") as f:
430 f.write("Pre-existing content that should be kept\n")
431
432 util.create_file_and_commit("feat: add more cat videos")
433 util.run_cli("changelog", "--incremental")
434 out = changelog_path.read_text(encoding="utf-8")
435
436 file_regression.check(out, extension=".md")
437
438
439 def test_changelog_without_revision(tmp_commitizen_project, util: UtilFixture):
440 (tmp_commitizen_project / "CHANGELOG.md").write_text(
441 """
442 # Unreleased
443
444 ## v1.0.0
445 """
446 )
447
448 # No revision
449 with pytest.raises(NoRevisionError):
450 util.run_cli("changelog", "--incremental")
451
452 util.create_file_and_commit("feat: new file")
453 util.create_tag("2.0.0")
454
455 # With different tag name and changelog content
456 with pytest.raises(NoRevisionError):
457 util.run_cli("changelog", "--incremental")
458
459
460 @pytest.mark.usefixtures("tmp_commitizen_project")
461 def test_changelog_incremental_with_revision(util: UtilFixture):
462 """combining incremental with a revision doesn't make sense"""
463 with pytest.raises(NotAllowed):
464 util.run_cli("changelog", "--incremental", "0.2.0")
465
466
467 @pytest.mark.usefixtures("chdir")
468 def test_changelog_in_non_git_project(util: UtilFixture):
469 with pytest.raises(NotAGitProjectError):
470 util.run_cli("changelog", "--incremental")
471
472
473 @pytest.mark.parametrize(
474 "commit_message",
475 [
476 "feat(users): email pattern corrected\n\n"
477 "BREAKING CHANGE: migrate by renaming user to users\n\n"
478 "footer content",
479 "feat(users): email pattern corrected\n\n"
480 "body content\n\n"
481 "BREAKING CHANGE: migrate by renaming user to users",
482 ],
483 )
484 @pytest.mark.usefixtures("tmp_commitizen_project")
485 def test_breaking_change_content_v1_beta(
486 capsys: pytest.CaptureFixture,
487 file_regression: FileRegressionFixture,
488 util: UtilFixture,
489 commit_message: str,
490 ):
491 util.create_file_and_commit(commit_message)
492 with pytest.raises(DryRunExit):
493 util.run_cli("changelog", "--dry-run")
494 out, _ = capsys.readouterr()
495 file_regression.check(out, extension=".md")
496
497
498 @pytest.mark.usefixtures("tmp_commitizen_project")
499 def test_breaking_change_content_v1_multiline(
500 capsys: pytest.CaptureFixture,
501 file_regression: FileRegressionFixture,
502 util: UtilFixture,
503 ):
504 commit_message = (
505 "feat(users): email pattern corrected\n\n"
506 "body content\n\n"
507 "BREAKING CHANGE: migrate by renaming user to users.\n"
508 "and then connect the thingy with the other thingy\n\n"
509 "footer content"
510 )
511 util.create_file_and_commit(commit_message)
512 with pytest.raises(DryRunExit):
513 util.run_cli("changelog", "--dry-run")
514 out, _ = capsys.readouterr()
515 file_regression.check(out, extension=".md")
516
517
518 @pytest.mark.usefixtures("tmp_commitizen_project")
519 def test_breaking_change_content_v1_with_exclamation_mark(
520 capsys: pytest.CaptureFixture,
521 file_regression: FileRegressionFixture,
522 util: UtilFixture,
523 ):
524 commit_message = "chore!: drop support for py36"
525 util.create_file_and_commit(commit_message)
526 with pytest.raises(DryRunExit):
527 util.run_cli("changelog", "--dry-run")
528 out, _ = capsys.readouterr()
529
530 file_regression.check(out, extension=".md")
531
532
533 @pytest.mark.usefixtures("tmp_commitizen_project")
534 def test_breaking_change_content_v1_with_exclamation_mark_feat(
535 capsys: pytest.CaptureFixture,
536 file_regression: FileRegressionFixture,
537 util: UtilFixture,
538 ):
539 commit_message = "feat(pipeline)!: some text with breaking change"
540 util.create_file_and_commit(commit_message)
541 with pytest.raises(DryRunExit):
542 util.run_cli("changelog", "--dry-run")
543 out, _ = capsys.readouterr()
544
545 file_regression.check(out, extension=".md")
546
547
548 @pytest.mark.usefixtures("tmp_commitizen_project")
549 def test_changelog_config_flag_increment(
550 changelog_path: Path,
551 config_path: Path,
552 file_regression: FileRegressionFixture,
553 util: UtilFixture,
554 ):
555 with config_path.open("a", encoding="utf-8") as f:
556 f.write("changelog_incremental = true\n")
557 with changelog_path.open("a", encoding="utf-8") as f:
558 f.write("\nnote: this should be persisted using increment\n")
559
560 util.create_file_and_commit("feat: add new output")
561
562 util.run_cli("changelog")
563
564 out = changelog_path.read_text(encoding="utf-8")
565
566 assert "this should be persisted using increment" in out
567 file_regression.check(out, extension=".md")
568
569
570 @pytest.mark.parametrize("test_input", ["rc", "alpha", "beta"])
571 @pytest.mark.usefixtures("tmp_commitizen_project")
572 @pytest.mark.freeze_time("2025-12-29")
573 def test_changelog_config_flag_merge_prerelease(
574 changelog_path: Path,
575 config_path: Path,
576 file_regression: FileRegressionFixture,
577 test_input: str,
578 util: UtilFixture,
579 ):
580 with config_path.open("a") as f:
581 f.write("changelog_merge_prerelease = true\n")
582
583 util.create_file_and_commit("irrelevant commit")
584 util.create_tag("1.0.0")
585
586 util.create_file_and_commit("feat: add new output")
587 util.create_file_and_commit("fix: output glitch")
588
589 util.run_cli("bump", "--prerelease", test_input, "--yes")
590
591 util.create_file_and_commit("fix: mama gotta work")
592 util.create_file_and_commit("feat: add more stuff")
593 util.create_file_and_commit("Merge into master")
594
595 util.run_cli("changelog")
596
597 out = changelog_path.read_text()
598
599 file_regression.check(out, extension=".md")
600
601
602 @pytest.mark.usefixtures("tmp_commitizen_project")
603 def test_changelog_config_start_rev_option(
604 capsys: pytest.CaptureFixture,
605 config_path: Path,
606 file_regression: FileRegressionFixture,
607 util: UtilFixture,
608 ):
609 # create commit and tag
610 util.create_file_and_commit("feat: new file")
611 util.run_cli("bump", "--yes")
612 capsys.readouterr()
613
614 util.create_file_and_commit("feat: after 0.2.0")
615 util.create_file_and_commit("feat: after 0.2")
616
617 with config_path.open("a", encoding="utf-8") as f:
618 f.write('changelog_start_rev = "0.2.0"\n')
619
620 with pytest.raises(DryRunExit):
621 util.run_cli("changelog", "--dry-run")
622
623 out, _ = capsys.readouterr()
624 file_regression.check(out, extension=".md")
625
626
627 @pytest.mark.usefixtures("tmp_commitizen_project")
628 def test_changelog_incremental_keep_a_changelog_sample_with_annotated_tag(
629 changelog_path: Path,
630 file_regression: FileRegressionFixture,
631 util: UtilFixture,
632 ):
633 """Fix #378"""
634 with changelog_path.open("w", encoding="utf-8") as f:
635 f.write(KEEP_A_CHANGELOG)
636 util.create_file_and_commit("irrelevant commit")
637 util.create_tag("1.0.0", annotated=True)
638
639 util.create_file_and_commit("feat: add new output")
640 util.create_file_and_commit("fix: output glitch")
641 util.create_file_and_commit("fix: mama gotta work")
642 util.create_file_and_commit("feat: add more stuff")
643 util.create_file_and_commit("Merge into master")
644
645 util.run_cli("changelog", "--incremental")
646
647 out = changelog_path.read_text(encoding="utf-8")
648
649 file_regression.check(out, extension=".md")
650
651
652 @pytest.mark.parametrize("test_input", ["rc", "alpha", "beta"])
653 @pytest.mark.usefixtures("tmp_commitizen_project")
654 @pytest.mark.freeze_time("2021-06-11")
655 def test_changelog_incremental_with_release_candidate_version(
656 changelog_path: Path,
657 file_regression: FileRegressionFixture,
658 test_input: str,
659 util: UtilFixture,
660 ):
661 """Fix #357"""
662 with changelog_path.open("w", encoding="utf-8") as f:
663 f.write(KEEP_A_CHANGELOG)
664 util.create_file_and_commit("irrelevant commit")
665 util.create_tag("1.0.0", annotated=True)
666
667 util.create_file_and_commit("feat: add new output")
668 util.create_file_and_commit("fix: output glitch")
669
670 util.run_cli("bump", "--changelog", "--prerelease", test_input, "--yes")
671
672 util.create_file_and_commit("fix: mama gotta work")
673 util.create_file_and_commit("feat: add more stuff")
674 util.create_file_and_commit("Merge into master")
675
676 util.run_cli("changelog", "--incremental")
677
678 out = changelog_path.read_text(encoding="utf-8")
679
680 file_regression.check(out, extension=".md")
681
682
683 @pytest.mark.parametrize(
684 ("from_pre", "to_pre"), itertools.product(["alpha", "beta", "rc"], repeat=2)
685 )
686 @pytest.mark.usefixtures("tmp_commitizen_project")
687 @pytest.mark.freeze_time("2021-06-11")
688 def test_changelog_incremental_with_prerelease_version_to_prerelease_version(
689 changelog_path: Path,
690 file_regression: FileRegressionFixture,
691 from_pre: str,
692 to_pre: str,
693 util: UtilFixture,
694 ):
695 with changelog_path.open("w") as f:
696 f.write(KEEP_A_CHANGELOG)
697 util.create_file_and_commit("irrelevant commit")
698 util.create_tag("1.0.0", annotated=True)
699
700 util.create_file_and_commit("feat: add new output")
701 util.create_file_and_commit("fix: output glitch")
702
703 util.run_cli("bump", "--changelog", "--prerelease", from_pre, "--yes")
704
705 util.run_cli("bump", "--changelog", "--prerelease", to_pre, "--yes")
706
707 out = changelog_path.read_text()
708
709 file_regression.check(out, extension=".md")
710
711
712 @pytest.mark.parametrize("test_input", ["rc", "alpha", "beta"])
713 @pytest.mark.usefixtures("tmp_commitizen_project")
714 @pytest.mark.freeze_time("2025-12-29")
715 def test_changelog_release_candidate_version_with_merge_prerelease(
716 changelog_path: Path,
717 file_regression: FileRegressionFixture,
718 test_input: str,
719 util: UtilFixture,
720 ):
721 """Fix #357"""
722 with changelog_path.open("w") as f:
723 f.write(KEEP_A_CHANGELOG)
724 util.create_file_and_commit("irrelevant commit")
725 util.create_tag("1.0.0")
726
727 util.create_file_and_commit("feat: add new output")
728 util.create_file_and_commit("fix: output glitch")
729
730 util.run_cli("bump", "--prerelease", test_input, "--yes")
731
732 util.create_file_and_commit("fix: mama gotta work")
733 util.create_file_and_commit("feat: add more stuff")
734 util.create_file_and_commit("Merge into master")
735
736 util.run_cli("changelog", "--merge-prerelease")
737
738 out = changelog_path.read_text()
739
740 file_regression.check(out, extension=".md")
741
742
743 @pytest.mark.parametrize("test_input", ["rc", "alpha", "beta"])
744 @pytest.mark.usefixtures("tmp_commitizen_project")
745 @pytest.mark.freeze_time("2023-04-16")
746 def test_changelog_incremental_with_merge_prerelease(
747 changelog_path: Path,
748 file_regression: FileRegressionFixture,
749 test_input: str,
750 util: UtilFixture,
751 ):
752 """Fix #357"""
753 with changelog_path.open("w") as f:
754 f.write(KEEP_A_CHANGELOG)
755 util.create_file_and_commit("irrelevant commit")
756 util.create_tag("1.0.0")
757
758 util.create_file_and_commit("feat: add new output")
759
760 util.run_cli("bump", "--prerelease", test_input, "--yes", "--changelog")
761
762 util.create_file_and_commit("fix: output glitch")
763
764 util.run_cli("bump", "--prerelease", test_input, "--yes")
765
766 util.create_file_and_commit("fix: mama gotta work")
767 util.create_file_and_commit("feat: add more stuff")
768 util.create_file_and_commit("Merge into master")
769
770 util.run_cli("changelog", "--merge-prerelease", "--incremental")
771
772 out = changelog_path.read_text()
773
774 file_regression.check(out, extension=".md")
775
776
777 @pytest.mark.usefixtures("tmp_commitizen_project")
778 def test_changelog_with_filename_as_empty_string(config_path: Path, util: UtilFixture):
779 with config_path.open("a", encoding="utf-8") as f:
780 f.write("changelog_file = true\n")
781
782 util.create_file_and_commit("feat: add new output")
783
784 with pytest.raises(NotAllowed):
785 util.run_cli("changelog")
786
787
788 @pytest.mark.usefixtures("tmp_commitizen_project")
789 @pytest.mark.freeze_time("2022-02-13")
790 def test_changelog_from_rev_first_version_from_arg(
791 config_path: Path,
792 changelog_path: Path,
793 file_regression: FileRegressionFixture,
794 util: UtilFixture,
795 ):
796 with config_path.open("a", encoding="utf-8") as f:
797 f.write('tag_format = "$version"\n')
798
799 # create commit and tag
800 util.create_file_and_commit("feat: new file")
801 util.run_cli("bump", "--yes")
802
803 util.create_file_and_commit("feat: after 0.2.0")
804 util.create_file_and_commit("feat: another feature")
805
806 util.run_cli("bump", "--yes")
807
808 util.run_cli("changelog", "0.2.0")
809 out = changelog_path.read_text(encoding="utf-8")
810
811 file_regression.check(out, extension=".md")
812
813
814 @pytest.mark.usefixtures("tmp_commitizen_project")
815 @pytest.mark.freeze_time("2022-02-13")
816 def test_changelog_from_rev_latest_version_from_arg(
817 config_path: Path,
818 changelog_path: Path,
819 file_regression: FileRegressionFixture,
820 util: UtilFixture,
821 ):
822 with config_path.open("a", encoding="utf-8") as f:
823 f.write('tag_format = "$version"\n')
824
825 # create commit and tag
826 util.create_file_and_commit("feat: new file")
827 util.run_cli("bump", "--yes")
828
829 util.create_file_and_commit("feat: after 0.2.0")
830 util.create_file_and_commit("feat: another feature")
831
832 util.run_cli("bump", "--yes")
833
834 util.run_cli("changelog", "0.3.0")
835
836 out = changelog_path.read_text()
837
838 file_regression.check(out, extension=".md")
839
840
841 @pytest.mark.usefixtures("tmp_commitizen_project")
842 @pytest.mark.parametrize(
843 ("rev_range", "tag"),
844 [
845 pytest.param("0.8.0", "0.2.0", id="single-not-found"),
846 pytest.param("0.1.0..0.3.0", "0.3.0", id="lower-bound-not-found"),
847 pytest.param("0.1.0..0.3.0", "0.1.0", id="upper-bound-not-found"),
848 pytest.param("0.3.0..0.4.0", "0.2.0", id="none-found"),
849 ],
850 )
851 def test_changelog_from_rev_range_not_found(
852 config_path: Path, rev_range: str, tag: str, util: UtilFixture
853 ):
854 """Provides an invalid revision ID to changelog command"""
855 with config_path.open("a", encoding="utf-8") as f:
856 f.write('tag_format = "$version"\n')
857
858 # create commit and tag
859 util.create_file_and_commit("feat: new file")
860 util.create_tag(tag)
861 util.create_file_and_commit("feat: new file")
862 util.create_tag("1.0.0")
863
864 with pytest.raises(NoCommitsFoundError, match="Could not find a valid revision"):
865 util.run_cli("changelog", rev_range) # it shouldn't exist
866
867
868 @pytest.mark.usefixtures("tmp_commitizen_project")
869 def test_changelog_multiple_matching_tags(
870 config_path: Path, changelog_path: Path, util: UtilFixture
871 ):
872 with config_path.open("a", encoding="utf-8") as f:
873 f.write('tag_format = "new-$version"\nlegacy_tag_formats = ["legacy-$version"]')
874
875 util.create_file_and_commit("feat: new file")
876 util.create_tag("legacy-1.0.0")
877 util.create_file_and_commit("feat: new file")
878 util.create_tag("legacy-2.0.0")
879 util.create_tag("new-2.0.0")
880
881 with pytest.warns() as warnings:
882 util.run_cli("changelog", "1.0.0..2.0.0") # it shouldn't exist
883
884 assert len(warnings) == 1
885 warning = warnings[0]
886 assert "Multiple tags found for version 2.0.0" in str(warning.message)
887
888 out = changelog_path.read_text()
889
890 # Ensure only one tag is rendered
891 assert out.count("2.0.0") == 1
892
893
894 @pytest.mark.usefixtures("tmp_commitizen_project")
895 def test_changelog_from_rev_range_default_tag_format(
896 changelog_path: Path, util: UtilFixture
897 ):
898 """Checks that rev_range is calculated with the default (None) tag format"""
899 # create commit and tag
900 util.create_file_and_commit("feat: new file")
901 util.run_cli("bump", "--yes")
902
903 util.create_file_and_commit("feat: after 0.2.0")
904 util.create_file_and_commit("feat: another feature")
905
906 util.run_cli("bump", "--yes")
907
908 util.run_cli("changelog", "0.3.0")
909
910 out = changelog_path.read_text()
911
912 assert "new file" not in out
913
914
915 @pytest.mark.usefixtures("tmp_commitizen_project")
916 @pytest.mark.freeze_time("2022-02-13")
917 def test_changelog_from_rev_version_range_including_first_tag(
918 config_path: Path,
919 changelog_path: Path,
920 file_regression: FileRegressionFixture,
921 util: UtilFixture,
922 ):
923 with config_path.open("a", encoding="utf-8") as f:
924 f.write('tag_format = "$version"\n')
925
926 # create commit and tag
927 util.create_file_and_commit("feat: new file")
928 util.run_cli("bump", "--yes")
929
930 util.create_file_and_commit("feat: after 0.2.0")
931 util.create_file_and_commit("feat: another feature")
932
933 util.run_cli("bump", "--yes")
934
935 util.run_cli("changelog", "0.2.0..0.3.0")
936 out = changelog_path.read_text(encoding="utf-8")
937
938 file_regression.check(out, extension=".md")
939
940
941 @pytest.mark.usefixtures("tmp_commitizen_project")
942 @pytest.mark.freeze_time("2022-02-13")
943 def test_changelog_from_rev_version_range_from_arg(
944 config_path: Path,
945 changelog_path: Path,
946 file_regression: FileRegressionFixture,
947 util: UtilFixture,
948 ):
949 with config_path.open("a", encoding="utf-8") as f:
950 f.write('tag_format = "$version"\n')
951
952 # create commit and tag
953 util.create_file_and_commit("feat: new file")
954 util.run_cli("bump", "--yes")
955 util.create_file_and_commit("feat: after 0.2.0")
956 util.create_file_and_commit("feat: another feature")
957
958 util.run_cli("bump", "--yes")
959
960 util.create_file_and_commit("feat: getting ready for this")
961
962 util.run_cli("bump", "--yes")
963
964 util.run_cli("changelog", "0.3.0..0.4.0")
965 out = changelog_path.read_text()
966
967 file_regression.check(out, extension=".md")
968
969
970 @pytest.mark.usefixtures("tmp_commitizen_project")
971 @pytest.mark.freeze_time("2022-02-13")
972 def test_changelog_from_rev_version_range_with_legacy_tags(
973 config_path: Path,
974 changelog_path: Path,
975 file_regression: FileRegressionFixture,
976 util: UtilFixture,
977 ):
978 Path(config_path).write_text(
979 "\n".join(
980 [
981 "[tool.commitizen]",
982 'version_provider = "scm"',
983 'tag_format = "v$version"',
984 "legacy_tag_formats = [",
985 ' "legacy-${version}",',
986 ' "old-${version}",',
987 "]",
988 ]
989 ),
990 )
991
992 util.create_file_and_commit("feat: new file")
993 util.create_tag("old-0.2.0")
994 util.create_file_and_commit("feat: new file")
995 util.create_tag("legacy-0.3.0")
996 util.create_file_and_commit("feat: new file")
997 util.create_tag("legacy-0.4.0")
998
999 util.run_cli("changelog", "0.2.0..0.4.0")
1000 file_regression.check(Path(changelog_path).read_text(), extension=".md")
1001
1002
1003 @pytest.mark.usefixtures("tmp_commitizen_project")
1004 @pytest.mark.freeze_time("2022-02-13")
1005 def test_changelog_from_rev_version_with_big_range_from_arg(
1006 config_path, changelog_path, file_regression, util: UtilFixture
1007 ):
1008 with config_path.open("a", encoding="utf-8") as f:
1009 f.write('tag_format = "$version"\n')
1010
1011 # create commit and tag
1012 util.create_file_and_commit("feat: new file")
1013
1014 util.run_cli("bump", "--yes")
1015
1016 util.create_file_and_commit("feat: after 0.2.0")
1017 util.create_file_and_commit("feat: another feature")
1018
1019 util.run_cli("bump", "--yes") # 0.3.0
1020 util.create_file_and_commit("feat: getting ready for this")
1021
1022 util.run_cli("bump", "--yes") # 0.4.0
1023 util.create_file_and_commit("fix: small error")
1024
1025 util.run_cli("bump", "--yes") # 0.4.1
1026 util.create_file_and_commit("feat: new shinny feature")
1027
1028 util.run_cli("bump", "--yes") # 0.5.0
1029 util.create_file_and_commit("feat: amazing different shinny feature")
1030
1031 util.run_cli("bump", "--yes") # 0.6.0
1032
1033 util.run_cli("changelog", "0.3.0..0.5.0")
1034 out = changelog_path.read_text()
1035
1036 file_regression.check(out, extension=".md")
1037
1038
1039 @pytest.mark.usefixtures("tmp_commitizen_project")
1040 @pytest.mark.freeze_time("2022-02-13")
1041 def test_changelog_from_rev_latest_version_dry_run(
1042 capsys: pytest.CaptureFixture,
1043 config_path: Path,
1044 file_regression: FileRegressionFixture,
1045 util: UtilFixture,
1046 ):
1047 with config_path.open("a") as f:
1048 f.write('tag_format = "$version"\n')
1049
1050 # create commit and tag
1051 util.create_file_and_commit("feat: new file")
1052 util.run_cli("bump", "--yes")
1053
1054 util.create_file_and_commit("feat: after 0.2.0")
1055 util.create_file_and_commit("feat: another feature")
1056
1057 util.run_cli("bump", "--yes")
1058 capsys.readouterr()
1059
1060 with pytest.raises(DryRunExit):
1061 util.run_cli("changelog", "0.3.0", "--dry-run")
1062
1063 out, _ = capsys.readouterr()
1064
1065 file_regression.check(out, extension=".md")
1066
1067
1068 @pytest.mark.usefixtures("tmp_commitizen_project")
1069 def test_invalid_subject_is_skipped(
1070 capsys: pytest.CaptureFixture,
1071 file_regression: FileRegressionFixture,
1072 util: UtilFixture,
1073 ):
1074 """Fix #510"""
1075 non_conformant_commit_title = (
1076 "Merge pull request #487 from manang/master\n\n"
1077 "feat: skip merge messages that start with Pull request\n"
1078 )
1079 util.create_file_and_commit(non_conformant_commit_title)
1080 util.create_file_and_commit("feat: a new world")
1081 with pytest.raises(DryRunExit):
1082 util.run_cli("changelog", "--dry-run")
1083 out, _ = capsys.readouterr()
1084
1085 file_regression.check(out, extension=".md")
1086
1087
1088 @pytest.mark.usefixtures("tmp_commitizen_project")
1089 @pytest.mark.freeze_time("2022-02-13")
1090 def test_changelog_with_customized_change_type_order(
1091 config_path: Path,
1092 changelog_path: Path,
1093 file_regression: FileRegressionFixture,
1094 util: UtilFixture,
1095 ):
1096 with config_path.open("a") as f:
1097 f.write('tag_format = "$version"\n')
1098 f.write(
1099 'change_type_order = ["BREAKING CHANGE", "Perf", "Fix", "Feat", "Refactor"]\n'
1100 )
1101
1102 # create commit and tag
1103 util.create_file_and_commit("feat: new file")
1104 util.run_cli("bump", "--yes")
1105 util.create_file_and_commit("feat: after 0.2.0")
1106 util.create_file_and_commit("feat: another feature")
1107 util.create_file_and_commit("fix: fix bug")
1108
1109 util.run_cli("bump", "--yes")
1110
1111 util.create_file_and_commit("feat: getting ready for this")
1112 util.create_file_and_commit("perf: perf improvement")
1113
1114 util.run_cli("bump", "--yes")
1115
1116 util.run_cli("changelog", "0.3.0..0.4.0")
1117 out = changelog_path.read_text()
1118
1119 file_regression.check(out, extension=".md")
1120
1121
1122 @pytest.mark.usefixtures("tmp_commitizen_project")
1123 def test_empty_commit_list(mocker: MockFixture, util: UtilFixture):
1124 util.create_file_and_commit("feat: a new world")
1125
1126 # test changelog properly handles when no commits are found for the revision
1127 mocker.patch("commitizen.git.get_commits", return_value=[])
1128 with pytest.raises(NoCommitsFoundError):
1129 util.run_cli("changelog")
1130
1131
1132 @pytest.mark.usefixtures("tmp_commitizen_project")
1133 @pytest.mark.freeze_time("2022-02-13")
1134 def test_changelog_prerelease_rev_with_use_scheme_semver(
1135 capsys: pytest.CaptureFixture,
1136 config_path: Path,
1137 file_regression: FileRegressionFixture,
1138 util: UtilFixture,
1139 ):
1140 with config_path.open("a") as f:
1141 f.write('tag_format = "$version"\nversion_scheme = "semver"')
1142
1143 # create commit and tag
1144 util.create_file_and_commit("feat: new file")
1145 util.run_cli("bump", "--yes")
1146
1147 util.create_file_and_commit("feat: after 0.2.0")
1148 util.create_file_and_commit("feat: another feature")
1149
1150 util.run_cli("bump", "--yes", "--prerelease", "alpha")
1151 capsys.readouterr()
1152
1153 assert git.tag_exist("0.3.0-a0")
1154
1155 with pytest.raises(DryRunExit):
1156 util.run_cli("changelog", "0.3.0-a0", "--dry-run")
1157
1158 out, _ = capsys.readouterr()
1159 file_regression.check(out, extension=".md")
1160
1161 util.run_cli("bump", "--yes", "--prerelease", "alpha")
1162 capsys.readouterr()
1163
1164 assert git.tag_exist("0.3.0-a1")
1165
1166 with pytest.raises(DryRunExit):
1167 util.run_cli("changelog", "0.3.0-a1", "--dry-run")
1168
1169 out, _ = capsys.readouterr()
1170 file_regression.check(out, extension=".second-prerelease.md")
1171
1172
1173 @pytest.mark.usefixtures("tmp_commitizen_project")
1174 def test_changelog_uses_version_tags_for_header(
1175 mocker: MockFixture, config: BaseConfig, util: UtilFixture
1176 ):
1177 """Tests that changelog headers always use version tags even if there are non-version tags
1178
1179 This tests a scenario fixed in this commit:
1180 The first header was using a non-version tag and outputting "## 0-not-a-version" instead of "## 1.0.0
1181 """
1182 util.create_file_and_commit("feat: commit in 1.0.0")
1183 util.create_tag("0-not-a-version")
1184 util.create_tag("1.0.0")
1185 util.create_tag("also-not-a-version")
1186
1187 write_patch = mocker.patch("commitizen.commands.changelog.out.write")
1188
1189 changelog = Changelog(
1190 config, {"dry_run": True, "incremental": True, "unreleased_version": None}
1191 )
1192
1193 with pytest.raises(DryRunExit):
1194 changelog()
1195
1196 changelog_output = write_patch.call_args[0][0]
1197
1198 assert changelog_output.startswith("## 1.0.0")
1199 assert "0-no-a-version" not in changelog_output
1200 assert "also-not-a-version" not in changelog_output
1201
1202
1203 @pytest.mark.usefixtures("tmp_commitizen_project")
1204 @pytest.mark.freeze_time("2022-02-13")
1205 def test_changelog_from_current_version_tag_with_nonversion_tag(
1206 mocker: MockFixture, config: BaseConfig, util: UtilFixture
1207 ):
1208 """Tests that changelog generation for a single version works even if
1209 there is a non-version tag in the list of tags
1210
1211 This tests a scenario which is fixed in this commit:
1212 You have a commit in between two versions (1.0.0..2.0.0) which is tagged with a non-version tag (not-a-version).
1213 In this case commitizen should disregard the non-version tag when determining the rev-range & generating the changelog.
1214 """
1215 util.create_file_and_commit("feat: initial commit")
1216 util.create_tag("1.0.0")
1217
1218 util.create_file_and_commit("feat: commit 1")
1219 util.create_tag("1-not-a-version")
1220
1221 util.create_file_and_commit("feat: commit 2")
1222
1223 util.create_file_and_commit("bump: version 1.0.0 → 2.0.0")
1224 util.create_tag("2.0.0")
1225
1226 write_patch = mocker.patch("commitizen.commands.changelog.out.write")
1227
1228 with pytest.raises(DryRunExit):
1229 Changelog(
1230 config,
1231 {
1232 "dry_run": True,
1233 "incremental": False,
1234 "unreleased_version": None,
1235 "rev_range": "2.0.0",
1236 },
1237 )()
1238
1239 write_patch.assert_called_with(
1240 "\
1241 ## 2.0.0 (2022-02-13)\n\n\
1242 ### Feat\n\n\
1243 - commit 2\n\
1244 - commit 1\n"
1245 )
1246
1247
1248 @pytest.mark.parametrize(
1249 ("arg", "cfg", "expected"),
1250 [
1251 pytest.param("", "", "default", id="default"),
1252 pytest.param("", "changelog.cfg", "from config", id="from-config"),
1253 pytest.param(
1254 "--template=changelog.cmd", "changelog.cfg", "from cmd", id="from-command"
1255 ),
1256 ],
1257 )
1258 def test_changelog_template_option_precedence(
1259 tmp_commitizen_project: Path,
1260 arg: str,
1261 cfg: str,
1262 expected: str,
1263 util: UtilFixture,
1264 changelog_file: Path,
1265 changelog_tpl: Path,
1266 ):
1267 project_root = tmp_commitizen_project
1268 cfg_template = project_root / "changelog.cfg"
1269 cmd_template = project_root / "changelog.cmd"
1270
1271 cfg_template.write_text("from config")
1272 cmd_template.write_text("from cmd")
1273 changelog_tpl.write_text("default")
1274
1275 util.create_file_and_commit("feat: new file")
1276
1277 if cfg:
1278 pyproject = project_root / "pyproject.toml"
1279 pyproject.write_text(
1280 dedent(
1281 f"""\
1282 [tool.commitizen]
1283 version = "0.1.0"
1284 template = "{cfg}"
1285 """
1286 )
1287 )
1288
1289 testargs = ["changelog"]
1290 if arg:
1291 testargs.append(arg)
1292 util.run_cli(*testargs)
1293
1294 assert changelog_file.read_text() == expected
1295
1296
1297 def test_changelog_template_extras_precedence(
1298 changelog_tpl: Path,
1299 mock_plugin: BaseCommitizen,
1300 pyproject: Path,
1301 changelog_file: Path,
1302 util: UtilFixture,
1303 ):
1304 changelog_tpl.write_text("{{first}} - {{second}} - {{third}}")
1305
1306 mock_plugin.template_extras = dict(
1307 first="from-plugin", second="from-plugin", third="from-plugin"
1308 )
1309
1310 pyproject.write_text(
1311 dedent(
1312 """\
1313 [tool.commitizen]
1314 version = "0.1.0"
1315 [tool.commitizen.extras]
1316 first = "from-config"
1317 second = "from-config"
1318 """
1319 )
1320 )
1321
1322 util.create_file_and_commit("feat: new file")
1323 util.run_cli("changelog", "--extra", "first=from-command")
1324
1325 assert changelog_file.read_text() == "from-command - from-config - from-plugin"
1326
1327
1328 @pytest.mark.usefixtures("tmp_commitizen_project")
1329 @pytest.mark.freeze_time("2021-06-11")
1330 def test_changelog_only_tag_matching_tag_format_included_prefix(
1331 changelog_path: Path,
1332 config_path: Path,
1333 util: UtilFixture,
1334 ):
1335 with config_path.open("a", encoding="utf-8") as f:
1336 f.write('\ntag_format = "custom${version}"\n')
1337 util.create_file_and_commit("feat: new file")
1338 util.create_tag("v0.2.0")
1339 util.create_file_and_commit("feat: another new file")
1340 util.create_tag("0.2.0")
1341 util.create_tag("random0.2.0")
1342 util.run_cli("bump", "--changelog", "--yes")
1343 util.create_file_and_commit("feat: another new file")
1344 util.run_cli("bump", "--changelog", "--yes")
1345 out = changelog_path.read_text()
1346 assert out.startswith("## custom0.3.0 (2021-06-11)")
1347 assert "## v0.2.0 (2021-06-11)" not in out
1348 assert "## 0.2.0 (2021-06-11)" not in out
1349
1350
1351 @pytest.mark.usefixtures("tmp_commitizen_project")
1352 def test_changelog_only_tag_matching_tag_format_included_prefix_sep(
1353 changelog_path: Path,
1354 config_path: Path,
1355 util: UtilFixture,
1356 ):
1357 with config_path.open("a", encoding="utf-8") as f:
1358 f.write('\ntag_format = "custom-${version}"\n')
1359 util.create_file_and_commit("feat: new file")
1360 util.create_tag("v0.2.0")
1361 util.create_file_and_commit("feat: another new file")
1362 util.create_tag("0.2.0")
1363 util.create_tag("random0.2.0")
1364 util.run_cli("bump", "--changelog", "--yes")
1365 util.create_file_and_commit("feat: new version another new file")
1366 util.create_file_and_commit("feat: new version some new file")
1367 util.run_cli("bump", "--changelog")
1368 out = changelog_path.read_text()
1369 assert out.startswith("## custom-0.3.0")
1370 assert "## v0.2.0" not in out
1371 assert "## 0.2.0" not in out
1372
1373
1374 @pytest.mark.usefixtures("tmp_commitizen_project")
1375 @pytest.mark.freeze_time("2021-06-11")
1376 def test_changelog_only_tag_matching_tag_format_included_suffix(
1377 changelog_path: Path,
1378 config_path: Path,
1379 util: UtilFixture,
1380 ):
1381 with config_path.open("a", encoding="utf-8") as f:
1382 f.write('\ntag_format = "${version}custom"\n')
1383 util.create_file_and_commit("feat: new file")
1384 util.create_tag("v0.2.0")
1385 util.create_file_and_commit("feat: another new file")
1386 util.create_tag("0.2.0")
1387 util.create_tag("random0.2.0")
1388 # bump to 0.2.0custom
1389 util.run_cli("bump", "--changelog", "--yes")
1390
1391 util.create_file_and_commit("feat: another new file")
1392 # bump to 0.3.0custom
1393 util.run_cli("bump", "--changelog", "--yes")
1394 out = changelog_path.read_text()
1395 assert out.startswith("## 0.3.0custom (2021-06-11)")
1396 assert "## v0.2.0 (2021-06-11)" not in out
1397 assert "## 0.2.0 (2021-06-11)" not in out
1398
1399
1400 @pytest.mark.usefixtures("tmp_commitizen_project")
1401 @pytest.mark.freeze_time("2021-06-11")
1402 def test_changelog_only_tag_matching_tag_format_included_suffix_sep(
1403 changelog_path: Path,
1404 config_path: Path,
1405 util: UtilFixture,
1406 ):
1407 with config_path.open("a", encoding="utf-8") as f:
1408 f.write('\ntag_format = "${version}-custom"\n')
1409 util.create_file_and_commit("feat: new file")
1410 util.create_tag("v0.2.0")
1411 util.create_file_and_commit("feat: another new file")
1412 util.create_tag("0.2.0")
1413 util.create_tag("random0.2.0")
1414 util.run_cli("bump", "--changelog", "--yes")
1415 util.create_file_and_commit("feat: another new file")
1416 util.run_cli("bump", "--changelog", "--yes")
1417 out = changelog_path.read_text()
1418 assert out.startswith("## 0.3.0-custom (2021-06-11)")
1419 assert "## v0.2.0 (2021-06-11)" not in out
1420 assert "## 0.2.0 (2021-06-11)" not in out
1421
1422
1423 @pytest.mark.usefixtures("tmp_commitizen_project")
1424 def test_changelog_legacy_tags(
1425 changelog_path: Path,
1426 config_path: Path,
1427 util: UtilFixture,
1428 ):
1429 with config_path.open("a", encoding="utf-8") as f:
1430 f.writelines(
1431 [
1432 'tag_format = "v${version}"\n',
1433 "legacy_tag_formats = [\n",
1434 ' "older-${version}",\n',
1435 ' "oldest-${version}",\n',
1436 "]\n",
1437 ]
1438 )
1439 util.create_file_and_commit("feat: new file")
1440 util.create_tag("oldest-0.1.0")
1441 util.create_file_and_commit("feat: new file")
1442 util.create_tag("older-0.2.0")
1443 util.create_file_and_commit("feat: another new file")
1444 util.create_tag("v0.3.0")
1445 util.create_file_and_commit("feat: another new file")
1446 util.create_tag("not-0.3.1")
1447 util.run_cli("bump", "--changelog", "--yes")
1448 out = changelog_path.read_text()
1449 assert "## v0.3.0" in out
1450 assert "## older-0.2.0" in out
1451 assert "## oldest-0.1.0" in out
1452 assert "## v0.3.0" in out
1453 assert "## not-0.3.1" not in out
1454
1455
1456 @pytest.mark.usefixtures("tmp_commitizen_project")
1457 @pytest.mark.freeze_time("2024-11-18")
1458 def test_changelog_incremental_change_tag_format(
1459 changelog_path: Path,
1460 config_path: Path,
1461 file_regression: FileRegressionFixture,
1462 util: UtilFixture,
1463 ):
1464 util.freezer.move_to("2024-11-18")
1465 config = Path(config_path)
1466 base_config = config.read_text()
1467 config.write_text(
1468 "\n".join(
1469 (
1470 base_config,
1471 'tag_format = "older-${version}"',
1472 )
1473 )
1474 )
1475 util.create_file_and_commit("feat: new file")
1476 util.create_tag("older-0.1.0")
1477 util.create_file_and_commit("feat: new file")
1478 util.create_tag("older-0.2.0")
1479 util.run_cli("changelog")
1480
1481 config.write_text(
1482 "\n".join(
1483 (
1484 base_config,
1485 'tag_format = "v${version}"',
1486 'legacy_tag_formats = ["older-${version}"]',
1487 )
1488 )
1489 )
1490 util.create_file_and_commit("feat: another new file")
1491 util.create_tag("v0.3.0")
1492 util.run_cli("changelog", "--incremental")
1493 out = changelog_path.read_text()
1494 assert "## v0.3.0" in out
1495 assert "## older-0.2.0" in out
1496 assert "## older-0.1.0" in out
1497 file_regression.check(out, extension=".md")
1498
1499
1500 @pytest.mark.usefixtures("tmp_commitizen_project")
1501 def test_changelog_ignored_tags(
1502 changelog_path: Path,
1503 config_path: Path,
1504 capsys: pytest.CaptureFixture,
1505 util: UtilFixture,
1506 ):
1507 with config_path.open("a", encoding="utf-8") as f:
1508 f.writelines(
1509 [
1510 'tag_format = "v${version}"\n',
1511 "ignored_tag_formats = [\n",
1512 ' "ignored",\n',
1513 ' "ignore-${version}",\n',
1514 "]\n",
1515 ]
1516 )
1517 util.create_file_and_commit("feat: new file")
1518 util.create_tag("ignore-0.1.0")
1519 util.create_file_and_commit("feat: new file")
1520 util.create_tag("ignored")
1521 util.create_file_and_commit("feat: another new file")
1522 util.create_tag("v0.3.0")
1523 util.create_file_and_commit("feat: another new file")
1524 util.create_tag("not-ignored")
1525 util.run_cli("bump", "--changelog", "--yes")
1526 out = changelog_path.read_text()
1527 assert "## ignore-0.1.0" not in out
1528 assert "## ignored" not in out
1529 assert "## not-ignored" not in out
1530 assert "## v0.3.0" in out
1531
1532 _, err = capsys.readouterr()
1533 assert "Invalid version tag: 'ignore-0.1.0'" not in err
1534 assert "Invalid version tag: 'ignored'" not in err
1535 assert "Invalid version tag: 'not-ignored'" in err
1536 assert "Invalid version tag: 'v0.3.0'" not in err
1537
1538
1539 def test_changelog_template_extra_quotes(
1540 changelog_tpl: Path,
1541 changelog_file: Path,
1542 util: UtilFixture,
1543 ):
1544 changelog_tpl.write_text("{{first}} - {{second}} - {{third}}")
1545 util.create_file_and_commit("feat: new file")
1546 util.run_cli(
1547 "changelog",
1548 "-e",
1549 "first=no-quote",
1550 "-e",
1551 "second='single quotes'",
1552 "-e",
1553 'third="double quotes"',
1554 )
1555
1556 assert changelog_file.read_text() == "no-quote - single quotes - double quotes"
1557
1558
1559 @pytest.mark.parametrize(
1560 ("extra", "expected"),
1561 [
1562 pytest.param("key=value=", "value=", id="2-equals"),
1563 pytest.param("key==value", "=value", id="2-consecutive-equals"),
1564 pytest.param("key==value==", "=value==", id="multiple-equals"),
1565 ],
1566 )
1567 def test_changelog_template_extra_weird_but_valid(
1568 changelog_tpl: Path,
1569 changelog_file: Path,
1570 extra: str,
1571 expected: str,
1572 util: UtilFixture,
1573 ):
1574 changelog_tpl.write_text("{{key}}")
1575 util.create_file_and_commit("feat: new file")
1576 util.run_cli("changelog", "-e", extra)
1577
1578 assert changelog_file.read_text() == expected
1579
1580
1581 @pytest.mark.parametrize("extra", ["no-equal", "", "=no-key"])
1582 def test_changelog_template_extra_bad_format(
1583 changelog_tpl: Path,
1584 extra: str,
1585 util: UtilFixture,
1586 ):
1587 changelog_tpl.write_text("")
1588 util.create_file_and_commit("feat: new file")
1589 with pytest.raises(InvalidCommandArgumentError):
1590 util.run_cli("changelog", "-e", extra)
1591
1592
1593 def test_export_changelog_template_from_default(
1594 any_changelog_format: ChangelogFormat,
1595 util: UtilFixture,
1596 changelog_jinja_file: Path,
1597 repo_root: Path,
1598 ):
1599 src = repo_root / "commitizen" / "templates" / any_changelog_format.template
1600
1601 util.run_cli("changelog", "--export-template", str(changelog_jinja_file))
1602
1603 assert changelog_jinja_file.exists()
1604 assert changelog_jinja_file.read_text() == src.read_text()
1605
1606
1607 def test_export_changelog_template_from_plugin(
1608 changelog_jinja_file: Path,
1609 mock_plugin: BaseCommitizen,
1610 changelog_format: ChangelogFormat,
1611 tmp_path: Path,
1612 util: UtilFixture,
1613 ):
1614 src = tmp_path / changelog_format.template
1615 tpl = "I am a custom template"
1616 src.write_text(tpl)
1617 mock_plugin.template_loader = FileSystemLoader(tmp_path)
1618
1619 util.run_cli("changelog", "--export-template", str(changelog_jinja_file))
1620
1621 assert changelog_jinja_file.exists()
1622 assert changelog_jinja_file.read_text() == tpl
1623
1624
1625 def test_export_changelog_template_fails_when_template_has_no_filename(
1626 mocker: MockFixture,
1627 changelog_jinja_file: Path,
1628 util: UtilFixture,
1629 ):
1630 # Mock a template object with no filename
1631 class FakeTemplate:
1632 filename = None
1633
1634 # Patch get_changelog_template to return a template without a filename
1635 mocker.patch(
1636 "commitizen.changelog.get_changelog_template", return_value=FakeTemplate()
1637 )
1638
1639 with pytest.raises(NotAllowed) as exc_info:
1640 util.run_cli("changelog", "--export-template", str(changelog_jinja_file))
1641
1642 assert not changelog_jinja_file.exists()
1643 assert "Template filename is not set" in str(exc_info.value)
1644
1645
1646 def test_changelog_template_incremental_variable(
1647 tmp_commitizen_project: Path,
1648 any_changelog_format: ChangelogFormat,
1649 util: UtilFixture,
1650 file_regression: FileRegressionFixture,
1651 ):
1652 """
1653 Test that the changelog template is not rendered when the incremental flag is not set.
1654 Reference: https://github.com/commitizen-tools/commitizen/discussions/1620
1655 """
1656 project_root = tmp_commitizen_project
1657 changelog_tpl = project_root / any_changelog_format.template
1658 changelog_tpl.write_text(
1659 dedent("""
1660 {% if not incremental %}
1661 # CHANGELOG
1662 {% endif %}
1663
1664 {% for entry in tree %}
1665
1666 ## {{ entry.version }}{% if entry.date %} ({{ entry.date }}){% endif %}
1667
1668 {% for change_key, changes in entry.changes.items() %}
1669
1670 {% if change_key %}
1671 ### {{ change_key }}
1672 {% endif %}
1673
1674 {% for change in changes %}
1675 {% if change.scope %}
1676 - **{{ change.scope }}**: {{ change.message }}
1677 {% elif change.message %}
1678 - {{ change.message }}
1679 {% endif %}
1680 {% endfor %}
1681 {% endfor %}
1682 {% endfor %}
1683 """)
1684 )
1685 target = "CHANGELOG.md"
1686
1687 util.create_file_and_commit("feat(foo): new file")
1688 util.run_cli("changelog", "--file-name", target)
1689 out = Path(target).read_text(encoding="utf-8")
1690 file_regression.check(out, extension=".md")
1691
1692 util.create_file_and_commit("refactor(bar): another new file")
1693 util.run_cli("changelog", "--file-name", target, "--incremental")
1694 out = Path(target).read_text(encoding="utf-8")
1695 file_regression.check(out, extension=".incremental.md")