Skip to content

Commit cd5e68a

Browse files
committed
refactor(git): use date as a function in GitTag to easily patch
1 parent 5ec1e79 commit cd5e68a

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

commitizen/changelog.py

-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ def get_start_and_end_rev(
320320
"""
321321
start: Optional[str] = None
322322
end: Optional[str] = None
323-
324323
try:
325324
start, end = version.split("..")
326325
except ValueError:

commitizen/git.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ class GitTag(GitObject):
3939
def __init__(self, name, rev, date):
4040
self.rev = rev.strip()
4141
self.name = name.strip()
42-
self.date = date.strip()
42+
self._date = date.strip()
4343

4444
def __repr__(self):
4545
return f"GitTag('{self.name}', '{self.rev}', '{self.date}')"
4646

47+
@property
48+
def date(self):
49+
return self._date
50+
4751
@classmethod
4852
def from_line(cls, line: str, inner_delimiter: str) -> "GitTag":
4953

tests/commands/test_changelog_command.py

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import sys
2+
import time
3+
24
from datetime import date
5+
from unittest import mock
36

47
import pytest
58

@@ -533,6 +536,7 @@ def test_changelog_with_filename_as_empty_string(mocker, changelog_path, config_
533536

534537
@pytest.mark.usefixtures("tmp_commitizen_project")
535538
@pytest.mark.freeze_time("2022-02-13")
539+
@mock.patch("commitizen.git.GitTag.date", "2022-02-13")
536540
def test_changelog_from_rev_first_version_from_arg(
537541
mocker, config_path, changelog_path, file_regression
538542
):
@@ -541,17 +545,18 @@ def test_changelog_from_rev_first_version_from_arg(
541545

542546
# create commit and tag
543547
create_file_and_commit("feat: new file")
548+
544549
testargs = ["cz", "bump", "--yes"]
545550
mocker.patch.object(sys, "argv", testargs)
546551
cli.main()
547-
552+
time.sleep(0.5)
548553
create_file_and_commit("feat: after 0.2.0")
549554
create_file_and_commit("feat: another feature")
550555

551556
testargs = ["cz", "bump", "--yes"]
552557
mocker.patch.object(sys, "argv", testargs)
553558
cli.main()
554-
559+
time.sleep(0.5)
555560
testargs = ["cz", "changelog", "0.2.0"]
556561
mocker.patch.object(sys, "argv", testargs)
557562
cli.main()
@@ -564,6 +569,7 @@ def test_changelog_from_rev_first_version_from_arg(
564569

565570
@pytest.mark.usefixtures("tmp_commitizen_project")
566571
@pytest.mark.freeze_time("2022-02-13")
572+
@mock.patch("commitizen.git.GitTag.date", "2022-02-13")
567573
def test_changelog_from_rev_latest_version_from_arg(
568574
mocker, config_path, changelog_path, file_regression
569575
):
@@ -575,14 +581,14 @@ def test_changelog_from_rev_latest_version_from_arg(
575581
testargs = ["cz", "bump", "--yes"]
576582
mocker.patch.object(sys, "argv", testargs)
577583
cli.main()
578-
584+
time.sleep(0.5)
579585
create_file_and_commit("feat: after 0.2.0")
580586
create_file_and_commit("feat: another feature")
581587

582588
testargs = ["cz", "bump", "--yes"]
583589
mocker.patch.object(sys, "argv", testargs)
584590
cli.main()
585-
591+
time.sleep(0.5)
586592
testargs = ["cz", "changelog", "0.3.0"]
587593
mocker.patch.object(sys, "argv", testargs)
588594
cli.main()
@@ -651,6 +657,7 @@ def test_changelog_from_rev_range_version_not_found(mocker, config_path):
651657

652658
@pytest.mark.usefixtures("tmp_commitizen_project")
653659
@pytest.mark.freeze_time("2022-02-13")
660+
@mock.patch("commitizen.git.GitTag.date", "2022-02-13")
654661
def test_changelog_from_rev_version_range_including_first_tag(
655662
mocker, config_path, changelog_path, file_regression
656663
):
@@ -682,6 +689,7 @@ def test_changelog_from_rev_version_range_including_first_tag(
682689

683690
@pytest.mark.usefixtures("tmp_commitizen_project")
684691
@pytest.mark.freeze_time("2022-02-13")
692+
@mock.patch("commitizen.git.GitTag.date", "2022-02-13")
685693
def test_changelog_from_rev_version_range_from_arg(
686694
mocker, config_path, changelog_path, file_regression
687695
):
@@ -693,19 +701,21 @@ def test_changelog_from_rev_version_range_from_arg(
693701
testargs = ["cz", "bump", "--yes"]
694702
mocker.patch.object(sys, "argv", testargs)
695703
cli.main()
696-
704+
time.sleep(0.5)
697705
create_file_and_commit("feat: after 0.2.0")
698706
create_file_and_commit("feat: another feature")
699707

700708
testargs = ["cz", "bump", "--yes"]
701709
mocker.patch.object(sys, "argv", testargs)
702710
cli.main()
711+
time.sleep(0.5)
703712

704713
create_file_and_commit("feat: getting ready for this")
705714

706715
testargs = ["cz", "bump", "--yes"]
707716
mocker.patch.object(sys, "argv", testargs)
708717
cli.main()
718+
time.sleep(0.5)
709719

710720
testargs = ["cz", "changelog", "0.3.0..0.4.0"]
711721
mocker.patch.object(sys, "argv", testargs)
@@ -718,6 +728,7 @@ def test_changelog_from_rev_version_range_from_arg(
718728

719729
@pytest.mark.usefixtures("tmp_commitizen_project")
720730
@pytest.mark.freeze_time("2022-02-13")
731+
@mock.patch("commitizen.git.GitTag.date", "2022-02-13")
721732
def test_changelog_from_rev_version_with_big_range_from_arg(
722733
mocker, config_path, changelog_path, file_regression
723734
):
@@ -726,40 +737,44 @@ def test_changelog_from_rev_version_with_big_range_from_arg(
726737

727738
# create commit and tag
728739
create_file_and_commit("feat: new file")
740+
729741
testargs = ["cz", "bump", "--yes"]
730742
mocker.patch.object(sys, "argv", testargs)
731743
cli.main()
744+
time.sleep(0.5)
732745

733746
create_file_and_commit("feat: after 0.2.0")
734747
create_file_and_commit("feat: another feature")
735748

736749
testargs = ["cz", "bump", "--yes"] # 0.3.0
737750
mocker.patch.object(sys, "argv", testargs)
738751
cli.main()
739-
752+
time.sleep(0.5)
740753
create_file_and_commit("feat: getting ready for this")
741754

742755
testargs = ["cz", "bump", "--yes"] # 0.4.0
743756
mocker.patch.object(sys, "argv", testargs)
744757
cli.main()
745-
758+
time.sleep(0.5)
746759
create_file_and_commit("fix: small error")
747760

748761
testargs = ["cz", "bump", "--yes"] # 0.4.1
749762
mocker.patch.object(sys, "argv", testargs)
750763
cli.main()
751-
764+
time.sleep(0.5)
752765
create_file_and_commit("feat: new shinny feature")
753766

754767
testargs = ["cz", "bump", "--yes"] # 0.5.0
755768
mocker.patch.object(sys, "argv", testargs)
756769
cli.main()
757-
770+
time.sleep(0.5)
758771
create_file_and_commit("feat: amazing different shinny feature")
772+
# dirty hack to avoid same time between tags
759773

760774
testargs = ["cz", "bump", "--yes"] # 0.6.0
761775
mocker.patch.object(sys, "argv", testargs)
762776
cli.main()
777+
time.sleep(0.5)
763778

764779
testargs = ["cz", "changelog", "0.3.0..0.5.0"]
765780
mocker.patch.object(sys, "argv", testargs)
@@ -772,6 +787,7 @@ def test_changelog_from_rev_version_with_big_range_from_arg(
772787

773788
@pytest.mark.usefixtures("tmp_commitizen_project")
774789
@pytest.mark.freeze_time("2022-02-13")
790+
@mock.patch("commitizen.git.GitTag.date", "2022-02-13")
775791
def test_changelog_from_rev_latest_version_dry_run(
776792
mocker, capsys, config_path, changelog_path, file_regression
777793
):
@@ -784,15 +800,15 @@ def test_changelog_from_rev_latest_version_dry_run(
784800
testargs = ["cz", "bump", "--yes"]
785801
mocker.patch.object(sys, "argv", testargs)
786802
cli.main()
787-
803+
time.sleep(0.5)
788804
create_file_and_commit("feat: after 0.2.0")
789805
create_file_and_commit("feat: another feature")
790806

791807
testargs = ["cz", "bump", "--yes"]
792808
mocker.patch.object(sys, "argv", testargs)
793809
cli.main()
794810
capsys.readouterr()
795-
811+
time.sleep(0.5)
796812
testargs = ["cz", "changelog", "0.3.0", "--dry-run"]
797813
mocker.patch.object(sys, "argv", testargs)
798814
with pytest.raises(DryRunExit):

0 commit comments

Comments
 (0)