Skip to content

Commit b222e96

Browse files
committed
- Updated tests to use new contexts
1 parent 16dd292 commit b222e96

4 files changed

Lines changed: 53 additions & 47 deletions

File tree

test/conftest.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,39 @@ class FakeCommit:
6666
trailers: Optional[dict] = None
6767

6868

69-
def commit_factory():
69+
def commit_factory(
70+
committer_name: Optional[str] = None,
71+
committer_email: Optional[str] = None,
72+
summary: Optional[str] = None,
73+
body: Optional[str] = None,
74+
trailers: Optional[dict] = None,
75+
):
7076
"""Generate commits."""
71-
committer = FakeCommitter(name=fake.name(), email=fake.ascii_email())
72-
summary = fake.sentence(nb_words=8, variable_nb_words=True)
73-
body = (
77+
committer = FakeCommitter(name=committer_name or fake.name(), email=committer_email or fake.ascii_email())
78+
summary = summary or fake.sentence(nb_words=8, variable_nb_words=True)
79+
body = body or "\n".join(
7480
[summary, fake.text(max_nb_chars=160), ""]
7581
+ [f"* {line}" for line in fake.sentences(nb=fake.random_int(max=5))]
7682
+ [
7783
"",
7884
]
7985
)
86+
body_list = [body]
8087
trailer_tokens = fake.random_elements(elements=trailer_weighting, length=fake.random_int(max=3))
81-
trailers = collections.defaultdict(list)
82-
for token in trailer_tokens:
83-
trailers[token].append((fake.name(), fake.ascii_email()))
88+
if trailers is None:
89+
trailers = collections.defaultdict(list)
90+
for token in trailer_tokens:
91+
trailers[token].append((fake.name(), fake.ascii_email()))
8492

8593
for token, items in trailers.items():
86-
for name, email in items:
87-
body.append(f"{token}: {name} <{email}>")
94+
body_list.extend(f"{token}: {name} <{email}>" for name, email in items)
8895

8996
return FakeCommit(
9097
hexsha=fake.sha1(),
9198
committed_datetime=fake.past_datetime(tzinfo=fake.pytimezone()),
9299
committer=committer,
93100
summary=summary,
94-
message="\n".join(body),
101+
message="\n".join(body_list),
95102
trailers=trailers,
96103
)
97104

test/fixtures/rendered_default_repo.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
- Chg: modified ``b`` XXX.
88

9-
109
## 0.0.3 (2022-01-05)
1110

1211
### New
@@ -30,10 +29,8 @@
3029
**subject:** This is a fake subject spanning to several lines
3130
as you can see
3231

33-
3432
- New: add file ``c``.
3533

36-
3734
## 0.0.2 (2022-01-02)
3835

3936
### New
@@ -42,7 +39,6 @@ as you can see
4239

4340
**change-id:** Ic8aaa0728a43936cd4c6e1ed590e01ba8f0fbf5b
4441

45-
4642
## 0.0.1 (2022-01-01)
4743

4844
### New

test/test_log_linearity.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ def test_tags_on_multiple_branches(bare_git_repo, capsys):
8080
ver003 = context[2]
8181
ver002 = context[3]
8282
ver001 = context[4]
83-
assert len(unreleased.sections) == 0
84-
assert len(ver004.sections[0].commits) == 3
85-
assert ver004.sections[0].commits[0].summary == "Commit 5 on master."
86-
assert ver004.sections[0].commits[1].summary == "Commit 3 on develop."
87-
assert ver004.sections[0].commits[2].summary == "Commit 2 on develop."
88-
assert len(ver003.sections[0].commits) == 1
89-
assert ver003.sections[0].commits[0].summary == "Commit 4 on master."
90-
assert len(ver002.sections[0].commits) == 1
91-
assert ver002.sections[0].commits[0].summary == "Commit 2 on develop."
92-
assert len(ver001.sections[0].commits) == 1
93-
assert ver001.sections[0].commits[0].summary == "Commit 1 on master."
83+
assert len(unreleased.grouped_commits) == 0
84+
assert len(ver004.grouped_commits[0].commits) == 3
85+
assert ver004.grouped_commits[0].commits[0].summary == "Commit 5 on master."
86+
assert ver004.grouped_commits[0].commits[1].summary == "Commit 3 on develop."
87+
assert ver004.grouped_commits[0].commits[2].summary == "Commit 2 on develop."
88+
assert len(ver003.grouped_commits[0].commits) == 1
89+
assert ver003.grouped_commits[0].commits[0].summary == "Commit 4 on master."
90+
assert len(ver002.grouped_commits[0].commits) == 1
91+
assert ver002.grouped_commits[0].commits[0].summary == "Commit 2 on develop."
92+
assert len(ver001.grouped_commits[0].commits) == 1
93+
assert ver001.grouped_commits[0].commits[0].summary == "Commit 1 on master."

test/test_templating.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
from test import conftest
66

77
import pytest
8+
from conftest import commit_factory
89
from faker import Faker
910
from pytest import param
1011

1112
from generate_changelog import configuration, templating
12-
from generate_changelog.configuration import DEFAULT_SECTION_PATTERNS, get_config
13+
from generate_changelog.configuration import DEFAULT_COMMIT_CLASSIFIERS, get_config
1314
from generate_changelog.context import CommitContext
1415

1516
FIXTURES_DIR = Path(__file__).parent / "fixtures"
@@ -19,22 +20,22 @@
1920
@pytest.mark.parametrize(
2021
["string", "expected"],
2122
(
22-
param("Newly added something", "New"),
23-
param("added something new", "New"),
24-
param("changed something new", "Updates"),
25-
param("Updates something old", "Updates"),
26-
param("renamed something I didn't like.", "Updates"),
27-
param("Removed something old", "Updates"),
28-
param("deletes something old", "Updates"),
29-
param("improved something old", "Updates"),
30-
param("refactored something old", "Updates"),
31-
param("fixed an error or something", "Fixes"),
32-
param("I don't know what this does", "Other"),
23+
param(commit_factory(summary="Newly added something"), "New"),
24+
param(commit_factory(summary="added something new"), "New"),
25+
param(commit_factory(summary="changed something new"), "Updates"),
26+
param(commit_factory(summary="Updates something old"), "Updates"),
27+
param(commit_factory(summary="renamed something I didn't like."), "Updates"),
28+
param(commit_factory(summary="Removed something old"), "Updates"),
29+
param(commit_factory(summary="deletes something old"), "Updates"),
30+
param(commit_factory(summary="improved something old"), "Updates"),
31+
param(commit_factory(summary="refactored something old"), "Updates"),
32+
param(commit_factory(summary="fixed an error or something"), "Fixes"),
33+
param(commit_factory(summary="I don't know what this does"), "Other"),
3334
),
3435
)
3536
def test_first_matching(string, expected):
3637
"""The first matching function should properly categorize a string."""
37-
assert templating.first_matching(DEFAULT_SECTION_PATTERNS, string) == expected
38+
assert templating.first_matching(DEFAULT_COMMIT_CLASSIFIERS, string) == expected
3839

3940

4041
def test_commit_context():
@@ -45,6 +46,7 @@ def test_commit_context():
4546
commit_datetime=commit.committed_datetime,
4647
committer=f"{commit.committer.name} <{commit.committer.email}>",
4748
summary=commit.summary,
49+
grouping=("a", "b"),
4850
body="\n".join(commit.message.splitlines()[1:]),
4951
)
5052
assert commit.hexsha[:7] == context.short_sha
@@ -62,6 +64,7 @@ def test_commit_with_no_email():
6264
commit_datetime=commit.committed_datetime,
6365
committer=f"{commit.committer.name} <{commit.committer.email}>",
6466
summary=commit.summary,
67+
grouping=(None,),
6568
body="\n".join(commit.message.splitlines()[1:]),
6669
metadata={"trailers": {"co-authored-by": [name_only]}},
6770
)
@@ -77,10 +80,10 @@ def test_get_context_from_tags(default_repo):
7780
v = context[0]
7881
assert v.label == get_config().unreleased_label
7982
assert v.date_time.date() == datetime.date(2022, 1, 6)
80-
assert len(v.sections) == 1
81-
assert v.sections[0].label == "Updates"
82-
assert len(v.sections[0].commits) == 1
83-
assert v.sections[0].commits[0].metadata["trailers"]["co-committed-by"] == [
83+
assert len(v.grouped_commits) == 1
84+
assert v.grouped_commits[0].grouping == ("Updates",)
85+
assert len(v.grouped_commits[0].commits) == 1
86+
assert v.grouped_commits[0].commits[0].metadata["trailers"]["co-committed-by"] == [
8487
"Juliet <juliet@example.com>",
8588
"Charly <charly@example.com>",
8689
]
@@ -89,11 +92,11 @@ def test_get_context_from_tags(default_repo):
8992
assert v.label == "0.0.3"
9093
assert v.previous_tag == "0.0.2"
9194
assert v.date_time.date() == datetime.date(2022, 1, 5)
92-
assert len(v.sections) == 1
93-
assert v.sections[0].label == "New"
94-
assert len(v.sections[0].commits) == 2
95-
assert len(v.sections[0].commits[0].metadata["trailers"]) == 5
96-
assert len(v.sections[0].commits[1].metadata["trailers"]) == 0
95+
assert len(v.grouped_commits) == 1
96+
assert v.grouped_commits[0].grouping == ("New",)
97+
assert len(v.grouped_commits[0].commits) == 2
98+
assert len(v.grouped_commits[0].commits[0].metadata["trailers"]) == 5
99+
assert len(v.grouped_commits[0].commits[1].metadata["trailers"]) == 0
97100

98101

99102
def test_render(default_repo, capsys):

0 commit comments

Comments
 (0)