filesatcommit_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 TestFilesAtCommitScan(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 no secrets present in files at first commit",
  23. opts: options.Options{
  24. Path: filepath.Join(repoBasePath, "basic"),
  25. Report: filepath.Join(expectPath, "basic", "results_files_at_ae8db4a2.json.got"),
  26. ReportFormat: "json",
  27. FilesAtCommit: "ae8db4a2306798fcb3a5b9cbe8c486027fc1931f",
  28. },
  29. empty: true,
  30. },
  31. {
  32. description: "basic repo with secrets present in files at third commit",
  33. opts: options.Options{
  34. Path: filepath.Join(repoBasePath, "basic"),
  35. Report: filepath.Join(expectPath, "basic", "results_files_at_208ae46.json.got"),
  36. ReportFormat: "json",
  37. FilesAtCommit: "208ae4669ade2563fcaf9f12922fa2c0a5b37c63",
  38. },
  39. wantPath: filepath.Join(expectPath, "basic", "results_files_at_208ae46.json"),
  40. },
  41. }
  42. for _, test := range tests {
  43. cfg, err := config.NewConfig(test.opts)
  44. if err != nil {
  45. t.Error(err)
  46. }
  47. scanner, err := scan.NewScanner(test.opts, cfg)
  48. if err != nil {
  49. t.Error(test.description, err)
  50. }
  51. scannerReport, err := scanner.Scan()
  52. if err != nil {
  53. t.Fatal(test.description, err)
  54. }
  55. err = scan.WriteReport(scannerReport, test.opts, cfg)
  56. if err != nil {
  57. t.Error(test.description, err)
  58. }
  59. if test.empty {
  60. if len(scannerReport.Leaks) != 0 {
  61. t.Errorf("%s wanted no leaks but got some instead: %+v", test.description, scannerReport.Leaks)
  62. }
  63. continue
  64. }
  65. if test.wantPath != "" {
  66. err := fileCheck(test.wantPath, test.opts.Report)
  67. if err != nil {
  68. t.Error(test.description, err)
  69. }
  70. }
  71. }
  72. }