Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display empty or None help parameter for parser option as empty string #7427

Merged
merged 10 commits into from Jul 29, 2020
13 changes: 10 additions & 3 deletions src/_pytest/helpconfig.py
Expand Up @@ -170,6 +170,9 @@ def showhelp(config: Config) -> None:
help, type, default = config._parser._inidict[name]
if type is None:
type = "string"
# Empty string parseable by textwrap whereas None isn't.
if help is None:
help = ""
spec = "{} ({}):".format(name, type)
tw.write(" %s" % spec)
spec_len = len(spec)
Expand All @@ -191,9 +194,13 @@ 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 a help string wasn't specified, just write an empty line.
hp310780 marked this conversation as resolved.
Show resolved Hide resolved
if not wrapped:
tw.write(" " * (indent_len - spec_len - 2))
hp310780 marked this conversation as resolved.
Show resolved Hide resolved
else:
tw.line(wrapped[0])
for line in wrapped[1:]:
tw.line(indent + line)

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


def test_empty_help_param(testdir):
hp310780 marked this conversation as resolved.
Show resolved Hide resolved
"""Tests 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
result.stdout.fnmatch_lines(["*test_ini*(bool):*"])


def test_none_help_param(testdir):
"""Tests help param None is displayed as empty string.
"""
testdir.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("test_ini", None, default=True, type="bool")
"""
)
result = testdir.runpytest("--help")
assert result.ret == 0
result.stdout.fnmatch_lines(["*test_ini*(bool):*"])
hp310780 marked this conversation as resolved.
Show resolved Hide resolved


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