Merge pull request #5056 from blueyed/argparsing-width
Inject width via pylib to argparse formatter
This commit is contained in:
commit
f05ca74d27
|
@ -0,0 +1 @@
|
|||
The HelpFormatter uses ``py.io.get_terminal_width`` for better width detection.
|
|
@ -405,6 +405,12 @@ class DropShorterLongHelpFormatter(argparse.HelpFormatter):
|
|||
- cache result on action object as this is called at least 2 times
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Use more accurate terminal width via pylib."""
|
||||
if "width" not in kwargs:
|
||||
kwargs["width"] = py.io.get_terminal_width()
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def _format_action_invocation(self, action):
|
||||
orgstr = argparse.HelpFormatter._format_action_invocation(self, action)
|
||||
if orgstr and orgstr[0] != "-": # only optional arguments
|
||||
|
|
|
@ -1194,6 +1194,21 @@ def test_help_and_version_after_argument_error(testdir):
|
|||
assert result.ret == ExitCode.USAGE_ERROR
|
||||
|
||||
|
||||
def test_help_formatter_uses_py_get_terminal_width(testdir, monkeypatch):
|
||||
from _pytest.config.argparsing import DropShorterLongHelpFormatter
|
||||
|
||||
monkeypatch.setenv("COLUMNS", "90")
|
||||
formatter = DropShorterLongHelpFormatter("prog")
|
||||
assert formatter._width == 90
|
||||
|
||||
monkeypatch.setattr("py.io.get_terminal_width", lambda: 160)
|
||||
formatter = DropShorterLongHelpFormatter("prog")
|
||||
assert formatter._width == 160
|
||||
|
||||
formatter = DropShorterLongHelpFormatter("prog", width=42)
|
||||
assert formatter._width == 42
|
||||
|
||||
|
||||
def test_config_does_not_load_blocked_plugin_from_args(testdir):
|
||||
"""This tests that pytest's config setup handles "-p no:X"."""
|
||||
p = testdir.makepyfile("def test(capfd): pass")
|
||||
|
|
Loading…
Reference in New Issue