diff_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package git
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. )
  8. func TestSplitLines(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. input string
  12. want []string
  13. }{
  14. {name: "empty string", input: "", want: []string{}},
  15. {name: "single line no newline", input: "abc", want: []string{"abc"}},
  16. {name: "single line trailing newline", input: "abc\n", want: []string{"abc"}},
  17. {name: "two lines", input: "a\nb\n", want: []string{"a", "b"}},
  18. {name: "blank line preserved", input: "a\n\nb\n", want: []string{"a", "", "b"}},
  19. {name: "crlf normalised", input: "a\r\nb\r\n", want: []string{"a", "b"}},
  20. {name: "only newline", input: "\n", want: []string{""}},
  21. }
  22. for _, test := range tests {
  23. t.Run(test.name, func(t *testing.T) {
  24. got := splitLines(test.input)
  25. if len(got) != len(test.want) {
  26. t.Fatalf("splitLines(%q) = %q, want %q", test.input, got, test.want)
  27. }
  28. for i := range got {
  29. if got[i] != test.want[i] {
  30. t.Errorf("splitLines(%q)[%d] = %q, want %q", test.input, i, got[i], test.want[i])
  31. }
  32. }
  33. })
  34. }
  35. }
  36. func TestDiffLines(t *testing.T) {
  37. tests := []struct {
  38. name string
  39. oldText []string
  40. newText []string
  41. want []string //"kind:text" pairs, in order
  42. }{
  43. {
  44. name: "identical",
  45. oldText: []string{"a", "b"},
  46. newText: []string{"a", "b"},
  47. want: []string{"context:a", "context:b"},
  48. },
  49. {
  50. name: "append a line",
  51. oldText: []string{"a"},
  52. newText: []string{"a", "b"},
  53. want: []string{"context:a", "add:b"},
  54. },
  55. {
  56. name: "delete a line",
  57. oldText: []string{"a", "b"},
  58. newText: []string{"a"},
  59. want: []string{"context:a", "del:b"},
  60. },
  61. {
  62. name: "replace middle line",
  63. oldText: []string{"a", "b", "c"},
  64. newText: []string{"a", "x", "c"},
  65. want: []string{"context:a", "del:b", "add:x", "context:c"},
  66. },
  67. {
  68. name: "from empty",
  69. oldText: []string{},
  70. newText: []string{"a", "b"},
  71. want: []string{"add:a", "add:b"},
  72. },
  73. {
  74. name: "to empty",
  75. oldText: []string{"a", "b"},
  76. newText: []string{},
  77. want: []string{"del:a", "del:b"},
  78. },
  79. {
  80. name: "both empty",
  81. oldText: []string{},
  82. newText: []string{},
  83. want: []string{},
  84. },
  85. {
  86. name: "insert at start",
  87. oldText: []string{"b"},
  88. newText: []string{"a", "b"},
  89. want: []string{"add:a", "context:b"},
  90. },
  91. }
  92. for _, test := range tests {
  93. t.Run(test.name, func(t *testing.T) {
  94. operations := diffLines(test.oldText, test.newText)
  95. got := []string{}
  96. for _, operation := range operations {
  97. got = append(got, operation.kind+":"+operation.text)
  98. }
  99. if strings.Join(got, "|") != strings.Join(test.want, "|") {
  100. t.Errorf("diffLines() = %v, want %v", got, test.want)
  101. }
  102. })
  103. }
  104. }
  105. func TestDiffLinesLargeRegionFallsBackToReplace(t *testing.T) {
  106. oldText := make([]string, maxLCSRegion+10)
  107. newText := make([]string, maxLCSRegion+10)
  108. for i := range oldText {
  109. oldText[i] = "old line"
  110. newText[i] = "new line"
  111. }
  112. operations := diffLines(oldText, newText)
  113. deletions, additions := 0, 0
  114. for _, operation := range operations {
  115. switch operation.kind {
  116. case "del":
  117. deletions++
  118. case "add":
  119. additions++
  120. }
  121. }
  122. if deletions != len(oldText) || additions != len(newText) {
  123. t.Errorf("large region diff = %d deletions / %d additions, want %d / %d",
  124. deletions, additions, len(oldText), len(newText))
  125. }
  126. }
  127. func TestBuildHunksLineNumbering(t *testing.T) {
  128. operations := []diffOp{
  129. {kind: "context", text: "a"},
  130. {kind: "del", text: "b"},
  131. {kind: "add", text: "B"},
  132. {kind: "context", text: "c"},
  133. }
  134. hunks := buildHunks(operations)
  135. if len(hunks) != 1 {
  136. t.Fatalf("buildHunks() = %d hunks, want 1", len(hunks))
  137. }
  138. hunk := hunks[0]
  139. if hunk.OldStart != 1 || hunk.NewStart != 1 {
  140. t.Errorf("hunk start = -%d +%d, want -1 +1", hunk.OldStart, hunk.NewStart)
  141. }
  142. if hunk.OldLines != 3 || hunk.NewLines != 3 {
  143. t.Errorf("hunk length = -%d +%d, want -3 +3", hunk.OldLines, hunk.NewLines)
  144. }
  145. if !strings.HasPrefix(hunk.Header, "@@ -1,3 +1,3 @@") {
  146. t.Errorf("hunk Header = %q, want it to start with %q", hunk.Header, "@@ -1,3 +1,3 @@")
  147. }
  148. wantLines := []DiffLine{
  149. {Type: "context", OldLine: 1, NewLine: 1, Content: "a"},
  150. {Type: "del", OldLine: 2, NewLine: 0, Content: "b"},
  151. {Type: "add", OldLine: 0, NewLine: 2, Content: "B"},
  152. {Type: "context", OldLine: 3, NewLine: 3, Content: "c"},
  153. }
  154. if len(hunk.Lines) != len(wantLines) {
  155. t.Fatalf("hunk has %d lines, want %d", len(hunk.Lines), len(wantLines))
  156. }
  157. for i, want := range wantLines {
  158. got := hunk.Lines[i]
  159. if got != want {
  160. t.Errorf("line %d = %+v, want %+v", i, got, want)
  161. }
  162. }
  163. }
  164. func TestBuildHunksNoChangesProducesNoHunks(t *testing.T) {
  165. operations := []diffOp{
  166. {kind: "context", text: "a"},
  167. {kind: "context", text: "b"},
  168. }
  169. if hunks := buildHunks(operations); len(hunks) != 0 {
  170. t.Errorf("buildHunks() with no changes = %d hunks, want 0", len(hunks))
  171. }
  172. }
  173. func TestBuildHunksSplitsDistantChanges(t *testing.T) {
  174. operations := []diffOp{{kind: "add", text: "start"}}
  175. for i := 0; i < 20; i++ {
  176. operations = append(operations, diffOp{kind: "context", text: "filler"})
  177. }
  178. operations = append(operations, diffOp{kind: "add", text: "end"})
  179. hunks := buildHunks(operations)
  180. if len(hunks) != 2 {
  181. t.Errorf("buildHunks() with two distant changes = %d hunks, want 2", len(hunks))
  182. }
  183. }
  184. func TestIsBinaryContent(t *testing.T) {
  185. //A NUL past the 8000 byte probe window is deliberately not detected, which
  186. //is the same trade-off git itself makes.
  187. lateNul := append([]byte(strings.Repeat("a", 8000)), 0x00)
  188. tests := []struct {
  189. name string
  190. content []byte
  191. want bool
  192. }{
  193. {name: "text", content: []byte("hello"), want: false},
  194. {name: "empty", content: []byte{}, want: false},
  195. {name: "nul at start", content: []byte{0x00, 'a'}, want: true},
  196. {name: "nul in the middle", content: []byte("abc\x00def"), want: true},
  197. {name: "nul beyond the probe window", content: lateNul, want: false},
  198. }
  199. for _, test := range tests {
  200. t.Run(test.name, func(t *testing.T) {
  201. if got := isBinaryContent(test.content); got != test.want {
  202. t.Errorf("isBinaryContent(%s) = %v, want %v", test.name, got, test.want)
  203. }
  204. })
  205. }
  206. }
  207. func TestBuildFileDiffFlags(t *testing.T) {
  208. tests := []struct {
  209. name string
  210. oldContent []byte
  211. newContent []byte
  212. oldExists bool
  213. newExists bool
  214. wantNew bool
  215. wantDeleted bool
  216. wantBinary bool
  217. wantAdditions int
  218. wantDeletions int
  219. }{
  220. {
  221. name: "new file",
  222. oldContent: []byte{},
  223. newContent: []byte("a\nb\n"),
  224. oldExists: false,
  225. newExists: true,
  226. wantNew: true,
  227. wantAdditions: 2,
  228. },
  229. {
  230. name: "deleted file",
  231. oldContent: []byte("a\n"),
  232. newContent: []byte{},
  233. oldExists: true,
  234. newExists: false,
  235. wantDeleted: true,
  236. wantDeletions: 1,
  237. },
  238. {
  239. name: "binary file",
  240. oldContent: []byte{0x00, 0x01},
  241. newContent: []byte{0x00, 0x02},
  242. oldExists: true,
  243. newExists: true,
  244. wantBinary: true,
  245. },
  246. {
  247. name: "one line changed",
  248. oldContent: []byte("a\nb\nc\n"),
  249. newContent: []byte("a\nB\nc\n"),
  250. oldExists: true,
  251. newExists: true,
  252. wantAdditions: 1,
  253. wantDeletions: 1,
  254. },
  255. }
  256. for _, test := range tests {
  257. t.Run(test.name, func(t *testing.T) {
  258. diff := buildFileDiff("file.txt", test.oldContent, test.newContent, test.oldExists, test.newExists)
  259. if diff.IsNew != test.wantNew {
  260. t.Errorf("IsNew = %v, want %v", diff.IsNew, test.wantNew)
  261. }
  262. if diff.IsDeleted != test.wantDeleted {
  263. t.Errorf("IsDeleted = %v, want %v", diff.IsDeleted, test.wantDeleted)
  264. }
  265. if diff.Binary != test.wantBinary {
  266. t.Errorf("Binary = %v, want %v", diff.Binary, test.wantBinary)
  267. }
  268. if diff.Additions != test.wantAdditions {
  269. t.Errorf("Additions = %d, want %d", diff.Additions, test.wantAdditions)
  270. }
  271. if diff.Deletions != test.wantDeletions {
  272. t.Errorf("Deletions = %d, want %d", diff.Deletions, test.wantDeletions)
  273. }
  274. })
  275. }
  276. }
  277. func TestBuildFileDiffTooLarge(t *testing.T) {
  278. diff := buildFileDiff("big.bin", nil, []byte("x"), true, true)
  279. if !diff.TooLarge {
  280. t.Errorf("TooLarge = false for an oversized side, want true")
  281. }
  282. if len(diff.Hunks) != 0 {
  283. t.Errorf("Hunks = %d, want 0 for an oversized diff", len(diff.Hunks))
  284. }
  285. }
  286. func TestDiffAgainstWorkingTree(t *testing.T) {
  287. manager := newTestManager(t)
  288. repoPath := newTestRepo(t, manager)
  289. commitFile(t, manager, repoPath, "a.txt", "one\ntwo\nthree\n", "first")
  290. writeFile(t, repoPath, "a.txt", "one\nTWO\nthree\n")
  291. diff, err := manager.Diff(repoPath, "a.txt")
  292. if err != nil {
  293. t.Fatalf("Diff() returned error: %v", err)
  294. }
  295. if diff.Additions != 1 || diff.Deletions != 1 {
  296. t.Errorf("Diff() = +%d -%d, want +1 -1", diff.Additions, diff.Deletions)
  297. }
  298. if diff.IsNew || diff.IsDeleted || diff.Binary {
  299. t.Errorf("Diff() flags = new:%v deleted:%v binary:%v, want all false",
  300. diff.IsNew, diff.IsDeleted, diff.Binary)
  301. }
  302. if len(diff.Hunks) != 1 {
  303. t.Fatalf("Diff() = %d hunks, want 1", len(diff.Hunks))
  304. }
  305. }
  306. func TestDiffNewUntrackedFile(t *testing.T) {
  307. manager := newTestManager(t)
  308. repoPath := newTestRepo(t, manager)
  309. commitFile(t, manager, repoPath, "a.txt", "one\n", "first")
  310. writeFile(t, repoPath, "new.txt", "fresh\ncontent\n")
  311. diff, err := manager.Diff(repoPath, "new.txt")
  312. if err != nil {
  313. t.Fatalf("Diff() returned error: %v", err)
  314. }
  315. if !diff.IsNew {
  316. t.Errorf("IsNew = false for an untracked file, want true")
  317. }
  318. if diff.Additions != 2 {
  319. t.Errorf("Additions = %d, want 2", diff.Additions)
  320. }
  321. }
  322. func TestDiffMissingFileFails(t *testing.T) {
  323. manager := newTestManager(t)
  324. repoPath := newTestRepo(t, manager)
  325. commitFile(t, manager, repoPath, "a.txt", "one\n", "first")
  326. if _, err := manager.Diff(repoPath, "nowhere.txt"); err == nil {
  327. t.Errorf("Diff() on a nonexistent path = nil error, want an error")
  328. }
  329. }
  330. func TestDiffRejectsEscapingPath(t *testing.T) {
  331. manager := newTestManager(t)
  332. repoPath := newTestRepo(t, manager)
  333. commitFile(t, manager, repoPath, "a.txt", "one\n", "first")
  334. if _, err := manager.Diff(repoPath, "../../secret.txt"); err == nil {
  335. t.Errorf("Diff() with an escaping path = nil error, want an error")
  336. }
  337. }
  338. func TestDiffCommitAndCommitFiles(t *testing.T) {
  339. manager := newTestManager(t)
  340. repoPath := newTestRepo(t, manager)
  341. commitFile(t, manager, repoPath, "a.txt", "one\n", "first")
  342. second := commitFile(t, manager, repoPath, "a.txt", "one\ntwo\n", "second")
  343. files, err := manager.CommitFiles(repoPath, second)
  344. if err != nil {
  345. t.Fatalf("CommitFiles() returned error: %v", err)
  346. }
  347. if len(files) != 1 || files[0].Path != "a.txt" || files[0].Status != "modified" {
  348. t.Fatalf("CommitFiles() = %+v, want a single modified a.txt", files)
  349. }
  350. diff, err := manager.DiffCommit(repoPath, second, "a.txt")
  351. if err != nil {
  352. t.Fatalf("DiffCommit() returned error: %v", err)
  353. }
  354. if diff.Additions != 1 || diff.Deletions != 0 {
  355. t.Errorf("DiffCommit() = +%d -%d, want +1 -0", diff.Additions, diff.Deletions)
  356. }
  357. }
  358. func TestCommitFilesOnInitialCommit(t *testing.T) {
  359. manager := newTestManager(t)
  360. repoPath := newTestRepo(t, manager)
  361. first := commitFile(t, manager, repoPath, "a.txt", "one\n", "first")
  362. files, err := manager.CommitFiles(repoPath, first)
  363. if err != nil {
  364. t.Fatalf("CommitFiles() returned error: %v", err)
  365. }
  366. if len(files) != 1 || files[0].Status != "added" {
  367. t.Errorf("CommitFiles() on the initial commit = %+v, want a single added file", files)
  368. }
  369. }
  370. func TestWorktreeContentHonoursSizeLimit(t *testing.T) {
  371. folder := t.TempDir()
  372. bigPath := filepath.Join(folder, "big.txt")
  373. big := make([]byte, maxDiffBytes+1)
  374. for i := range big {
  375. big[i] = 'a'
  376. }
  377. if err := os.WriteFile(bigPath, big, 0664); err != nil {
  378. t.Fatalf("cannot write oversized file: %v", err)
  379. }
  380. content, exists, err := worktreeContent(folder, "big.txt")
  381. if err != nil {
  382. t.Fatalf("worktreeContent() returned error: %v", err)
  383. }
  384. if !exists {
  385. t.Errorf("exists = false for an oversized file, want true")
  386. }
  387. if content != nil {
  388. t.Errorf("content = %d bytes for an oversized file, want nil", len(content))
  389. }
  390. }