Skip to content
Open
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
31 changes: 31 additions & 0 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,11 @@ def convert_arg_line_to_args(self, arg_line: str) -> list[str]:
# Load config files and look for ``codespell`` options.
cfg_files = ["setup.cfg", ".codespellrc"]
if options.config:
if not os.path.isfile(options.config):
raise _FileUsageError(
parser,
f"ERROR: cannot find config file: {options.config}",
)
cfg_files.append(options.config)
config = configparser.ConfigParser(interpolation=None)

Expand All @@ -743,6 +748,11 @@ def convert_arg_line_to_args(self, arg_line: str) -> list[str]:
if os.path.isfile("pyproject.toml"):
toml_files.append("pyproject.toml")
if options.toml:
if not os.path.isfile(options.toml):
raise _FileUsageError(
parser,
f"ERROR: cannot find toml file: {options.toml}",
)
toml_files.append(options.toml)
tomllib_raise_error = True
if toml_files:
Expand Down Expand Up @@ -1421,6 +1431,20 @@ def _script_main() -> int:
return 130


class _FileUsageError(Exception):
"""A file argument given on the command line cannot be used.

Raised from ``parse_options``, where the config files are read, so that
``main`` can print the usage and return ``EX_USAGE`` like it does for the
other command-line validation errors.
"""

def __init__(self, parser: argparse.ArgumentParser, message: str) -> None:
self.parser = parser
self.message = message
super().__init__(message)


def _usage_error(parser: argparse.ArgumentParser, message: str) -> int:
parser.print_usage()
print(message, file=sys.stderr)
Expand Down Expand Up @@ -1452,6 +1476,8 @@ def main(*args: str) -> int:
"""Contains flow control"""
try:
options, parser, used_cfg_files = parse_options(args)
except _FileUsageError as e:
return _usage_error(e.parser, e.message)
except configparser.Error as e:
print(
f"ERROR: ill-formed config file: {e.message}",
Expand Down Expand Up @@ -1594,6 +1620,11 @@ def main(*args: str) -> int:
if options.exclude_file:
exclude_files = flatten_clean_comma_separated_arguments(options.exclude_file)
for exclude_file in exclude_files:
if not os.path.isfile(exclude_file):
return _usage_error(
parser,
f"ERROR: cannot find exclude-file: {exclude_file}",
)
build_exclude_hashes(exclude_file, exclude_lines)

file_opener = FileOpener(
Expand Down
15 changes: 15 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ def test_basic(
code, _, stderr = result
assert code == EX_USAGE # bad type
assert "Unknown builtin dictionary" in stderr
result = cs.main(fname, "--config", tmp_path / "does-not-exist", std=True)
assert isinstance(result, tuple)
code, _, stderr = result
assert code == EX_USAGE
assert "cannot find config file" in stderr
result = cs.main(fname, "--toml", tmp_path / "does-not-exist", std=True)
assert isinstance(result, tuple)
code, _, stderr = result
assert code == EX_USAGE
assert "cannot find toml file" in stderr
result = cs.main(fname, "--exclude-file", tmp_path / "does-not-exist", std=True)
assert isinstance(result, tuple)
code, _, stderr = result
assert code == EX_USAGE
assert "cannot find exclude-file" in stderr
result = cs.main(fname, "-D", tmp_path / "foo", std=True)
assert isinstance(result, tuple)
code, _, stderr = result
Expand Down