scan_test.go 20 KB

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