gitea源码

gitdiff_test.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package gitdiff
  5. import (
  6. "strconv"
  7. "strings"
  8. "testing"
  9. issues_model "code.gitea.io/gitea/models/issues"
  10. "code.gitea.io/gitea/models/unittest"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/git/gitcmd"
  14. "code.gitea.io/gitea/modules/json"
  15. "code.gitea.io/gitea/modules/setting"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/require"
  18. )
  19. func TestParsePatch_skipTo(t *testing.T) {
  20. type testcase struct {
  21. name string
  22. gitdiff string
  23. wantErr bool
  24. addition int
  25. deletion int
  26. oldFilename string
  27. filename string
  28. skipTo string
  29. }
  30. tests := []testcase{
  31. {
  32. name: "readme.md2readme.md",
  33. gitdiff: `diff --git "a/A \\ B" "b/A \\ B"
  34. --- "a/A \\ B"
  35. +++ "b/A \\ B"
  36. @@ -1,3 +1,6 @@
  37. # gitea-github-migrator
  38. +
  39. + Build Status
  40. - Latest Release
  41. Docker Pulls
  42. + cut off
  43. + cut off
  44. diff --git "\\a/README.md" "\\b/README.md"
  45. --- "\\a/README.md"
  46. +++ "\\b/README.md"
  47. @@ -1,3 +1,6 @@
  48. # gitea-github-migrator
  49. +
  50. + Build Status
  51. - Latest Release
  52. Docker Pulls
  53. + cut off
  54. + cut off
  55. `,
  56. addition: 4,
  57. deletion: 1,
  58. filename: "README.md",
  59. oldFilename: "README.md",
  60. skipTo: "README.md",
  61. },
  62. {
  63. name: "A \\ B",
  64. gitdiff: `diff --git "a/A \\ B" "b/A \\ B"
  65. --- "a/A \\ B"
  66. +++ "b/A \\ B"
  67. @@ -1,3 +1,6 @@
  68. # gitea-github-migrator
  69. +
  70. + Build Status
  71. - Latest Release
  72. Docker Pulls
  73. + cut off
  74. + cut off`,
  75. addition: 4,
  76. deletion: 1,
  77. filename: "A \\ B",
  78. oldFilename: "A \\ B",
  79. skipTo: "A \\ B",
  80. },
  81. {
  82. name: "A \\ B",
  83. gitdiff: `diff --git "\\a/README.md" "\\b/README.md"
  84. --- "\\a/README.md"
  85. +++ "\\b/README.md"
  86. @@ -1,3 +1,6 @@
  87. # gitea-github-migrator
  88. +
  89. + Build Status
  90. - Latest Release
  91. Docker Pulls
  92. + cut off
  93. + cut off
  94. diff --git "a/A \\ B" "b/A \\ B"
  95. --- "a/A \\ B"
  96. +++ "b/A \\ B"
  97. @@ -1,3 +1,6 @@
  98. # gitea-github-migrator
  99. +
  100. + Build Status
  101. - Latest Release
  102. Docker Pulls
  103. + cut off
  104. + cut off`,
  105. addition: 4,
  106. deletion: 1,
  107. filename: "A \\ B",
  108. oldFilename: "A \\ B",
  109. skipTo: "A \\ B",
  110. },
  111. {
  112. name: "readme.md2readme.md",
  113. gitdiff: `diff --git "a/A \\ B" "b/A \\ B"
  114. --- "a/A \\ B"
  115. +++ "b/A \\ B"
  116. @@ -1,3 +1,6 @@
  117. # gitea-github-migrator
  118. +
  119. + Build Status
  120. - Latest Release
  121. Docker Pulls
  122. + cut off
  123. + cut off
  124. diff --git "a/A \\ B" "b/A \\ B"
  125. --- "a/A \\ B"
  126. +++ "b/A \\ B"
  127. @@ -1,3 +1,6 @@
  128. # gitea-github-migrator
  129. +
  130. + Build Status
  131. - Latest Release
  132. Docker Pulls
  133. + cut off
  134. + cut off
  135. diff --git "\\a/README.md" "\\b/README.md"
  136. --- "\\a/README.md"
  137. +++ "\\b/README.md"
  138. @@ -1,3 +1,6 @@
  139. # gitea-github-migrator
  140. +
  141. + Build Status
  142. - Latest Release
  143. Docker Pulls
  144. + cut off
  145. + cut off
  146. `,
  147. addition: 4,
  148. deletion: 1,
  149. filename: "README.md",
  150. oldFilename: "README.md",
  151. skipTo: "README.md",
  152. },
  153. }
  154. for _, testcase := range tests {
  155. t.Run(testcase.name, func(t *testing.T) {
  156. got, err := ParsePatch(t.Context(), setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(testcase.gitdiff), testcase.skipTo)
  157. if (err != nil) != testcase.wantErr {
  158. t.Errorf("ParsePatch(%q) error = %v, wantErr %v", testcase.name, err, testcase.wantErr)
  159. return
  160. }
  161. gotMarshaled, _ := json.MarshalIndent(got, "", " ")
  162. if len(got.Files) != 1 {
  163. t.Errorf("ParsePath(%q) did not receive 1 file:\n%s", testcase.name, string(gotMarshaled))
  164. return
  165. }
  166. file := got.Files[0]
  167. if file.Addition != testcase.addition {
  168. t.Errorf("ParsePath(%q) does not have correct file addition %d, wanted %d", testcase.name, file.Addition, testcase.addition)
  169. }
  170. if file.Deletion != testcase.deletion {
  171. t.Errorf("ParsePath(%q) did not have correct file deletion %d, wanted %d", testcase.name, file.Deletion, testcase.deletion)
  172. }
  173. if file.OldName != testcase.oldFilename {
  174. t.Errorf("ParsePath(%q) did not have correct OldName %q, wanted %q", testcase.name, file.OldName, testcase.oldFilename)
  175. }
  176. if file.Name != testcase.filename {
  177. t.Errorf("ParsePath(%q) did not have correct Name %q, wanted %q", testcase.name, file.Name, testcase.filename)
  178. }
  179. })
  180. }
  181. }
  182. func TestParsePatch_singlefile(t *testing.T) {
  183. type testcase struct {
  184. name string
  185. gitdiff string
  186. wantErr bool
  187. addition int
  188. deletion int
  189. oldFilename string
  190. filename string
  191. }
  192. tests := []testcase{
  193. {
  194. name: "readme.md2readme.md",
  195. gitdiff: `diff --git "\\a/README.md" "\\b/README.md"
  196. --- "\\a/README.md"
  197. +++ "\\b/README.md"
  198. @@ -1,3 +1,6 @@
  199. # gitea-github-migrator
  200. +
  201. + Build Status
  202. - Latest Release
  203. Docker Pulls
  204. + cut off
  205. + cut off
  206. `,
  207. addition: 4,
  208. deletion: 1,
  209. filename: "README.md",
  210. oldFilename: "README.md",
  211. },
  212. {
  213. name: "A \\ B",
  214. gitdiff: `diff --git "a/A \\ B" "b/A \\ B"
  215. --- "a/A \\ B"
  216. +++ "b/A \\ B"
  217. @@ -1,3 +1,6 @@
  218. # gitea-github-migrator
  219. +
  220. + Build Status
  221. - Latest Release
  222. Docker Pulls
  223. + cut off
  224. + cut off`,
  225. addition: 4,
  226. deletion: 1,
  227. filename: "A \\ B",
  228. oldFilename: "A \\ B",
  229. },
  230. {
  231. name: "really weird filename",
  232. gitdiff: `diff --git "\\a/a b/file b/a a/file" "\\b/a b/file b/a a/file"
  233. index d2186f1..f5c8ed2 100644
  234. --- "\\a/a b/file b/a a/file" ` + `
  235. +++ "\\b/a b/file b/a a/file" ` + `
  236. @@ -1,3 +1,2 @@
  237. Create a weird file.
  238. ` + `
  239. -and what does diff do here?
  240. \ No newline at end of file`,
  241. addition: 0,
  242. deletion: 1,
  243. filename: "a b/file b/a a/file",
  244. oldFilename: "a b/file b/a a/file",
  245. },
  246. {
  247. name: "delete file with blanks",
  248. gitdiff: `diff --git "\\a/file with blanks" "\\b/file with blanks"
  249. deleted file mode 100644
  250. index 898651a..0000000
  251. --- "\\a/file with blanks" ` + `
  252. +++ /dev/null
  253. @@ -1,5 +0,0 @@
  254. -a blank file
  255. -
  256. -has a couple o line
  257. -
  258. -the 5th line is the last
  259. `,
  260. addition: 0,
  261. deletion: 5,
  262. filename: "file with blanks",
  263. oldFilename: "file with blanks",
  264. },
  265. {
  266. name: "rename a—as",
  267. gitdiff: `diff --git "a/\360\243\220\265b\342\200\240vs" "b/a\342\200\224as"
  268. similarity index 100%
  269. rename from "\360\243\220\265b\342\200\240vs"
  270. rename to "a\342\200\224as"
  271. `,
  272. addition: 0,
  273. deletion: 0,
  274. oldFilename: "𣐵b†vs",
  275. filename: "a—as",
  276. },
  277. {
  278. name: "rename with spaces",
  279. gitdiff: `diff --git "\\a/a b/file b/a a/file" "\\b/a b/a a/file b/b file"
  280. similarity index 100%
  281. rename from a b/file b/a a/file
  282. rename to a b/a a/file b/b file
  283. `,
  284. oldFilename: "a b/file b/a a/file",
  285. filename: "a b/a a/file b/b file",
  286. },
  287. {
  288. name: "ambiguous deleted",
  289. gitdiff: `diff --git a/b b/b b/b b/b
  290. deleted file mode 100644
  291. index 92e798b..0000000
  292. --- a/b b/b` + "\t" + `
  293. +++ /dev/null
  294. @@ -1 +0,0 @@
  295. -b b/b
  296. `,
  297. oldFilename: "b b/b",
  298. filename: "b b/b",
  299. addition: 0,
  300. deletion: 1,
  301. },
  302. {
  303. name: "ambiguous addition",
  304. gitdiff: `diff --git a/b b/b b/b b/b
  305. new file mode 100644
  306. index 0000000..92e798b
  307. --- /dev/null
  308. +++ b/b b/b` + "\t" + `
  309. @@ -0,0 +1 @@
  310. +b b/b
  311. `,
  312. oldFilename: "b b/b",
  313. filename: "b b/b",
  314. addition: 1,
  315. deletion: 0,
  316. },
  317. {
  318. name: "rename",
  319. gitdiff: `diff --git a/b b/b b/b b/b b/b b/b
  320. similarity index 100%
  321. rename from b b/b b/b b/b b/b
  322. rename to b
  323. `,
  324. oldFilename: "b b/b b/b b/b b/b",
  325. filename: "b",
  326. },
  327. {
  328. name: "ambiguous 1",
  329. gitdiff: `diff --git a/b b/b b/b b/b b/b b/b
  330. similarity index 100%
  331. rename from b b/b b/b b/b b/b
  332. rename to b
  333. `,
  334. oldFilename: "b b/b b/b b/b b/b",
  335. filename: "b",
  336. },
  337. {
  338. name: "ambiguous 2",
  339. gitdiff: `diff --git a/b b/b b/b b/b b/b b/b
  340. similarity index 100%
  341. rename from b b/b b/b b/b
  342. rename to b b/b
  343. `,
  344. oldFilename: "b b/b b/b b/b",
  345. filename: "b b/b",
  346. },
  347. {
  348. name: "minuses-and-pluses",
  349. gitdiff: `diff --git a/minuses-and-pluses b/minuses-and-pluses
  350. index 6961180..9ba1a00 100644
  351. --- a/minuses-and-pluses
  352. +++ b/minuses-and-pluses
  353. @@ -1,4 +1,4 @@
  354. --- 1st line
  355. -++ 2nd line
  356. --- 3rd line
  357. -++ 4th line
  358. +++ 1st line
  359. +-- 2nd line
  360. +++ 3rd line
  361. +-- 4th line
  362. `,
  363. oldFilename: "minuses-and-pluses",
  364. filename: "minuses-and-pluses",
  365. addition: 4,
  366. deletion: 4,
  367. },
  368. }
  369. for _, testcase := range tests {
  370. t.Run(testcase.name, func(t *testing.T) {
  371. got, err := ParsePatch(t.Context(), setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(testcase.gitdiff), "")
  372. if (err != nil) != testcase.wantErr {
  373. t.Errorf("ParsePatch(%q) error = %v, wantErr %v", testcase.name, err, testcase.wantErr)
  374. return
  375. }
  376. gotMarshaled, _ := json.MarshalIndent(got, "", " ")
  377. if len(got.Files) != 1 {
  378. t.Errorf("ParsePath(%q) did not receive 1 file:\n%s", testcase.name, string(gotMarshaled))
  379. return
  380. }
  381. file := got.Files[0]
  382. if file.Addition != testcase.addition {
  383. t.Errorf("ParsePath(%q) does not have correct file addition %d, wanted %d", testcase.name, file.Addition, testcase.addition)
  384. }
  385. if file.Deletion != testcase.deletion {
  386. t.Errorf("ParsePath(%q) did not have correct file deletion %d, wanted %d", testcase.name, file.Deletion, testcase.deletion)
  387. }
  388. if file.OldName != testcase.oldFilename {
  389. t.Errorf("ParsePath(%q) did not have correct OldName %q, wanted %q", testcase.name, file.OldName, testcase.oldFilename)
  390. }
  391. if file.Name != testcase.filename {
  392. t.Errorf("ParsePath(%q) did not have correct Name %q, wanted %q", testcase.name, file.Name, testcase.filename)
  393. }
  394. })
  395. }
  396. // Test max lines
  397. diffBuilder := &strings.Builder{}
  398. diff := `diff --git a/newfile2 b/newfile2
  399. new file mode 100644
  400. index 0000000..6bb8f39
  401. --- /dev/null
  402. +++ b/newfile2
  403. @@ -0,0 +1,35 @@
  404. `
  405. diffBuilder.WriteString(diff)
  406. for i := range 35 {
  407. diffBuilder.WriteString("+line" + strconv.Itoa(i) + "\n")
  408. }
  409. diff = diffBuilder.String()
  410. result, err := ParsePatch(t.Context(), 20, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff), "")
  411. if err != nil {
  412. t.Errorf("There should not be an error: %v", err)
  413. }
  414. if !result.Files[0].IsIncomplete {
  415. t.Errorf("Files should be incomplete! %v", result.Files[0])
  416. }
  417. result, err = ParsePatch(t.Context(), 40, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff), "")
  418. if err != nil {
  419. t.Errorf("There should not be an error: %v", err)
  420. }
  421. if result.Files[0].IsIncomplete {
  422. t.Errorf("Files should not be incomplete! %v", result.Files[0])
  423. }
  424. result, err = ParsePatch(t.Context(), 40, 5, setting.Git.MaxGitDiffFiles, strings.NewReader(diff), "")
  425. if err != nil {
  426. t.Errorf("There should not be an error: %v", err)
  427. }
  428. if !result.Files[0].IsIncomplete {
  429. t.Errorf("Files should be incomplete! %v", result.Files[0])
  430. }
  431. // Test max characters
  432. diff = `diff --git a/newfile2 b/newfile2
  433. new file mode 100644
  434. index 0000000..6bb8f39
  435. --- /dev/null
  436. +++ b/newfile2
  437. @@ -0,0 +1,35 @@
  438. `
  439. diffBuilder.Reset()
  440. diffBuilder.WriteString(diff)
  441. for i := range 33 {
  442. diffBuilder.WriteString("+line" + strconv.Itoa(i) + "\n")
  443. }
  444. diffBuilder.WriteString("+line33")
  445. for range 512 {
  446. diffBuilder.WriteString("0123456789ABCDEF")
  447. }
  448. diffBuilder.WriteByte('\n')
  449. diffBuilder.WriteString("+line" + strconv.Itoa(34) + "\n")
  450. diffBuilder.WriteString("+line" + strconv.Itoa(35) + "\n")
  451. diff = diffBuilder.String()
  452. result, err = ParsePatch(t.Context(), 20, 4096, setting.Git.MaxGitDiffFiles, strings.NewReader(diff), "")
  453. if err != nil {
  454. t.Errorf("There should not be an error: %v", err)
  455. }
  456. if !result.Files[0].IsIncomplete {
  457. t.Errorf("Files should be incomplete! %v", result.Files[0])
  458. }
  459. result, err = ParsePatch(t.Context(), 40, 4096, setting.Git.MaxGitDiffFiles, strings.NewReader(diff), "")
  460. if err != nil {
  461. t.Errorf("There should not be an error: %v", err)
  462. }
  463. if !result.Files[0].IsIncomplete {
  464. t.Errorf("Files should be incomplete! %v", result.Files[0])
  465. }
  466. diff = `diff --git "a/README.md" "b/README.md"
  467. --- a/README.md
  468. +++ b/README.md
  469. @@ -1,3 +1,6 @@
  470. # gitea-github-migrator
  471. +
  472. + Build Status
  473. - Latest Release
  474. Docker Pulls
  475. + cut off
  476. + cut off`
  477. _, err = ParsePatch(t.Context(), setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff), "")
  478. if err != nil {
  479. t.Errorf("ParsePatch failed: %s", err)
  480. }
  481. diff2 := `diff --git "a/A \\ B" "b/A \\ B"
  482. --- "a/A \\ B"
  483. +++ "b/A \\ B"
  484. @@ -1,3 +1,6 @@
  485. # gitea-github-migrator
  486. +
  487. + Build Status
  488. - Latest Release
  489. Docker Pulls
  490. + cut off
  491. + cut off`
  492. _, err = ParsePatch(t.Context(), setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff2), "")
  493. if err != nil {
  494. t.Errorf("ParsePatch failed: %s", err)
  495. }
  496. diff2a := `diff --git "a/A \\ B" b/A/B
  497. --- "a/A \\ B"
  498. +++ b/A/B
  499. @@ -1,3 +1,6 @@
  500. # gitea-github-migrator
  501. +
  502. + Build Status
  503. - Latest Release
  504. Docker Pulls
  505. + cut off
  506. + cut off`
  507. _, err = ParsePatch(t.Context(), setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff2a), "")
  508. if err != nil {
  509. t.Errorf("ParsePatch failed: %s", err)
  510. }
  511. diff3 := `diff --git a/README.md b/README.md
  512. --- a/README.md
  513. +++ b/README.md
  514. @@ -1,3 +1,6 @@
  515. # gitea-github-migrator
  516. +
  517. + Build Status
  518. - Latest Release
  519. Docker Pulls
  520. + cut off
  521. + cut off`
  522. _, err = ParsePatch(t.Context(), setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff3), "")
  523. if err != nil {
  524. t.Errorf("ParsePatch failed: %s", err)
  525. }
  526. }
  527. func setupDefaultDiff() *Diff {
  528. return &Diff{
  529. Files: []*DiffFile{
  530. {
  531. Name: "README.md",
  532. Sections: []*DiffSection{
  533. {
  534. Lines: []*DiffLine{
  535. {
  536. LeftIdx: 4,
  537. RightIdx: 4,
  538. },
  539. },
  540. },
  541. },
  542. },
  543. },
  544. }
  545. }
  546. func TestDiff_LoadCommentsNoOutdated(t *testing.T) {
  547. assert.NoError(t, unittest.PrepareTestDatabase())
  548. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
  549. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
  550. diff := setupDefaultDiff()
  551. assert.NoError(t, diff.LoadComments(t.Context(), issue, user, false))
  552. assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2)
  553. }
  554. func TestDiff_LoadCommentsWithOutdated(t *testing.T) {
  555. assert.NoError(t, unittest.PrepareTestDatabase())
  556. issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
  557. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
  558. diff := setupDefaultDiff()
  559. assert.NoError(t, diff.LoadComments(t.Context(), issue, user, true))
  560. assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 3)
  561. }
  562. func TestDiffLine_CanComment(t *testing.T) {
  563. assert.False(t, (&DiffLine{Type: DiffLineSection}).CanComment())
  564. assert.False(t, (&DiffLine{Type: DiffLineAdd, Comments: []*issues_model.Comment{{Content: "bla"}}}).CanComment())
  565. assert.True(t, (&DiffLine{Type: DiffLineAdd}).CanComment())
  566. assert.True(t, (&DiffLine{Type: DiffLineDel}).CanComment())
  567. assert.True(t, (&DiffLine{Type: DiffLinePlain}).CanComment())
  568. }
  569. func TestDiffLine_GetCommentSide(t *testing.T) {
  570. assert.Equal(t, "previous", (&DiffLine{Comments: []*issues_model.Comment{{Line: -3}}}).GetCommentSide())
  571. assert.Equal(t, "proposed", (&DiffLine{Comments: []*issues_model.Comment{{Line: 3}}}).GetCommentSide())
  572. }
  573. func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) {
  574. gitRepo, err := git.OpenRepository(t.Context(), "../../modules/git/tests/repos/repo5_pulls")
  575. require.NoError(t, err)
  576. defer gitRepo.Close()
  577. for _, behavior := range []gitcmd.TrustedCmdArgs{{"-w"}, {"--ignore-space-at-eol"}, {"-b"}, nil} {
  578. diffs, err := GetDiffForAPI(t.Context(), gitRepo,
  579. &DiffOptions{
  580. AfterCommitID: "d8e0bbb45f200e67d9a784ce55bd90821af45ebd",
  581. BeforeCommitID: "72866af952e98d02a73003501836074b286a78f6",
  582. MaxLines: setting.Git.MaxGitDiffLines,
  583. MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
  584. MaxFiles: 1,
  585. WhitespaceBehavior: behavior,
  586. })
  587. require.NoError(t, err, "Error when diff with WhitespaceBehavior=%s", behavior)
  588. assert.True(t, diffs.IsIncomplete)
  589. assert.Len(t, diffs.Files, 1)
  590. for _, f := range diffs.Files {
  591. assert.NotEmpty(t, f.Sections, "Diff file %q should have sections", f.Name)
  592. }
  593. }
  594. }
  595. func TestNoCrashes(t *testing.T) {
  596. type testcase struct {
  597. gitdiff string
  598. }
  599. tests := []testcase{
  600. {
  601. gitdiff: "diff --git \n--- a\t\n",
  602. },
  603. {
  604. gitdiff: "diff --git \"0\n",
  605. },
  606. }
  607. for _, testcase := range tests {
  608. // It shouldn't crash, so don't care about the output.
  609. ParsePatch(t.Context(), setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(testcase.gitdiff), "")
  610. }
  611. }