@@ -7370,6 +7370,40 @@ def test_subparser_prog_is_stored_without_color(self):
73707370 help_text = demo_parser .format_help ()
73717371 self .assertNotIn ('\x1b [' , help_text )
73727372
7373+ def test_print_help_uses_target_file_for_color_decision (self ):
7374+ parser = argparse .ArgumentParser (prog = 'PROG' , color = True )
7375+ parser .add_argument ('--opt' )
7376+ output = io .StringIO ()
7377+ calls = []
7378+
7379+ def fake_can_colorize (* , file = None ):
7380+ calls .append (file )
7381+ return file is None
7382+
7383+ with swap_attr (_colorize , 'can_colorize' , fake_can_colorize ):
7384+ parser .print_help (file = output )
7385+
7386+ self .assertIs (calls [- 1 ], output )
7387+ self .assertIn (output , calls )
7388+ self .assertNotIn ('\x1b [' , output .getvalue ())
7389+
7390+ def test_print_usage_uses_target_file_for_color_decision (self ):
7391+ parser = argparse .ArgumentParser (prog = 'PROG' , color = True )
7392+ parser .add_argument ('--opt' )
7393+ output = io .StringIO ()
7394+ calls = []
7395+
7396+ def fake_can_colorize (* , file = None ):
7397+ calls .append (file )
7398+ return file is None
7399+
7400+ with swap_attr (_colorize , 'can_colorize' , fake_can_colorize ):
7401+ parser .print_usage (file = output )
7402+
7403+ self .assertIs (calls [- 1 ], output )
7404+ self .assertIn (output , calls )
7405+ self .assertNotIn ('\x1b [' , output .getvalue ())
7406+
73737407
73747408class TestModule (unittest .TestCase ):
73757409 def test_deprecated__version__ (self ):
0 commit comments