Prevent null pointer exception in Sonarqube (#334)

* fix(formatters) null value causes npe in sonarqube

the json encoding of uninitialized arrays is null. this causes a npe in
sonarqube tool. we should return an empty array rather than a null value
here.

relates to: #333
This commit is contained in:
Grant Murphy 2019-07-09 13:36:09 +10:00 committed by GitHub
parent 39f7e7b9e0
commit 4b59c94808
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 11 deletions

View file

@ -117,8 +117,8 @@ func reportSonarqube(rootPaths []string, w io.Writer, data *reportInfo) error {
return err
}
func convertToSonarIssues(rootPaths []string, data *reportInfo) (sonarIssues, error) {
var si sonarIssues
func convertToSonarIssues(rootPaths []string, data *reportInfo) (*sonarIssues, error) {
si := &sonarIssues{[]sonarIssue{}}
for _, issue := range data.Issues {
var sonarFilePath string
for _, rootPath := range rootPaths {

View file

@ -32,7 +32,7 @@ var _ = Describe("Formatter", func() {
NumFound: 0,
},
}
want := sonarIssues{
want := &sonarIssues{
SonarIssues: []sonarIssue{
{
EngineID: "gosec",
@ -56,7 +56,7 @@ var _ = Describe("Formatter", func() {
issues, err := convertToSonarIssues([]string{rootPath}, data)
Expect(err).ShouldNot(HaveOccurred())
Expect(issues).To(Equal(want))
Expect(*issues).To(Equal(*want))
})
It("it should parse the report info with files in subfolders", func() {
@ -80,7 +80,7 @@ var _ = Describe("Formatter", func() {
NumFound: 0,
},
}
want := sonarIssues{
want := &sonarIssues{
SonarIssues: []sonarIssue{
{
EngineID: "gosec",
@ -104,7 +104,7 @@ var _ = Describe("Formatter", func() {
issues, err := convertToSonarIssues([]string{rootPath}, data)
Expect(err).ShouldNot(HaveOccurred())
Expect(issues).To(Equal(want))
Expect(*issues).To(Equal(*want))
})
It("it should not parse the report info for files from other projects", func() {
data := &reportInfo{
@ -127,15 +127,15 @@ var _ = Describe("Formatter", func() {
NumFound: 0,
},
}
want := sonarIssues{
SonarIssues: nil,
want := &sonarIssues{
SonarIssues: []sonarIssue{},
}
rootPath := "/home/src/project2"
issues, err := convertToSonarIssues([]string{rootPath}, data)
Expect(err).ShouldNot(HaveOccurred())
Expect(issues).To(Equal(want))
Expect(*issues).To(Equal(*want))
})
It("it should parse the report info for multiple projects projects", func() {
@ -168,7 +168,7 @@ var _ = Describe("Formatter", func() {
NumFound: 0,
},
}
want := sonarIssues{
want := &sonarIssues{
SonarIssues: []sonarIssue{
{
EngineID: "gosec",
@ -207,7 +207,7 @@ var _ = Describe("Formatter", func() {
issues, err := convertToSonarIssues(rootPaths, data)
Expect(err).ShouldNot(HaveOccurred())
Expect(issues).To(Equal(want))
Expect(*issues).To(Equal(*want))
})
})
})