forked from Checkmarx/kics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsarif_test.go
65 lines (58 loc) · 2.03 KB
/
sarif_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package report
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/Checkmarx/kics/pkg/model"
reportModel "github.com/Checkmarx/kics/pkg/report/model"
"github.com/Checkmarx/kics/test"
"github.com/stretchr/testify/require"
)
type reportTestCase struct {
caseTest jsonCaseTest
expectedResult model.Summary
}
type sarifReport struct {
basePath string `json:"-"`
Schema string `json:"$schema"`
SarifVersion string `json:"version"`
Runs []reportModel.SarifRun `json:"runs"`
}
var sarifTests = []reportTestCase{
{
caseTest: jsonCaseTest{
summary: test.SummaryMock,
path: "./testdir",
filename: "testout",
},
expectedResult: test.SummaryMock,
},
}
// TestPrintSarifReport tests the functions [PrintSarifReport()] and all the methods called by them
func TestPrintSarifReport(t *testing.T) {
for idx, test := range sarifTests {
t.Run(fmt.Sprintf("Sarif File test case %d", idx), func(t *testing.T) {
if err := os.MkdirAll(test.caseTest.path, os.ModePerm); err != nil {
t.Fatal(err)
}
err := PrintSarifReport(test.caseTest.path, test.caseTest.filename, test.caseTest.summary)
checkFileExists(t, err, &test, "sarif")
jsonResult, err := os.ReadFile(filepath.Join(test.caseTest.path, test.caseTest.filename+".sarif"))
require.NoError(t, err)
var resultSarif sarifReport
err = json.Unmarshal(jsonResult, &resultSarif)
require.NoError(t, err)
require.Equal(t, "", resultSarif.basePath)
require.Equal(t, "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", resultSarif.Schema)
require.Equal(t, "2.1.0", resultSarif.SarifVersion)
require.Len(t, resultSarif.Runs, len(test.expectedResult.Queries))
os.RemoveAll(test.caseTest.path)
})
}
}
func checkFileExists(t *testing.T, err error, tc *reportTestCase, extension string) {
require.NoError(t, err)
require.FileExists(t, filepath.Join(tc.caseTest.path, tc.caseTest.filename+fmt.Sprintf(".%s", extension)))
}