Skip to content

Commit

Permalink
fix: do not interpret None in config files
Browse files Browse the repository at this point in the history
This commit addresses
[this comment (pawamoy#55)](pawamoy#55 (comment)).

Please note that this will disallow unsetting options if a hierarchy of
config files were to be implemented at some point.
  • Loading branch information
oesteban committed Aug 21, 2023
1 parent 7449e78 commit 798c1e8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
1 change: 0 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,6 @@ output = 'output.log'
parse-refs = false
parse-trailers = false
repository = '.'
sections = 'none'
template = 'angular'
version-regex = '^## \[(?P<version>v?[^\]]+)'
```
Expand Down
18 changes: 11 additions & 7 deletions src/git_changelog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def read_config(
or new_settings.get("tool.git-changelog", {})
)

if not new_settings: # Likely, pyproject.toml did not have a git-changelog section
if not new_settings: # pyproject.toml did not have a git-changelog section
continue

# Settings can have hyphens like in the CLI
Expand All @@ -348,14 +348,18 @@ def read_config(

# Massage found values to meet expectations
# Parse sections
if "sections" in new_settings and new_settings["sections"] is not None:
sections = new_settings["sections"]
if "sections" in new_settings:
# Remove "sections" from dict, only restore if the list is valid
sections = new_settings.pop("sections", None)
if isinstance(sections, str):
sections = [s.strip() for s in sections.split(",")]
sections = sections.split(",")

new_settings["sections"] = [
s.strip() for s in sections if s.strip() and s.strip() != "none"
] or None
sections = [
s.strip() for s in sections if isinstance(s, str) and s.strip()
]

if sections: # toml doesn't store null/nil
new_settings["sections"] = sections

project_config.update(new_settings)
break
Expand Down
8 changes: 5 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ def test_passing_repository_and_sections(tmp_path: Path, args: tuple[str]) -> No
@pytest.mark.parametrize("is_pyproject", [True, False, None])
@pytest.mark.parametrize(("sections", "sections_value"), [
(None, None),
("none", None),
("force-null", None),
("", None),
("none, none, none", None),
(",,", None),
("force-null", None),
("a, b, ", ["a", "b"]),
("a, , ", ["a"]),
("a, b, c", ["a", "b", "c"]),
(["a", "b", "c"], ["a", "b", "c"]),
# Uncomment if None/null is once allowed as a value
# ("none", None),
# ("none, none, none", None),
])
@pytest.mark.parametrize("parse_refs", [None, False, True])
def test_config_reading(
Expand Down

0 comments on commit 798c1e8

Please sign in to comment.