Skip to content

Commit

Permalink
Modify structure of dict read from TOML to match dict read from INI (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriPapadopoulos committed Aug 31, 2023
1 parent 861f87a commit 5536558
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 8 additions & 1 deletion codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import re
import sys
import textwrap
from typing import Dict, List, Match, Optional, Pattern, Sequence, Set, Tuple
from typing import Any, Dict, List, Match, Optional, Pattern, Sequence, Set, Tuple

# autogenerated by setuptools_scm
from ._version import __version__ as VERSION # type: ignore # noqa: N812
Expand Down Expand Up @@ -301,6 +301,11 @@ def _split_lines(self, text: str, width: int) -> List[str]:
return out


def _toml_to_parseconfig(toml_dict: Dict[str, Any]) -> Dict[str, Any]:
"""Convert a dict read from a TOML file to the parseconfig.read_dict() format."""
return {k: "" if v is True else v for k, v in toml_dict.items() if v is not False}


def parse_options(
args: Sequence[str],
) -> Tuple[argparse.Namespace, argparse.ArgumentParser, List[str]]:
Expand Down Expand Up @@ -566,6 +571,8 @@ def parse_options(
for toml_file in toml_files:
with open(toml_file, "rb") as f:
data = tomllib.load(f).get("tool", {})
if "codespell" in data:
data["codespell"] = _toml_to_parseconfig(data["codespell"])
config.read_dict(data)

# Collect which config files are going to be used
Expand Down
17 changes: 11 additions & 6 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,14 +1068,16 @@ def test_config_toml(
d.mkdir()
(d / "bad.txt").write_text("abandonned donn\n")
(d / "good.txt").write_text("good")
(d / "abandonned.txt").write_text("")

# Should fail when checking both.
result = cs.main(d, count=True, std=True)
# Should fail when checking all files.
result = cs.main(d, "--check-filenames", count=True, std=True)
assert isinstance(result, tuple)
code, stdout, _ = result
# Code in this case is not exit code, but count of misspellings.
assert code == 2
assert code == 3
assert "bad.txt" in stdout
assert "abandonned.txt" in stdout

if kind == "cfg":
conffile = tmp_path / "setup.cfg"
Expand All @@ -1097,16 +1099,18 @@ def test_config_toml(
"""\
[tool.codespell]
skip = 'bad.txt,whatever.txt'
count = false
check-filenames = false
count = true
"""
)

# Should pass when skipping bad.txt
result = cs.main(d, *args, count=True, std=True)
# Should pass when skipping bad.txt or abandonned.txt
result = cs.main(d, *args, std=True)
assert isinstance(result, tuple)
code, stdout, _ = result
assert code == 0
assert "bad.txt" not in stdout
assert "abandonned.txt" not in stdout

# And both should automatically work if they're in cwd
cwd = Path.cwd()
Expand All @@ -1119,6 +1123,7 @@ def test_config_toml(
os.chdir(cwd)
assert code == 0
assert "bad.txt" not in stdout
assert "abandonned.txt" not in stdout


@contextlib.contextmanager
Expand Down

0 comments on commit 5536558

Please sign in to comment.