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

Fix handling of empty help with addini #459

Merged
merged 1 commit into from Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/_pytest/config/argparsing.py
Expand Up @@ -44,7 +44,7 @@ def __init__(
self._groups = [] # type: List[OptionGroup]
self._processopt = processopt
self._usage = usage
self._inidict = {} # type: Dict[str, Tuple[str, Optional[str], Any]]
self._inidict = {} # type: Dict[str, Tuple[Optional[str], Optional[str], Any]]
self._ininames = [] # type: List[str]
self.extra_info = {} # type: Dict[str, Any]

Expand Down 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