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
}
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()
close(validationResults)
logWG.Wait()
o.Flush()
if err = o.Flush(); err != nil {
fmt.Fprint(os.Stderr, "failed flushing output")
}
if !success {
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 := "", ""
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) {
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 res []byte
@ -102,8 +104,10 @@ func (o *jsono) Flush() {
}
if err != nil {
fmt.Fprintf(o.w, "error print results: %s", err)
return
return err
}
fmt.Fprintf(o.w, "%s\n", res)
return nil
}

View file

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

View file

@ -13,8 +13,8 @@ const (
)
type Output interface {
Write(filename, kind, version string, err error, skipped bool)
Flush()
Write(filename, kind, version string, err error, skipped bool) error
Flush() error
}
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()
defer o.Unlock()
switch status(err, skipped) {
var err error
switch status(reserr, skipped) {
case VALID:
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++
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++
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++
case SKIPPED:
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++
}
return err
}
func (o *text) Flush() {
func (o *text) Flush() error {
var err error
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) {
type result struct {
fileName, kind, version string
err error
skipped bool
fileName, kind, version string
err error
skipped bool
}
for _, testCase := range []struct {
name string
name string
withSummary bool
verbose bool
verbose bool
res []result
res []result
expect string
} {
}{
{
"a single deployment, no summary, no verbose",
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)
}
}
}
}