scan_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. package scan
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "reflect"
  8. "runtime"
  9. "sort"
  10. "testing"
  11. "github.com/zricethezav/gitleaks/v6/config"
  12. "github.com/zricethezav/gitleaks/v6/manager"
  13. "github.com/zricethezav/gitleaks/v6/options"
  14. "github.com/sergi/go-diff/diffmatchpatch"
  15. )
  16. const testRepoBase = "../test_data/test_repos/"
  17. func TestScan(t *testing.T) {
  18. moveDotGit("dotGit", ".git")
  19. defer moveDotGit(".git", "dotGit")
  20. tests := []struct {
  21. description string
  22. opts options.Options
  23. wantPath string
  24. wantErr error
  25. emptyRepo bool
  26. wantEmpty bool
  27. }{
  28. {
  29. description: "test local repo one aws leak",
  30. opts: options.Options{
  31. RepoPath: "../test_data/test_repos/test_repo_1",
  32. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  33. ReportFormat: "json",
  34. },
  35. wantPath: "../test_data/test_local_repo_one_aws_leak.json",
  36. },
  37. {
  38. description: "test local repo one aws leak threaded",
  39. opts: options.Options{
  40. Threads: runtime.GOMAXPROCS(0),
  41. RepoPath: "../test_data/test_repos/test_repo_1",
  42. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  43. ReportFormat: "json",
  44. },
  45. wantPath: "../test_data/test_local_repo_one_aws_leak.json",
  46. },
  47. {
  48. description: "test non existent repo",
  49. opts: options.Options{
  50. RepoPath: "../test_data/test_repos/no_repo_here",
  51. ReportFormat: "json",
  52. },
  53. emptyRepo: true,
  54. },
  55. {
  56. description: "test local repo one aws leak allowlisted",
  57. opts: options.Options{
  58. RepoPath: "../test_data/test_repos/test_repo_1",
  59. ReportFormat: "json",
  60. Config: "../test_data/test_configs/aws_key_allowlist_python_files.toml",
  61. },
  62. wantEmpty: true,
  63. },
  64. {
  65. description: "test local repo two leaks",
  66. opts: options.Options{
  67. RepoPath: "../test_data/test_repos/test_repo_2",
  68. Report: "../test_data/test_local_repo_two_leaks.json.got",
  69. ReportFormat: "json",
  70. },
  71. wantPath: "../test_data/test_local_repo_two_leaks.json",
  72. },
  73. {
  74. description: "test local repo two leaks from Commit",
  75. opts: options.Options{
  76. RepoPath: "../test_data/test_repos/test_repo_2",
  77. Report: "../test_data/test_local_repo_two_leaks_commit_from.json.got",
  78. ReportFormat: "json",
  79. CommitFrom: "996865bb912f3bc45898a370a13aadb315014b55",
  80. },
  81. wantPath: "../test_data/test_local_repo_two_leaks_commit_from.json",
  82. },
  83. {
  84. description: "test local repo two leaks to Commit",
  85. opts: options.Options{
  86. RepoPath: "../test_data/test_repos/test_repo_2",
  87. Report: "../test_data/test_local_repo_two_leaks_commit_to.json.got",
  88. ReportFormat: "json",
  89. CommitTo: "996865bb912f3bc45898a370a13aadb315014b55",
  90. },
  91. wantPath: "../test_data/test_local_repo_two_leaks_commit_to.json",
  92. },
  93. {
  94. description: "test local repo two leaks to from Commit",
  95. opts: options.Options{
  96. RepoPath: "../test_data/test_repos/test_repo_2",
  97. Report: "../test_data/test_local_repo_two_leaks_commit_to_from.json.got",
  98. ReportFormat: "json",
  99. CommitFrom: "d8ac0b73aeeb45843319cdc5ce506516eb49bf7a",
  100. CommitTo: "51f6dcf6b89b93f4075ba92c400b075631a6cc93",
  101. },
  102. wantPath: "../test_data/test_local_repo_two_leaks_commit_to_from.json",
  103. },
  104. {
  105. description: "test local repo two leaks list Commits",
  106. opts: options.Options{
  107. RepoPath: "../test_data/test_repos/test_repo_2",
  108. Report: "../test_data/test_local_repo_two_leaks_commit_range.json.got",
  109. ReportFormat: "json",
  110. Commits: "d8ac0b73aeeb45843319cdc5ce506516eb49bf7a,996865bb912f3bc45898a370a13aadb315014b55,17471a5fda722a9e423f1a0d3f0d267ea009d41c,51f6dcf6b89b93f4075ba92c400b075631a6cc93,b10b3e2cb320a8c211fda94c4567299d37de7776",
  111. },
  112. wantPath: "../test_data/test_local_repo_two_leaks_commit_range.json",
  113. },
  114. {
  115. description: "test local repo two leaks file list commits",
  116. opts: options.Options{
  117. RepoPath: "../test_data/test_repos/test_repo_2",
  118. Report: "../test_data/test_local_repo_two_leaks_file_commit_range.json.got",
  119. ReportFormat: "json",
  120. CommitsFile: "../test_data/test_options/test_local_repo_commits.txt",
  121. },
  122. wantPath: "../test_data/test_local_repo_two_leaks_file_commit_range.json",
  123. },
  124. {
  125. description: "test local repo two leaks globally allowlisted",
  126. opts: options.Options{
  127. RepoPath: "../test_data/test_repos/test_repo_2",
  128. Config: "../test_data/test_configs/aws_key_global_allowlist_file.toml",
  129. ReportFormat: "json",
  130. },
  131. wantEmpty: true,
  132. },
  133. {
  134. description: "test local repo two leaks allowlisted",
  135. opts: options.Options{
  136. RepoPath: "../test_data/test_repos/test_repo_2",
  137. Config: "../test_data/test_configs/aws_key_allowlist_files.toml",
  138. ReportFormat: "json",
  139. },
  140. wantEmpty: true,
  141. },
  142. {
  143. description: "test local repo three leaks dev branch with reportGroup set",
  144. opts: options.Options{
  145. RepoPath: "../test_data/test_repos/test_repo_3",
  146. Report: "../test_data/test_local_repo_three_leaks_with_report_groups.json.got",
  147. Config: "../test_data/test_configs/aws_key_with_report_groups.toml",
  148. Branch: "dev",
  149. ReportFormat: "json",
  150. },
  151. wantPath: "../test_data/test_local_repo_three_leaks_with_report_groups.json",
  152. },
  153. {
  154. description: "test local repo three leaks dev branch",
  155. opts: options.Options{
  156. RepoPath: "../test_data/test_repos/test_repo_3",
  157. Report: "../test_data/test_local_repo_three_leaks.json.got",
  158. Config: "../test_data/test_configs/aws_key.toml",
  159. Branch: "dev",
  160. ReportFormat: "json",
  161. },
  162. wantPath: "../test_data/test_local_repo_three_leaks.json",
  163. },
  164. {
  165. description: "test local repo branch does not exist",
  166. opts: options.Options{
  167. RepoPath: "../test_data/test_repos/test_repo_3",
  168. Branch: "nobranch",
  169. ReportFormat: "json",
  170. },
  171. wantEmpty: true,
  172. },
  173. {
  174. description: "test local repo one aws leak single Commit",
  175. opts: options.Options{
  176. RepoPath: "../test_data/test_repos/test_repo_1",
  177. Report: "../test_data/test_local_repo_one_aws_leak_commit.json.got",
  178. Commit: "6557c92612d3b35979bd426d429255b3bf9fab74",
  179. ReportFormat: "json",
  180. },
  181. wantPath: "../test_data/test_local_repo_one_aws_leak_commit.json",
  182. },
  183. {
  184. description: "test local repo one aws leak AND leak on python files",
  185. opts: options.Options{
  186. RepoPath: "../test_data/test_repos/test_repo_1",
  187. Report: "../test_data/test_local_repo_one_aws_leak_and_file_leak.json.got",
  188. Config: "../test_data/test_configs/aws_key_file_regex.toml",
  189. ReportFormat: "json",
  190. },
  191. wantPath: "../test_data/test_local_repo_one_aws_leak_and_file_leak.json",
  192. },
  193. {
  194. description: "test owner path",
  195. opts: options.Options{
  196. OwnerPath: "../test_data/test_repos/",
  197. Report: "../test_data/test_local_owner_aws_leak.json.got",
  198. ReportFormat: "json",
  199. },
  200. wantPath: "../test_data/test_local_owner_aws_leak.json",
  201. },
  202. {
  203. description: "test owner path allowlist repo",
  204. opts: options.Options{
  205. OwnerPath: "../test_data/test_repos/",
  206. Report: "../test_data/test_local_owner_aws_leak_allowlist_repo.json.got",
  207. ReportFormat: "json",
  208. Config: "../test_data/test_configs/aws_key_local_owner_allowlist_repo.toml",
  209. },
  210. wantPath: "../test_data/test_local_owner_aws_leak_allowlist_repo.json",
  211. },
  212. {
  213. description: "test entropy and regex",
  214. opts: options.Options{
  215. RepoPath: "../test_data/test_repos/test_repo_1",
  216. Report: "../test_data/test_regex_entropy.json.got",
  217. Config: "../test_data/test_configs/regex_entropy.toml",
  218. ReportFormat: "json",
  219. },
  220. wantPath: "../test_data/test_regex_entropy.json",
  221. },
  222. {
  223. description: "test local repo four entropy alternative config",
  224. opts: options.Options{
  225. RepoPath: "../test_data/test_repos/test_repo_4",
  226. Report: "../test_data/test_local_repo_four_alt_config_entropy.json.got",
  227. RepoConfig: true,
  228. ReportFormat: "json",
  229. },
  230. wantPath: "../test_data/test_local_repo_four_alt_config_entropy.json",
  231. },
  232. {
  233. description: "test local repo four entropy alternative config",
  234. opts: options.Options{
  235. RepoPath: "../test_data/test_repos/test_repo_1",
  236. Report: "../test_data/test_regex_allowlist.json.got",
  237. Config: "../test_data/test_configs/aws_key_aws_allowlisted.toml",
  238. ReportFormat: "json",
  239. },
  240. wantEmpty: true,
  241. },
  242. {
  243. description: "test local repo one aws leak timeout",
  244. opts: options.Options{
  245. RepoPath: "../test_data/test_repos/test_repo_1",
  246. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  247. ReportFormat: "json",
  248. Timeout: "10ns",
  249. },
  250. wantEmpty: true,
  251. },
  252. {
  253. description: "test local repo one aws leak long timeout",
  254. opts: options.Options{
  255. RepoPath: "../test_data/test_repos/test_repo_1",
  256. Report: "../test_data/test_local_repo_one_aws_leak.json.got",
  257. ReportFormat: "json",
  258. Timeout: "2m",
  259. },
  260. wantPath: "../test_data/test_local_repo_one_aws_leak.json",
  261. },
  262. {
  263. description: "test owner path depth=2",
  264. opts: options.Options{
  265. OwnerPath: "../test_data/test_repos/",
  266. Report: "../test_data/test_local_owner_aws_leak_depth_2.json.got",
  267. ReportFormat: "json",
  268. Depth: 2,
  269. },
  270. wantPath: "../test_data/test_local_owner_aws_leak_depth_2.json",
  271. },
  272. {
  273. description: "test local repo five files at Commit",
  274. opts: options.Options{
  275. RepoPath: "../test_data/test_repos/test_repo_5",
  276. Report: "../test_data/test_local_repo_five_files_at_commit.json.got",
  277. FilesAtCommit: "a4c9fb737d5552fd96fce5cc7eedb23353ba9ed0",
  278. ReportFormat: "json",
  279. },
  280. wantPath: "../test_data/test_local_repo_five_files_at_commit.json",
  281. },
  282. {
  283. description: "test local repo five files at latest Commit",
  284. opts: options.Options{
  285. RepoPath: "../test_data/test_repos/test_repo_5",
  286. Report: "../test_data/test_local_repo_five_files_at_latest_commit.json.got",
  287. FilesAtCommit: "latest",
  288. ReportFormat: "json",
  289. },
  290. wantPath: "../test_data/test_local_repo_five_files_at_commit.json",
  291. },
  292. {
  293. description: "test local repo five at Commit",
  294. opts: options.Options{
  295. RepoPath: "../test_data/test_repos/test_repo_5",
  296. Report: "../test_data/test_local_repo_five_commit.json.got",
  297. Commit: "a4c9fb737d5552fd96fce5cc7eedb23353ba9ed0",
  298. ReportFormat: "json",
  299. Config: "../test_data/test_configs/generic.toml",
  300. },
  301. wantPath: "../test_data/test_local_repo_five_commit.json",
  302. },
  303. {
  304. description: "test local repo five at latest Commit",
  305. opts: options.Options{
  306. RepoPath: "../test_data/test_repos/test_repo_5",
  307. Report: "../test_data/test_local_repo_five_at_latest_commit.json.got",
  308. Commit: "latest",
  309. ReportFormat: "json",
  310. Config: "../test_data/test_configs/generic.toml",
  311. },
  312. wantPath: "../test_data/test_local_repo_five_at_latest_commit.json",
  313. },
  314. {
  315. description: "test local repo six filename",
  316. opts: options.Options{
  317. RepoPath: "../test_data/test_repos/test_repo_6",
  318. Report: "../test_data/test_local_repo_six_filename.json.got",
  319. Config: "../test_data/test_configs/regex_filename.toml",
  320. ReportFormat: "json",
  321. },
  322. wantPath: "../test_data/test_local_repo_six_filename.json",
  323. },
  324. {
  325. description: "test local repo six filepath",
  326. opts: options.Options{
  327. RepoPath: "../test_data/test_repos/test_repo_6",
  328. Report: "../test_data/test_local_repo_six_filepath.json.got",
  329. Config: "../test_data/test_configs/regex_filepath.toml",
  330. ReportFormat: "json",
  331. },
  332. wantPath: "../test_data/test_local_repo_six_filepath.json",
  333. },
  334. {
  335. description: "test local repo six filename and filepath",
  336. opts: options.Options{
  337. RepoPath: "../test_data/test_repos/test_repo_6",
  338. Report: "../test_data/test_local_repo_six_filepath_filename.json.got",
  339. Config: "../test_data/test_configs/regex_filepath_filename.toml",
  340. ReportFormat: "json",
  341. },
  342. wantPath: "../test_data/test_local_repo_six_filepath_filename.json",
  343. },
  344. {
  345. description: "test local repo six path globally allowlisted",
  346. opts: options.Options{
  347. RepoPath: "../test_data/test_repos/test_repo_6",
  348. Report: "../test_data/test_local_repo_six_path_globally_allowlisted.json.got",
  349. Config: "../test_data/test_configs/aws_key_global_allowlist_path.toml",
  350. ReportFormat: "json",
  351. },
  352. wantPath: "../test_data/test_local_repo_six_path_globally_allowlisted.json",
  353. },
  354. {
  355. description: "test local repo six leaks since date",
  356. opts: options.Options{
  357. RepoPath: "../test_data/test_repos/test_repo_6",
  358. Report: "../test_data/test_local_repo_six_leaks_since_date.json.got",
  359. ReportFormat: "json",
  360. CommitSince: "2019-10-25",
  361. },
  362. wantPath: "../test_data/test_local_repo_six_leaks_since_date.json",
  363. },
  364. {
  365. description: "test local repo two leaks until date",
  366. opts: options.Options{
  367. RepoPath: "../test_data/test_repos/test_repo_6",
  368. Report: "../test_data/test_local_repo_six_leaks_until_date.json.got",
  369. ReportFormat: "json",
  370. CommitUntil: "2019-10-25",
  371. },
  372. wantPath: "../test_data/test_local_repo_six_leaks_until_date.json",
  373. },
  374. {
  375. description: "test local repo four leaks timerange Commit",
  376. opts: options.Options{
  377. RepoPath: "../test_data/test_repos/test_repo_4",
  378. Report: "../test_data/test_local_repo_four_leaks_commit_timerange.json.got",
  379. ReportFormat: "json",
  380. CommitSince: "2019-10-25T13:01:27-0400",
  381. CommitUntil: "2019-10-25T13:12:32-0400",
  382. },
  383. wantPath: "../test_data/test_local_repo_four_leaks_commit_timerange.json",
  384. },
  385. {
  386. description: "test local repo two allowlist Commit config",
  387. opts: options.Options{
  388. RepoPath: "../test_data/test_repos/test_repo_2",
  389. Report: "../test_data/test_local_repo_two_allowlist_commits.json.got",
  390. Config: "../test_data/test_configs/allowlist_commit.toml",
  391. ReportFormat: "json",
  392. },
  393. wantPath: "../test_data/test_local_repo_two_allowlist_commits.json",
  394. },
  395. {
  396. description: "test local repo two deletion",
  397. opts: options.Options{
  398. RepoPath: "../test_data/test_repos/test_repo_2",
  399. Report: "../test_data/test_local_repo_two_leaks_deletion.json.got",
  400. ReportFormat: "json",
  401. Deletion: true,
  402. },
  403. wantPath: "../test_data/test_local_repo_two_leaks_deletion.json",
  404. },
  405. {
  406. description: "test local repo eight (merges)",
  407. opts: options.Options{
  408. RepoPath: "../test_data/test_repos/test_repo_8",
  409. Report: "../test_data/test_local_repo_eight.json.got",
  410. ReportFormat: "json",
  411. },
  412. wantPath: "../test_data/test_local_repo_eight.json",
  413. },
  414. {
  415. description: "test local repo nine",
  416. opts: options.Options{
  417. RepoPath: "../test_data/test_repos/test_repo_9",
  418. Report: "../test_data/test_local_repo_nine_aws_leak.json.got",
  419. Config: "../test_data/test_configs/large_with_global_allowlist_regex.toml",
  420. ReportFormat: "json",
  421. },
  422. wantPath: "../test_data/test_local_repo_nine_aws_leak.json",
  423. },
  424. }
  425. for _, test := range tests {
  426. fmt.Println(test.description)
  427. cfg, err := config.NewConfig(test.opts)
  428. if err != nil {
  429. t.Error(err)
  430. }
  431. m, err := manager.NewManager(test.opts, cfg)
  432. if err != nil {
  433. t.Error(err)
  434. }
  435. err = Run(m)
  436. if test.wantErr != nil {
  437. if err == nil {
  438. t.Errorf("did not receive wantErr: %v", test.wantErr)
  439. }
  440. if err.Error() != test.wantErr.Error() {
  441. t.Errorf("wantErr does not equal err received: %v", err.Error())
  442. }
  443. continue
  444. }
  445. err = m.Report()
  446. if err != nil {
  447. t.Error(err)
  448. }
  449. if test.wantEmpty {
  450. if len(m.GetLeaks()) != 0 {
  451. t.Errorf("wanted no leaks but got some instead: %+v", m.GetLeaks())
  452. }
  453. continue
  454. }
  455. if test.wantPath != "" {
  456. err := fileCheck(test.wantPath, test.opts.Report)
  457. if err != nil {
  458. t.Error(err)
  459. }
  460. }
  461. }
  462. }
  463. func TestScanUncommited(t *testing.T) {
  464. moveDotGit("dotGit", ".git")
  465. defer moveDotGit(".git", "dotGit")
  466. tests := []struct {
  467. description string
  468. opts options.Options
  469. wantPath string
  470. wantErr error
  471. emptyRepo bool
  472. wantEmpty bool
  473. fileToChange string
  474. addition string
  475. }{
  476. {
  477. description: "test scan local one leak",
  478. opts: options.Options{
  479. RepoPath: "../test_data/test_repos/test_repo_1",
  480. Report: "../test_data/test_local_repo_one_aws_leak_uncommitted.json.got",
  481. Uncommited: true,
  482. ReportFormat: "json",
  483. },
  484. wantPath: "../test_data/test_local_repo_one_aws_leak_uncommitted.json",
  485. fileToChange: "server.test.py",
  486. addition: " aws_access_key_id='AKIAIO5FODNN7DXAMPLE'\n\n",
  487. },
  488. {
  489. description: "test scan local no leak",
  490. opts: options.Options{
  491. RepoPath: "../test_data/test_repos/test_repo_1",
  492. Uncommited: true,
  493. ReportFormat: "json",
  494. },
  495. wantEmpty: true,
  496. fileToChange: "server.test.py",
  497. addition: "nothing bad",
  498. },
  499. {
  500. description: "test scan repo with no commits",
  501. opts: options.Options{
  502. RepoPath: "../test_data/test_repos/test_repo_7",
  503. Report: "../test_data/test_local_repo_seven_aws_leak_uncommitted.json.got",
  504. Uncommited: true,
  505. ReportFormat: "json",
  506. },
  507. wantPath: "../test_data/test_local_repo_seven_aws_leak_uncommitted.json",
  508. },
  509. }
  510. for _, test := range tests {
  511. var (
  512. old []byte
  513. err error
  514. )
  515. fmt.Println(test.description)
  516. if test.fileToChange != "" {
  517. old, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange))
  518. if err != nil {
  519. t.Error(err)
  520. }
  521. altered, err := os.OpenFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange),
  522. os.O_WRONLY|os.O_APPEND, 0644)
  523. if err != nil {
  524. t.Error(err)
  525. }
  526. _, err = altered.WriteString(test.addition)
  527. if err != nil {
  528. t.Error(err)
  529. }
  530. }
  531. cfg, err := config.NewConfig(test.opts)
  532. if err != nil {
  533. t.Error(err)
  534. }
  535. m, err := manager.NewManager(test.opts, cfg)
  536. if err != nil {
  537. t.Error(err)
  538. }
  539. if err := Run(m); err != nil {
  540. t.Error(err)
  541. }
  542. if err := m.Report(); err != nil {
  543. t.Error(err)
  544. }
  545. if test.fileToChange != "" {
  546. err = ioutil.WriteFile(fmt.Sprintf("%s/%s", test.opts.RepoPath, test.fileToChange), old, 0)
  547. if err != nil {
  548. t.Error(err)
  549. }
  550. }
  551. if test.wantEmpty {
  552. continue
  553. }
  554. if test.wantPath != "" {
  555. err := fileCheck(test.wantPath, test.opts.Report)
  556. if err != nil {
  557. t.Error(err)
  558. }
  559. }
  560. }
  561. }
  562. func fileCheck(wantPath, gotPath string) error {
  563. var (
  564. gotLeaks []manager.Leak
  565. wantLeaks []manager.Leak
  566. )
  567. want, err := ioutil.ReadFile(wantPath)
  568. if err != nil {
  569. return err
  570. }
  571. got, err := ioutil.ReadFile(gotPath)
  572. if err != nil {
  573. return err
  574. }
  575. err = json.Unmarshal(got, &gotLeaks)
  576. if err != nil {
  577. return err
  578. }
  579. err = json.Unmarshal(want, &wantLeaks)
  580. if err != nil {
  581. return nil
  582. }
  583. sort.Slice(gotLeaks, func(i, j int) bool { return (gotLeaks)[i].Commit < (gotLeaks)[j].Commit })
  584. sort.Slice(wantLeaks, func(i, j int) bool { return (wantLeaks)[i].Commit < (wantLeaks)[j].Commit })
  585. if !reflect.DeepEqual(gotLeaks, wantLeaks) {
  586. dmp := diffmatchpatch.New()
  587. diffs := dmp.DiffMain(string(want), string(got), false)
  588. return fmt.Errorf("%s does not equal %s: %s", wantPath, gotPath, dmp.DiffPrettyText(diffs))
  589. }
  590. if err := os.Remove(gotPath); err != nil {
  591. return err
  592. }
  593. return nil
  594. }
  595. func moveDotGit(from, to string) error {
  596. repoDirs, err := ioutil.ReadDir("../test_data/test_repos")
  597. if err != nil {
  598. return err
  599. }
  600. for _, dir := range repoDirs {
  601. if !dir.IsDir() {
  602. continue
  603. }
  604. err = os.Rename(fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), from),
  605. fmt.Sprintf("%s/%s/%s", testRepoBase, dir.Name(), to))
  606. if err != nil {
  607. return err
  608. }
  609. }
  610. return nil
  611. }