Skip to content

Commit 83a728f

Browse files
committed
Added accessor for current configuration.
- `get_config()` will instantiate a new configuration or return the existing configuration.
1 parent 70a7495 commit 83a728f

6 files changed

Lines changed: 29 additions & 18 deletions

File tree

generate_changelog/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import typer
77
from git import Repo
88

9-
from generate_changelog.configuration import DEFAULT_CONFIG_FILE_NAMES, get_default_config, write_default_config
9+
from generate_changelog.configuration import DEFAULT_CONFIG_FILE_NAMES, write_default_config
1010

1111
app = typer.Typer()
1212

@@ -46,10 +46,11 @@ def main(
4646
):
4747
"""Generate a change log from git commits."""
4848
from generate_changelog import templating
49+
from generate_changelog.configuration import get_config
4950
from generate_changelog.pipeline import pipeline_factory
5051

5152
# Load default configuration
52-
config = get_default_config()
53+
config = get_config()
5354

5455
if user_config := config_file or next(
5556
(Path.cwd() / Path(name) for name in DEFAULT_CONFIG_FILE_NAMES if (Path.cwd() / Path(name)).exists()), None

generate_changelog/configuration.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Configuration setup for CLGen."""
1+
"""Configuration for generate_changelog."""
22
from typing import Callable, Optional, Union
33

44
from dataclasses import asdict, dataclass, field
@@ -98,7 +98,7 @@
9898

9999
@dataclass
100100
class Configuration:
101-
"""Configuration for CLGen."""
101+
"""Configuration for generate_changelog."""
102102

103103
variables: dict = field(default_factory=dict)
104104
"""User variables for reference in other parts of the configuration."""
@@ -171,6 +171,7 @@ def get_default_config() -> Configuration:
171171
subject_pipeline=DEFAULT_SUBJECT_PIPELINE,
172172
starting_tag_pipeline=DEFAULT_STARTING_TAG_PIPELINE,
173173
output_pipeline=DEFAULT_OUTPUT_PIPELINE,
174+
valid_author_tokens=VALID_AUTHOR_TOKENS,
174175
)
175176

176177

@@ -182,4 +183,13 @@ def write_default_config(filename: Path):
182183
yaml.safe_dump(config, f, sort_keys=False)
183184

184185

185-
CONFIG = get_default_config()
186+
_CONFIG = None
187+
188+
189+
def get_config() -> Configuration:
190+
"""Return the current configuration."""
191+
global _CONFIG
192+
193+
if _CONFIG is None:
194+
_CONFIG = get_default_config()
195+
return _CONFIG

generate_changelog/git_ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from git import Repo
1111

12-
from generate_changelog.configuration import CONFIG
12+
from generate_changelog.configuration import get_config
1313

1414
GIT_FORMAT_KEYS = {
1515
"sha1": "%H",
@@ -76,7 +76,7 @@ def parse_commits(repository: Repo, starting_rev: Optional[str] = None, ending_r
7676

7777
log_opts = ["-z", "--topo-order", "--pretty=tformat:%H"]
7878

79-
if CONFIG.include_merges:
79+
if get_config().include_merges:
8080
log_opts.append("--no-merges")
8181

8282
log_opts.append(revs)

test/test_configuration.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
def test_update_from_file():
1313
"""A configuration file should update the default configuration."""
1414
config_file_path = FIXTURES_DIR / "sample_config.yml"
15-
configuration.CONFIG.update_from_file(config_file_path)
16-
assert configuration.CONFIG.unreleased_label == "Not Done Yet"
17-
assert not hasattr(configuration.CONFIG, "not_valid")
15+
configuration.get_config().update_from_file(config_file_path)
16+
assert configuration.get_config().unreleased_label == "Not Done Yet"
17+
assert not hasattr(configuration.get_config(), "not_valid")
1818

1919
with pytest.raises(click.exceptions.Exit):
20-
configuration.CONFIG.update_from_file(FIXTURES_DIR / "missing.yml")
20+
configuration.get_config().update_from_file(FIXTURES_DIR / "missing.yml")
2121

2222
with pytest.raises(click.exceptions.Exit):
23-
configuration.CONFIG.update_from_file(FIXTURES_DIR)
23+
configuration.get_config().update_from_file(FIXTURES_DIR)
2424

2525

2626
def test_write_default_config(tmp_path):

test/test_git_ops.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pytest import param
55

66
from generate_changelog import git_ops
7-
from generate_changelog.configuration import CONFIG
7+
from generate_changelog.configuration import get_config
88

99

1010
@pytest.mark.parametrize(
@@ -37,7 +37,7 @@ def test_get_tags(default_repo):
3737

3838
def test_get_commits_by_all_tags(default_repo):
3939
"""Commits should be grouped by tags and filtered."""
40-
grouping = git_ops.get_commits_by_tags(default_repo, CONFIG.tag_pattern)
40+
grouping = git_ops.get_commits_by_tags(default_repo, get_config().tag_pattern)
4141

4242
assert len(grouping) == 4
4343

@@ -49,7 +49,7 @@ def test_get_commits_by_all_tags(default_repo):
4949

5050
def test_get_commits_since_tag(default_repo):
5151
"""Commits should be grouped by tags and filtered since tag."""
52-
grouping = git_ops.get_commits_by_tags(default_repo, CONFIG.tag_pattern, "0.0.2")
52+
grouping = git_ops.get_commits_by_tags(default_repo, get_config().tag_pattern, "0.0.2")
5353

5454
assert len(grouping) == 2
5555

test/test_templating.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pytest import param
1010

1111
from generate_changelog import configuration, templating
12-
from generate_changelog.configuration import CONFIG, DEFAULT_SECTION_PATTERNS
12+
from generate_changelog.configuration import DEFAULT_SECTION_PATTERNS, get_config
1313

1414
FIXTURES_DIR = Path(__file__).parent / "fixtures"
1515
fake = Faker()
@@ -70,11 +70,11 @@ def test_commit_with_no_email():
7070

7171
def test_get_context_from_tags(default_repo):
7272
"""Get context from tags should return gits correctly filtered."""
73-
context = templating.get_context_from_tags(default_repo, CONFIG)
73+
context = templating.get_context_from_tags(default_repo, get_config())
7474
assert len(context) == 4
7575

7676
v = context[0]
77-
assert v.label == CONFIG.unreleased_label
77+
assert v.label == get_config().unreleased_label
7878
assert v.date_time.date() == datetime.date(2022, 1, 6)
7979
assert len(v.sections) == 1
8080
assert v.sections[0].label == "Updates"

0 commit comments

Comments
 (0)