diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 0290b92717..f6ed607060 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -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) @@ -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: @@ -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) @@ -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}", @@ -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( diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 7ab1c8efc8..b0abdf9d5c 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/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