From 8c47db724c7965bb383ba1df9226f045d0b943ec Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 23 Jul 2019 15:24:22 +0200 Subject: [PATCH] Improve output when parsing an ini configuration fails --- changelog/5650.bugfix.rst | 1 + src/_pytest/config/findpaths.py | 6 +++++- testing/test_config.py | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 changelog/5650.bugfix.rst diff --git a/changelog/5650.bugfix.rst b/changelog/5650.bugfix.rst new file mode 100644 index 000000000..db57a40b9 --- /dev/null +++ b/changelog/5650.bugfix.rst @@ -0,0 +1 @@ +Improved output when parsing an ini configuration file fails. diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index fa2024470..3f91bbd07 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -32,7 +32,11 @@ def getcfg(args, config=None): for inibasename in inibasenames: p = base.join(inibasename) if exists(p): - iniconfig = py.iniconfig.IniConfig(p) + try: + iniconfig = py.iniconfig.IniConfig(p) + except py.iniconfig.ParseError as exc: + raise UsageError(str(exc)) + if ( inibasename == "setup.cfg" and "tool:pytest" in iniconfig.sections diff --git a/testing/test_config.py b/testing/test_config.py index ff993e401..cacf218d4 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -122,6 +122,14 @@ class TestParseIni: config = testdir.parseconfigure(sub) assert config.getini("minversion") == "2.0" + def test_ini_parse_error(self, testdir): + testdir.tmpdir.join("pytest.ini").write('addopts = -x') + result = testdir.runpytest() + assert result.ret != 0 + result.stderr.fnmatch_lines([ + "ERROR: *pytest.ini:1: no section header defined" + ]) + @pytest.mark.xfail(reason="probably not needed") def test_confcutdir(self, testdir): sub = testdir.mkdir("sub")