Skip to content

Commit a6ec2ad

Browse files
committed
Escape ‘%’ in console.OutStyle arguments
1 parent b832ff3 commit a6ec2ad

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

pkg/minikube/console/console.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,19 @@ func HasStyle(style string) bool {
7070

7171
// OutStyle writes a stylized and formatted message to stdout
7272
func OutStyle(style, format string, a ...interface{}) error {
73-
OutStyle, err := applyStyle(style, useColor, fmt.Sprintf(format, a...))
73+
outStyled, err := applyStyle(style, useColor, format, a...)
74+
// format the output string and escape any outstanding '%' signs so that they don't
75+
// get interpreted as a formatting directive down the line
76+
outStyled = strings.Replace(outStyled, "%", "%%", -1)
77+
7478
if err != nil {
7579
glog.Errorf("applyStyle(%s): %v", style, err)
7680
if oerr := OutLn(format, a...); oerr != nil {
7781
glog.Errorf("Out failed: %v", oerr)
7882
}
7983
return err
8084
}
81-
return Out(OutStyle)
85+
return Out(outStyled)
8286
}
8387

8488
// Out writes a basic formatted string to stdout

pkg/minikube/console/console_test.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,37 @@ func TestOutStyle(t *testing.T) {
5151
style string
5252
envValue string
5353
message string
54+
params []interface{}
5455
want string
5556
}{
56-
{"happy", "true", "This is happy.", "😄 This is happy.\n"},
57-
{"Docker", "true", "This is Docker.", "🐳 This is Docker.\n"},
58-
{"option", "true", "This is option.", " ▪ This is option.\n"},
59-
60-
{"happy", "false", "This is happy.", "o This is happy.\n"},
61-
{"Docker", "false", "This is Docker.", "- This is Docker.\n"},
62-
{"option", "false", "This is option.", " - This is option.\n"},
57+
{"happy", "true", "This is happy.", nil, "😄 This is happy.\n"},
58+
{"Docker", "true", "This is Docker.", nil, "🐳 This is Docker.\n"},
59+
{"option", "true", "This is option.", nil, " ▪ This is option.\n"},
60+
{
61+
"option",
62+
"true",
63+
"Message with params: %s %s",
64+
[]interface{}{"encode '%' signs", "%s%%%d"},
65+
" ▪ Message with params: encode '%' signs %s%%%d\n",
66+
},
67+
68+
{"happy", "false", "This is happy.", nil, "o This is happy.\n"},
69+
{"Docker", "false", "This is Docker.", nil, "- This is Docker.\n"},
70+
{"option", "false", "This is option.", nil, " - This is option.\n"},
71+
{
72+
"option",
73+
"false",
74+
"Message with params: %s %s",
75+
[]interface{}{"encode '%' signs", "%s%%%d"},
76+
" - Message with params: encode '%' signs %s%%%d\n",
77+
},
6378
}
6479
for _, tc := range tests {
6580
t.Run(tc.style+"-"+tc.envValue, func(t *testing.T) {
6681
os.Setenv(OverrideEnv, tc.envValue)
6782
f := newFakeFile()
6883
SetOutFile(f)
69-
if err := OutStyle(tc.style, tc.message); err != nil {
84+
if err := OutStyle(tc.style, tc.message, tc.params...); err != nil {
7085
t.Errorf("unexpected error: %q", err)
7186
}
7287
got := f.String()

0 commit comments

Comments
 (0)