repo_test.go 2.0 KB

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