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.
198 lines
5.5 KiB
198 lines
5.5 KiB
5 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package gitdiff
|
||
9 years ago
|
|
||
|
import (
|
||
9 years ago
|
"html/template"
|
||
6 years ago
|
"strings"
|
||
9 years ago
|
"testing"
|
||
8 years ago
|
|
||
5 years ago
|
"code.gitea.io/gitea/models"
|
||
6 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
|
|
||
8 years ago
|
dmp "github.com/sergi/go-diff/diffmatchpatch"
|
||
6 years ago
|
"github.com/stretchr/testify/assert"
|
||
9 years ago
|
)
|
||
|
|
||
|
func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
|
||
9 years ago
|
if s1 != string(s2) {
|
||
|
t.Errorf("%s should be equal %s", s2, s1)
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
func TestDiffToHTML(t *testing.T) {
|
||
6 years ago
|
assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
|
||
7 years ago
|
{Type: dmp.DiffEqual, Text: "foo "},
|
||
|
{Type: dmp.DiffInsert, Text: "bar"},
|
||
|
{Type: dmp.DiffDelete, Text: " baz"},
|
||
|
{Type: dmp.DiffEqual, Text: " biz"},
|
||
8 years ago
|
}, DiffLineAdd))
|
||
9 years ago
|
|
||
6 years ago
|
assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
|
||
7 years ago
|
{Type: dmp.DiffEqual, Text: "foo "},
|
||
|
{Type: dmp.DiffDelete, Text: "bar"},
|
||
|
{Type: dmp.DiffInsert, Text: " baz"},
|
||
|
{Type: dmp.DiffEqual, Text: " biz"},
|
||
8 years ago
|
}, DiffLineDel))
|
||
9 years ago
|
}
|
||
6 years ago
|
|
||
|
const exampleDiff = `diff --git a/README.md b/README.md
|
||
|
--- a/README.md
|
||
|
+++ b/README.md
|
||
|
@@ -1,3 +1,6 @@
|
||
|
# gitea-github-migrator
|
||
|
+
|
||
|
+ Build Status
|
||
|
- Latest Release
|
||
|
Docker Pulls
|
||
|
+ cut off
|
||
|
+ cut off`
|
||
|
|
||
|
func TestCutDiffAroundLine(t *testing.T) {
|
||
|
result := CutDiffAroundLine(strings.NewReader(exampleDiff), 4, false, 3)
|
||
|
resultByLine := strings.Split(result, "\n")
|
||
|
assert.Len(t, resultByLine, 7)
|
||
|
// Check if headers got transferred
|
||
|
assert.Equal(t, "diff --git a/README.md b/README.md", resultByLine[0])
|
||
|
assert.Equal(t, "--- a/README.md", resultByLine[1])
|
||
|
assert.Equal(t, "+++ b/README.md", resultByLine[2])
|
||
|
// Check if hunk header is calculated correctly
|
||
|
assert.Equal(t, "@@ -2,2 +3,2 @@", resultByLine[3])
|
||
|
// Check if line got transferred
|
||
|
assert.Equal(t, "+ Build Status", resultByLine[4])
|
||
|
|
||
|
// Must be same result as before since old line 3 == new line 5
|
||
|
newResult := CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
|
||
|
assert.Equal(t, result, newResult, "Must be same result as before since old line 3 == new line 5")
|
||
|
|
||
|
newResult = CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 300)
|
||
|
assert.Equal(t, exampleDiff, newResult)
|
||
|
|
||
|
emptyResult := CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 0)
|
||
|
assert.Empty(t, emptyResult)
|
||
|
|
||
|
// Line is out of scope
|
||
|
emptyResult = CutDiffAroundLine(strings.NewReader(exampleDiff), 434, false, 0)
|
||
|
assert.Empty(t, emptyResult)
|
||
|
}
|
||
|
|
||
|
func BenchmarkCutDiffAroundLine(b *testing.B) {
|
||
|
for n := 0; n < b.N; n++ {
|
||
|
CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func ExampleCutDiffAroundLine() {
|
||
|
const diff = `diff --git a/README.md b/README.md
|
||
|
--- a/README.md
|
||
|
+++ b/README.md
|
||
|
@@ -1,3 +1,6 @@
|
||
|
# gitea-github-migrator
|
||
|
+
|
||
|
+ Build Status
|
||
|
- Latest Release
|
||
|
Docker Pulls
|
||
|
+ cut off
|
||
|
+ cut off`
|
||
|
result := CutDiffAroundLine(strings.NewReader(diff), 4, false, 3)
|
||
|
println(result)
|
||
|
}
|
||
|
|
||
6 years ago
|
func TestParsePatch(t *testing.T) {
|
||
|
var diff = `diff --git "a/README.md" "b/README.md"
|
||
|
--- a/README.md
|
||
|
+++ b/README.md
|
||
|
@@ -1,3 +1,6 @@
|
||
|
# gitea-github-migrator
|
||
|
+
|
||
|
+ Build Status
|
||
|
- Latest Release
|
||
|
Docker Pulls
|
||
|
+ cut off
|
||
|
+ cut off`
|
||
|
result, err := ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff))
|
||
|
if err != nil {
|
||
|
t.Errorf("ParsePatch failed: %s", err)
|
||
|
}
|
||
|
println(result)
|
||
|
|
||
|
var diff2 = `diff --git "a/A \\ B" "b/A \\ B"
|
||
|
--- "a/A \\ B"
|
||
|
+++ "b/A \\ B"
|
||
|
@@ -1,3 +1,6 @@
|
||
|
# gitea-github-migrator
|
||
|
+
|
||
|
+ Build Status
|
||
|
- Latest Release
|
||
|
Docker Pulls
|
||
|
+ cut off
|
||
|
+ cut off`
|
||
|
result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff2))
|
||
|
if err != nil {
|
||
|
t.Errorf("ParsePatch failed: %s", err)
|
||
|
}
|
||
|
println(result)
|
||
|
|
||
|
var diff3 = `diff --git a/README.md b/README.md
|
||
|
--- a/README.md
|
||
|
+++ b/README.md
|
||
|
@@ -1,3 +1,6 @@
|
||
|
# gitea-github-migrator
|
||
|
+
|
||
|
+ Build Status
|
||
|
- Latest Release
|
||
|
Docker Pulls
|
||
|
+ cut off
|
||
|
+ cut off`
|
||
|
result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff3))
|
||
|
if err != nil {
|
||
|
t.Errorf("ParsePatch failed: %s", err)
|
||
|
}
|
||
|
println(result)
|
||
|
}
|
||
|
|
||
6 years ago
|
func setupDefaultDiff() *Diff {
|
||
|
return &Diff{
|
||
|
Files: []*DiffFile{
|
||
|
{
|
||
|
Name: "README.md",
|
||
|
Sections: []*DiffSection{
|
||
|
{
|
||
|
Lines: []*DiffLine{
|
||
|
{
|
||
|
LeftIdx: 4,
|
||
|
RightIdx: 4,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
func TestDiff_LoadComments(t *testing.T) {
|
||
5 years ago
|
assert.NoError(t, models.PrepareTestDatabase())
|
||
|
|
||
|
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
|
||
|
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
|
||
6 years ago
|
diff := setupDefaultDiff()
|
||
|
assert.NoError(t, diff.LoadComments(issue, user))
|
||
|
assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2)
|
||
|
}
|
||
|
|
||
|
func TestDiffLine_CanComment(t *testing.T) {
|
||
|
assert.False(t, (&DiffLine{Type: DiffLineSection}).CanComment())
|
||
5 years ago
|
assert.False(t, (&DiffLine{Type: DiffLineAdd, Comments: []*models.Comment{{Content: "bla"}}}).CanComment())
|
||
6 years ago
|
assert.True(t, (&DiffLine{Type: DiffLineAdd}).CanComment())
|
||
|
assert.True(t, (&DiffLine{Type: DiffLineDel}).CanComment())
|
||
|
assert.True(t, (&DiffLine{Type: DiffLinePlain}).CanComment())
|
||
|
}
|
||
|
|
||
|
func TestDiffLine_GetCommentSide(t *testing.T) {
|
||
5 years ago
|
assert.Equal(t, "previous", (&DiffLine{Comments: []*models.Comment{{Line: -3}}}).GetCommentSide())
|
||
|
assert.Equal(t, "proposed", (&DiffLine{Comments: []*models.Comment{{Line: 3}}}).GetCommentSide())
|
||
6 years ago
|
}
|