Skip to content

report file arguments that cannot be used - #4070

Open
qianeric-backup wants to merge 2 commits into
codespell-project:mainfrom
qianeric-backup:fix/report-missing-file-arguments
Open

qianeric-backup wants to merge 2 commits into
codespell-project:mainfrom
qianeric-backup:fix/report-missing-file-arguments

Conversation

@qianeric-backup

Copy link
Copy Markdown

Three options take a path, and each one mishandled a path that does not exist.

--config and --toml

configparser.read() silently ignores missing files, so a typo in the CI job silently ran codespell with no configuration at all:

$ codespell --config .codespellrc src/
src/foo.py:1: teh ==> the
$ echo $?
65

--toml crashed instead, because that file is opened directly:

$ codespell --toml pyproject.toml src/
Traceback (most recent call last):
  File ".../codespell_lib/_codespell.py", line 760, in parse_options
    with open(toml_file, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'pyproject.toml'

--exclude-file

Unlike --ignore-words, it has no existence check, so it ends in the same traceback:

$ codespell --exclude-file .codespell-exclude src/
Traceback (most recent call last):
  File ".../codespell_lib/_codespell.py", line 1597, in main
    build_exclude_hashes(exclude_file, exclude_lines)
FileNotFoundError: [Errno 2] No such file or directory: '.codespell-exclude'

--ignore-words already answers this case properly:

            if not os.path.isfile(ignore_words_file):
                return _usage_error(
                    parser,
                    f"ERROR: cannot find ignore-words file: {ignore_words_file}",
                )

Fix

parse_options() raises the new _FileUsageError, and main() turns it into the usual usage error, so all three behave like --ignore-words:

$ codespell --config /nope.toml src/ ; echo $?
usage: codespell ...
ERROR: cannot find config file: /nope.toml
64
case before after
--config <missing> exit 65, runs unconfigured ERROR: cannot find config file: ..., exit 64
--toml <missing> traceback, exit 1 ERROR: cannot find toml file: ..., exit 64
--exclude-file <missing> traceback, exit 1 ERROR: cannot find exclude-file: ..., exit 64
--config/--toml/--exclude-file valid works unchanged
auto-detected setup.cfg/.codespellrc/pyproject.toml missing ignored unchanged

The _FileUsageError type is needed because the three checks live in parse_options(), which returns (options, parser, used_cfg_files) and cannot return an exit code; _usage_error() stays as it is and is reused unchanged.

Tests

test_basic gains assertions for the three cases; they fail on the current tree (assert 0 == 64 for the silent --config case, tracebacks for the other two). pytest codespell_lib/tests/ gives 132 passed / 19 failed on this machine both before and after the change — the 19 failures are pre-existing and unrelated (interactive-prompt and bitmask-option tests that need a TTY / pytest-subtests setup this sandbox does not provide).

Three options take a path, and each one mishandled a path that does not exist:

* `--config`, `--toml`: `configparser.read()` silently ignores missing files, so
  a typo in the CI job silently ran codespell with no configuration at all:

      $ codespell --config .codespellrc src/
      src/foo.py:1: teh ==> the
      $ echo $?
      65

  `--toml` crashed instead, because the file is opened directly:

      $ codespell --toml pyproject.toml src/
      Traceback (most recent call last):
        File ".../codespell_lib/_codespell.py", line 760, in parse_options
          with open(toml_file, "rb") as f:
      FileNotFoundError: [Errno 2] No such file or directory: 'pyproject.toml'

* `--exclude-file`: unlike `--ignore-words`, it has no existence check, so it
  ended in the same traceback:

      $ codespell --exclude-file .codespell-exclude src/
      Traceback (most recent call last):
        File ".../codespell_lib/_codespell.py", line 1597, in main
          build_exclude_hashes(exclude_file, exclude_lines)
      FileNotFoundError: [Errno 2] No such file or directory: '.codespell-exclude'

  `--ignore-words` already answers this case properly:

      if not os.path.isfile(ignore_words_file):
          return _usage_error(
              parser,
              f"ERROR: cannot find ignore-words file: {ignore_words_file}",
          )

## Fix

`parse_options()` raises the new `_FileUsageError`, and `main()` turns it into
the usual usage error, so all three behave like `--ignore-words`:

```
$ codespell --config /nope.toml src/ ; echo $?
usage: codespell ...
ERROR: cannot find config file: /nope.toml
64
```

| case | before | after |
|---|---|---|
| `--config <missing>` | exit 65, runs unconfigured | `ERROR: cannot find config file: ...`, exit 64 |
| `--toml <missing>` | traceback, exit 1 | `ERROR: cannot find toml file: ...`, exit 64 |
| `--exclude-file <missing>` | traceback, exit 1 | `ERROR: cannot find exclude-file: ...`, exit 64 |
| `--config`/`--toml`/`--exclude-file` valid | works | unchanged |
| auto-detected `setup.cfg`/`.codespellrc`/`pyproject.toml` missing | ignored | unchanged |

The `_FileUsageError` type is needed because the three checks live in
`parse_options()`, which returns `(options, parser, used_cfg_files)` and cannot
return an exit code; `_usage_error()` stays as it is and is reused unchanged.

## Tests

`test_basic` gains assertions for the three cases; they fail on the current tree
(`assert 0 == 64` for the silent `--config` case, tracebacks for the other two).
`pytest codespell_lib/tests/` gives 132 passed / 19 failed on this machine both
before and after the change -- the 19 failures are pre-existing and unrelated
(interactive-prompt and bitmask-option tests that need a TTY/`pytest-subtests`
setup this sandbox does not provide).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant