File tree 3 files changed +22
-0
lines changed 3 files changed +22
-0
lines changed Original file line number Diff line number Diff line change
1
+ The HelpFormatter uses ``py.io.get_terminal_width `` for better width detection.
Original file line number Diff line number Diff line change @@ -405,6 +405,12 @@ class DropShorterLongHelpFormatter(argparse.HelpFormatter):
405
405
- cache result on action object as this is called at least 2 times
406
406
"""
407
407
408
+ def __init__ (self , * args , ** kwargs ):
409
+ """Use more accurate terminal width via pylib."""
410
+ if "width" not in kwargs :
411
+ kwargs ["width" ] = py .io .get_terminal_width ()
412
+ super ().__init__ (* args , ** kwargs )
413
+
408
414
def _format_action_invocation (self , action ):
409
415
orgstr = argparse .HelpFormatter ._format_action_invocation (self , action )
410
416
if orgstr and orgstr [0 ] != "-" : # only optional arguments
Original file line number Diff line number Diff line change @@ -1194,6 +1194,21 @@ def pytest_addoption(parser):
1194
1194
assert result .ret == ExitCode .USAGE_ERROR
1195
1195
1196
1196
1197
+ def test_help_formatter_uses_py_get_terminal_width (testdir , monkeypatch ):
1198
+ from _pytest .config .argparsing import DropShorterLongHelpFormatter
1199
+
1200
+ monkeypatch .setenv ("COLUMNS" , "90" )
1201
+ formatter = DropShorterLongHelpFormatter ("prog" )
1202
+ assert formatter ._width == 90
1203
+
1204
+ monkeypatch .setattr ("py.io.get_terminal_width" , lambda : 160 )
1205
+ formatter = DropShorterLongHelpFormatter ("prog" )
1206
+ assert formatter ._width == 160
1207
+
1208
+ formatter = DropShorterLongHelpFormatter ("prog" , width = 42 )
1209
+ assert formatter ._width == 42
1210
+
1211
+
1197
1212
def test_config_does_not_load_blocked_plugin_from_args (testdir ):
1198
1213
"""This tests that pytest's config setup handles "-p no:X"."""
1199
1214
p = testdir .makepyfile ("def test(capfd): pass" )
You can’t perform that action at this time.
0 commit comments