pytest icon indicating copy to clipboard operation
pytest copied to clipboard

-c/--config-file: Validate missing configuration paths consistently

Open archiep867-sys opened this issue 1 month ago • 1 comments

Problem

The -c/--config-file option handles invalid paths inconsistently depending on the file extension.

For unrecognized extensions, pytest silently continues without validating that the specified file exists. For recognized extensions such as .ini, .cfg, and .toml, a missing file can instead raise an unhandled FileNotFoundError traceback.

Both cases should produce the same clean pytest usage error.

Expected Behavior

When the path passed to -c/--config-file does not exist, pytest should:

  • Raise a UsageError.
  • Report the missing path clearly.
  • Stop without running the test suite.
  • Behave consistently regardless of the file extension.

Proposed Fix

Validate the -c/--config-file path before dispatching based on its file extension.

Add regression tests covering:

  • A missing config file with a recognized extension.
  • A missing config file with an unrecognized extension.
  • Verification that both cases produce the expected usage error.

Add an appropriate changelog entry under changelog/.

archiep867-sys avatar Oct 07 '26 04:10 archiep867-sys

🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.

Thanks for the report. This duplicates #14716, which #14807 already fixed on main. The fix is not in 9.1.1 and ships with the next feature release (9.2).

Checked against both versions:

  • 9.1.1: -c nope.ini and -c nope.toml fail with a FileNotFoundError traceback, and -c nope.xyz runs the suite.
  • main: all three stop with ERROR: Config file '…' not found. Check your '-c/--config-file' option.

main also reports a directory, or an existing file in a format pytest has no loader for, as a usage error. See changelog/14716.breaking.rst.

Closing as a duplicate of #14716.


Generated by Claude Code

RonnyPfannschmidt avatar Oct 07 '26 14:10 RonnyPfannschmidt