Skip to content

Commit e316a4d

Browse files
authored
Merge pull request #4026 from 11janci/jjanik-console-escaping
Escape ‘%’ in console.OutStyle arguments
2 parents f265a22 + 160dc85 commit e316a4d

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

pkg/minikube/console/console.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,20 @@ 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...)
7474
if err != nil {
7575
glog.Errorf("applyStyle(%s): %v", style, err)
7676
if oerr := OutLn(format, a...); oerr != nil {
7777
glog.Errorf("Out failed: %v", oerr)
7878
}
7979
return err
8080
}
81-
return Out(OutStyle)
81+
82+
// escape any outstanding '%' signs so that they don't get interpreted
83+
// as a formatting directive down the line
84+
outStyled = strings.Replace(outStyled, "%", "%%", -1)
85+
86+
return Out(outStyled)
8287
}
8388

8489
// Out writes a basic formatted string to stdout
@@ -101,14 +106,19 @@ func OutLn(format string, a ...interface{}) error {
101106

102107
// ErrStyle writes a stylized and formatted error message to stderr
103108
func ErrStyle(style, format string, a ...interface{}) error {
104-
format, err := applyStyle(style, useColor, fmt.Sprintf(format, a...))
109+
format, err := applyStyle(style, useColor, format, a...)
105110
if err != nil {
106111
glog.Errorf("applyStyle(%s): %v", style, err)
107112
if oerr := ErrLn(format, a...); oerr != nil {
108113
glog.Errorf("Err(%s) failed: %v", format, oerr)
109114
}
110115
return err
111116
}
117+
118+
// escape any outstanding '%' signs so that they don't get interpreted
119+
// as a formatting directive down the line
120+
format = strings.Replace(format, "%", "%%", -1)
121+
112122
return Err(format)
113123
}
114124

pkg/minikube/console/console_test.go

+28-12
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()
@@ -94,6 +109,7 @@ func TestOut(t *testing.T) {
94109
{format: "xyz123", want: "xyz123"},
95110
{format: "Installing Kubernetes version %s ...", lang: language.Arabic, arg: "v1.13", want: "... v1.13 تثبيت Kubernetes الإصدار"},
96111
{format: "Installing Kubernetes version %s ...", lang: language.AmericanEnglish, arg: "v1.13", want: "Installing Kubernetes version v1.13 ..."},
112+
{format: "Parameter encoding: %s", arg: "%s%%%d", want: "Parameter encoding: %s%%%d"},
97113
}
98114
for _, tc := range tests {
99115
t.Run(tc.format, func(t *testing.T) {
@@ -116,13 +132,13 @@ func TestErr(t *testing.T) {
116132
os.Setenv(OverrideEnv, "0")
117133
f := newFakeFile()
118134
SetErrFile(f)
119-
if err := Err("xyz123\n"); err != nil {
135+
if err := Err("xyz123 %s\n", "%s%%%d"); err != nil {
120136
t.Errorf("unexpected error: %q", err)
121137
}
122138

123139
OutLn("unrelated message")
124140
got := f.String()
125-
want := "xyz123\n"
141+
want := "xyz123 %s%%%d\n"
126142

127143
if got != want {
128144
t.Errorf("Err() = %q, want %q", got, want)
@@ -133,11 +149,11 @@ func TestErrStyle(t *testing.T) {
133149
os.Setenv(OverrideEnv, "1")
134150
f := newFakeFile()
135151
SetErrFile(f)
136-
if err := ErrStyle("fatal", "It broke"); err != nil {
152+
if err := ErrStyle("fatal", "error: %s", "%s%%%d"); err != nil {
137153
t.Errorf("unexpected error: %q", err)
138154
}
139155
got := f.String()
140-
want := "💣 It broke\n"
156+
want := "💣 error: %s%%%d\n"
141157
if got != want {
142158
t.Errorf("ErrStyle() = %q, want %q", got, want)
143159
}

0 commit comments

Comments
 (0)