Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/5056.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The HelpFormatter uses ``py.io.get_terminal_width`` for better width detection.
6 changes: 6 additions & 0 deletions src/_pytest/config/argparsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use setdefault(...) and shutil.get_terminal_width?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setdefault maybe, but otherwise it is meant to use an improved version (pytest-dev/py#219).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm I'm firmly in the camp that we should stop adding things to pylib

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, but better then to pytest - assuming that Python does not fix it itself. IIRC there's an old issue / PR idling there.
Anyway, this is merged, and pylib's PR is also idling. So no worries.. ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather put it in pytest, I've been trying to factor out pylib and this does the opposite!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, maybe later.
Feel free to fix this one up like you suggested initially then if it bothers you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FTR, I'm also in the camp that we should avoid adding new things to pylib at all costs, even though dropping it as a dependency is a far away dream.

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
Expand Down
15 changes: 15 additions & 0 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,21 @@ def pytest_addoption(parser):
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")
Expand Down