Skip to content

Commit

Permalink
Merge pull request #717 from roxspring/feature/gh-716-cli-success-mes…
Browse files Browse the repository at this point in the history
…sage

Added success message when no violations found
  • Loading branch information
maxim-tschumak committed Jun 4, 2018
2 parents 097b5d2 + 7b868c2 commit 98e5d17
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 5 deletions.
1 change: 1 addition & 0 deletions cli/zally/utils/formatters/formatter.go
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions cli/zally/utils/formatters/markdown_formatter.go
Expand Up @@ -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 != "" {
Expand Down
17 changes: 17 additions & 0 deletions cli/zally/utils/formatters/markdown_formatter_test.go
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions cli/zally/utils/formatters/pretty_formatter.go
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"strings"

"github.com/logrusorgru/aurora"
"github.com/zalando/zally/cli/zally/domain"
)

Expand Down Expand Up @@ -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 ""
}
Expand Down
51 changes: 51 additions & 0 deletions cli/zally/utils/formatters/pretty_formatter_test.go
Expand Up @@ -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))

Expand Down Expand Up @@ -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)
})
}
4 changes: 4 additions & 0 deletions cli/zally/utils/result_printer.go
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions cli/zally/utils/result_printer_test.go
Expand Up @@ -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) {
Expand Down

0 comments on commit 98e5d17

Please sign in to comment.