report_test.go 1.5 KB

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