commit_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 TestCommitScan(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: "empty repo",
  23. opts: options.Options{
  24. Path: filepath.Join(repoBasePath, "empty"),
  25. Report: filepath.Join(expectPath, "empty", "empty_report.json.got"),
  26. },
  27. empty: true,
  28. },
  29. {
  30. description: "basic repo with default config at specific commit",
  31. opts: options.Options{
  32. Path: filepath.Join(repoBasePath, "basic"),
  33. Report: filepath.Join(expectPath, "basic", "results_208ae46.json.got"),
  34. ReportFormat: "json",
  35. Commit: "208ae4669ade2563fcaf9f12922fa2c0a5b37c63",
  36. },
  37. wantPath: filepath.Join(expectPath, "basic", "results_208ae46.json"),
  38. },
  39. {
  40. description: "basic repo with custom config at specific commit",
  41. opts: options.Options{
  42. Path: filepath.Join(repoBasePath, "with_config"),
  43. Report: filepath.Join(expectPath, "with_config", "results_e7c0aff3.json.got"),
  44. ReportFormat: "json",
  45. RepoConfigPath: "gitleaks.toml",
  46. Commit: "e7c0aff3e8a60b50a85432fdf933f8beff013743",
  47. },
  48. wantPath: filepath.Join(expectPath, "with_config", "results_e7c0aff3.json"),
  49. },
  50. }
  51. for _, test := range tests {
  52. cfg, err := config.NewConfig(test.opts)
  53. if err != nil {
  54. t.Error(err)
  55. }
  56. scanner, err := scan.NewScanner(test.opts, cfg)
  57. if err != nil {
  58. t.Error(test.description, err)
  59. }
  60. scannerReport, err := scanner.Scan()
  61. if err != nil {
  62. t.Fatal(test.description, err)
  63. }
  64. err = scan.WriteReport(scannerReport, test.opts, cfg)
  65. if err != nil {
  66. t.Error(test.description, err)
  67. }
  68. if test.empty {
  69. if len(scannerReport.Leaks) != 0 {
  70. t.Errorf("%s wanted no leaks but got some instead: %+v", test.description, scannerReport.Leaks)
  71. }
  72. continue
  73. }
  74. if test.wantPath != "" {
  75. err := fileCheck(test.wantPath, test.opts.Report)
  76. if err != nil {
  77. t.Error(test.description, err)
  78. }
  79. }
  80. }
  81. }