Skip to content

Commit

Permalink
Merge pull request #7577 from nicoddemus/backport-7427
Browse files Browse the repository at this point in the history
[6.0.x] Fix --help crash on add_ini(.., help='') and improve message on help=None (#7427)
  • Loading branch information
nicoddemus committed Jul 29, 2020
2 parents e876157 + 8963644 commit b945b39
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelog/7394.bugfix.rst
@@ -0,0 +1,2 @@
Passing an empty ``help`` value to ``Parser.add_option`` is now accepted instead of crashing when running ``pytest --help``.
Passing ``None`` raises a more informative ``TypeError``.
9 changes: 6 additions & 3 deletions src/_pytest/helpconfig.py
Expand Up @@ -170,6 +170,8 @@ def showhelp(config: Config) -> None:
help, type, default = config._parser._inidict[name]
if type is None:
type = "string"
if help is None:
raise TypeError("help argument cannot be None for {}".format(name))
spec = "{} ({}):".format(name, type)
tw.write(" %s" % spec)
spec_len = len(spec)
Expand All @@ -191,9 +193,10 @@ def showhelp(config: Config) -> None:
tw.write(" " * (indent_len - spec_len - 2))
wrapped = textwrap.wrap(help, columns - indent_len, break_on_hyphens=False)

tw.line(wrapped[0])
for line in wrapped[1:]:
tw.line(indent + line)
if wrapped:
tw.line(wrapped[0])
for line in wrapped[1:]:
tw.line(indent + line)

tw.line()
tw.line("environment variables:")
Expand Down
35 changes: 35 additions & 0 deletions testing/test_helpconfig.py
Expand Up @@ -38,6 +38,41 @@ def test_help(testdir):
)


def test_none_help_param_raises_exception(testdir):
"""Tests a None help param raises a TypeError.
"""
testdir.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("test_ini", None, default=True, type="bool")
"""
)
result = testdir.runpytest("--help")
result.stderr.fnmatch_lines(
["*TypeError: help argument cannot be None for test_ini*"]
)


def test_empty_help_param(testdir):
"""Tests an empty help param is displayed correctly.
"""
testdir.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("test_ini", "", default=True, type="bool")
"""
)
result = testdir.runpytest("--help")
assert result.ret == 0
lines = [
" required_plugins (args):",
" plugins that must be present for pytest to run*",
" test_ini (bool):*",
"environment variables:",
]
result.stdout.fnmatch_lines(lines, consecutive=True)


def test_hookvalidation_unknown(testdir):
testdir.makeconftest(
"""
Expand Down

0 comments on commit b945b39

Please sign in to comment.