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 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
|
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from pytest_lazy_fixtures.lazy_fixture import lf as lazy_fixture
from semantic_release.hvcs.github import Github
from tests.const import (
MAIN_PROG_NAME,
VERSION_SUBCMD,
)
from tests.fixtures.commit_parsers import conventional_minor_commits
from tests.fixtures.git_repo import get_commit_def_of_conventional_commit
from tests.fixtures.repos import (
repo_w_git_flow_w_rc_n_alpha_prereleases_n_conventional_commits_using_tag_format,
repo_w_no_tags_conventional_commits,
repo_w_trunk_only_conventional_commits,
repo_w_trunk_only_conventional_commits_using_tag_format,
)
from tests.fixtures.repos.trunk_based_dev.repo_w_no_tags import (
repo_w_no_tags_conventional_commits_using_tag_format,
repo_w_no_tags_conventional_commits_w_zero_version,
)
from tests.util import (
add_text_to_file,
assert_exit_code,
assert_successful_exit_code,
)
if TYPE_CHECKING:
from unittest.mock import MagicMock
from requests_mock import Mocker
from tests.conftest import RunCliFn
from tests.e2e.conftest import StripLoggingMessagesFn
from tests.fixtures.git_repo import (
BuiltRepoResult,
GetCfgValueFromDefFn,
GetCommitDefFn,
GetVersionsFromRepoBuildDefFn,
SimulateChangeCommitsNReturnChangelogEntryFn,
)
@pytest.mark.parametrize(
"repo_result, commits, force_args, next_release_version",
[
(
lazy_fixture(repo_w_trunk_only_conventional_commits.__name__),
lazy_fixture(conventional_minor_commits.__name__),
cli_args,
next_release_version,
)
for cli_args, next_release_version in (
# Dynamic version bump determination (based on commits)
([], "0.2.0"),
# Dynamic version bump determination (based on commits) with build metadata
(["--build-metadata", "build.12345"], "0.2.0+build.12345"),
# Forced version bump
(["--prerelease"], "0.1.1-rc.1"),
(["--patch"], "0.1.2"),
(["--minor"], "0.2.0"),
(["--major"], "1.0.0"),
# Forced version bump with --build-metadata
(["--patch", "--build-metadata", "build.12345"], "0.1.2+build.12345"),
# Forced version bump with --as-prerelease
(["--prerelease", "--as-prerelease"], "0.1.1-rc.1"),
(["--patch", "--as-prerelease"], "0.1.2-rc.1"),
(["--minor", "--as-prerelease"], "0.2.0-rc.1"),
(["--major", "--as-prerelease"], "1.0.0-rc.1"),
# Forced version bump with --as-prerelease and modified --prerelease-token
(
["--patch", "--as-prerelease", "--prerelease-token", "beta"],
"0.1.2-beta.1",
),
# Forced version bump with --as-prerelease and modified --prerelease-token
# and --build-metadata
(
[
"--patch",
"--as-prerelease",
"--prerelease-token",
"beta",
"--build-metadata",
"build.12345",
],
"0.1.2-beta.1+build.12345",
),
)
],
)
def test_version_print_next_version(
repo_result: BuiltRepoResult,
commits: list[str],
force_args: list[str],
next_release_version: str,
file_in_repo: str,
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
):
"""
Given a generic repository at the latest release version and a subsequent commit,
When running the version command with the --print flag,
Then the expected next version should be printed and exit without
making any changes to the repository.
Note: The point of this test is to only verify that the `--print` flag does not
make any changes to the repository--not to validate if the next version is calculated
correctly per the repository structure (see test_version_release &
test_version_force_level for correctness).
However, we do validate that --print & a force option and/or --as-prerelease options
work together to print the next version correctly but not make a change to the repo.
"""
repo = repo_result["repo"]
# Make a commit to ensure we have something to release
# otherwise the "no release will be made" logic will kick in first
add_text_to_file(repo, file_in_repo)
repo.git.commit(m=commits[-1], a=True)
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print", *force_args]
result = run_cli(cli_cmd[1:], env={Github.DEFAULT_ENV_TOKEN_NAME: "1234"})
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit.hexsha
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert f"{next_release_version}\n" == result.stdout
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_before == head_after
assert not tags_set_difference
assert mocked_git_push.call_count == 0
assert post_mocker.call_count == 0
@pytest.mark.parametrize(
"repo_result, commits, force_args, next_release_version",
[
*[
pytest.param(
lazy_fixture(repo_fixture_name),
lazy_fixture(conventional_minor_commits.__name__),
cli_args,
next_release_version,
marks=marks if marks else [],
)
for repo_fixture_name, marks in (
(repo_w_trunk_only_conventional_commits.__name__, None),
(
repo_w_trunk_only_conventional_commits_using_tag_format.__name__,
pytest.mark.comprehensive,
),
)
for cli_args, next_release_version in (
# Dynamic version bump determination (based on commits)
([], "0.2.0"),
# Dynamic version bump determination (based on commits) with build metadata
(["--build-metadata", "build.12345"], "0.2.0+build.12345"),
# Forced version bump
(["--prerelease"], "0.1.1-rc.1"),
(["--patch"], "0.1.2"),
(["--minor"], "0.2.0"),
(["--major"], "1.0.0"),
# Forced version bump with --build-metadata
(["--patch", "--build-metadata", "build.12345"], "0.1.2+build.12345"),
# Forced version bump with --as-prerelease
(["--prerelease", "--as-prerelease"], "0.1.1-rc.1"),
(["--patch", "--as-prerelease"], "0.1.2-rc.1"),
(["--minor", "--as-prerelease"], "0.2.0-rc.1"),
(["--major", "--as-prerelease"], "1.0.0-rc.1"),
# Forced version bump with --as-prerelease and modified --prerelease-token
(
["--patch", "--as-prerelease", "--prerelease-token", "beta"],
"0.1.2-beta.1",
),
# Forced version bump with --as-prerelease and modified --prerelease-token
# and --build-metadata
(
[
"--patch",
"--as-prerelease",
"--prerelease-token",
"beta",
"--build-metadata",
"build.12345",
],
"0.1.2-beta.1+build.12345",
),
)
],
*[
pytest.param(
lazy_fixture(repo_fixture_name),
[],
cli_args,
next_release_version,
marks=pytest.mark.comprehensive,
)
for repo_fixture_name in (
repo_w_no_tags_conventional_commits_w_zero_version.__name__,
)
for cli_args, next_release_version in (
# Dynamic version bump determination (based on commits)
([], "0.1.0"),
# Dynamic version bump determination (based on commits) with build metadata
(["--build-metadata", "build.12345"], "0.1.0+build.12345"),
# Forced version bump
(["--prerelease"], "0.0.0-rc.1"),
(["--patch"], "0.0.1"),
(["--minor"], "0.1.0"),
(["--major"], "1.0.0"),
# Forced version bump with --build-metadata
(["--patch", "--build-metadata", "build.12345"], "0.0.1+build.12345"),
# Forced version bump with --as-prerelease
(["--prerelease", "--as-prerelease"], "0.0.0-rc.1"),
(["--patch", "--as-prerelease"], "0.0.1-rc.1"),
(["--minor", "--as-prerelease"], "0.1.0-rc.1"),
(["--major", "--as-prerelease"], "1.0.0-rc.1"),
# Forced version bump with --as-prerelease and modified --prerelease-token
(
["--patch", "--as-prerelease", "--prerelease-token", "beta"],
"0.0.1-beta.1",
),
# Forced version bump with --as-prerelease and modified --prerelease-token
# and --build-metadata
(
[
"--patch",
"--as-prerelease",
"--prerelease-token",
"beta",
"--build-metadata",
"build.12345",
],
"0.0.1-beta.1+build.12345",
),
)
],
],
)
def test_version_print_tag_prints_next_tag(
repo_result: BuiltRepoResult,
commits: list[str],
force_args: list[str],
next_release_version: str,
get_cfg_value_from_def: GetCfgValueFromDefFn,
file_in_repo: str,
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
):
"""
Given a generic repository at the latest release version and a subsequent commit,
When running the version command with the --print-tag flag,
Then the expected next release tag should be printed and exit without
making any changes to the repository.
Note: The point of this test is to only verify that the `--print-tag` flag does not
make any changes to the repository--not to validate if the next version is calculated
correctly per the repository structure (see test_version_release &
test_version_force_level for correctness).
However, we do validate that --print-tag & a force option and/or --as-prerelease options
work together to print the next release tag correctly but not make a change to the repo.
"""
repo = repo_result["repo"]
repo_def = repo_result["definition"]
tag_format_str: str = get_cfg_value_from_def(repo_def, "tag_format_str") # type: ignore[assignment]
next_release_tag = tag_format_str.format(version=next_release_version)
if len(commits) > 1:
# Make a commit to ensure we have something to release
# otherwise the "no release will be made" logic will kick in first
add_text_to_file(repo, file_in_repo)
repo.git.commit(m=commits[-1], a=True)
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-tag", *force_args]
result = run_cli(cli_cmd[1:], env={Github.DEFAULT_ENV_TOKEN_NAME: "1234"})
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit.hexsha
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert f"{next_release_tag}\n" == result.stdout
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_before == head_after
assert not tags_set_difference
assert mocked_git_push.call_count == 0
assert post_mocker.call_count == 0
@pytest.mark.parametrize(
"repo_result, commits, force_args, next_release_version",
[
pytest.param(
lazy_fixture(repo_fixture_name),
[],
cli_args,
next_release_version,
marks=pytest.mark.comprehensive,
)
for repo_fixture_name in (
repo_w_no_tags_conventional_commits.__name__,
repo_w_no_tags_conventional_commits_using_tag_format.__name__,
)
for cli_args, next_release_version in (
# Dynamic version bump determination (based on commits)
([], "1.0.0"),
# Dynamic version bump determination (based on commits) with build metadata
(["--build-metadata", "build.12345"], "1.0.0+build.12345"),
# Forced version bump
(["--prerelease"], "0.0.0-rc.1"),
(["--patch"], "0.0.1"),
(["--minor"], "0.1.0"),
(["--major"], "1.0.0"),
# Forced version bump with --build-metadata
(["--patch", "--build-metadata", "build.12345"], "0.0.1+build.12345"),
# Forced version bump with --as-prerelease
(["--prerelease", "--as-prerelease"], "0.0.0-rc.1"),
(["--patch", "--as-prerelease"], "0.0.1-rc.1"),
(["--minor", "--as-prerelease"], "0.1.0-rc.1"),
(["--major", "--as-prerelease"], "1.0.0-rc.1"),
# Forced version bump with --as-prerelease and modified --prerelease-token
(
["--patch", "--as-prerelease", "--prerelease-token", "beta"],
"0.0.1-beta.1",
),
# Forced version bump with --as-prerelease and modified --prerelease-token
# and --build-metadata
(
[
"--patch",
"--as-prerelease",
"--prerelease-token",
"beta",
"--build-metadata",
"build.12345",
],
"0.0.1-beta.1+build.12345",
),
)
],
)
def test_version_print_tag_prints_next_tag_no_zero_versions(
repo_result: BuiltRepoResult,
commits: list[str],
force_args: list[str],
next_release_version: str,
get_cfg_value_from_def: GetCfgValueFromDefFn,
file_in_repo: str,
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
):
"""
Given a generic repository at the latest release version and a subsequent commit,
When running the version command with the --print-tag flag,
Then the expected next release tag should be printed and exit without
making any changes to the repository.
Note: The point of this test is to only verify that the `--print-tag` flag does not
make any changes to the repository--not to validate if the next version is calculated
correctly per the repository structure (see test_version_release &
test_version_force_level for correctness).
However, we do validate that --print-tag & a force option and/or --as-prerelease options
work together to print the next release tag correctly but not make a change to the repo.
"""
repo = repo_result["repo"]
repo_def = repo_result["definition"]
tag_format_str: str = get_cfg_value_from_def(repo_def, "tag_format_str") # type: ignore[assignment]
next_release_tag = tag_format_str.format(version=next_release_version)
if len(commits) > 1:
# Make a commit to ensure we have something to release
# otherwise the "no release will be made" logic will kick in first
add_text_to_file(repo, file_in_repo)
repo.git.commit(m=commits[-1], a=True)
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-tag", *force_args]
result = run_cli(cli_cmd[1:], env={Github.DEFAULT_ENV_TOKEN_NAME: "1234"})
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit.hexsha
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert f"{next_release_tag}\n" == result.stdout
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_before == head_after
assert not tags_set_difference
assert mocked_git_push.call_count == 0
assert post_mocker.call_count == 0
@pytest.mark.parametrize(
"repo_result",
[lazy_fixture(repo_w_trunk_only_conventional_commits.__name__)],
)
def test_version_print_last_released_prints_version(
repo_result: BuiltRepoResult,
get_versions_from_repo_build_def: GetVersionsFromRepoBuildDefFn,
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
latest_release_version = get_versions_from_repo_build_def(
repo_result["definition"]
)[-1]
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-last-released"]
result = run_cli(cli_cmd[1:], env={Github.DEFAULT_ENV_TOKEN_NAME: "1234"})
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit.hexsha
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_version}\n" == result.stdout
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_before == head_after
assert not tags_set_difference
assert mocked_git_push.call_count == 0
assert post_mocker.call_count == 0
@pytest.mark.parametrize(
"repo_result, commits",
[
(
lazy_fixture(repo_w_trunk_only_conventional_commits.__name__),
lazy_fixture(conventional_minor_commits.__name__),
)
],
)
def test_version_print_last_released_prints_released_if_commits(
repo_result: BuiltRepoResult,
get_versions_from_repo_build_def: GetVersionsFromRepoBuildDefFn,
commits: list[str],
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
file_in_repo: str,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
latest_release_version = get_versions_from_repo_build_def(
repo_result["definition"]
)[-1]
# Make a commit so the head is not on the last release
add_text_to_file(repo, file_in_repo)
repo.git.commit(m=commits[0], a=True)
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-last-released"]
result = run_cli(cli_cmd[1:])
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit.hexsha
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_version}\n" == result.stdout
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_before == head_after
assert not tags_set_difference
assert mocked_git_push.call_count == 0
assert post_mocker.call_count == 0
@pytest.mark.parametrize(
"repo_result",
[lazy_fixture(repo_w_no_tags_conventional_commits.__name__)],
)
def test_version_print_last_released_prints_nothing_if_no_tags(
repo_result: BuiltRepoResult,
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
caplog: pytest.LogCaptureFixture,
):
repo = repo_result["repo"]
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_sha_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-last-released"]
result = run_cli(cli_cmd[1:])
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate (no release actions should have occurred on print)
assert_successful_exit_code(result, cli_cmd)
assert result.stdout == ""
# must use capture log to see this, because we use the logger to print this message
# not click's output
assert "No release tags found." in caplog.text
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_sha_before == head_after.hexsha # No commit has been made
assert not tags_set_difference # No tag created
assert mocked_git_push.call_count == 0 # no git push of tag or commit
assert post_mocker.call_count == 0 # no vcs release
@pytest.mark.parametrize(
"repo_result",
[lazy_fixture(repo_w_trunk_only_conventional_commits.__name__)],
)
def test_version_print_last_released_on_detached_head(
repo_result: BuiltRepoResult,
get_versions_from_repo_build_def: GetVersionsFromRepoBuildDefFn,
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
latest_release_version = get_versions_from_repo_build_def(
repo_result["definition"]
)[-1]
# Setup: put the repo in a detached head state
repo.git.checkout("HEAD", detach=True)
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_sha_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-last-released"]
result = run_cli(cli_cmd[1:])
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate (expected -> actual)
assert_successful_exit_code(result, cli_cmd)
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_version}\n" == result.stdout
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_sha_before == head_after.hexsha # No commit has been made
assert not tags_set_difference # No tag created
assert mocked_git_push.call_count == 0 # no git push of tag or commit
assert post_mocker.call_count == 0 # no vcs release
@pytest.mark.parametrize(
"repo_result",
[lazy_fixture(repo_w_trunk_only_conventional_commits.__name__)],
)
def test_version_print_last_released_on_nonrelease_branch(
repo_result: BuiltRepoResult,
get_versions_from_repo_build_def: GetVersionsFromRepoBuildDefFn,
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
latest_release_version = get_versions_from_repo_build_def(
repo_result["definition"]
)[-1]
# Setup: put the repo on a non-release branch
repo.create_head("next").checkout()
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_sha_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-last-released"]
result = run_cli(cli_cmd[1:])
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate (expected -> actual)
assert_successful_exit_code(result, cli_cmd)
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_version}\n" == result.stdout
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_sha_before == head_after.hexsha # No commit has been made
assert not tags_set_difference # No tag created
assert mocked_git_push.call_count == 0 # no git push of tag or commit
assert post_mocker.call_count == 0 # no vcs release
@pytest.mark.parametrize(
"repo_result",
[
lazy_fixture(repo_w_trunk_only_conventional_commits.__name__),
pytest.param(
lazy_fixture(
repo_w_git_flow_w_rc_n_alpha_prereleases_n_conventional_commits_using_tag_format.__name__
),
marks=pytest.mark.comprehensive,
),
],
)
def test_version_print_last_released_tag_prints_correct_tag(
repo_result: BuiltRepoResult,
get_cfg_value_from_def: GetCfgValueFromDefFn,
get_versions_from_repo_build_def: GetVersionsFromRepoBuildDefFn,
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
repo_def = repo_result["definition"]
tag_format_str: str = get_cfg_value_from_def(repo_def, "tag_format_str") # type: ignore[assignment]
latest_release_version = get_versions_from_repo_build_def(repo_def)[-1]
latest_release_tag = tag_format_str.format(version=latest_release_version)
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-last-released-tag"]
result = run_cli(cli_cmd[1:])
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit.hexsha
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_tag}\n" == result.stdout
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_before == head_after
assert not tags_set_difference
assert mocked_git_push.call_count == 0
assert post_mocker.call_count == 0
@pytest.mark.parametrize(
"repo_result, commits",
[
(
lazy_fixture(repo_w_trunk_only_conventional_commits.__name__),
lazy_fixture(conventional_minor_commits.__name__),
),
pytest.param(
lazy_fixture(
repo_w_git_flow_w_rc_n_alpha_prereleases_n_conventional_commits_using_tag_format.__name__
),
lazy_fixture(conventional_minor_commits.__name__),
marks=pytest.mark.comprehensive,
),
],
)
def test_version_print_last_released_tag_prints_released_if_commits(
repo_result: BuiltRepoResult,
get_cfg_value_from_def: GetCfgValueFromDefFn,
get_versions_from_repo_build_def: GetVersionsFromRepoBuildDefFn,
commits: list[str],
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
file_in_repo: str,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
repo_def = repo_result["definition"]
tag_format_str: str = get_cfg_value_from_def(repo_def, "tag_format_str") # type: ignore[assignment]
latest_release_version = get_versions_from_repo_build_def(repo_def)[-1]
latest_release_tag = tag_format_str.format(version=latest_release_version)
# Make a commit so the head is not on the last release
add_text_to_file(repo, file_in_repo)
repo.git.commit(m=commits[0], a=True)
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-last-released-tag"]
result = run_cli(cli_cmd[1:])
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit.hexsha
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_tag}\n" == result.stdout
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_before == head_after
assert not tags_set_difference
assert mocked_git_push.call_count == 0
assert post_mocker.call_count == 0
@pytest.mark.parametrize(
"repo_result",
[lazy_fixture(repo_w_no_tags_conventional_commits.__name__)],
)
def test_version_print_last_released_tag_prints_nothing_if_no_tags(
repo_result: BuiltRepoResult,
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
caplog: pytest.LogCaptureFixture,
):
repo = repo_result["repo"]
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_sha_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-last-released-tag"]
result = run_cli(cli_cmd[1:])
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate (no release actions should have occurred on print)
assert_successful_exit_code(result, cli_cmd)
assert result.stdout == ""
# must use capture log to see this, because we use the logger to print this message
# not click's output
assert "No release tags found." in caplog.text
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_sha_before == head_after.hexsha # No commit has been made
assert not tags_set_difference # No tag created
assert mocked_git_push.call_count == 0 # no git push of tag or commit
assert post_mocker.call_count == 0 # no vcs release
@pytest.mark.parametrize(
"repo_result",
[
lazy_fixture(repo_w_trunk_only_conventional_commits.__name__),
pytest.param(
lazy_fixture(
repo_w_git_flow_w_rc_n_alpha_prereleases_n_conventional_commits_using_tag_format.__name__
),
marks=pytest.mark.comprehensive,
),
],
)
def test_version_print_last_released_tag_on_detached_head(
repo_result: BuiltRepoResult,
get_cfg_value_from_def: GetCfgValueFromDefFn,
get_versions_from_repo_build_def: GetVersionsFromRepoBuildDefFn,
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
repo_def = repo_result["definition"]
tag_format_str: str = get_cfg_value_from_def(repo_def, "tag_format_str") # type: ignore[assignment]
latest_release_version = get_versions_from_repo_build_def(repo_def)[-1]
latest_release_tag = tag_format_str.format(version=latest_release_version)
# Setup: put the repo in a detached head state
repo.git.checkout("HEAD", detach=True)
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_sha_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-last-released-tag"]
result = run_cli(cli_cmd[1:])
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate (expected -> actual)
assert_successful_exit_code(result, cli_cmd)
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_tag}\n" == result.stdout
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_sha_before == head_after.hexsha # No commit has been made
assert not tags_set_difference # No tag created
assert mocked_git_push.call_count == 0 # no git push of tag or commit
assert post_mocker.call_count == 0 # no vcs release
@pytest.mark.parametrize(
"repo_result",
[
lazy_fixture(repo_w_trunk_only_conventional_commits.__name__),
pytest.param(
lazy_fixture(
repo_w_git_flow_w_rc_n_alpha_prereleases_n_conventional_commits_using_tag_format.__name__
),
marks=pytest.mark.comprehensive,
),
],
)
def test_version_print_last_released_tag_on_nonrelease_branch(
repo_result: BuiltRepoResult,
get_cfg_value_from_def: GetCfgValueFromDefFn,
get_versions_from_repo_build_def: GetVersionsFromRepoBuildDefFn,
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
repo_def = repo_result["definition"]
tag_format_str: str = get_cfg_value_from_def(repo_def, "tag_format_str") # type: ignore[assignment]
latest_release_version = get_versions_from_repo_build_def(repo_def)[-1]
last_release_tag = tag_format_str.format(version=latest_release_version)
# Setup: put the repo on a non-release branch
repo.create_head("next").checkout()
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_sha_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-last-released-tag"]
result = run_cli(cli_cmd[1:])
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate (expected -> actual)
assert_successful_exit_code(result, cli_cmd)
assert not strip_logging_messages(result.stderr)
assert f"{last_release_tag}\n" == result.stdout
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_sha_before == head_after.hexsha
assert not tags_set_difference
assert mocked_git_push.call_count == 0
assert post_mocker.call_count == 0
@pytest.mark.parametrize(
"repo_result, get_commit_def_fn",
[
(
lazy_fixture(repo_w_trunk_only_conventional_commits.__name__),
lazy_fixture(get_commit_def_of_conventional_commit.__name__),
)
],
)
def test_version_print_next_version_fails_on_detached_head(
repo_result: BuiltRepoResult,
run_cli: RunCliFn,
simulate_change_commits_n_rtn_changelog_entry: SimulateChangeCommitsNReturnChangelogEntryFn,
get_commit_def_fn: GetCommitDefFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
expected_error_msg = (
"Detached HEAD state cannot match any release groups; no release will be made"
)
# Setup: put the repo in a detached head state
repo.git.checkout("HEAD", detach=True)
# Setup: make a commit to ensure we have something to release
simulate_change_commits_n_rtn_changelog_entry(
repo,
[get_commit_def_fn("fix: make a patch fix to codebase")],
)
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print"]
result = run_cli(cli_cmd[1:])
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit.hexsha
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate (expected -> actual)
assert_exit_code(1, result, cli_cmd)
assert not result.stdout
assert f"{expected_error_msg}\n" == strip_logging_messages(result.stderr)
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_before == head_after
assert not tags_set_difference
assert mocked_git_push.call_count == 0
assert post_mocker.call_count == 0
@pytest.mark.parametrize(
"repo_result, get_commit_def_fn",
[
(
lazy_fixture(repo_w_trunk_only_conventional_commits.__name__),
lazy_fixture(get_commit_def_of_conventional_commit.__name__),
)
],
)
def test_version_print_next_tag_fails_on_detached_head(
repo_result: BuiltRepoResult,
run_cli: RunCliFn,
simulate_change_commits_n_rtn_changelog_entry: SimulateChangeCommitsNReturnChangelogEntryFn,
get_commit_def_fn: GetCommitDefFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
expected_error_msg = (
"Detached HEAD state cannot match any release groups; no release will be made"
)
# Setup: put the repo in a detached head state
repo.git.checkout("HEAD", detach=True)
# Setup: make a commit to ensure we have something to release
simulate_change_commits_n_rtn_changelog_entry(
repo,
[get_commit_def_fn("fix: make a patch fix to codebase")],
)
# Setup: take measurement before running the version command
repo_status_before = repo.git.status(short=True)
head_before = repo.head.commit.hexsha
tags_before = {tag.name for tag in repo.tags}
# Act
cli_cmd = [MAIN_PROG_NAME, VERSION_SUBCMD, "--print-tag"]
result = run_cli(cli_cmd[1:])
# take measurement after running the version command
repo_status_after = repo.git.status(short=True)
head_after = repo.head.commit.hexsha
tags_after = {tag.name for tag in repo.tags}
tags_set_difference = set.difference(tags_after, tags_before)
# Evaluate (expected -> actual)
assert_exit_code(1, result, cli_cmd)
assert not result.stdout
assert f"{expected_error_msg}\n" == strip_logging_messages(result.stderr)
# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
assert head_before == head_after
assert not tags_set_difference
assert mocked_git_push.call_count == 0
assert post_mocker.call_count == 0
|