From c2b0e5c0a2d58c2a40c227a2e4ebaaabc34df39b Mon Sep 17 00:00:00 2001 From: David Wertenteil Date: Sat, 4 Mar 2023 23:04:26 +0200 Subject: [PATCH] Do not display URL when message is empty Signed-off-by: David Wertenteil --- .../reporter/v2/mockreporter.go | 21 +++--- .../reporter/v2/mockreporter_test.go | 66 +++++++++++++++---- 2 files changed, 67 insertions(+), 20 deletions(-) diff --git a/core/pkg/resultshandling/reporter/v2/mockreporter.go b/core/pkg/resultshandling/reporter/v2/mockreporter.go index d4a2e191..a4489dab 100644 --- a/core/pkg/resultshandling/reporter/v2/mockreporter.go +++ b/core/pkg/resultshandling/reporter/v2/mockreporter.go @@ -7,8 +7,11 @@ import ( "os" "github.com/kubescape/kubescape/v2/core/cautils" + "github.com/kubescape/kubescape/v2/core/pkg/resultshandling/reporter" ) +var _ reporter.IReport = &ReportMock{} + type ReportMock struct { query string message string @@ -36,17 +39,19 @@ func (reportMock *ReportMock) GetURL() string { return "" } - q := u.Query() - q.Add("utm_source", "GitHub") - q.Add("utm_medium", "CLI") - q.Add("utm_campaign", "Submit") - - u.RawQuery = q.Encode() - return u.String() } func (reportMock *ReportMock) DisplayReportURL() { + if m := reportMock.strToDisplay(); m != "" { + cautils.InfoTextDisplay(os.Stderr, m) + } +} + +func (reportMock *ReportMock) strToDisplay() string { + if reportMock.message == "" { + return "" + } sep := "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" message := sep + "\n" @@ -55,5 +60,5 @@ func (reportMock *ReportMock) DisplayReportURL() { message += "For more details: " + link + "\n" } message += sep + "\n" - cautils.InfoTextDisplay(os.Stderr, fmt.Sprintf("\n%s\n", message)) + return fmt.Sprintf("\n%s\n", message) } diff --git a/core/pkg/resultshandling/reporter/v2/mockreporter_test.go b/core/pkg/resultshandling/reporter/v2/mockreporter_test.go index 2a699a54..83d14a12 100644 --- a/core/pkg/resultshandling/reporter/v2/mockreporter_test.go +++ b/core/pkg/resultshandling/reporter/v2/mockreporter_test.go @@ -1,10 +1,13 @@ package reporter -import "testing" +import ( + "testing" +) func TestReportMock_GetURL(t *testing.T) { type fields struct { - query string + query string + message string } tests := []struct { name string @@ -13,19 +16,17 @@ func TestReportMock_GetURL(t *testing.T) { }{ { name: "TestReportMock_GetURL", - fields: struct { - query string - }{ - query: "https://kubescape.io", + fields: fields{ + query: "https://kubescape.io", + message: "some message", }, - want: "https://kubescape.io?utm_campaign=Submit&utm_medium=CLI&utm_source=GitHub", + want: "https://kubescape.io", }, { name: "TestReportMock_GetURL_empty", - fields: struct { - query string - }{ - query: "", + fields: fields{ + query: "", + message: "", }, want: "", }, @@ -33,7 +34,8 @@ func TestReportMock_GetURL(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { reportMock := &ReportMock{ - query: tt.fields.query, + query: tt.fields.query, + message: tt.fields.message, } if got := reportMock.GetURL(); got != tt.want { t.Errorf("ReportMock.GetURL() = %v, want %v", got, tt.want) @@ -41,3 +43,43 @@ func TestReportMock_GetURL(t *testing.T) { }) } } + +func TestReportMock_strToDisplay(t *testing.T) { + type fields struct { + query string + message string + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "TestReportMock_strToDisplay", + fields: fields{ + query: "https://kubescape.io", + message: "some message", + }, + want: "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nScan results have not been submitted: some message\nFor more details: https://kubescape.io\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n", + }, + { + name: "TestReportMock_strToDisplay_empty", + fields: fields{ + query: "https://kubescape.io", + message: "", + }, + want: "", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reportMock := &ReportMock{ + query: tt.fields.query, + message: tt.fields.message, + } + if got := reportMock.strToDisplay(); got != tt.want { + t.Errorf("ReportMock.strToDisplay() = %v, want %v", got, tt.want) + } + }) + } +}