From f718b2c919b76a1c435a67bc013fb176adf3e7a9 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Mon, 1 Jun 2020 19:09:53 +0200 Subject: [PATCH] first simple test for json output --- pkg/output/json.go | 4 +- pkg/output/json_test.go | 95 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 pkg/output/json_test.go diff --git a/pkg/output/json.go b/pkg/output/json.go index a5ee196..09d96b4 100644 --- a/pkg/output/json.go +++ b/pkg/output/json.go @@ -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, diff --git a/pkg/output/json_test.go b/pkg/output/json_test.go new file mode 100644 index 0000000..3b6129c --- /dev/null +++ b/pkg/output/json_test.go @@ -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) + } + } +} \ No newline at end of file