Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2020925

Browse files
committed
refactor: speed up testing and wait for tags
1 parent cd5e68a commit 2020925

File tree

11 files changed

+76
-47
lines changed

11 files changed

+76
-47
lines changed

‎commitizen/bump.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ def _version_to_regex(version: str):
196196
return re.compile(f"{clean_regex}")
197197

198198

199-
def create_tag(version: Union[Version, str], tag_format: Optional[str] = None) -> str:
199+
def normalize_tag(
200+
version: Union[Version, str], tag_format: Optional[str] = None
201+
) -> str:
200202
"""The tag and the software version might be different.
201203
202204
That's why this function exists.

‎commitizen/changelog.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from jinja2 import Environment, PackageLoader
3535

3636
from commitizen import defaults
37+
from commitizen.bump import normalize_tag
3738
from commitizen.exceptions import InvalidConfigurationError
3839
from commitizen.git import GitCommit, GitTag
3940

@@ -310,7 +311,7 @@ def get_smart_tag_range(
310311

311312

312313
def get_start_and_end_rev(
313-
tags: List[GitTag], version: str, tag_format: str, create_tag: Callable
314+
tags: List[GitTag], version: str, tag_format: str
314315
) -> Tuple[Optional[str], Optional[str]]:
315316
"""Find the tags for the given version.
316317
@@ -325,14 +326,14 @@ def get_start_and_end_rev(
325326
except ValueError:
326327
end = version
327328

328-
end_tag = create_tag(end, tag_format=tag_format)
329+
end_tag = normalize_tag(end, tag_format=tag_format)
329330

330331
start_tag = None
331332
if start:
332-
start_tag = create_tag(start, tag_format=tag_format)
333+
start_tag = normalize_tag(start, tag_format=tag_format)
333334

334335
tags_range = get_smart_tag_range(tags, start=end_tag, end=start_tag)
335-
if len(tags_range) ==0:
336+
if nottags_range:
336337
return None, None
337338

338339
start_rev: Optional[str] = tags_range[-1].name

‎commitizen/commands/bump.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def __call__(self): # noqa: C901
101101
is_files_only: Optional[bool] = self.arguments["files_only"]
102102
is_local_version: Optional[bool] = self.arguments["local_version"]
103103

104-
current_tag_version: str = bump.create_tag(
104+
current_tag_version: str = bump.normalize_tag(
105105
current_version, tag_format=tag_format
106106
)
107107

@@ -149,7 +149,7 @@ def __call__(self): # noqa: C901
149149
is_local_version=is_local_version,
150150
)
151151

152-
new_tag_version = bump.create_tag(new_version, tag_format=tag_format)
152+
new_tag_version = bump.normalize_tag(new_version, tag_format=tag_format)
153153
message = bump.create_commit_message(
154154
current_version, new_version, bump_commit_message
155155
)

‎commitizen/commands/changelog.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from operator import itemgetter
44
from typing import Callable, Dict, List, Optional
55

6-
from commitizen import bump, changelog, factory, git, out
6+
from commitizen import changelog, factory, git, out
77
from commitizen.config import BaseConfig
88
from commitizen.exceptions import (
99
DryRunExit,
@@ -138,7 +138,6 @@ def __call__(self):
138138
tags,
139139
version=self.rev_range,
140140
tag_format=self.tag_format,
141-
create_tag=bump.create_tag,
142141
)
143142

144143
commits = git.get_commits(

‎commitizen/git.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ def get_commits(
8686
)
8787

8888
if start:
89-
c = cmd.run(f"{git_log_cmd} {start}..{end}")
89+
command = f"{git_log_cmd} {start}..{end}"
9090
else:
91-
c = cmd.run(f"{git_log_cmd} {end}")
92-
91+
command = f"{git_log_cmd} {end}"
92+
c=cmd.run(command)
9393
if not c.out:
9494
return []
9595

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ types-termcolor = "^0.1.1"
7979
mkdocs = "^1.0"
8080
mkdocs-material = "^4.1"
8181
pydocstyle = "^5.0.2"
82+
pytest-xdist = "^2.5.0"
8283

8384
[tool.poetry.scripts]
8485
cz = "commitizen.cli:main"

‎scripts/test‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if [ -d 'venv' ] ; then
55
export PREFIX="venv/bin/"
66
fi
77

8-
${PREFIX}pytest --cov-report term-missing --cov-report=xml:coverage.xml --cov=commitizen tests/
8+
${PREFIX}pytest -n 3 --cov-report term-missing --cov-report=xml:coverage.xml --cov=commitizen tests/
99
${PREFIX}black commitizen tests --check
1010
${PREFIX}isort --check-only commitizen tests
1111
${PREFIX}flake8 commitizen/ tests/

‎tests/commands/test_changelog_command.py‎

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import sys
2-
import time
3-
42
from datetime import date
5-
from unittest import mock
63

74
import pytest
85

@@ -15,7 +12,7 @@
1512
NotAGitProjectError,
1613
NotAllowed,
1714
)
18-
from tests.utils import create_file_and_commit
15+
from tests.utils import create_file_and_commit, wait_for_tag
1916

2017

2118
@pytest.mark.usefixtures("tmp_commitizen_project")
@@ -536,10 +533,11 @@ def test_changelog_with_filename_as_empty_string(mocker, changelog_path, config_
536533

537534
@pytest.mark.usefixtures("tmp_commitizen_project")
538535
@pytest.mark.freeze_time("2022年02月13日")
539-
@mock.patch("commitizen.git.GitTag.date", "2022年02月13日")
540536
def test_changelog_from_rev_first_version_from_arg(
541537
mocker, config_path, changelog_path, file_regression
542538
):
539+
mocker.patch("commitizen.git.GitTag.date", "2022年02月13日")
540+
543541
with open(config_path, "a") as f:
544542
f.write('tag_format = "$version"\n')
545543

@@ -549,18 +547,18 @@ def test_changelog_from_rev_first_version_from_arg(
549547
testargs = ["cz", "bump", "--yes"]
550548
mocker.patch.object(sys, "argv", testargs)
551549
cli.main()
552-
time.sleep(0.5)
550+
wait_for_tag()
551+
553552
create_file_and_commit("feat: after 0.2.0")
554553
create_file_and_commit("feat: another feature")
555554

556555
testargs = ["cz", "bump", "--yes"]
557556
mocker.patch.object(sys, "argv", testargs)
558557
cli.main()
559-
time.sleep(0.5)
558+
560559
testargs = ["cz", "changelog", "0.2.0"]
561560
mocker.patch.object(sys, "argv", testargs)
562561
cli.main()
563-
564562
with open(changelog_path, "r") as f:
565563
out = f.read()
566564

@@ -569,10 +567,11 @@ def test_changelog_from_rev_first_version_from_arg(
569567

570568
@pytest.mark.usefixtures("tmp_commitizen_project")
571569
@pytest.mark.freeze_time("2022年02月13日")
572-
@mock.patch("commitizen.git.GitTag.date", "2022年02月13日")
573570
def test_changelog_from_rev_latest_version_from_arg(
574571
mocker, config_path, changelog_path, file_regression
575572
):
573+
mocker.patch("commitizen.git.GitTag.date", "2022年02月13日")
574+
576575
with open(config_path, "a") as f:
577576
f.write('tag_format = "$version"\n')
578577

@@ -581,14 +580,17 @@ def test_changelog_from_rev_latest_version_from_arg(
581580
testargs = ["cz", "bump", "--yes"]
582581
mocker.patch.object(sys, "argv", testargs)
583582
cli.main()
584-
time.sleep(0.5)
583+
wait_for_tag()
584+
585585
create_file_and_commit("feat: after 0.2.0")
586586
create_file_and_commit("feat: another feature")
587587

588588
testargs = ["cz", "bump", "--yes"]
589589
mocker.patch.object(sys, "argv", testargs)
590590
cli.main()
591-
time.sleep(0.5)
591+
592+
wait_for_tag()
593+
592594
testargs = ["cz", "changelog", "0.3.0"]
593595
mocker.patch.object(sys, "argv", testargs)
594596
cli.main()
@@ -613,12 +615,15 @@ def test_changelog_from_rev_single_version_not_found(
613615
mocker.patch.object(sys, "argv", testargs)
614616
cli.main()
615617

618+
wait_for_tag()
619+
616620
create_file_and_commit("feat: after 0.2.0")
617621
create_file_and_commit("feat: another feature")
618622

619623
testargs = ["cz", "bump", "--yes"]
620624
mocker.patch.object(sys, "argv", testargs)
621625
cli.main()
626+
wait_for_tag()
622627

623628
testargs = ["cz", "changelog", "0.8.0"] # it shouldn't exist
624629
mocker.patch.object(sys, "argv", testargs)
@@ -657,10 +662,11 @@ def test_changelog_from_rev_range_version_not_found(mocker, config_path):
657662

658663
@pytest.mark.usefixtures("tmp_commitizen_project")
659664
@pytest.mark.freeze_time("2022年02月13日")
660-
@mock.patch("commitizen.git.GitTag.date", "2022年02月13日")
661665
def test_changelog_from_rev_version_range_including_first_tag(
662666
mocker, config_path, changelog_path, file_regression
663667
):
668+
mocker.patch("commitizen.git.GitTag.date", "2022年02月13日")
669+
664670
with open(config_path, "a") as f:
665671
f.write('tag_format = "$version"\n')
666672

@@ -683,16 +689,16 @@ def test_changelog_from_rev_version_range_including_first_tag(
683689
with open(changelog_path, "r") as f:
684690
out = f.read()
685691

686-
print(out)
687692
file_regression.check(out, extension=".md")
688693

689694

690695
@pytest.mark.usefixtures("tmp_commitizen_project")
691696
@pytest.mark.freeze_time("2022年02月13日")
692-
@mock.patch("commitizen.git.GitTag.date", "2022年02月13日")
693697
def test_changelog_from_rev_version_range_from_arg(
694698
mocker, config_path, changelog_path, file_regression
695699
):
700+
mocker.patch("commitizen.git.GitTag.date", "2022年02月13日")
701+
696702
with open(config_path, "a") as f:
697703
f.write('tag_format = "$version"\n')
698704

@@ -701,21 +707,21 @@ def test_changelog_from_rev_version_range_from_arg(
701707
testargs = ["cz", "bump", "--yes"]
702708
mocker.patch.object(sys, "argv", testargs)
703709
cli.main()
704-
time.sleep(0.5)
710+
wait_for_tag()
705711
create_file_and_commit("feat: after 0.2.0")
706712
create_file_and_commit("feat: another feature")
707713

708714
testargs = ["cz", "bump", "--yes"]
709715
mocker.patch.object(sys, "argv", testargs)
710716
cli.main()
711-
time.sleep(0.5)
717+
wait_for_tag()
712718

713719
create_file_and_commit("feat: getting ready for this")
714720

715721
testargs = ["cz", "bump", "--yes"]
716722
mocker.patch.object(sys, "argv", testargs)
717723
cli.main()
718-
time.sleep(0.5)
724+
wait_for_tag()
719725

720726
testargs = ["cz", "changelog", "0.3.0..0.4.0"]
721727
mocker.patch.object(sys, "argv", testargs)
@@ -728,10 +734,11 @@ def test_changelog_from_rev_version_range_from_arg(
728734

729735
@pytest.mark.usefixtures("tmp_commitizen_project")
730736
@pytest.mark.freeze_time("2022年02月13日")
731-
@mock.patch("commitizen.git.GitTag.date", "2022年02月13日")
732737
def test_changelog_from_rev_version_with_big_range_from_arg(
733738
mocker, config_path, changelog_path, file_regression
734739
):
740+
mocker.patch("commitizen.git.GitTag.date", "2022年02月13日")
741+
735742
with open(config_path, "a") as f:
736743
f.write('tag_format = "$version"\n')
737744

@@ -741,40 +748,40 @@ def test_changelog_from_rev_version_with_big_range_from_arg(
741748
testargs = ["cz", "bump", "--yes"]
742749
mocker.patch.object(sys, "argv", testargs)
743750
cli.main()
744-
time.sleep(0.5)
751+
wait_for_tag()
745752

746753
create_file_and_commit("feat: after 0.2.0")
747754
create_file_and_commit("feat: another feature")
748755

749756
testargs = ["cz", "bump", "--yes"] # 0.3.0
750757
mocker.patch.object(sys, "argv", testargs)
751758
cli.main()
752-
time.sleep(0.5)
759+
wait_for_tag()
753760
create_file_and_commit("feat: getting ready for this")
754761

755762
testargs = ["cz", "bump", "--yes"] # 0.4.0
756763
mocker.patch.object(sys, "argv", testargs)
757764
cli.main()
758-
time.sleep(0.5)
765+
wait_for_tag()
759766
create_file_and_commit("fix: small error")
760767

761768
testargs = ["cz", "bump", "--yes"] # 0.4.1
762769
mocker.patch.object(sys, "argv", testargs)
763770
cli.main()
764-
time.sleep(0.5)
771+
wait_for_tag()
765772
create_file_and_commit("feat: new shinny feature")
766773

767774
testargs = ["cz", "bump", "--yes"] # 0.5.0
768775
mocker.patch.object(sys, "argv", testargs)
769776
cli.main()
770-
time.sleep(0.5)
777+
wait_for_tag()
771778
create_file_and_commit("feat: amazing different shinny feature")
772779
# dirty hack to avoid same time between tags
773780

774781
testargs = ["cz", "bump", "--yes"] # 0.6.0
775782
mocker.patch.object(sys, "argv", testargs)
776783
cli.main()
777-
time.sleep(0.5)
784+
wait_for_tag()
778785

779786
testargs = ["cz", "changelog", "0.3.0..0.5.0"]
780787
mocker.patch.object(sys, "argv", testargs)
@@ -787,10 +794,10 @@ def test_changelog_from_rev_version_with_big_range_from_arg(
787794

788795
@pytest.mark.usefixtures("tmp_commitizen_project")
789796
@pytest.mark.freeze_time("2022年02月13日")
790-
@mock.patch("commitizen.git.GitTag.date", "2022年02月13日")
791797
def test_changelog_from_rev_latest_version_dry_run(
792798
mocker, capsys, config_path, changelog_path, file_regression
793799
):
800+
mocker.patch("commitizen.git.GitTag.date", "2022年02月13日")
794801

795802
with open(config_path, "a") as f:
796803
f.write('tag_format = "$version"\n')
@@ -800,15 +807,17 @@ def test_changelog_from_rev_latest_version_dry_run(
800807
testargs = ["cz", "bump", "--yes"]
801808
mocker.patch.object(sys, "argv", testargs)
802809
cli.main()
803-
time.sleep(0.5)
810+
wait_for_tag()
811+
804812
create_file_and_commit("feat: after 0.2.0")
805813
create_file_and_commit("feat: another feature")
806814

807815
testargs = ["cz", "bump", "--yes"]
808816
mocker.patch.object(sys, "argv", testargs)
809817
cli.main()
810818
capsys.readouterr()
811-
time.sleep(0.5)
819+
wait_for_tag()
820+
812821
testargs = ["cz", "changelog", "0.3.0", "--dry-run"]
813822
mocker.patch.object(sys, "argv", testargs)
814823
with pytest.raises(DryRunExit):

‎tests/test_bump_create_tag.py‎ renamed to ‎tests/test_bump_normalize_tag.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
@pytest.mark.parametrize("test_input,expected", conversion)
2020
def test_create_tag(test_input, expected):
2121
version, format = test_input
22-
new_tag = bump.create_tag(Version(version), format)
22+
new_tag = bump.normalize_tag(Version(version), format)
2323
assert new_tag == expected

‎tests/test_changelog_parser.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ def changelog_content() -> str:
3333

3434

3535
@pytest.fixture
36-
def existing_changelog_file():
37-
changelog_path = "tests/CHANGELOG.md"
36+
def existing_changelog_file(tmpdir):
37+
with tmpdir.as_cwd():
38+
changelog_path = os.path.join(os.getcwd(), "CHANGELOG.md")
39+
# changelog_path = "tests/CHANGELOG.md"
3840

39-
with open(changelog_path, "w") as f:
40-
f.write(CHANGELOG_TEMPLATE)
41+
with open(changelog_path, "w") as f:
42+
f.write(CHANGELOG_TEMPLATE)
4143

42-
yield changelog_path
44+
yield changelog_path
4345

44-
os.remove(changelog_path)
46+
os.remove(changelog_path)
4547

4648

4749
def test_read_changelog_blocks(existing_changelog_file):

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /