Skip to content

Commit 2ff6640

Browse files
committed
Added tests to cover missing lines in argparse_custom.py
1 parent 9d39c14 commit 2ff6640

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ prompt is displayed.
8181
- `cmd2.Cmd.select` has been revamped to use the
8282
[choice](https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/asking_for_a_choice.html)
8383
function from `prompt-toolkit` when both **stdin** and **stdout** are TTYs
84+
- Add support for Python 3.15 by fixing various bugs related to internal `argparse` changes
8485
- Added `common_prefix` method to `cmd2.string_utils` module as a replacement for
8586
`os.path.commonprefix` since that is now deprecated in Python 3.15
8687

tests/test_argparse_custom.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,46 @@ def test_completion_items_as_choices(capsys) -> None:
353353
# Confirm error text contains correct value type of int
354354
_out, err = capsys.readouterr()
355355
assert 'invalid choice: 3 (choose from 1, 2)' in err
356+
357+
358+
def test_formatter_coverage(mocker) -> None:
359+
import sys
360+
361+
from cmd2.argparse_custom import (
362+
Cmd2HelpFormatter,
363+
Cmd2RichArgparseConsole,
364+
)
365+
366+
# Line 1031: self._console = console (inside console.setter)
367+
formatter = Cmd2HelpFormatter(prog='test')
368+
new_console = Cmd2RichArgparseConsole()
369+
formatter.console = new_console
370+
assert formatter._console is new_console
371+
372+
# Line 1041: return (inside _set_color if sys.version_info < (3, 14))
373+
mocker.patch('cmd2.argparse_custom.sys.version_info', (3, 13, 0))
374+
# This should return early without calling super()._set_color
375+
mock_set_color = mocker.patch('rich_argparse.RichHelpFormatter._set_color')
376+
formatter._set_color(True)
377+
mock_set_color.assert_not_called()
378+
379+
# Line 1045 and 1047: except TypeError and super()._set_color(color)
380+
mocker.patch('cmd2.argparse_custom.sys.version_info', (3, 15, 0))
381+
382+
# Reset mock and make it raise TypeError when called with kwargs
383+
mock_set_color.reset_mock()
384+
385+
def side_effect(color, **kwargs):
386+
if kwargs:
387+
raise TypeError("unexpected keyword argument 'file'")
388+
return
389+
390+
mock_set_color.side_effect = side_effect
391+
392+
# This call should trigger the TypeError and then the fallback call
393+
formatter._set_color(True, file=sys.stdout)
394+
395+
# It should have been called twice: once with kwargs (failed) and once without (fallback)
396+
assert mock_set_color.call_count == 2
397+
mock_set_color.assert_any_call(True, file=sys.stdout)
398+
mock_set_color.assert_any_call(True)

0 commit comments

Comments
 (0)