Skip to content

Commit

Permalink
Fix handling of empty help with addini
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Nov 2, 2020
1 parent 9c6d220 commit 8632435
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/_pytest/config/argparsing.py
Expand Up @@ -195,14 +195,15 @@ def parse_known_and_unknown_args(
def addini(
self,
name: str,
help: str,
help: "Optional[str]",
type: Optional["Literal['pathlist', 'args', 'linelist', 'bool', 'int']"] = None,
*args,
**kwargs
) -> None:
""" register an ini-file option.
:param str name: name of the ini-variable
:param help: help text to display (``None`` suppresses it).
:param str type: type of the variable, one of
``pathlist``, ``args``, ``linelist``, ``bool``, or ``int``.
:kwparam default: default value if no ini-file option exists but is queried.
Expand Down
10 changes: 6 additions & 4 deletions src/_pytest/helpconfig.py
Expand Up @@ -161,6 +161,8 @@ def showhelp(config: Config) -> None:
indent = " " * indent_len
for name in config._parser._ininames:
help, type, default = config._parser._inidict[name]
if help is None:
continue
if type is None:
type = "string"
spec = "{} ({}):".format(name, type)
Expand All @@ -183,10 +185,10 @@ def showhelp(config: Config) -> None:
# Display help starting after the spec, following lines indented.
print(" " * (indent_len - spec_len - 2), end="")
wrapped = textwrap.wrap(help, wrap_width, break_on_hyphens=False)

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

print()
print("environment variables:")
Expand Down
30 changes: 30 additions & 0 deletions testing/test_helpconfig.py
Expand Up @@ -50,6 +50,36 @@ def test_help(testdir: Testdir) -> None:
result.stdout.no_fnmatch_line("logging:")


def test_help_for_empty_ini_params(testdir: "Testdir") -> None:
testdir.makeconftest(
"""
from argparse import SUPPRESS
def pytest_addoption(parser):
parser.addini("t_ini_none", None, default=True, type="bool")
parser.addini("t_ini_empty", "", default=True, type="bool")
parser.addoption("--t_opt_none", help=None)
parser.addoption("--t_opt_empty", help="")
parser.addoption("--t_opt_suppress", help=SUPPRESS)
"""
)
result = testdir.runpytest("--help")
result.stdout.fnmatch_lines(
[
"custom options:",
" --t_opt_none=T_OPT_NONE",
" --t_opt_empty=T_OPT_EMPTY",
"",
"[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:",
" t_ini_empty (bool): ",
]
)
result_str = result.stdout.str()
assert "t_ini_none" not in result_str
assert "t_opt_suppress" not in result_str


@pytest.mark.parametrize("method", ("runpytest_inprocess", "runpytest_subprocess"))
def test_help_unconfigures_always(method: str, testdir: Testdir) -> None:
testdir.makeconftest(
Expand Down

0 comments on commit 8632435

Please sign in to comment.