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.

diff_test.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. const exampleDiff = `diff --git a/README.md b/README.md
  11. --- a/README.md
  12. +++ b/README.md
  13. @@ -1,3 +1,6 @@
  14. # gitea-github-migrator
  15. +
  16. + Build Status
  17. - Latest Release
  18. Docker Pulls
  19. + cut off
  20. + cut off`
  21. const breakingDiff = `diff --git a/aaa.sql b/aaa.sql
  22. index d8e4c92..19dc8ad 100644
  23. --- a/aaa.sql
  24. +++ b/aaa.sql
  25. @@ -1,9 +1,10 @@
  26. --some comment
  27. --- some comment 5
  28. +--some coment 2
  29. +-- some comment 3
  30. create or replace procedure test(p1 varchar2)
  31. is
  32. begin
  33. ---new comment
  34. dbms_output.put_line(p1);
  35. +--some other comment
  36. end;
  37. /
  38. `
  39. func TestCutDiffAroundLine(t *testing.T) {
  40. result, err := CutDiffAroundLine(strings.NewReader(exampleDiff), 4, false, 3)
  41. assert.NoError(t, err)
  42. resultByLine := strings.Split(result, "\n")
  43. assert.Len(t, resultByLine, 7)
  44. // Check if headers got transferred
  45. assert.Equal(t, "diff --git a/README.md b/README.md", resultByLine[0])
  46. assert.Equal(t, "--- a/README.md", resultByLine[1])
  47. assert.Equal(t, "+++ b/README.md", resultByLine[2])
  48. // Check if hunk header is calculated correctly
  49. assert.Equal(t, "@@ -2,2 +3,2 @@", resultByLine[3])
  50. // Check if line got transferred
  51. assert.Equal(t, "+ Build Status", resultByLine[4])
  52. // Must be same result as before since old line 3 == new line 5
  53. newResult, err := CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
  54. assert.NoError(t, err)
  55. assert.Equal(t, result, newResult, "Must be same result as before since old line 3 == new line 5")
  56. newResult, err = CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 300)
  57. assert.NoError(t, err)
  58. assert.Equal(t, exampleDiff, newResult)
  59. emptyResult, err := CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 0)
  60. assert.NoError(t, err)
  61. assert.Empty(t, emptyResult)
  62. // Line is out of scope
  63. emptyResult, err = CutDiffAroundLine(strings.NewReader(exampleDiff), 434, false, 0)
  64. assert.NoError(t, err)
  65. assert.Empty(t, emptyResult)
  66. // Handle minus diffs properly
  67. minusDiff, err := CutDiffAroundLine(strings.NewReader(breakingDiff), 2, false, 4)
  68. assert.NoError(t, err)
  69. expected := `diff --git a/aaa.sql b/aaa.sql
  70. --- a/aaa.sql
  71. +++ b/aaa.sql
  72. @@ -1,9 +1,10 @@
  73. --some comment
  74. --- some comment 5
  75. +--some coment 2`
  76. assert.Equal(t, expected, minusDiff)
  77. // Handle minus diffs properly
  78. minusDiff, err = CutDiffAroundLine(strings.NewReader(breakingDiff), 3, false, 4)
  79. assert.NoError(t, err)
  80. expected = `diff --git a/aaa.sql b/aaa.sql
  81. --- a/aaa.sql
  82. +++ b/aaa.sql
  83. @@ -1,9 +1,10 @@
  84. --some comment
  85. --- some comment 5
  86. +--some coment 2
  87. +-- some comment 3`
  88. assert.Equal(t, expected, minusDiff)
  89. }
  90. func BenchmarkCutDiffAroundLine(b *testing.B) {
  91. for n := 0; n < b.N; n++ {
  92. CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
  93. }
  94. }
  95. func ExampleCutDiffAroundLine() {
  96. const diff = `diff --git a/README.md b/README.md
  97. --- a/README.md
  98. +++ b/README.md
  99. @@ -1,3 +1,6 @@
  100. # gitea-github-migrator
  101. +
  102. + Build Status
  103. - Latest Release
  104. Docker Pulls
  105. + cut off
  106. + cut off`
  107. result, _ := CutDiffAroundLine(strings.NewReader(diff), 4, false, 3)
  108. println(result)
  109. }
  110. func TestParseDiffHunkString(t *testing.T) {
  111. leftLine, leftHunk, rightLine, rightHunk := ParseDiffHunkString("@@ -19,3 +19,5 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER")
  112. assert.EqualValues(t, 19, leftLine)
  113. assert.EqualValues(t, 3, leftHunk)
  114. assert.EqualValues(t, 19, rightLine)
  115. assert.EqualValues(t, 5, rightHunk)
  116. }