@@ -7580,6 +7580,58 @@ def test_argparse_color_custom_usage(self):
75807580 ),
75817581 )
75827582
7583+ def test_argparse_color_wrapping_matches_uncolored (self ):
7584+ # gh-142035: color codes must not affect where help text wraps.
7585+ # Stripping the escapes from colored help must yield exactly the
7586+ # same text as the uncolored help across representative widths.
7587+ def build (color , path = "output.txt" ):
7588+ parser = argparse .ArgumentParser (prog = "PROG" , color = color )
7589+ parser .add_argument (
7590+ "--mode" ,
7591+ default = "auto" ,
7592+ choices = ("auto" , "fast" , "slow" ),
7593+ help = "select the operating mode from the available choices "
7594+ "%(choices)s and note the default is %(default)s here" ,
7595+ )
7596+ parser .add_argument (
7597+ "--path" ,
7598+ default = path ,
7599+ help = "write output to %(default)s and continue processing" ,
7600+ )
7601+ return parser
7602+
7603+ env = self .enterContext (os_helper .EnvironmentVarGuard ())
7604+ paths = (
7605+ "output.txt" ,
7606+ "/var/lib/application/cache/unusually_long_generated_filename" ,
7607+ "production-read-only-replica" ,
7608+ )
7609+ for path in paths :
7610+ for columns in ("80" , "60" , "45" , "30" , "20" ):
7611+ with self .subTest (path = path , columns = columns ):
7612+ env ["COLUMNS" ] = columns
7613+ colored = build (color = True , path = path ).format_help ()
7614+ plain = build (color = False , path = path ).format_help ()
7615+ self .assertIn ("\x1b [" , colored )
7616+ self .assertEqual (_colorize .decolor (colored ), plain )
7617+
7618+ def test_argparse_color_preserved_when_wrapping_between_words (self ):
7619+ parser = argparse .ArgumentParser (prog = "PROG" , color = True )
7620+ parser .add_argument (
7621+ "--mode" , default = "auto" ,
7622+ help = "select the %(default)s operating mode from the available "
7623+ "options and continue with several more words" ,
7624+ )
7625+
7626+ env = self .enterContext (os_helper .EnvironmentVarGuard ())
7627+ env ["COLUMNS" ] = "40"
7628+ help_text = parser .format_help ()
7629+
7630+ self .assertIn (
7631+ f"{ self .theme .interpolated_value } auto{ self .theme .reset } " ,
7632+ help_text ,
7633+ )
7634+
75837635 def test_custom_formatter_function (self ):
75847636 def custom_formatter (prog ):
75857637 return argparse .RawTextHelpFormatter (prog , indent_increment = 5 )
0 commit comments