baseline_test.go 2.6 KB

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