Skip to content

Commit b945b39

Browse files
authored
Merge pull request #7577 from nicoddemus/backport-7427
[6.0.x] Fix --help crash on add_ini(.., help='') and improve message on help=None (#7427)
2 parents e876157 + 8963644 commit b945b39

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

changelog/7394.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Passing an empty ``help`` value to ``Parser.add_option`` is now accepted instead of crashing when running ``pytest --help``.
2+
Passing ``None`` raises a more informative ``TypeError``.

src/_pytest/helpconfig.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ def showhelp(config: Config) -> None:
170170
help, type, default = config._parser._inidict[name]
171171
if type is None:
172172
type = "string"
173+
if help is None:
174+
raise TypeError("help argument cannot be None for {}".format(name))
173175
spec = "{} ({}):".format(name, type)
174176
tw.write(" %s" % spec)
175177
spec_len = len(spec)
@@ -191,9 +193,10 @@ def showhelp(config: Config) -> None:
191193
tw.write(" " * (indent_len - spec_len - 2))
192194
wrapped = textwrap.wrap(help, columns - indent_len, break_on_hyphens=False)
193195

194-
tw.line(wrapped[0])
195-
for line in wrapped[1:]:
196-
tw.line(indent + line)
196+
if wrapped:
197+
tw.line(wrapped[0])
198+
for line in wrapped[1:]:
199+
tw.line(indent + line)
197200

198201
tw.line()
199202
tw.line("environment variables:")

testing/test_helpconfig.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,41 @@ def test_help(testdir):
3838
)
3939

4040

41+
def test_none_help_param_raises_exception(testdir):
42+
"""Tests a None help param raises a TypeError.
43+
"""
44+
testdir.makeconftest(
45+
"""
46+
def pytest_addoption(parser):
47+
parser.addini("test_ini", None, default=True, type="bool")
48+
"""
49+
)
50+
result = testdir.runpytest("--help")
51+
result.stderr.fnmatch_lines(
52+
["*TypeError: help argument cannot be None for test_ini*"]
53+
)
54+
55+
56+
def test_empty_help_param(testdir):
57+
"""Tests an empty help param is displayed correctly.
58+
"""
59+
testdir.makeconftest(
60+
"""
61+
def pytest_addoption(parser):
62+
parser.addini("test_ini", "", default=True, type="bool")
63+
"""
64+
)
65+
result = testdir.runpytest("--help")
66+
assert result.ret == 0
67+
lines = [
68+
" required_plugins (args):",
69+
" plugins that must be present for pytest to run*",
70+
" test_ini (bool):*",
71+
"environment variables:",
72+
]
73+
result.stdout.fnmatch_lines(lines, consecutive=True)
74+
75+
4176
def test_hookvalidation_unknown(testdir):
4277
testdir.makeconftest(
4378
"""

0 commit comments

Comments
 (0)