Skip to content

Commit

Permalink
Add feedback when there are no violations found
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabiana Scala committed Oct 7, 2020
1 parent 3dc380f commit bb7fb8d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cmd/root.go
Expand Up @@ -115,6 +115,10 @@ func rootRunE(cmd *cobra.Command, args []string) error {
err = fmt.Errorf("files with violations: %d", violations)
}

if violations == 0 {
fmt.Println("No violations found. Stay woke \u270a")
}

return err
}

Expand Down
44 changes: 41 additions & 3 deletions cmd/root_test.go
@@ -1,6 +1,10 @@
package cmd

import (
"bytes"
"io"
"os"
"sync"
"regexp"
"testing"

Expand All @@ -25,9 +29,13 @@ func TestRunE(t *testing.T) {
exitOneOnFailure = false
noIgnore = false
})
t.Run("no violations", func(t *testing.T) {
err := rootRunE(new(cobra.Command), []string{"../testdata"})
assert.NoError(t, err)
t.Run("no violations found", func(t *testing.T) {
got := captureOutput(func() {
err := rootRunE(new(cobra.Command), []string{"../testdata/good.yml"})
assert.NoError(t, err)
})
expected := "No violations found. Stay woke \u270a\n"
assert.Equal(t, expected, got)
})
t.Run("violations w error", func(t *testing.T) {
exitOneOnFailure = true
Expand All @@ -36,3 +44,33 @@ func TestRunE(t *testing.T) {
assert.Regexp(t, regexp.MustCompile(`^files with violations: \d`), err.Error())
})
}
// Returns output of `os.Stdout` as string.
// Based on https://medium.com/@hau12a1/golang-capturing-log-println-and-fmt-println-output-770209c791b4
func captureOutput(f func()) string {
reader, writer, err := os.Pipe()
if err != nil {
panic(err)
}
stdout := os.Stdout
stderr := os.Stderr
defer func() {
os.Stdout = stdout
os.Stderr = stderr
}()
os.Stdout = writer
os.Stderr = writer

out := make(chan string)
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
var buf bytes.Buffer
wg.Done()
_, _ = io.Copy(&buf, reader)
out <- buf.String()
}()
wg.Wait()
f()
writer.Close()
return <-out
}

0 comments on commit bb7fb8d

Please sign in to comment.