From a1969e208c98171a1a0728d8c8e5a0e1b9342ace Mon Sep 17 00:00:00 2001 From: Cosmin Cojocar Date: Thu, 16 Jan 2020 09:05:38 +0100 Subject: [PATCH] Handle all errors in the formatter tests (#431) Signed-off-by: Cosmin Cojocar --- output/formatter_test.go | 60 ++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/output/formatter_test.go b/output/formatter_test.go index 41a851f..46bfe62 100644 --- a/output/formatter_test.go +++ b/output/formatter_test.go @@ -4,11 +4,12 @@ import ( "bytes" "encoding/json" "fmt" + "strings" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/securego/gosec" "gopkg.in/yaml.v2" - "strings" ) func createIssue(ruleID string, cwe gosec.Cwe) gosec.Issue { @@ -261,10 +262,11 @@ var _ = Describe("Formatter", func() { error := map[string][]gosec.Error{} buf := new(bytes.Buffer) - CreateReport(buf, "csv", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + err := CreateReport(buf, "csv", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + Expect(err).ShouldNot(HaveOccurred()) pattern := "/home/src/project/test.go,1,test,HIGH,HIGH,testcode,CWE-%s\n" expect := fmt.Sprintf(pattern, cwe.ID) - Expect(string(buf.Bytes())).To(Equal(expect)) + Expect(string(buf.String())).To(Equal(expect)) } }) It("xml formatted report should contain the CWE mapping", func() { @@ -274,10 +276,11 @@ var _ = Describe("Formatter", func() { error := map[string][]gosec.Error{} buf := new(bytes.Buffer) - CreateReport(buf, "xml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{NumFiles: 0, NumLines: 0, NumNosec: 0, NumFound: 0}, error) + err := CreateReport(buf, "xml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{NumFiles: 0, NumLines: 0, NumNosec: 0, NumFound: 0}, error) + Expect(err).ShouldNot(HaveOccurred()) pattern := "Results:\n\n\n[/home/src/project/test.go:1] - %s (CWE-%s): test (Confidence: HIGH, Severity: HIGH)\n > testcode\n\n\nSummary:\n Files: 0\n Lines: 0\n Nosec: 0\n Issues: 0\n\n" expect := fmt.Sprintf(pattern, rule, cwe.ID) - Expect(string(buf.Bytes())).To(Equal(expect)) + Expect(string(buf.String())).To(Equal(expect)) } }) It("json formatted report should contain the CWE mapping", func() { @@ -290,10 +293,11 @@ var _ = Describe("Formatter", func() { expect := new(bytes.Buffer) enc := json.NewEncoder(expect) - enc.Encode(data) - + err := enc.Encode(data) + Expect(err).ShouldNot(HaveOccurred()) buf := new(bytes.Buffer) - CreateReport(buf, "json", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + err = CreateReport(buf, "json", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + Expect(err).ShouldNot(HaveOccurred()) result := stripString(buf.String()) expectation := stripString(expect.String()) Expect(result).To(Equal(expectation)) @@ -309,10 +313,11 @@ var _ = Describe("Formatter", func() { expect := new(bytes.Buffer) enc := json.NewEncoder(expect) - enc.Encode(data) - + err := enc.Encode(data) + Expect(err).ShouldNot(HaveOccurred()) buf := new(bytes.Buffer) - CreateReport(buf, "html", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + err = CreateReport(buf, "html", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + Expect(err).ShouldNot(HaveOccurred()) result := stripString(buf.String()) expectation := stripString(expect.String()) Expect(result).To(ContainSubstring(expectation)) @@ -328,10 +333,11 @@ var _ = Describe("Formatter", func() { expect := new(bytes.Buffer) enc := yaml.NewEncoder(expect) - enc.Encode(data) - + err := enc.Encode(data) + Expect(err).ShouldNot(HaveOccurred()) buf := new(bytes.Buffer) - CreateReport(buf, "yaml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + err = CreateReport(buf, "yaml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + Expect(err).ShouldNot(HaveOccurred()) result := stripString(buf.String()) expectation := stripString(expect.String()) Expect(result).To(ContainSubstring(expectation)) @@ -347,10 +353,11 @@ var _ = Describe("Formatter", func() { expect := new(bytes.Buffer) enc := yaml.NewEncoder(expect) - enc.Encode(data) - + err := enc.Encode(data) + Expect(err).ShouldNot(HaveOccurred()) buf := new(bytes.Buffer) - CreateReport(buf, "junit-xml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + err = CreateReport(buf, "junit-xml", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + Expect(err).ShouldNot(HaveOccurred()) expectation := stripString(fmt.Sprintf("[/home/src/project/test.go:1] - test (Confidence: 2, Severity: 2, CWE: %s)", cwe.ID)) result := stripString(buf.String()) Expect(result).To(ContainSubstring(expectation)) @@ -366,10 +373,11 @@ var _ = Describe("Formatter", func() { expect := new(bytes.Buffer) enc := yaml.NewEncoder(expect) - enc.Encode(data) - + err := enc.Encode(data) + Expect(err).ShouldNot(HaveOccurred()) buf := new(bytes.Buffer) - CreateReport(buf, "text", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + err = CreateReport(buf, "text", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + Expect(err).ShouldNot(HaveOccurred()) expectation := stripString(fmt.Sprintf("[/home/src/project/test.go:1] - %s (CWE-%s): test (Confidence: HIGH, Severity: HIGH)", rule, cwe.ID)) result := stripString(buf.String()) Expect(result).To(ContainSubstring(expectation)) @@ -381,12 +389,15 @@ var _ = Describe("Formatter", func() { issue := createIssue(rule, cwe) error := map[string][]gosec.Error{} buf := new(bytes.Buffer) - CreateReport(buf, "sonarqube", []string{"/home/src/project"}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + err := CreateReport(buf, "sonarqube", []string{"/home/src/project"}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + Expect(err).ShouldNot(HaveOccurred()) + result := stripString(buf.String()) expect := new(bytes.Buffer) enc := json.NewEncoder(expect) - enc.Encode(cwe) + err = enc.Encode(cwe) + Expect(err).ShouldNot(HaveOccurred()) expectation := stripString(expect.String()) Expect(result).To(ContainSubstring(expectation)) @@ -399,10 +410,11 @@ var _ = Describe("Formatter", func() { error := map[string][]gosec.Error{} buf := new(bytes.Buffer) - CreateReport(buf, "golint", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + err := CreateReport(buf, "golint", []string{}, []*gosec.Issue{&issue}, &gosec.Metrics{}, error) + Expect(err).ShouldNot(HaveOccurred()) pattern := "/home/src/project/test.go:1:1: [CWE-%s] test (Rule:%s, Severity:HIGH, Confidence:HIGH)\n" expect := fmt.Sprintf(pattern, cwe.ID, rule) - Expect(string(buf.Bytes())).To(Equal(expect)) + Expect(string(buf.String())).To(Equal(expect)) } }) })