report_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package report
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strconv"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "github.com/zricethezav/gitleaks/v8/config"
  10. )
  11. const (
  12. expectPath = "../testdata/expected/"
  13. )
  14. func TestReport(t *testing.T) {
  15. tests := []struct {
  16. findings []Finding
  17. ext string
  18. wantEmpty bool
  19. }{
  20. {
  21. ext: "json",
  22. findings: []Finding{
  23. {
  24. RuleID: "test-rule",
  25. },
  26. },
  27. },
  28. {
  29. ext: ".json",
  30. findings: []Finding{
  31. {
  32. RuleID: "test-rule",
  33. },
  34. },
  35. },
  36. {
  37. ext: ".jsonj",
  38. findings: []Finding{
  39. {
  40. RuleID: "test-rule",
  41. },
  42. },
  43. wantEmpty: true,
  44. },
  45. {
  46. ext: ".csv",
  47. findings: []Finding{
  48. {
  49. RuleID: "test-rule",
  50. },
  51. },
  52. },
  53. {
  54. ext: "csv",
  55. findings: []Finding{
  56. {
  57. RuleID: "test-rule",
  58. },
  59. },
  60. },
  61. {
  62. ext: "CSV",
  63. findings: []Finding{
  64. {
  65. RuleID: "test-rule",
  66. },
  67. },
  68. },
  69. {
  70. ext: ".xml",
  71. findings: []Finding{
  72. {
  73. RuleID: "test-rule",
  74. },
  75. },
  76. },
  77. {
  78. ext: "junit",
  79. findings: []Finding{
  80. {
  81. RuleID: "test-rule",
  82. },
  83. },
  84. },
  85. // {
  86. // ext: "SARIF",
  87. // findings: []Finding{
  88. // {
  89. // RuleID: "test-rule",
  90. // },
  91. // },
  92. // },
  93. }
  94. for i, test := range tests {
  95. t.Run(test.ext, func(t *testing.T) {
  96. tmpfile, err := os.Create(filepath.Join(t.TempDir(), strconv.Itoa(i)+test.ext))
  97. require.NoError(t, err)
  98. err = Write(test.findings, config.Config{}, test.ext, tmpfile.Name())
  99. require.NoError(t, err)
  100. got, err := os.ReadFile(tmpfile.Name())
  101. require.NoError(t, err)
  102. assert.FileExists(t, tmpfile.Name())
  103. if test.wantEmpty {
  104. assert.Empty(t, got)
  105. return
  106. }
  107. assert.NotEmpty(t, got)
  108. })
  109. }
  110. }