From b1f95247e40594c052c86a9c093a043e4fd96916 Mon Sep 17 00:00:00 2001 From: ferkans-amir Date: Tue, 28 Jul 2026 10:22:39 +0200 Subject: [PATCH 1/3] fix(config): raise UsageError for invalid or missing -c/--config-file paths --- src/_pytest/config/findpaths.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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: From 98342841be34c12557a9da2355bc90cc1a31e09e Mon Sep 17 00:00:00 2001 From: ferkans-amir Date: Tue, 28 Jul 2026 15:27:47 +0200 Subject: [PATCH 2/3] changelog: add newsfragment for #14793 --- changelog/14793.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/14793.bugfix.rst diff --git a/changelog/14793.bugfix.rst b/changelog/14793.bugfix.rst new file mode 100644 index 00000000000..2ee2a951c6c --- /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 unparseable configurations. From bfb8479b3d87a58418dd43ba3229494f851cfc0b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 28 Jul 2026 13:28:23 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- changelog/14793.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/14793.bugfix.rst b/changelog/14793.bugfix.rst index 2ee2a951c6c..96beec94b8a 100644 --- a/changelog/14793.bugfix.rst +++ b/changelog/14793.bugfix.rst @@ -1 +1 @@ -Raise a clean :class:`~_pytest.config.UsageError` when explicit ``-c`` or ``--config-file`` paths do not exist or have unparseable configurations. +Raise a clean :class:`~_pytest.config.UsageError` when explicit ``-c`` or ``--config-file`` paths do not exist or have unparsable configurations.