report_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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: ".xml",
  69. findings: []Finding{
  70. {
  71. RuleID: "test-rule",
  72. },
  73. },
  74. },
  75. {
  76. ext: "junit",
  77. findings: []Finding{
  78. {
  79. RuleID: "test-rule",
  80. },
  81. },
  82. },
  83. // {
  84. // ext: "SARIF",
  85. // findings: []Finding{
  86. // {
  87. // RuleID: "test-rule",
  88. // },
  89. // },
  90. // },
  91. }
  92. for i, test := range tests {
  93. tmpfile, err := os.Create(filepath.Join(t.TempDir(), strconv.Itoa(i)+test.ext))
  94. if err != nil {
  95. t.Error(err)
  96. }
  97. err = Write(test.findings, config.Config{}, test.ext, tmpfile.Name())
  98. if err != nil {
  99. t.Error(err)
  100. }
  101. got, err := os.ReadFile(tmpfile.Name())
  102. if err != nil {
  103. t.Error(err)
  104. }
  105. if len(got) == 0 && !test.wantEmpty {
  106. t.Errorf("got empty file with extension " + test.ext)
  107. }
  108. if test.wantEmpty {
  109. if len(got) > 0 {
  110. t.Errorf("Expected empty file, got %s", got)
  111. }
  112. continue
  113. }
  114. }
  115. }