commits_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package scan_test
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/zricethezav/gitleaks/v7/config"
  6. "github.com/zricethezav/gitleaks/v7/options"
  7. "github.com/zricethezav/gitleaks/v7/scan"
  8. )
  9. func TestCommitsScan(t *testing.T) {
  10. err := moveDotGit("dotGit", ".git")
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. defer moveDotGit(".git", "dotGit")
  15. tests := []struct {
  16. description string
  17. opts options.Options
  18. wantPath string
  19. empty bool
  20. }{
  21. {
  22. description: "basic repo with default config ranging first and third commit",
  23. opts: options.Options{
  24. Path: filepath.Join(repoBasePath, "basic"),
  25. Report: filepath.Join(expectPath, "basic", "results_ae8db4a2_208ae46.json.got"),
  26. ReportFormat: "json",
  27. Commits: "ae8db4a2306798fcb3a5b9cbe8c486027fc1931f,208ae4669ade2563fcaf9f12922fa2c0a5b37c63",
  28. },
  29. wantPath: filepath.Join(expectPath, "basic", "results_ae8db4a2_208ae46.json"),
  30. },
  31. {
  32. description: "repo with config first two commits",
  33. opts: options.Options{
  34. Path: filepath.Join(repoBasePath, "with_config"),
  35. Report: filepath.Join(expectPath, "with_config", "results_ae8db4a_e7c0aff.json.got"),
  36. ReportFormat: "json",
  37. RepoConfigPath: "gitleaks.toml",
  38. Commits: "ae8db4a2306798fcb3a5b9cbe8c486027fc1931f,e7c0aff3e8a60b50a85432fdf933f8beff013743",
  39. },
  40. wantPath: filepath.Join(expectPath, "with_config", "results_ae8db4a_e7c0aff.json"),
  41. },
  42. {
  43. description: "basic repo with depth=1",
  44. opts: options.Options{
  45. Path: filepath.Join(repoBasePath, "basic"),
  46. Report: filepath.Join(expectPath, "basic", "results_depth_1.json.got"),
  47. ReportFormat: "json",
  48. Depth: 1,
  49. },
  50. wantPath: filepath.Join(expectPath, "basic", "results_depth_1.json"),
  51. },
  52. {
  53. description: "basic repo with default config ranging first and third commit with a non-existent commit in middle",
  54. opts: options.Options{
  55. Path: filepath.Join(repoBasePath, "basic"),
  56. Report: filepath.Join(expectPath, "basic", "results_ae8db4a2_208ae46.json.got"),
  57. ReportFormat: "json",
  58. Commits: "ae8db4a2306798fcb3a5b9cbe8c486027fc1931f,nocommithere,208ae4669ade2563fcaf9f12922fa2c0a5b37c63",
  59. },
  60. wantPath: filepath.Join(expectPath, "basic", "results_ae8db4a2_208ae46.json"),
  61. },
  62. }
  63. for _, test := range tests {
  64. cfg, err := config.NewConfig(test.opts)
  65. if err != nil {
  66. t.Error(err)
  67. }
  68. scanner, err := scan.NewScanner(test.opts, cfg)
  69. if err != nil {
  70. t.Error(test.description, err)
  71. }
  72. scannerReport, err := scanner.Scan()
  73. if err != nil {
  74. t.Fatal(test.description, err)
  75. }
  76. err = scan.WriteReport(scannerReport, test.opts, cfg)
  77. if err != nil {
  78. t.Error(test.description, err)
  79. }
  80. if test.empty {
  81. if len(scannerReport.Leaks) != 0 {
  82. t.Errorf("%s wanted no leaks but got some instead: %+v", test.description, scannerReport.Leaks)
  83. }
  84. continue
  85. }
  86. if test.wantPath != "" {
  87. err := fileCheck(test.wantPath, test.opts.Report)
  88. if err != nil {
  89. t.Error(test.description, err)
  90. }
  91. }
  92. }
  93. }