@@ -1022,6 +1022,56 @@ def cli(password):
10221022 assert "Confirm Password: " in result .output
10231023
10241024
1025+ @pytest .mark .parametrize (
1026+ ("call" , "user_input" , "color" , "expect" ),
1027+ [
1028+ pytest .param (
1029+ lambda : click .confirm (click .style ("Hello World!" , fg = "green" )),
1030+ "y" ,
1031+ False ,
1032+ "Hello World! [y/N]: y\n " ,
1033+ id = "confirm-no-color" ,
1034+ ),
1035+ pytest .param (
1036+ lambda : click .confirm (click .style ("Hello World!" , fg = "green" )),
1037+ "y" ,
1038+ True ,
1039+ "\x1b [32mHello World!\x1b [0m [y/N]: y\n " ,
1040+ id = "confirm-color" ,
1041+ ),
1042+ pytest .param (
1043+ lambda : click .prompt (click .style ("Name" , fg = "green" )),
1044+ "Bob" ,
1045+ False ,
1046+ "Name: Bob\n " ,
1047+ id = "prompt-no-color" ,
1048+ ),
1049+ pytest .param (
1050+ lambda : click .prompt (click .style ("Name" , fg = "green" )),
1051+ "Bob" ,
1052+ True ,
1053+ "\x1b [32mName\x1b [0m: Bob\n " ,
1054+ id = "prompt-color" ,
1055+ ),
1056+ ],
1057+ )
1058+ def test_prompt_and_confirm_ansi_respects_color (
1059+ runner , call , user_input , color , expect
1060+ ):
1061+ """``confirm`` and ``prompt`` strip ANSI color and style codes from the
1062+ prompt when color is disabled and keep them when it is enabled, like
1063+ ``echo``. The prompt is written by ``input`` rather than ``echo``, so it
1064+ used to keep the codes regardless (issue 3572).
1065+ """
1066+
1067+ @click .command ()
1068+ def cli ():
1069+ call ()
1070+
1071+ result = runner .invoke (cli , input = user_input , color = color )
1072+ assert result .output == expect
1073+
1074+
10251075def test_false_show_default_cause_no_default_display_in_prompt (runner ):
10261076 @click .command ()
10271077 @click .option ("--arg1" , show_default = False , prompt = True , default = "my-default-value" )
0 commit comments