Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
EX_OK = 0
EX_USAGE = 64
EX_DATAERR = 65
EX_CONFIG = 78

# OPTIONS:
#
Expand Down Expand Up @@ -586,7 +587,7 @@ def parse_options(
used_cfg_files.append(cfg_file)

# Use config files
config.read(cfg_files)
config.read(used_cfg_files)
if config.has_section("codespell"):
# Build a "fake" argv list using option name and value.
cfg_args = []
Expand Down Expand Up @@ -1021,7 +1022,14 @@ def _script_main() -> int:

def main(*args: str) -> int:
"""Contains flow control"""
options, parser, used_cfg_files = parse_options(args)
try:
options, parser, used_cfg_files = parse_options(args)
except configparser.Error as e:
print(
f"ERROR: ill-formed config file: {e.message}",
file=sys.stderr,
)
return EX_CONFIG
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will mean you can't ever use Codespell if your setup.cfg has an invalid config for example (as you can't override just append these):

if options.config:
cfg_files.append(options.config)

Not sure what the best solution is, but thought it's worth flagging before we merge...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe warn if it's the root-level setup.cfg and error otherwise?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was already the case, with an exception raised. Nobody has complained.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was already the case, with an exception raised. Nobody has complained.

Oops, I didn't think of that! 🤦


# Report used config files
if not options.quiet_level & QuietLevels.CONFIG_FILES:
Expand Down
34 changes: 31 additions & 3 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@
import pytest

import codespell_lib as cs_
from codespell_lib._codespell import EX_DATAERR, EX_OK, EX_USAGE, uri_regex_def
from codespell_lib._codespell import (
EX_CONFIG,
EX_DATAERR,
EX_OK,
EX_USAGE,
uri_regex_def,
)


def test_constants() -> None:
"""Test our EX constants."""
assert EX_OK == 0
assert EX_USAGE == 64
assert EX_DATAERR == 65
assert EX_CONFIG == 78


class MainWrapper:
Expand All @@ -42,7 +49,7 @@ def main(
assert frame is not None
capsys = frame.f_locals["capsys"]
stdout, stderr = capsys.readouterr()
assert code in (EX_OK, EX_USAGE, EX_DATAERR)
assert code in (EX_OK, EX_USAGE, EX_DATAERR, EX_CONFIG)
if code == EX_DATAERR: # have some misspellings
code = int(stderr.split("\n")[-2])
elif code == EX_OK and count:
Expand Down Expand Up @@ -1028,7 +1035,7 @@ def test_uri_regex_def() -> None:
assert not uri_regex.findall(boilerplate % uri), uri


def test_quiet_option_32(
def test_quiet_level_32(
tmp_path: Path,
tmpdir: pytest.TempPathFactory,
capsys: pytest.CaptureFixture[str],
Expand Down Expand Up @@ -1057,6 +1064,27 @@ def test_quiet_option_32(
assert "setup.cfg" in stdout


def test_ill_formed_ini_config_file(
tmp_path: Path,
tmpdir: pytest.TempPathFactory,
capsys: pytest.CaptureFixture[str],
) -> None:
d = tmp_path / "files"
d.mkdir()
conf = str(tmp_path / "setup.cfg")
with open(conf, "w") as f:
# It should contain but lacks a section.
f.write("foobar =\n")
args = ("--config", conf)

# Should not raise a configparser.Error exception.
result = cs.main(str(d), *args, std=True)
assert isinstance(result, tuple)
code, _, stderr = result
assert code == 78
assert "ill-formed config file" in stderr


@pytest.mark.parametrize("kind", ("toml", "cfg"))
def test_config_toml(
tmp_path: Path,
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,6 @@ max-complexity = 45
[tool.ruff.pylint]
allow-magic-value-types = ["bytes", "int", "str",]
max-args = 12
max-branches = 48
max-returns = 10
max-branches = 49
max-returns = 11
max-statements = 111