Skip to content

Commit

Permalink
Fix nil pointer dereference in JUnit reporter (#1307)
Browse files Browse the repository at this point in the history
Fixes #1306

Thanks @OhMyGuus for reporting the issue!

(Unrelated to the issue, but I'm also reverting the URL change of that integration
 test we changed recently... as the URL in OPA is now back to what it was before :)

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert authored Dec 18, 2024
1 parent 7f8e54d commit 748c2e8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/capabilities/capabilities_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestLookupFromURL(t *testing.T) {

caps, err := Lookup(
context.Background(),
"https://raw.githubusercontent.com/open-policy-agent/opa/refs/heads/main/v1/capabilities/v0.55.0.json",
"https://raw.githubusercontent.com/open-policy-agent/opa/main/capabilities/v0.55.0.json",
)
if err != nil {
t.Errorf("unexpected error from Lookup: %v", err)
Expand Down
7 changes: 6 additions & 1 deletion pkg/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ func (tr JUnitReporter) Publish(_ context.Context, r report.Report) error {
}

for _, violation := range violationsPerFile[file] {
text := ""
if violation.Location.Text != nil {
text = strings.TrimSpace(*violation.Location.Text)
}

testsuite.AddTestcase(junit.Testcase{
Name: fmt.Sprintf("%s/%s: %s", violation.Category, violation.Title, violation.Description),
Classname: violation.Location.String(),
Expand All @@ -522,7 +527,7 @@ func (tr JUnitReporter) Publish(_ context.Context, r report.Report) error {
violation.Description,
violation.Category,
violation.Location.String(),
strings.TrimSpace(*violation.Location.Text),
text,
getDocumentationURL(violation)),
},
})
Expand Down
11 changes: 11 additions & 0 deletions pkg/reporter/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,17 @@ func TestJUnitReporterPublishNoViolations(t *testing.T) {
}
}

func TestJUnitReporterPublishViolationWithoutText(t *testing.T) {
t.Parallel()

var buf bytes.Buffer
if err := NewJUnitReporter(&buf).Publish(context.Background(), report.Report{
Violations: []report.Violation{{Title: "no-text"}},
}); err != nil {
t.Fatal(err)
}
}

func MustReadFile(t *testing.T, path string) string {
t.Helper()

Expand Down

0 comments on commit 748c2e8

Please sign in to comment.