MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / test_parser_error_output_to

Function test_parser_error_output_to

tests/test_argparse_utils.py:665–699  ·  view source on GitHub ↗

Test that error() shadows to sys.stderr and uses it for styling.

(mocker: MockerFixture)

Source from the content-addressed store, hash-verified

663
664
665def test_parser_error_output_to(mocker: MockerFixture) -> None:
666 """Test that error() shadows to sys.stderr and uses it for styling."""
667 from cmd2 import rich_utils
668
669 parser = Cmd2ArgumentParser(prog="test")
670
671 # Mock exit to prevent actual exit
672 mocker.patch.object(parser, "exit")
673
674 # Mock print_usage to prevent actual printing
675 mocker.patch.object(parser, "print_usage")
676
677 # Mock _get_formatter to return a formatter with a mocked console
678 mock_formatter = mocker.Mock(spec=rich_utils.Cmd2HelpFormatter)
679 mock_console = mocker.MagicMock(spec=rich_utils.Cmd2RichArgparseConsole)
680 mock_formatter.console = mock_console
681
682 mocker.patch.object(parser, "_get_formatter", return_value=mock_formatter)
683
684 # Mock capture context manager
685 mock_capture = mocker.MagicMock()
686 mock_console.capture.return_value.__enter__.return_value = mock_capture
687 mock_capture.get.return_value = "Styled Error"
688
689 parser.error("some message")
690
691 # Verify that during error processing, current_output_file was shadowed to sys.stderr
692 # Check that print_usage was called with sys.stderr
693 parser.print_usage.assert_called_once_with(sys.stderr) # type: ignore[unreachable]
694
695 # Check that formatter's console was used to print the error
696 mock_console.print.assert_called_once()
697 args, kwargs = mock_console.print.call_args
698 assert "Error: some message" in args[0]
699 assert kwargs["style"] == argparse_utils.Cmd2Style.ERROR
700
701
702def test_parser_implicit_output_to(mocker: MockerFixture) -> None:

Callers

nothing calls this directly

Calls 2

errorMethod · 0.95
Cmd2ArgumentParserClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…