baseline_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package detect
  2. import (
  3. "errors"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/zricethezav/gitleaks/v8/report"
  7. )
  8. func TestIsNew(t *testing.T) {
  9. tests := []struct {
  10. findings report.Finding
  11. baseline []report.Finding
  12. expect bool
  13. }{
  14. {
  15. findings: report.Finding{
  16. Author: "a",
  17. Commit: "0000",
  18. },
  19. baseline: []report.Finding{
  20. {
  21. Author: "a",
  22. Commit: "0000",
  23. },
  24. },
  25. expect: false,
  26. },
  27. {
  28. findings: report.Finding{
  29. Author: "a",
  30. Commit: "0000",
  31. },
  32. baseline: []report.Finding{
  33. {
  34. Author: "a",
  35. Commit: "0002",
  36. },
  37. },
  38. expect: true,
  39. },
  40. {
  41. findings: report.Finding{
  42. Author: "a",
  43. Commit: "0000",
  44. Tags: []string{"a", "b"},
  45. },
  46. baseline: []report.Finding{
  47. {
  48. Author: "a",
  49. Commit: "0000",
  50. Tags: []string{"a", "c"},
  51. },
  52. },
  53. expect: false, // Updated tags doesn't make it a new finding
  54. },
  55. }
  56. for _, test := range tests {
  57. assert.Equal(t, test.expect, IsNew(test.findings, test.baseline))
  58. }
  59. }
  60. func TestFileLoadBaseline(t *testing.T) {
  61. tests := []struct {
  62. Filename string
  63. ExpectedError error
  64. }{
  65. {
  66. Filename: "../testdata/baseline/baseline.csv",
  67. ExpectedError: errors.New("the format of the file ../testdata/baseline/baseline.csv is not supported"),
  68. },
  69. {
  70. Filename: "../testdata/baseline/baseline.sarif",
  71. ExpectedError: errors.New("the format of the file ../testdata/baseline/baseline.sarif is not supported"),
  72. },
  73. {
  74. Filename: "../testdata/baseline/notfound.json",
  75. ExpectedError: errors.New("could not open ../testdata/baseline/notfound.json"),
  76. },
  77. }
  78. for _, test := range tests {
  79. _, err := LoadBaseline(test.Filename)
  80. assert.Equal(t, test.ExpectedError.Error(), err.Error())
  81. }
  82. }
  83. func TestIgnoreIssuesInBaseline(t *testing.T) {
  84. tests := []struct {
  85. findings []report.Finding
  86. baseline []report.Finding
  87. expectCount int
  88. }{
  89. {
  90. findings: []report.Finding{
  91. {
  92. Author: "a",
  93. Commit: "5",
  94. },
  95. },
  96. baseline: []report.Finding{
  97. {
  98. Author: "a",
  99. Commit: "5",
  100. },
  101. },
  102. expectCount: 0,
  103. },
  104. {
  105. findings: []report.Finding{
  106. {
  107. Author: "a",
  108. Commit: "5",
  109. Fingerprint: "a",
  110. },
  111. },
  112. baseline: []report.Finding{
  113. {
  114. Author: "a",
  115. Commit: "5",
  116. Fingerprint: "b",
  117. },
  118. },
  119. expectCount: 0,
  120. },
  121. }
  122. for _, test := range tests {
  123. d, _ := NewDetectorDefaultConfig()
  124. d.baseline = test.baseline
  125. for _, finding := range test.findings {
  126. d.addFinding(finding)
  127. }
  128. assert.Equal(t, test.expectCount, len(d.findings))
  129. }
  130. }