| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package report
- import (
- "os"
- "path/filepath"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- func TestWriteJSON(t *testing.T) {
- tests := []struct {
- findings []Finding
- testReportName string
- expected string
- wantEmpty bool
- }{
- {
- testReportName: "simple",
- expected: filepath.Join(expectPath, "report", "json_simple.json"),
- findings: []Finding{
- {
- Description: "",
- RuleID: "test-rule",
- Match: "line containing secret",
- Secret: "a secret",
- StartLine: 1,
- EndLine: 2,
- StartColumn: 1,
- EndColumn: 2,
- Message: "opps",
- File: "auth.py",
- SymlinkFile: "",
- Commit: "0000000000000000",
- Author: "John Doe",
- Email: "johndoe@gmail.com",
- Date: "10-19-2003",
- Tags: []string{},
- },
- }},
- {
- testReportName: "empty",
- expected: filepath.Join(expectPath, "report", "empty.json"),
- findings: []Finding{}},
- }
- for _, test := range tests {
- t.Run(test.testReportName, func(t *testing.T) {
- tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".json"))
- require.NoError(t, err)
- err = writeJson(test.findings, tmpfile)
- require.NoError(t, err)
- assert.FileExists(t, tmpfile.Name())
- got, err := os.ReadFile(tmpfile.Name())
- require.NoError(t, err)
- if test.wantEmpty {
- assert.Empty(t, got)
- return
- }
- want, err := os.ReadFile(test.expected)
- require.NoError(t, err)
- assert.Equal(t, want, got)
- })
- }
- }
|