Do not display URL when message is empty

Signed-off-by: David Wertenteil <dwertent@armosec.io>
This commit is contained in:
David Wertenteil
2023-03-04 23:04:26 +02:00
parent 6c54aff451
commit c2b0e5c0a2
2 changed files with 67 additions and 20 deletions

View File

@@ -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)
}

View File

@@ -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)
}
})
}
}