first simple test for json output

This commit is contained in:
Yann Hamon 2020-06-01 19:09:53 +02:00
parent 9b129d021a
commit f718b2c919
2 changed files with 97 additions and 2 deletions

View file

@ -22,11 +22,11 @@ type jsono struct {
nValid, nInvalid, nErrors, nSkipped int
}
func JSON(w io.Writer, withSummary bool, quiet bool) Output {
func JSON(w io.Writer, withSummary bool, verbose bool) Output {
return &jsono{
w: w,
withSummary: withSummary,
verbose: quiet,
verbose: verbose,
results: []result{},
nValid: 0,
nInvalid: 0,

95
pkg/output/json_test.go Normal file
View file

@ -0,0 +1,95 @@
package output
import (
"bytes"
"testing"
)
func TestJSONWrite(t *testing.T) {
type result struct {
fileName, kind, version string
err error
skipped bool
}
for _, testCase := range []struct {
name string
withSummary bool
verbose bool
res []result
expect string
} {
{
"a single deployment, no summary",
false,
false,
[]result{
{
"deployment.yml",
"Deployment",
"apps/v1",
nil,
false,
},
},
`{
"resources": [
{
"filename": "deployment.yml",
"kind": "Deployment",
"version": "apps/v1",
"status": "VALID",
"msg": ""
}
]
}
`,
},
{
"a single deployment, with summary",
true,
false,
[]result{
{
"deployment.yml",
"Deployment",
"apps/v1",
nil,
false,
},
},
`{
"resources": [
{
"filename": "deployment.yml",
"kind": "Deployment",
"version": "apps/v1",
"status": "VALID",
"msg": ""
}
],
"summary": {
"valid": 1,
"invalid": 0,
"errors": 0,
"skipped": 0
}
}
`,
},
} {
w := new(bytes.Buffer)
o := JSON(w, testCase.withSummary, testCase.verbose)
for _, res := range testCase.res {
o.Write(res.fileName, res.kind, res.version, res.err, res.skipped)
}
o.Flush()
if w.String() != testCase.expect {
t.Fatalf("%s - expected %s, got %s", testCase.name, testCase.expect, w)
}
}
}