diff --git a/changelog/14793.bugfix.rst b/changelog/14793.bugfix.rst new file mode 100644 index 00000000000..96beec94b8a --- /dev/null +++ b/changelog/14793.bugfix.rst @@ -0,0 +1 @@ +Raise a clean :class:`~_pytest.config.UsageError` when explicit ``-c`` or ``--config-file`` paths do not exist or have unparsable configurations. diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index 2a4bed319a9..d9c336dfc2b 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -44,14 +44,13 @@ class ConfigValue: def _parse_ini_config(path: Path) -> iniconfig.IniConfig: - """Parse the given generic '.ini' file using legacy IniConfig parser, returning - the parsed object. + """Parse an ini file. Raise UsageError if the file cannot be parsed. """ try: return iniconfig.IniConfig(str(path)) - except iniconfig.ParseError as exc: + except (FileNotFoundError, iniconfig.ParseError) as exc: raise UsageError(str(exc)) from exc @@ -313,8 +312,12 @@ def determine_setup( if inifile: inipath_ = absolutepath(inifile) + if not inipath_.is_file(): + raise UsageError(f"Config file not found: {inipath_}") inipath: Path | None = inipath_ - inicfg = load_config_dict_from_file(inipath_) or {} + inicfg = load_config_dict_from_file(inipath_) + if inicfg is None: + raise UsageError(f"Unrecognized or invalid config file: {inipath_}") if rootdir_cmd_arg is None: rootdir = inipath_.parent else: