You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

csv_test.go 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package gitdiff
  4. import (
  5. "encoding/csv"
  6. "strings"
  7. "testing"
  8. csv_module "code.gitea.io/gitea/modules/csv"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestCSVDiff(t *testing.T) {
  13. cases := []struct {
  14. diff string
  15. base string
  16. head string
  17. cells [][]TableDiffCellType
  18. }{
  19. // case 0 - initial commit of a csv
  20. {
  21. diff: `diff --git a/unittest.csv b/unittest.csv
  22. --- a/unittest.csv
  23. +++ b/unittest.csv
  24. @@ -0,0 +1,2 @@
  25. +col1,col2
  26. +a,a`,
  27. base: "",
  28. head: `col1,col2
  29. a,a`,
  30. cells: [][]TableDiffCellType{
  31. {TableDiffCellAdd, TableDiffCellAdd},
  32. {TableDiffCellAdd, TableDiffCellAdd},
  33. },
  34. },
  35. // case 1 - adding 1 row at end
  36. {
  37. diff: `diff --git a/unittest.csv b/unittest.csv
  38. --- a/unittest.csv
  39. +++ b/unittest.csv
  40. @@ -1,2 +1,3 @@
  41. col1,col2
  42. -a,a
  43. +a,a
  44. +b,b`,
  45. base: `col1,col2
  46. a,a`,
  47. head: `col1,col2
  48. a,a
  49. b,b`,
  50. cells: [][]TableDiffCellType{
  51. {TableDiffCellUnchanged, TableDiffCellUnchanged},
  52. {TableDiffCellUnchanged, TableDiffCellUnchanged},
  53. {TableDiffCellAdd, TableDiffCellAdd},
  54. },
  55. },
  56. // case 2 - row deleted
  57. {
  58. diff: `diff --git a/unittest.csv b/unittest.csv
  59. --- a/unittest.csv
  60. +++ b/unittest.csv
  61. @@ -1,3 +1,2 @@
  62. col1,col2
  63. -a,a
  64. b,b`,
  65. base: `col1,col2
  66. a,a
  67. b,b`,
  68. head: `col1,col2
  69. b,b`,
  70. cells: [][]TableDiffCellType{
  71. {TableDiffCellUnchanged, TableDiffCellUnchanged},
  72. {TableDiffCellDel, TableDiffCellDel},
  73. {TableDiffCellUnchanged, TableDiffCellUnchanged},
  74. },
  75. },
  76. // case 3 - row changed
  77. {
  78. diff: `diff --git a/unittest.csv b/unittest.csv
  79. --- a/unittest.csv
  80. +++ b/unittest.csv
  81. @@ -1,2 +1,2 @@
  82. col1,col2
  83. -b,b
  84. +b,c`,
  85. base: `col1,col2
  86. b,b`,
  87. head: `col1,col2
  88. b,c`,
  89. cells: [][]TableDiffCellType{
  90. {TableDiffCellUnchanged, TableDiffCellUnchanged},
  91. {TableDiffCellUnchanged, TableDiffCellChanged},
  92. },
  93. },
  94. // case 4 - all deleted
  95. {
  96. diff: `diff --git a/unittest.csv b/unittest.csv
  97. --- a/unittest.csv
  98. +++ b/unittest.csv
  99. @@ -1,2 +0,0 @@
  100. -col1,col2
  101. -b,c`,
  102. base: `col1,col2
  103. b,c`,
  104. head: "",
  105. cells: [][]TableDiffCellType{
  106. {TableDiffCellDel, TableDiffCellDel},
  107. {TableDiffCellDel, TableDiffCellDel},
  108. },
  109. },
  110. // case 5 - renames first column
  111. {
  112. diff: `diff --git a/unittest.csv b/unittest.csv
  113. --- a/unittest.csv
  114. +++ b/unittest.csv
  115. @@ -1,3 +1,3 @@
  116. -col1,col2,col3
  117. +cola,col2,col3
  118. a,b,c`,
  119. base: `col1,col2,col3
  120. a,b,c`,
  121. head: `cola,col2,col3
  122. a,b,c`,
  123. cells: [][]TableDiffCellType{
  124. {TableDiffCellDel, TableDiffCellAdd, TableDiffCellUnchanged, TableDiffCellUnchanged},
  125. {TableDiffCellDel, TableDiffCellAdd, TableDiffCellUnchanged, TableDiffCellUnchanged},
  126. },
  127. },
  128. // case 6 - inserts a column after first, deletes last column
  129. {
  130. diff: `diff --git a/unittest.csv b/unittest.csv
  131. --- a/unittest.csv
  132. +++ b/unittest.csv
  133. @@ -1,2 +1,2 @@
  134. -col1,col2,col3
  135. -a,b,c
  136. +col1,col1a,col2
  137. +a,d,b`,
  138. base: `col1,col2,col3
  139. a,b,c`,
  140. head: `col1,col1a,col2
  141. a,d,b`,
  142. cells: [][]TableDiffCellType{
  143. {TableDiffCellUnchanged, TableDiffCellAdd, TableDiffCellDel, TableDiffCellMovedUnchanged},
  144. {TableDiffCellUnchanged, TableDiffCellAdd, TableDiffCellDel, TableDiffCellMovedUnchanged},
  145. },
  146. },
  147. // case 7 - deletes first column, inserts column after last
  148. {
  149. diff: `diff --git a/unittest.csv b/unittest.csv
  150. --- a/unittest.csv
  151. +++ b/unittest.csv
  152. @@ -1,2 +1,2 @@
  153. -col1,col2,col3
  154. -a,b,c
  155. +col2,col3,col4
  156. +b,c,d`,
  157. base: `col1,col2,col3
  158. a,b,c`,
  159. head: `col2,col3,col4
  160. b,c,d`,
  161. cells: [][]TableDiffCellType{
  162. {TableDiffCellDel, TableDiffCellUnchanged, TableDiffCellUnchanged, TableDiffCellAdd},
  163. {TableDiffCellDel, TableDiffCellUnchanged, TableDiffCellUnchanged, TableDiffCellAdd},
  164. },
  165. },
  166. // case 8 - two columns deleted, 2 added
  167. {
  168. diff: `diff --git a/unittest.csv b/unittest.csv
  169. --- a/unittest.csv
  170. +++ b/unittest.csv
  171. @@ -1,2 +1,2 @@
  172. -col1,col2,col
  173. -a,b,c
  174. +col3,col4,col5
  175. +c,d,e`,
  176. base: `col1,col2,col3
  177. a,b,c`,
  178. head: `col3,col4,col5
  179. c,d,e`,
  180. cells: [][]TableDiffCellType{
  181. {TableDiffCellDel, TableDiffCellMovedUnchanged, TableDiffCellDel, TableDiffCellAdd, TableDiffCellAdd},
  182. {TableDiffCellDel, TableDiffCellMovedUnchanged, TableDiffCellDel, TableDiffCellAdd, TableDiffCellAdd},
  183. },
  184. },
  185. }
  186. for n, c := range cases {
  187. diff, err := ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(c.diff), "")
  188. if err != nil {
  189. t.Errorf("ParsePatch failed: %s", err)
  190. }
  191. var baseReader *csv.Reader
  192. if len(c.base) > 0 {
  193. baseReader, err = csv_module.CreateReaderAndDetermineDelimiter(nil, strings.NewReader(c.base))
  194. if err != nil {
  195. t.Errorf("CreateReaderAndDetermineDelimiter failed: %s", err)
  196. }
  197. }
  198. var headReader *csv.Reader
  199. if len(c.head) > 0 {
  200. headReader, err = csv_module.CreateReaderAndDetermineDelimiter(nil, strings.NewReader(c.head))
  201. if err != nil {
  202. t.Errorf("CreateReaderAndDetermineDelimiter failed: %s", err)
  203. }
  204. }
  205. result, err := CreateCsvDiff(diff.Files[0], baseReader, headReader)
  206. assert.NoError(t, err)
  207. assert.Len(t, result, 1, "case %d: should be one section", n)
  208. section := result[0]
  209. assert.Len(t, section.Rows, len(c.cells), "case %d: should be %d rows", n, len(c.cells))
  210. for i, row := range section.Rows {
  211. assert.Len(t, row.Cells, len(c.cells[i]), "case %d: row %d should have %d cells", n, i, len(c.cells[i]))
  212. for j, cell := range row.Cells {
  213. assert.Equal(t, c.cells[i][j], cell.Type, "case %d: row %d cell %d should be equal", n, i, j)
  214. }
  215. }
  216. }
  217. }