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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
|
from __future__ import annotations
import os
from datetime import datetime, timezone
from typing import TYPE_CHECKING
import pytest
from freezegun import freeze_time
from pytest_lazy_fixtures.lazy_fixture import lf as lazy_fixture
from semantic_release.changelog.context import ChangelogMode
from semantic_release.cli.config import ChangelogOutputFormat
from tests.const import MAIN_PROG_NAME, VERSION_SUBCMD
from tests.fixtures.example_project import (
default_md_changelog_insertion_flag,
default_rst_changelog_insertion_flag,
example_changelog_md,
example_changelog_rst,
)
from tests.fixtures.repos import (
repo_w_git_flow_w_alpha_prereleases_n_conventional_commits,
repo_w_git_flow_w_alpha_prereleases_n_emoji_commits,
repo_w_git_flow_w_alpha_prereleases_n_scipy_commits,
repo_w_git_flow_w_rc_n_alpha_prereleases_n_conventional_commits,
repo_w_git_flow_w_rc_n_alpha_prereleases_n_conventional_commits_using_tag_format,
repo_w_git_flow_w_rc_n_alpha_prereleases_n_emoji_commits,
repo_w_git_flow_w_rc_n_alpha_prereleases_n_scipy_commits,
repo_w_github_flow_w_default_release_channel_conventional_commits,
repo_w_github_flow_w_default_release_channel_emoji_commits,
repo_w_github_flow_w_default_release_channel_scipy_commits,
repo_w_github_flow_w_feature_release_channel_conventional_commits,
repo_w_github_flow_w_feature_release_channel_emoji_commits,
repo_w_github_flow_w_feature_release_channel_scipy_commits,
repo_w_no_tags_conventional_commits,
repo_w_no_tags_conventional_commits_unmasked_initial_release,
repo_w_no_tags_emoji_commits,
repo_w_no_tags_scipy_commits,
repo_w_trunk_only_conventional_commits,
repo_w_trunk_only_emoji_commits,
repo_w_trunk_only_n_prereleases_conventional_commits,
repo_w_trunk_only_n_prereleases_emoji_commits,
repo_w_trunk_only_n_prereleases_scipy_commits,
repo_w_trunk_only_scipy_commits,
)
from tests.util import assert_successful_exit_code
if TYPE_CHECKING:
from pathlib import Path
from tests.conftest import FormatDateStrFn, GetStableDateNowFn, RunCliFn
from tests.fixtures.example_project import UpdatePyprojectTomlFn
from tests.fixtures.git_repo import (
BuiltRepoResult,
CommitConvention,
GetCommitsFromRepoBuildDefFn,
GetVersionsFromRepoBuildDefFn,
)
@pytest.mark.parametrize(
"changelog_file, insertion_flag",
[
(
# ChangelogOutputFormat.MARKDOWN
lazy_fixture(example_changelog_md.__name__),
lazy_fixture(default_md_changelog_insertion_flag.__name__),
),
(
# ChangelogOutputFormat.RESTRUCTURED_TEXT
lazy_fixture(example_changelog_rst.__name__),
lazy_fixture(default_rst_changelog_insertion_flag.__name__),
),
],
)
@pytest.mark.parametrize(
"repo_result, cache_key, tag_format",
[
(
lazy_fixture(repo_w_trunk_only_conventional_commits.__name__),
f"psr/repos/{repo_w_trunk_only_conventional_commits.__name__}",
"v{version}",
),
*[
pytest.param(
lazy_fixture(repo_fixture),
f"psr/repos/{repo_fixture}",
"v{version}" if tag_format is None else tag_format,
marks=pytest.mark.comprehensive,
)
for repo_fixture, tag_format in [
# Must have a previous release/tag
*[
(
repo_fixture_name,
None,
)
for repo_fixture_name in [
# repo_with_single_branch_conventional_commits.__name__, # default
repo_w_trunk_only_emoji_commits.__name__,
repo_w_trunk_only_scipy_commits.__name__,
]
],
*[
(
repo_fixture_name,
None,
)
for repo_fixture_name in [
repo_w_trunk_only_n_prereleases_conventional_commits.__name__,
repo_w_trunk_only_n_prereleases_emoji_commits.__name__,
repo_w_trunk_only_n_prereleases_scipy_commits.__name__,
]
],
*[
(
repo_fixture_name,
None,
)
for repo_fixture_name in [
repo_w_github_flow_w_default_release_channel_conventional_commits.__name__,
repo_w_github_flow_w_default_release_channel_emoji_commits.__name__,
repo_w_github_flow_w_default_release_channel_scipy_commits.__name__,
]
],
*[
(
repo_fixture_name,
None,
)
for repo_fixture_name in [
repo_w_github_flow_w_feature_release_channel_conventional_commits.__name__,
repo_w_github_flow_w_feature_release_channel_emoji_commits.__name__,
repo_w_github_flow_w_feature_release_channel_scipy_commits.__name__,
]
],
*[
(
repo_fixture_name,
None,
)
for repo_fixture_name in [
repo_w_git_flow_w_alpha_prereleases_n_conventional_commits.__name__,
repo_w_git_flow_w_alpha_prereleases_n_emoji_commits.__name__,
repo_w_git_flow_w_alpha_prereleases_n_scipy_commits.__name__,
]
],
*[
(
repo_fixture_name,
None,
)
for repo_fixture_name in [
repo_w_git_flow_w_rc_n_alpha_prereleases_n_conventional_commits.__name__,
repo_w_git_flow_w_rc_n_alpha_prereleases_n_emoji_commits.__name__,
repo_w_git_flow_w_rc_n_alpha_prereleases_n_scipy_commits.__name__,
]
],
*[
(
repo_fixture_name,
"submod-v{version}",
)
for repo_fixture_name in [
repo_w_git_flow_w_rc_n_alpha_prereleases_n_conventional_commits_using_tag_format.__name__,
]
],
]
],
],
)
def test_version_updates_changelog_w_new_version(
repo_result: BuiltRepoResult,
get_versions_from_repo_build_def: GetVersionsFromRepoBuildDefFn,
tag_format: str,
update_pyproject_toml: UpdatePyprojectTomlFn,
run_cli: RunCliFn,
changelog_file: Path,
insertion_flag: str,
cache: pytest.Cache,
cache_key: str,
stable_now_date: GetStableDateNowFn,
):
"""
Given a previously released custom modified changelog file,
When the version command is run with changelog.mode set to "update",
Then the version is created and the changelog file is updated with new release info
while maintaining the previously customized content
"""
repo = repo_result["repo"]
latest_tag = tag_format.format(
version=get_versions_from_repo_build_def(repo_result["definition"])[-1]
)
if not (repo_build_data := cache.get(cache_key, None)):
pytest.fail("Repo build date not found in cache")
repo_build_datetime = datetime.strptime(repo_build_data["build_date"], "%Y-%m-%d")
now_datetime = stable_now_date().replace(
year=repo_build_datetime.year,
month=repo_build_datetime.month,
day=repo_build_datetime.day,
)
# Custom text to maintain (must be different from the default)
custom_text = "---{ls}{ls}Custom footer text{ls}".format(ls=os.linesep)
# Capture expected changelog content
with changelog_file.open(newline=os.linesep) as rfd:
initial_changelog_parts = rfd.read().split(insertion_flag)
expected_changelog_content = str.join(
insertion_flag,
[
initial_changelog_parts[0],
str.join(
os.linesep,
[
initial_changelog_parts[1],
"",
custom_text,
],
),
],
)
# Reverse last release
repo.git.tag("-d", latest_tag)
repo.git.reset("--hard", "HEAD~1")
# Set the project configurations
update_pyproject_toml(
"tool.semantic_release.changelog.mode", ChangelogMode.UPDATE.value
)
update_pyproject_toml(
"tool.semantic_release.changelog.default_templates.changelog_file",
str(changelog_file.name),
)
# Modify the current changelog with our custom text at bottom
# Universal newlines is ok here since we are writing it back out
# and not working with the os-specific insertion flag
changelog_file.write_text(
str.join(
"\n",
[
changelog_file.read_text(),
"",
custom_text,
],
)
)
with freeze_time(now_datetime.astimezone(timezone.utc)):
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--no-push", "--changelog"]
result = run_cli(cli_cmd[1:])
# Capture the new changelog content (os aware because of expected content)
with changelog_file.open(newline=os.linesep) as rfd:
actual_content = rfd.read()
# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert expected_changelog_content == actual_content
@pytest.mark.parametrize(
"changelog_format, changelog_file, insertion_flag",
[
(
ChangelogOutputFormat.MARKDOWN,
lazy_fixture(example_changelog_md.__name__),
lazy_fixture(default_md_changelog_insertion_flag.__name__),
),
(
ChangelogOutputFormat.RESTRUCTURED_TEXT,
lazy_fixture(example_changelog_rst.__name__),
lazy_fixture(default_rst_changelog_insertion_flag.__name__),
),
],
)
@pytest.mark.parametrize(
"repo_result, cache_key",
[
(
lazy_fixture(repo_w_no_tags_conventional_commits.__name__),
f"psr/repos/{repo_w_no_tags_conventional_commits.__name__}",
),
*[
pytest.param(
lazy_fixture(repo_fixture),
f"psr/repos/{repo_fixture}",
marks=pytest.mark.comprehensive,
)
for repo_fixture in [
# Must not have a single release/tag
# repo_with_no_tags_conventional_commits.__name__, # default
repo_w_no_tags_emoji_commits.__name__,
repo_w_no_tags_scipy_commits.__name__,
]
],
],
)
def test_version_updates_changelog_wo_prev_releases(
repo_result: BuiltRepoResult,
cache_key: str,
cache: pytest.Cache,
run_cli: RunCliFn,
update_pyproject_toml: UpdatePyprojectTomlFn,
changelog_format: ChangelogOutputFormat,
changelog_file: Path,
insertion_flag: str,
stable_now_date: GetStableDateNowFn,
format_date_str: FormatDateStrFn,
):
"""
Given the repository has no releases and the user has provided a initialized changelog,
When the version command is run with changelog.mode set to "update",
Then the version is created and the changelog file is updated with only an initial release statement
"""
if not (repo_build_data := cache.get(cache_key, None)):
pytest.fail("Repo build date not found in cache")
repo_build_datetime = datetime.strptime(repo_build_data["build_date"], "%Y-%m-%d")
now_datetime = stable_now_date().replace(
year=repo_build_datetime.year,
month=repo_build_datetime.month,
day=repo_build_datetime.day,
)
repo_build_date_str = format_date_str(now_datetime)
# Custom text to maintain (must be different from the default)
custom_text = "---{ls}{ls}Custom footer text{ls}".format(ls=os.linesep)
# Set the project configurations
update_pyproject_toml(
"tool.semantic_release.changelog.mode", ChangelogMode.UPDATE.value
)
update_pyproject_toml(
"tool.semantic_release.changelog.default_templates.changelog_file",
str(changelog_file.name),
)
version = "v1.0.0"
rst_version_header = f"{version} ({repo_build_date_str})"
txt_after_insertion_flag = {
ChangelogOutputFormat.MARKDOWN: str.join(
os.linesep,
[
f"## {version} ({repo_build_date_str})",
"",
"- Initial Release",
],
),
ChangelogOutputFormat.RESTRUCTURED_TEXT: str.join(
os.linesep,
[
f".. _changelog-{version}:",
"",
rst_version_header,
f"{'=' * len(rst_version_header)}",
"",
"* Initial Release",
],
),
}
# Capture and modify the current changelog content to become the expected output
# We much use os.linesep here since the insertion flag is os-specific
with changelog_file.open(newline=os.linesep) as rfd:
initial_changelog_parts = rfd.read().split(insertion_flag)
# content is os-specific because of the insertion flag & how we read the original file
expected_changelog_content = str.join(
insertion_flag,
[
initial_changelog_parts[0],
str.join(
os.linesep,
[
os.linesep,
txt_after_insertion_flag[changelog_format],
"",
custom_text,
],
),
],
)
# Grab the Unreleased changelog & create the initialized user changelog
# force output to not perform any newline translations
with changelog_file.open(mode="w", newline="") as wfd:
wfd.write(
str.join(
insertion_flag,
[initial_changelog_parts[0], f"{os.linesep * 2}{custom_text}"],
)
)
wfd.flush()
# Act
with freeze_time(now_datetime.astimezone(timezone.utc)):
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--no-push", "--changelog"]
result = run_cli(cli_cmd[1:])
# Evaluate
assert_successful_exit_code(result, cli_cmd)
# Ensure changelog exists
assert changelog_file.exists()
# Capture the new changelog content (os aware because of expected content)
with changelog_file.open(newline=os.linesep) as rfd:
actual_content = rfd.read()
# Check that the changelog footer is maintained and updated with Unreleased info
assert expected_changelog_content == actual_content
@pytest.mark.parametrize(
"changelog_format, changelog_file, insertion_flag",
[
(
ChangelogOutputFormat.MARKDOWN,
lazy_fixture(example_changelog_md.__name__),
lazy_fixture(default_md_changelog_insertion_flag.__name__),
),
(
ChangelogOutputFormat.RESTRUCTURED_TEXT,
lazy_fixture(example_changelog_rst.__name__),
lazy_fixture(default_rst_changelog_insertion_flag.__name__),
),
],
)
@pytest.mark.parametrize(
"repo_result, cache_key",
[
pytest.param(
lazy_fixture(repo_fixture),
f"psr/repos/{repo_fixture}",
marks=pytest.mark.comprehensive,
)
for repo_fixture in [
# Must not have a single release/tag
repo_w_no_tags_conventional_commits_unmasked_initial_release.__name__,
]
],
)
def test_version_updates_changelog_wo_prev_releases_n_unmasked_initial_release(
repo_result: BuiltRepoResult,
cache_key: str,
cache: pytest.Cache,
run_cli: RunCliFn,
update_pyproject_toml: UpdatePyprojectTomlFn,
changelog_format: ChangelogOutputFormat,
changelog_file: Path,
insertion_flag: str,
stable_now_date: GetStableDateNowFn,
format_date_str: FormatDateStrFn,
):
"""
Given the repository has no releases and the user has provided a initialized changelog,
When the version command is run with changelog.mode set to "update",
Then the version is created and the changelog file is updated with new release info
"""
if not (repo_build_data := cache.get(cache_key, None)):
pytest.fail("Repo build date not found in cache")
repo_build_datetime = datetime.strptime(repo_build_data["build_date"], "%Y-%m-%d")
now_datetime = stable_now_date().replace(
year=repo_build_datetime.year,
month=repo_build_datetime.month,
day=repo_build_datetime.day,
)
repo_build_date_str = format_date_str(now_datetime)
# Custom text to maintain (must be different from the default)
custom_text = "---{ls}{ls}Custom footer text{ls}".format(ls=os.linesep)
# Set the project configurations
update_pyproject_toml(
"tool.semantic_release.changelog.mode", ChangelogMode.UPDATE.value
)
update_pyproject_toml(
"tool.semantic_release.changelog.default_templates.changelog_file",
str(changelog_file.name),
)
version = "v1.0.0"
rst_version_header = f"{version} ({repo_build_date_str})"
search_n_replacements = {
ChangelogOutputFormat.MARKDOWN: (
"## Unreleased",
f"## {version} ({repo_build_date_str})",
),
ChangelogOutputFormat.RESTRUCTURED_TEXT: (
".. _changelog-unreleased:{ls}{ls}Unreleased{ls}{underline}".format(
ls=os.linesep,
underline="=" * len("Unreleased"),
),
str.join(
os.linesep,
[
f".. _changelog-{version}:",
"",
rst_version_header,
f"{'=' * len(rst_version_header)}",
],
),
),
}
search_text = search_n_replacements[changelog_format][0]
replacement_text = search_n_replacements[changelog_format][1]
# Capture and modify the current changelog content to become the expected output
# We much use os.linesep here since the insertion flag is os-specific
with changelog_file.open(newline=os.linesep) as rfd:
initial_changelog_parts = rfd.read().split(insertion_flag)
# content is os-specific because of the insertion flag & how we read the original file
expected_changelog_content = str.join(
insertion_flag,
[
initial_changelog_parts[0],
str.join(
os.linesep,
[
initial_changelog_parts[1].replace(
search_text,
replacement_text,
),
"",
custom_text,
],
),
],
)
# Grab the Unreleased changelog & create the initalized user changelog
# force output to not perform any newline translations
with changelog_file.open(mode="w", newline="") as wfd:
wfd.write(
str.join(
insertion_flag,
[initial_changelog_parts[0], f"{os.linesep * 2}{custom_text}"],
)
)
wfd.flush()
# Act
with freeze_time(now_datetime.astimezone(timezone.utc)):
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--no-push", "--changelog"]
result = run_cli(cli_cmd[1:])
# Evaluate
assert_successful_exit_code(result, cli_cmd)
# Ensure changelog exists
assert changelog_file.exists()
# Capture the new changelog content (os aware because of expected content)
with changelog_file.open(newline=os.linesep) as rfd:
actual_content = rfd.read()
# Check that the changelog footer is maintained and updated with Unreleased info
assert expected_changelog_content == actual_content
@pytest.mark.parametrize(
"changelog_file",
[
lazy_fixture(example_changelog_md.__name__),
lazy_fixture(example_changelog_rst.__name__),
],
)
@pytest.mark.parametrize(
"repo_result, cache_key, tag_format",
[
(
lazy_fixture(repo_w_trunk_only_conventional_commits.__name__),
f"psr/repos/{repo_w_trunk_only_conventional_commits.__name__}",
"v{version}",
),
*[
pytest.param(
lazy_fixture(repo_fixture),
f"psr/repos/{repo_fixture}",
"v{version}" if tag_format is None else tag_format,
marks=pytest.mark.comprehensive,
)
for repo_fixture, tag_format in [
# Must have a previous release/tag
*[
(
repo_fixture_name,
None,
)
for repo_fixture_name in [
# repo_with_single_branch_conventional_commits.__name__, # default
repo_w_trunk_only_emoji_commits.__name__,
repo_w_trunk_only_scipy_commits.__name__,
]
],
*[
(
repo_fixture_name,
None,
)
for repo_fixture_name in [
repo_w_trunk_only_n_prereleases_conventional_commits.__name__,
repo_w_trunk_only_n_prereleases_emoji_commits.__name__,
repo_w_trunk_only_n_prereleases_scipy_commits.__name__,
]
],
*[
(
repo_fixture_name,
None,
)
for repo_fixture_name in [
repo_w_github_flow_w_default_release_channel_conventional_commits.__name__,
repo_w_github_flow_w_default_release_channel_emoji_commits.__name__,
repo_w_github_flow_w_default_release_channel_scipy_commits.__name__,
]
],
*[
(
repo_fixture_name,
None,
)
for repo_fixture_name in [
repo_w_github_flow_w_feature_release_channel_conventional_commits.__name__,
repo_w_github_flow_w_feature_release_channel_emoji_commits.__name__,
repo_w_github_flow_w_feature_release_channel_scipy_commits.__name__,
]
],
*[
(
repo_fixture_name,
None,
)
for repo_fixture_name in [
repo_w_git_flow_w_alpha_prereleases_n_conventional_commits.__name__,
repo_w_git_flow_w_alpha_prereleases_n_emoji_commits.__name__,
repo_w_git_flow_w_alpha_prereleases_n_scipy_commits.__name__,
]
],
*[
(
repo_fixture_name,
None,
)
for repo_fixture_name in [
repo_w_git_flow_w_rc_n_alpha_prereleases_n_conventional_commits.__name__,
repo_w_git_flow_w_rc_n_alpha_prereleases_n_emoji_commits.__name__,
repo_w_git_flow_w_rc_n_alpha_prereleases_n_scipy_commits.__name__,
]
],
*[
(
repo_fixture_name,
"submod-v{version}",
)
for repo_fixture_name in [
repo_w_git_flow_w_rc_n_alpha_prereleases_n_conventional_commits_using_tag_format.__name__,
]
],
]
],
],
)
def test_version_initializes_changelog_in_update_mode_w_no_prev_changelog(
repo_result: BuiltRepoResult,
cache_key: str,
get_versions_from_repo_build_def: GetVersionsFromRepoBuildDefFn,
tag_format: str,
run_cli: RunCliFn,
update_pyproject_toml: UpdatePyprojectTomlFn,
changelog_file: Path,
cache: pytest.Cache,
stable_now_date: GetStableDateNowFn,
):
"""
Given that the changelog file does not exist,
When the version command is run with changelog.mode set to "update",
Then the version is created and the changelog file is initialized
with the default content.
"""
repo = repo_result["repo"]
latest_tag = tag_format.format(
version=get_versions_from_repo_build_def(repo_result["definition"])[-1]
)
if not (repo_build_data := cache.get(cache_key, None)):
pytest.fail("Repo build date not found in cache")
repo_build_datetime = datetime.strptime(repo_build_data["build_date"], "%Y-%m-%d")
now_datetime = stable_now_date().replace(
year=repo_build_datetime.year,
month=repo_build_datetime.month,
day=repo_build_datetime.day,
)
# Capture the expected changelog content
expected_changelog_content = changelog_file.read_text()
# Reverse last release
repo.git.tag("-d", latest_tag)
repo.git.reset("--hard", "HEAD~1")
# Set the project configurations
update_pyproject_toml(
"tool.semantic_release.changelog.mode", ChangelogMode.UPDATE.value
)
update_pyproject_toml(
"tool.semantic_release.changelog.default_templates.changelog_file",
str(changelog_file.name),
)
# Remove any previous changelog to update
os.remove(str(changelog_file.resolve()))
# Act
with freeze_time(now_datetime.astimezone(timezone.utc)):
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--no-push", "--changelog"]
result = run_cli(cli_cmd[1:])
# Evaluate
assert_successful_exit_code(result, cli_cmd)
# Check that the changelog file was re-created
assert changelog_file.exists()
actual_content = changelog_file.read_text()
# Check that the changelog content is the same as before
assert expected_changelog_content == actual_content
@pytest.mark.parametrize(
"changelog_file, insertion_flag",
[
(
lazy_fixture(example_changelog_md.__name__),
lazy_fixture(default_md_changelog_insertion_flag.__name__),
),
(
lazy_fixture(example_changelog_rst.__name__),
lazy_fixture(default_rst_changelog_insertion_flag.__name__),
),
],
)
@pytest.mark.usefixtures(repo_w_trunk_only_conventional_commits.__name__)
def test_version_maintains_changelog_in_update_mode_w_no_flag(
changelog_file: Path,
run_cli: RunCliFn,
update_pyproject_toml: UpdatePyprojectTomlFn,
insertion_flag: str,
):
"""
Given that the changelog file exists but does not contain the insertion flag,
When the version command is run with changelog.mode set to "update",
Then the version is created but the changelog file is not updated.
"""
update_pyproject_toml(
"tool.semantic_release.changelog.mode", ChangelogMode.UPDATE.value
)
update_pyproject_toml(
"tool.semantic_release.changelog.default_templates.changelog_file",
str(changelog_file.name),
)
# Remove the insertion flag from the existing changelog
with changelog_file.open(newline=os.linesep) as rfd:
expected_changelog_content = rfd.read().replace(
f"{insertion_flag}{os.linesep}",
"",
1,
)
# no newline translations
with changelog_file.open("w", newline="") as wfd:
wfd.write(expected_changelog_content)
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--no-push", "--changelog"]
result = run_cli(cli_cmd[1:])
# Evaluate
assert_successful_exit_code(result, cli_cmd)
# Ensure changelog exists
assert changelog_file.exists()
# Capture the new changelog content (os aware because of expected content)
with changelog_file.open(newline=os.linesep) as rfd:
actual_content = rfd.read()
# Check that the changelog content is the same as before
assert expected_changelog_content == actual_content
@pytest.mark.parametrize(
"changelog_file",
[
lazy_fixture(example_changelog_md.__name__),
lazy_fixture(example_changelog_rst.__name__),
],
)
@pytest.mark.parametrize(
"repo_result, cache_key, commit_type, tag_format",
[
(
lazy_fixture(repo_fixture),
f"psr/repos/{repo_fixture}",
repo_fixture.split("_")[-2],
"v{version}",
)
for repo_fixture in [
# Must have a previous release/tag
repo_w_trunk_only_conventional_commits.__name__,
]
],
)
def test_version_updates_changelog_w_new_version_n_filtered_commit(
repo_result: BuiltRepoResult,
cache: pytest.Cache,
cache_key: str,
get_versions_from_repo_build_def: GetVersionsFromRepoBuildDefFn,
commit_type: CommitConvention,
tag_format: str,
update_pyproject_toml: UpdatePyprojectTomlFn,
run_cli: RunCliFn,
changelog_file: Path,
stable_now_date: GetStableDateNowFn,
get_commits_from_repo_build_def: GetCommitsFromRepoBuildDefFn,
):
"""
Given a project that has a version bumping change but also an exclusion pattern for the same change type,
When the version command is run,
Then the version is created and the changelog file is updated with the excluded commit
info anyway.
"""
repo = repo_result["repo"]
latest_version = get_versions_from_repo_build_def(repo_result["definition"])[-1]
latest_tag = tag_format.format(version=latest_version)
repo_definition = get_commits_from_repo_build_def(repo_result["definition"])
if not (repo_build_data := cache.get(cache_key, None)):
pytest.fail("Repo build date not found in cache")
repo_build_datetime = datetime.strptime(repo_build_data["build_date"], "%Y-%m-%d")
now_datetime = stable_now_date().replace(
year=repo_build_datetime.year,
month=repo_build_datetime.month,
day=repo_build_datetime.day,
)
# expected version bump commit (that should be in changelog)
bumping_commit = repo_definition[latest_version]["commits"][-1]
expected_bump_message = bumping_commit["desc"].capitalize()
# Capture the expected changelog content
expected_changelog_content = changelog_file.read_text()
# Reverse last release
repo.git.tag("-d", latest_tag)
repo.git.reset("--hard", "HEAD~1")
# Set the project configurations
update_pyproject_toml(
"tool.semantic_release.changelog.mode", ChangelogMode.UPDATE.value
)
update_pyproject_toml(
"tool.semantic_release.changelog.default_templates.changelog_file",
str(changelog_file.name),
)
update_pyproject_toml(
"tool.semantic_release.changelog.exclude_commit_patterns",
[f"{bumping_commit['msg'].split(':', maxsplit=1)[0]}: .*"],
)
# Act
with freeze_time(now_datetime.astimezone(timezone.utc)):
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--no-push", "--changelog"]
result = run_cli(cli_cmd[1:])
# Capture the new changelog content (os aware because of expected content)
actual_content = changelog_file.read_text()
# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert expected_changelog_content == actual_content
for msg_part in expected_bump_message.split("\n\n"):
assert msg_part.capitalize() in actual_content
|