sarif_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package report
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/zricethezav/gitleaks/v8/config"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestWriteSarif(t *testing.T) {
  11. tests := []struct {
  12. findings []Finding
  13. testReportName string
  14. expected string
  15. wantEmpty bool
  16. cfgName string
  17. }{
  18. {
  19. cfgName: "simple",
  20. testReportName: "simple",
  21. expected: filepath.Join(expectPath, "report", "sarif_simple.sarif"),
  22. findings: []Finding{
  23. {
  24. RuleID: "test-rule",
  25. Description: "A test rule",
  26. Match: "line containing secret",
  27. Secret: "a secret",
  28. StartLine: 1,
  29. EndLine: 2,
  30. StartColumn: 1,
  31. EndColumn: 2,
  32. Message: "opps",
  33. File: "auth.py",
  34. Commit: "0000000000000000",
  35. Author: "John Doe",
  36. Email: "johndoe@gmail.com",
  37. Date: "10-19-2003",
  38. Tags: []string{"tag1", "tag2", "tag3"},
  39. },
  40. }},
  41. }
  42. for _, test := range tests {
  43. t.Run(test.cfgName, func(t *testing.T) {
  44. tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".json"))
  45. require.NoError(t, err)
  46. reporter := SarifReporter{
  47. OrderedRules: []config.Rule{
  48. {
  49. RuleID: "aws-access-key",
  50. Description: "AWS Access Key",
  51. },
  52. {
  53. RuleID: "pypi",
  54. Description: "PyPI upload token",
  55. },
  56. },
  57. }
  58. err = reporter.Write(tmpfile, test.findings)
  59. require.NoError(t, err)
  60. assert.FileExists(t, tmpfile.Name())
  61. got, err := os.ReadFile(tmpfile.Name())
  62. require.NoError(t, err)
  63. if test.wantEmpty {
  64. assert.Empty(t, got)
  65. return
  66. }
  67. want, err := os.ReadFile(test.expected)
  68. require.NoError(t, err)
  69. assert.Equal(t, string(want), string(got))
  70. })
  71. }
  72. }