Skip to content

Commit 1e9d5d0

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

File tree

6 files changed

+54
-29
lines changed

6 files changed

+54
-29
lines changed

commitizen/git.py

+3-3
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

+1
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

+1-1
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

+26-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import sys
2-
import time
3-
42
from datetime import date
53
from unittest import mock
64

@@ -15,7 +13,7 @@
1513
NotAGitProjectError,
1614
NotAllowed,
1715
)
18-
from tests.utils import create_file_and_commit
16+
from tests.utils import create_file_and_commit, wait_for_tag
1917

2018

2119
@pytest.mark.usefixtures("tmp_commitizen_project")
@@ -549,18 +547,19 @@ 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+
559+
# time.sleep(1)
560560
testargs = ["cz", "changelog", "0.2.0"]
561561
mocker.patch.object(sys, "argv", testargs)
562562
cli.main()
563-
564563
with open(changelog_path, "r") as f:
565564
out = f.read()
566565

@@ -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)
@@ -701,21 +706,21 @@ def test_changelog_from_rev_version_range_from_arg(
701706
testargs = ["cz", "bump", "--yes"]
702707
mocker.patch.object(sys, "argv", testargs)
703708
cli.main()
704-
time.sleep(0.5)
709+
wait_for_tag()
705710
create_file_and_commit("feat: after 0.2.0")
706711
create_file_and_commit("feat: another feature")
707712

708713
testargs = ["cz", "bump", "--yes"]
709714
mocker.patch.object(sys, "argv", testargs)
710715
cli.main()
711-
time.sleep(0.5)
716+
wait_for_tag()
712717

713718
create_file_and_commit("feat: getting ready for this")
714719

715720
testargs = ["cz", "bump", "--yes"]
716721
mocker.patch.object(sys, "argv", testargs)
717722
cli.main()
718-
time.sleep(0.5)
723+
wait_for_tag()
719724

720725
testargs = ["cz", "changelog", "0.3.0..0.4.0"]
721726
mocker.patch.object(sys, "argv", testargs)
@@ -741,40 +746,40 @@ def test_changelog_from_rev_version_with_big_range_from_arg(
741746
testargs = ["cz", "bump", "--yes"]
742747
mocker.patch.object(sys, "argv", testargs)
743748
cli.main()
744-
time.sleep(0.5)
749+
wait_for_tag()
745750

746751
create_file_and_commit("feat: after 0.2.0")
747752
create_file_and_commit("feat: another feature")
748753

749754
testargs = ["cz", "bump", "--yes"] # 0.3.0
750755
mocker.patch.object(sys, "argv", testargs)
751756
cli.main()
752-
time.sleep(0.5)
757+
wait_for_tag()
753758
create_file_and_commit("feat: getting ready for this")
754759

755760
testargs = ["cz", "bump", "--yes"] # 0.4.0
756761
mocker.patch.object(sys, "argv", testargs)
757762
cli.main()
758-
time.sleep(0.5)
763+
wait_for_tag()
759764
create_file_and_commit("fix: small error")
760765

761766
testargs = ["cz", "bump", "--yes"] # 0.4.1
762767
mocker.patch.object(sys, "argv", testargs)
763768
cli.main()
764-
time.sleep(0.5)
769+
wait_for_tag()
765770
create_file_and_commit("feat: new shinny feature")
766771

767772
testargs = ["cz", "bump", "--yes"] # 0.5.0
768773
mocker.patch.object(sys, "argv", testargs)
769774
cli.main()
770-
time.sleep(0.5)
775+
wait_for_tag()
771776
create_file_and_commit("feat: amazing different shinny feature")
772777
# dirty hack to avoid same time between tags
773778

774779
testargs = ["cz", "bump", "--yes"] # 0.6.0
775780
mocker.patch.object(sys, "argv", testargs)
776781
cli.main()
777-
time.sleep(0.5)
782+
wait_for_tag()
778783

779784
testargs = ["cz", "changelog", "0.3.0..0.5.0"]
780785
mocker.patch.object(sys, "argv", testargs)
@@ -800,15 +805,17 @@ def test_changelog_from_rev_latest_version_dry_run(
800805
testargs = ["cz", "bump", "--yes"]
801806
mocker.patch.object(sys, "argv", testargs)
802807
cli.main()
803-
time.sleep(0.5)
808+
wait_for_tag()
809+
804810
create_file_and_commit("feat: after 0.2.0")
805811
create_file_and_commit("feat: another feature")
806812

807813
testargs = ["cz", "bump", "--yes"]
808814
mocker.patch.object(sys, "argv", testargs)
809815
cli.main()
810816
capsys.readouterr()
811-
time.sleep(0.5)
817+
wait_for_tag()
818+
812819
testargs = ["cz", "changelog", "0.3.0", "--dry-run"]
813820
mocker.patch.object(sys, "argv", testargs)
814821
with pytest.raises(DryRunExit):

tests/test_changelog_parser.py

+8-6
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):

tests/utils.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
import uuid
23
from pathlib import Path
34
from typing import Optional
@@ -19,3 +20,17 @@ def create_file_and_commit(message: str, filename: Optional[str] = None):
1920
Path(f"./{filename}").touch()
2021
cmd.run("git add .")
2122
git.commit(message)
23+
24+
25+
def wait_for_tag():
26+
"""Wait for tag.
27+
28+
The resolution of timestamps is 1 second, so we need to wait
29+
to create another tag unfortunately.
30+
31+
This means:
32+
If we create 2 tags under the same second, they might be returned in the wrong order
33+
34+
See https://stackoverflow.com/questions/28237043/what-is-the-resolution-of-gits-commit-date-or-author-date-timestamps
35+
"""
36+
time.sleep(1.1)

0 commit comments

Comments
 (0)