Skip to content

Commit

Permalink
test: add a case for custom marshaler
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <knqyf263@gmail.com>
  • Loading branch information
knqyf263 committed Sep 10, 2024
1 parent 2eb7523 commit 19ea9ed
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions pkg/report/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (
"testing"
"time"

"github.com/package-url/packageurl-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

dbTypes "github.com/aquasecurity/trivy-db/pkg/types"

"github.com/aquasecurity/trivy/pkg/clock"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/report"
"github.com/aquasecurity/trivy/pkg/types"
)
Expand Down Expand Up @@ -156,6 +159,33 @@ func TestReportWriter_Template(t *testing.T) {
template: `{{ $high := 0 }}{{ $critical := 0 }}{{ range . }}{{ range .Vulnerabilities}}{{ if eq .Severity "HIGH" }}{{ $high = add $high 1 }}{{ end }}{{ if eq .Severity "CRITICAL" }}{{ $critical = add $critical 1 }}{{ end }}{{ end }}Critical: {{ $critical }}, High: {{ $high }}{{ end }}`,
expected: `Critical: 2, High: 1`,
},
{
name: "custom JSON marshaler",
detectedVulns: []types.DetectedVulnerability{
{
VulnerabilityID: "CVE-2019-0000",
PkgName: "foo",
Status: dbTypes.StatusAffected,
PkgIdentifier: ftypes.PkgIdentifier{
PURL: &packageurl.PackageURL{
Type: packageurl.TypeNPM,
Name: "foobar",
Version: "1.2.3",
},
},
},
},
template: `{{ range . }}{{ range .Vulnerabilities}}{{ toPrettyJson . }}{{ end }}{{ end }}`,
expected: `{
"VulnerabilityID": "CVE-2019-0000",
"PkgName": "foo",
"PkgIdentifier": {
"PURL": "pkg:npm/foobar@1.2.3"
},
"Status": "affected",
"Layer": {}
}`,
},
{
name: "happy path: env var parsing",
detectedVulns: []types.DetectedVulnerability{},
Expand Down

0 comments on commit 19ea9ed

Please sign in to comment.