diff --git a/cli/zally/utils/formatters/formatter.go b/cli/zally/utils/formatters/formatter.go index 55930ac85..97671c858 100644 --- a/cli/zally/utils/formatters/formatter.go +++ b/cli/zally/utils/formatters/formatter.go @@ -6,6 +6,7 @@ import "github.com/zalando/zally/cli/zally/domain" type Formatter interface { FormatViolations(header string, violations []domain.Violation) string FormatViolationsCount(violationsCount *domain.ViolationsCount) string + FormatMessage(message string) string FormatServerMessage(message string) string FormatRule(rule *domain.Rule) string } diff --git a/cli/zally/utils/formatters/markdown_formatter.go b/cli/zally/utils/formatters/markdown_formatter.go index 1ad8528ac..2aa97bd7f 100644 --- a/cli/zally/utils/formatters/markdown_formatter.go +++ b/cli/zally/utils/formatters/markdown_formatter.go @@ -47,6 +47,14 @@ func (f *MarkdownFormatter) FormatRule(rule *domain.Rule) string { return buffer.String() } +// FormatMessage formats message +func (f *MarkdownFormatter) FormatMessage(message string) string { + if message != "" { + return fmt.Sprintf("\n%s\n\n", message) + } + return "" +} + // FormatServerMessage formats server message func (f *MarkdownFormatter) FormatServerMessage(message string) string { if message != "" { diff --git a/cli/zally/utils/formatters/markdown_formatter_test.go b/cli/zally/utils/formatters/markdown_formatter_test.go index 1d7a35885..6f63edff5 100644 --- a/cli/zally/utils/formatters/markdown_formatter_test.go +++ b/cli/zally/utils/formatters/markdown_formatter_test.go @@ -89,6 +89,23 @@ func TestMarkdownFormatRule(t *testing.T) { }) } +func TestMarkdownFormatMessage(t *testing.T) { + var formatter MarkdownFormatter + + t.Run("Formats nothing when no message", func(t *testing.T) { + actualResult := formatter.FormatMessage("") + + tests.AssertEquals(t, "", actualResult) + }) + + t.Run("Formats message when specified", func(t *testing.T) { + actualResult := formatter.FormatMessage("Congratulations!") + expectedResult := "\nCongratulations!\n\n" + + tests.AssertEquals(t, expectedResult, actualResult) + }) +} + func TestMarkdownFormatServerMessage(t *testing.T) { var formatter MarkdownFormatter diff --git a/cli/zally/utils/formatters/pretty_formatter.go b/cli/zally/utils/formatters/pretty_formatter.go index a0e623638..177ef2005 100644 --- a/cli/zally/utils/formatters/pretty_formatter.go +++ b/cli/zally/utils/formatters/pretty_formatter.go @@ -5,7 +5,6 @@ import ( "fmt" "strings" - "github.com/logrusorgru/aurora" "github.com/zalando/zally/cli/zally/domain" ) @@ -59,10 +58,18 @@ func (f *PrettyFormatter) FormatRule(rule *domain.Rule) string { return buffer.String() } +// FormatMessage formats message +func (f *PrettyFormatter) FormatMessage(message string) string { + if message != "" { + return fmt.Sprintf("\n%s\n\n", f.colorizer.auroraInstance.Green(message)) + } + return "" +} + // FormatServerMessage formats server message func (f *PrettyFormatter) FormatServerMessage(message string) string { if message != "" { - return fmt.Sprintf("\n\n%s%s\n\n\n", f.formatHeader("Server message:"), aurora.Green(message)) + return fmt.Sprintf("\n\n%s%s\n\n\n", f.formatHeader("Server message:"), f.colorizer.auroraInstance.Green(message)) } return "" } diff --git a/cli/zally/utils/formatters/pretty_formatter_test.go b/cli/zally/utils/formatters/pretty_formatter_test.go index 2cca63756..2cac492cc 100644 --- a/cli/zally/utils/formatters/pretty_formatter_test.go +++ b/cli/zally/utils/formatters/pretty_formatter_test.go @@ -75,6 +75,23 @@ func TestPrettyFormatRule(t *testing.T) { }) } +func TestPrettyFormatMessage(t *testing.T) { + prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(true)) + + t.Run("Formats nothing when no message", func(t *testing.T) { + actualResult := prettyFormatter.FormatMessage("") + + tests.AssertEquals(t, "", actualResult) + }) + + t.Run("Formats message when specified", func(t *testing.T) { + actualResult := prettyFormatter.FormatMessage("Congratulations!") + expectedResult := "\n\x1b[32mCongratulations!\x1b[0m\n\n" + + tests.AssertEquals(t, expectedResult, actualResult) + }) +} + func TestPrettyFormatServerMessage(t *testing.T) { prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(true)) @@ -158,3 +175,37 @@ func TestPrettyFormatHeader(t *testing.T) { tests.AssertEquals(t, "", result) }) } + +func TestTextFormatMessage(t *testing.T) { + prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(false)) + + t.Run("Formats nothing when no message", func(t *testing.T) { + actualResult := prettyFormatter.FormatServerMessage("") + + tests.AssertEquals(t, "", actualResult) + }) + + t.Run("Formats message when specified", func(t *testing.T) { + actualResult := prettyFormatter.FormatMessage("Congratulations!") + expectedResult := "\nCongratulations!\n\n" + + tests.AssertEquals(t, expectedResult, actualResult) + }) +} + +func TestTextFormatServerMessage(t *testing.T) { + prettyFormatter := NewPrettyFormatter(NewPrettyColorizer(false)) + + t.Run("Formats nothing when no message", func(t *testing.T) { + actualResult := prettyFormatter.FormatServerMessage("") + + tests.AssertEquals(t, "", actualResult) + }) + + t.Run("Formats message when specified", func(t *testing.T) { + actualResult := prettyFormatter.FormatServerMessage("Hello world!") + expectedResult := "\n\nServer message:\n===============\n\nHello world!\n\n\n" + + tests.AssertEquals(t, expectedResult, actualResult) + }) +} diff --git a/cli/zally/utils/result_printer.go b/cli/zally/utils/result_printer.go index 296caa401..34c7ff26d 100644 --- a/cli/zally/utils/result_printer.go +++ b/cli/zally/utils/result_printer.go @@ -33,6 +33,10 @@ func (r *ResultPrinter) PrintViolations(violations *domain.Violations) { fmt.Fprint(r.buffer, r.formatter.FormatViolationsCount(&violations.ViolationsCount)) } fmt.Fprint(r.buffer, r.formatter.FormatServerMessage(violations.Message)) + + if violations.Message == "" && len(violations.Violations) == 0 { + fmt.Fprint(r.buffer, r.formatter.FormatMessage("Congratulations! No violations found.")) + } } // PrintRules prints a list of supported rules diff --git a/cli/zally/utils/result_printer_test.go b/cli/zally/utils/result_printer_test.go index b691268a5..05da58c62 100644 --- a/cli/zally/utils/result_printer_test.go +++ b/cli/zally/utils/result_printer_test.go @@ -110,14 +110,18 @@ func TestPrintViolations(t *testing.T) { violationsCount.May = 3 violationsCount.Hint = 4 - t.Run("PrintViolations prints nothing if no violations", func(t *testing.T) { + t.Run("PrintViolations prints success if no violations", func(t *testing.T) { buffer.Reset() var violations domain.Violations resultPrinter.PrintViolations(&violations) - result := string(buffer.Bytes()) - tests.AssertEquals(t, "", result) + actualResult := string(buffer.Bytes()) + expectedResult := fmt.Sprintf( + "%s", + formatter.FormatMessage("Congratulations! No violations found.")) + + tests.AssertEquals(t, expectedResult, actualResult) }) t.Run("PrintViolations returns list of violation strings", func(t *testing.T) {