report_test.go 1.7 KB

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