diff --git a/pkg/minikube/console/console.go b/pkg/minikube/console/console.go index df9025427412..291ff6730a36 100644 --- a/pkg/minikube/console/console.go +++ b/pkg/minikube/console/console.go @@ -70,7 +70,11 @@ func HasStyle(style string) bool { // OutStyle writes a stylized and formatted message to stdout func OutStyle(style, format string, a ...interface{}) error { - OutStyle, err := applyStyle(style, useColor, fmt.Sprintf(format, a...)) + outStyled, err := applyStyle(style, useColor, format, a...) + // format the output string and escape any outstanding '%' signs so that they don't + // get interpreted as a formatting directive down the line + outStyled = strings.Replace(outStyled, "%", "%%", -1) + if err != nil { glog.Errorf("applyStyle(%s): %v", style, err) if oerr := OutLn(format, a...); oerr != nil { @@ -78,7 +82,7 @@ func OutStyle(style, format string, a ...interface{}) error { } return err } - return Out(OutStyle) + return Out(outStyled) } // Out writes a basic formatted string to stdout diff --git a/pkg/minikube/console/console_test.go b/pkg/minikube/console/console_test.go index 7e0af8e1da26..1b86421cffd6 100644 --- a/pkg/minikube/console/console_test.go +++ b/pkg/minikube/console/console_test.go @@ -51,22 +51,37 @@ func TestOutStyle(t *testing.T) { style string envValue string message string + params []interface{} want string }{ - {"happy", "true", "This is happy.", "😄 This is happy.\n"}, - {"Docker", "true", "This is Docker.", "🐳 This is Docker.\n"}, - {"option", "true", "This is option.", " ▪ This is option.\n"}, - - {"happy", "false", "This is happy.", "o This is happy.\n"}, - {"Docker", "false", "This is Docker.", "- This is Docker.\n"}, - {"option", "false", "This is option.", " - This is option.\n"}, + {"happy", "true", "This is happy.", nil, "😄 This is happy.\n"}, + {"Docker", "true", "This is Docker.", nil, "🐳 This is Docker.\n"}, + {"option", "true", "This is option.", nil, " ▪ This is option.\n"}, + { + "option", + "true", + "Message with params: %s %s", + []interface{}{"encode '%' signs", "%s%%%d"}, + " ▪ Message with params: encode '%' signs %s%%%d\n", + }, + + {"happy", "false", "This is happy.", nil, "o This is happy.\n"}, + {"Docker", "false", "This is Docker.", nil, "- This is Docker.\n"}, + {"option", "false", "This is option.", nil, " - This is option.\n"}, + { + "option", + "false", + "Message with params: %s %s", + []interface{}{"encode '%' signs", "%s%%%d"}, + " - Message with params: encode '%' signs %s%%%d\n", + }, } for _, tc := range tests { t.Run(tc.style+"-"+tc.envValue, func(t *testing.T) { os.Setenv(OverrideEnv, tc.envValue) f := newFakeFile() SetOutFile(f) - if err := OutStyle(tc.style, tc.message); err != nil { + if err := OutStyle(tc.style, tc.message, tc.params...); err != nil { t.Errorf("unexpected error: %q", err) } got := f.String()