From d34fbaed1a2096ae3ccdf21850edb1220fb11b8b Mon Sep 17 00:00:00 2001 From: Armel Soro Date: Tue, 26 Sep 2023 08:53:45 +0200 Subject: [PATCH] Make warning messages more visible (#7097) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Wrap warning messages to make them more visible For example, instead of displaying: ``` ⚠ You are using "default" project, odo may not work as expected in the default project. ``` We are now displaying: ``` ======================================================================================== ⚠ You are using "default" project, odo may not work as expected in the default project. ======================================================================================== ``` * Display warning message about default project/namespace in a single block * Add unit test * Fix sample outputs for doc automation tests * Revert "Fix sample outputs for doc automation tests" This reverts commit 98a6554c34ba55643a5f92276601a174a04b9879. * Ignore '===' warning header and footer for doc automation tests --- pkg/log/status.go | 38 +++++++++++------ pkg/log/status_test.go | 64 ++++++++++++++++++++++++++++ pkg/odo/genericclioptions/util.go | 8 ++-- tests/helper/helper_documentation.go | 3 ++ 4 files changed, 96 insertions(+), 17 deletions(-) create mode 100644 pkg/log/status_test.go diff --git a/pkg/log/status.go b/pkg/log/status.go index 17f891884b9..f2135df1af6 100644 --- a/pkg/log/status.go +++ b/pkg/log/status.go @@ -292,8 +292,7 @@ func Warning(a ...interface{}) { // ⚠ func Fwarning(out io.Writer, a ...interface{}) { if !IsJSON() { - yellow := color.New(color.FgYellow).SprintFunc() - fmt.Fprintf(out, "%s%s%s%s", prefixSpacing, yellow(getWarningString()), suffixSpacing, fmt.Sprintln(a...)) + Fwarningf(out, "%s", a...) } } @@ -301,10 +300,7 @@ func Fwarning(out io.Writer, a ...interface{}) { // // ⚠ func Warningf(format string, a ...interface{}) { - if !IsJSON() { - yellow := color.New(color.FgYellow).SprintFunc() - fmt.Fprintf(GetStderr(), " %s%s%s\n", yellow(getWarningString()), suffixSpacing, fmt.Sprintf(format, a...)) - } + Fwarningf(GetStderr(), format, a...) } // Fwarningf will output in an appropriate "warning" manner @@ -313,8 +309,26 @@ func Warningf(format string, a ...interface{}) { func Fwarningf(w io.Writer, format string, a ...interface{}) { if !IsJSON() { yellow := color.New(color.FgYellow).SprintFunc() - fmt.Fprintf(w, " %s%s%s\n", yellow(getWarningString()), suffixSpacing, fmt.Sprintf(format, a...)) + fullMessage := fmt.Sprintf("%s%s%s", getWarningString(), suffixSpacing, fmt.Sprintf(format, a...)) + fmt.Fprintln(w, yellow(wrapWarningMessage(fullMessage))) + } +} + +func wrapWarningMessage(fullMessage string) string { + if fullMessage == "" || strings.TrimSpace(fullMessage) == "" { + return fullMessage } + split := strings.Split(fullMessage, "\n") + max := 0 + for _, s := range split { + if len(s) > max { + max = len(s) + } + } + h := strings.Repeat("=", max) + return fmt.Sprintf(`%[1]s +%[2]s +%[1]s`, h, fullMessage, h) } // Fsuccess will output in an appropriate "progress" manner in out writer @@ -330,13 +344,9 @@ func Fsuccess(out io.Writer, a ...interface{}) { // DisplayExperimentalWarning displays the experimental mode warning message. func DisplayExperimentalWarning() { if !IsJSON() { - yellow := color.New(color.FgYellow).SprintFunc() - h := "============================================================================" - fmt.Fprintln(GetStdout(), yellow(fmt.Sprintf(`%[1]s -%s Experimental mode enabled. Use at your own risk. -More details on https://odo.dev/docs/user-guides/advanced/experimental-mode -%[1]s -`, h, getWarningString()))) + msg := `Experimental mode enabled. Use at your own risk. +More details on https://odo.dev/docs/user-guides/advanced/experimental-mode` + Fwarningf(GetStdout(), msg) } } diff --git a/pkg/log/status_test.go b/pkg/log/status_test.go new file mode 100644 index 00000000000..2ee498ebf93 --- /dev/null +++ b/pkg/log/status_test.go @@ -0,0 +1,64 @@ +package log + +import ( + "testing" + + "github.com/google/go-cmp/cmp" +) + +func Test_wrapWarningMessage(t *testing.T) { + type args struct { + fullMessage string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "empty message", + args: args{ + fullMessage: "", + }, + want: "", + }, + { + name: "single-line message", + args: args{ + fullMessage: "Lorem Ipsum Dolor Sit Amet", + }, + want: `========================== +Lorem Ipsum Dolor Sit Amet +==========================`, + }, + { + name: "multi-line message", + args: args{ + fullMessage: ` +Lorem ipsum dolor sit amet, consectetur adipiscing elit. +Aenean vel faucibus ex. +Nulla in magna sem. +Vivamus condimentum ultricies erat, in ullamcorper risus tempor nec. +Nam sed risus blandit, +`, + }, + want: `==================================================================== + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. +Aenean vel faucibus ex. +Nulla in magna sem. +Vivamus condimentum ultricies erat, in ullamcorper risus tempor nec. +Nam sed risus blandit, + +====================================================================`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := wrapWarningMessage(tt.args.fullMessage) + if diff := cmp.Diff(tt.want, got); diff != "" { + t.Errorf("wrapWarningMessage() mismatch (-want +got):\n%s", diff) + } + }) + } +} diff --git a/pkg/odo/genericclioptions/util.go b/pkg/odo/genericclioptions/util.go index a068c7077e4..3ea4b9cf260 100644 --- a/pkg/odo/genericclioptions/util.go +++ b/pkg/odo/genericclioptions/util.go @@ -2,10 +2,12 @@ package genericclioptions import ( "fmt" + + v1 "k8s.io/api/core/v1" + "github.com/redhat-developer/odo/pkg/kclient" "github.com/redhat-developer/odo/pkg/log" pkgUtil "github.com/redhat-developer/odo/pkg/util" - v1 "k8s.io/api/core/v1" dfutil "github.com/devfile/library/v2/pkg/util" ) @@ -47,7 +49,7 @@ func WarnIfDefaultNamespace(namespace string, kubeClient kclient.ClientInterface noun = "project" } fmt.Println() - log.Warningf("You are using \"default\" %[1]s, odo may not work as expected in the default %[1]s.", noun) - log.Warningf("You may set a new %[1]s by running `odo create %[1]s `, or set an existing one by running `odo set %[1]s `", noun) + log.Warningf(`You are using "default" %[1]s, odo may not work as expected in the default %[1]s. +You may set a new %[1]s by running `+"`odo create %[1]s `, or set an existing one by running `odo set %[1]s `", noun) } } diff --git a/tests/helper/helper_documentation.go b/tests/helper/helper_documentation.go index 14c623ca6b9..e8f5edbfb34 100644 --- a/tests/helper/helper_documentation.go +++ b/tests/helper/helper_documentation.go @@ -67,6 +67,9 @@ func StripSpinner(docString string) (returnString string) { if strings.HasPrefix(line, "⚠") && !strings.Contains(line, "Pod is Pending") { continue } + if strings.HasPrefix(line, "===") { + continue + } // for some reason, splitting the docString by \n does not split the spinner frames, // so we perform a side operation to remove the extra spinner frames that are not present in the final output