diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 332895ea7b..f5d9c7595d 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -731,6 +731,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) @@ -741,6 +746,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: @@ -1419,6 +1429,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) @@ -1450,6 +1474,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}", @@ -1592,6 +1618,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( diff --git a/pyproject.toml b/pyproject.toml index 55e7681de2..97f150d48d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -157,7 +157,7 @@ ignore = [ ] [tool.ruff.lint.mccabe] -max-complexity = 45 +max-complexity = 46 [tool.ruff.lint.per-file-ignores] "codespell_lib/_codespell.py" = ["A003"] @@ -168,5 +168,5 @@ max-complexity = 45 allow-magic-value-types = ["bytes", "int", "str",] max-args = 14 max-branches = 48 -max-returns = 12 +max-returns = 14 max-statements = 120 diff --git a/tests/test_basic.py b/tests/test_basic.py index 7ab1c8efc8..b0abdf9d5c 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -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