output write/flush return an error

This commit is contained in:
Yann Hamon 2020-06-01 23:17:06 +02:00
parent dba9f97c1a
commit 42e5fd0c43
6 changed files with 47 additions and 32 deletions

View file

@ -220,7 +220,9 @@ func realMain() int {
success = false success = false
} }
o.Write(result.filename, result.kind, result.version, result.err, result.skipped) if err = o.Write(result.filename, result.kind, result.version, result.err, result.skipped); err != nil {
fmt.Fprint(os.Stderr, "failed writing log\n")
}
} }
} }
}() }()
@ -255,7 +257,9 @@ func realMain() int {
wg.Wait() wg.Wait()
close(validationResults) close(validationResults)
logWG.Wait() logWG.Wait()
o.Flush() if err = o.Flush(); err != nil {
fmt.Fprint(os.Stderr, "failed flushing output")
}
if !success { if !success {
return 1 return 1

View file

@ -35,7 +35,7 @@ func JSON(w io.Writer, withSummary bool, verbose bool) Output {
} }
} }
func (o *jsono) Write(filename, kind, version string, err error, skipped bool) { func (o *jsono) Write(filename, kind, version string, err error, skipped bool) error {
msg, st := "", "" msg, st := "", ""
s := status(err, skipped) s := status(err, skipped)
@ -60,9 +60,11 @@ func (o *jsono) Write(filename, kind, version string, err error, skipped bool) {
if o.verbose || (s != VALID && s != SKIPPED) { if o.verbose || (s != VALID && s != SKIPPED) {
o.results = append(o.results, result{Filename: filename, Kind: kind, Version: version, Status: st, Msg: msg}) o.results = append(o.results, result{Filename: filename, Kind: kind, Version: version, Status: st, Msg: msg})
} }
return nil
} }
func (o *jsono) Flush() { func (o *jsono) Flush() error {
var err error var err error
var res []byte var res []byte
@ -102,8 +104,10 @@ func (o *jsono) Flush() {
} }
if err != nil { if err != nil {
fmt.Fprintf(o.w, "error print results: %s", err) return err
return
} }
fmt.Fprintf(o.w, "%s\n", res) fmt.Fprintf(o.w, "%s\n", res)
return nil
} }

View file

@ -7,19 +7,19 @@ import (
func TestJSONWrite(t *testing.T) { func TestJSONWrite(t *testing.T) {
type result struct { type result struct {
fileName, kind, version string fileName, kind, version string
err error err error
skipped bool skipped bool
} }
for _, testCase := range []struct { for _, testCase := range []struct {
name string name string
withSummary bool withSummary bool
verbose bool verbose bool
res []result res []result
expect string expect string
} { }{
{ {
"a single deployment, no summary, no verbose", "a single deployment, no summary, no verbose",
false, false,
@ -107,4 +107,4 @@ func TestJSONWrite(t *testing.T) {
t.Fatalf("%s - expected %s, got %s", testCase.name, testCase.expect, w) t.Fatalf("%s - expected %s, got %s", testCase.name, testCase.expect, w)
} }
} }
} }

View file

@ -13,8 +13,8 @@ const (
) )
type Output interface { type Output interface {
Write(filename, kind, version string, err error, skipped bool) Write(filename, kind, version string, err error, skipped bool) error
Flush() Flush() error
} }
func status(err error, skipped bool) int { func status(err error, skipped bool) int {

View file

@ -26,32 +26,39 @@ func Text(w io.Writer, withSummary, verbose bool) Output {
} }
} }
func (o *text) Write(filename, kind, version string, err error, skipped bool) { func (o *text) Write(filename, kind, version string, reserr error, skipped bool) error {
o.Lock() o.Lock()
defer o.Unlock() defer o.Unlock()
switch status(err, skipped) { var err error
switch status(reserr, skipped) {
case VALID: case VALID:
if o.verbose { if o.verbose {
fmt.Fprintf(o.w, "%s - %s is valid\n", filename, kind) _, err = fmt.Fprintf(o.w, "%s - %s is valid\n", filename, kind)
} }
o.nValid++ o.nValid++
case INVALID: case INVALID:
fmt.Fprintf(o.w, "%s - %s is invalid: %s\n", filename, kind, err) _, err = fmt.Fprintf(o.w, "%s - %s is invalid: %s\n", filename, kind, reserr)
o.nInvalid++ o.nInvalid++
case ERROR: case ERROR:
fmt.Fprintf(o.w, "%s - %s failed validation: %s\n", filename, kind, err) _, err = fmt.Fprintf(o.w, "%s - %s failed validation: %s\n", filename, kind, reserr)
o.nErrors++ o.nErrors++
case SKIPPED: case SKIPPED:
if o.verbose { if o.verbose {
fmt.Fprintf(o.w, "%s - %s skipped\n", filename, kind) _, err = fmt.Fprintf(o.w, "%s - %s skipped\n", filename, kind)
} }
o.nSkipped++ o.nSkipped++
} }
return err
} }
func (o *text) Flush() { func (o *text) Flush() error {
var err error
if o.withSummary { if o.withSummary {
fmt.Fprintf(o.w, "Run summary - Valid: %d, Invalid: %d, Errors: %d Skipped: %d\n", o.nValid, o.nInvalid, o.nErrors, o.nSkipped) _, err = fmt.Fprintf(o.w, "Run summary - Valid: %d, Invalid: %d, Errors: %d Skipped: %d\n", o.nValid, o.nInvalid, o.nErrors, o.nSkipped)
} }
return err
} }

View file

@ -7,19 +7,19 @@ import (
func TestTextWrite(t *testing.T) { func TestTextWrite(t *testing.T) {
type result struct { type result struct {
fileName, kind, version string fileName, kind, version string
err error err error
skipped bool skipped bool
} }
for _, testCase := range []struct { for _, testCase := range []struct {
name string name string
withSummary bool withSummary bool
verbose bool verbose bool
res []result res []result
expect string expect string
} { }{
{ {
"a single deployment, no summary, no verbose", "a single deployment, no summary, no verbose",
false, false,
@ -80,4 +80,4 @@ Run summary - Valid: 1, Invalid: 0, Errors: 0 Skipped: 0
t.Errorf("%s - expected: %s, got: %s", testCase.name, testCase.expect, w) t.Errorf("%s - expected: %s, got: %s", testCase.name, testCase.expect, w)
} }
} }
} }