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