2019-09-05 22:20:09 -04:00
|
|
|
// 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
|
2016-01-03 16:26:46 -05:00
|
|
|
|
|
|
|
import (
|
2019-11-28 04:05:57 -05:00
|
|
|
"fmt"
|
2016-01-31 11:16:29 -05:00
|
|
|
"html/template"
|
2018-08-06 00:43:22 -04:00
|
|
|
"strings"
|
2016-01-31 11:16:29 -05:00
|
|
|
"testing"
|
2017-06-12 11:01:09 -04:00
|
|
|
|
2019-09-05 22:20:09 -04:00
|
|
|
"code.gitea.io/gitea/models"
|
2019-11-28 04:05:57 -05:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-03-14 12:09:53 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2017-06-12 11:01:09 -04:00
|
|
|
dmp "github.com/sergi/go-diff/diffmatchpatch"
|
2018-08-06 00:43:22 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-01-03 16:26:46 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
|
2016-01-31 11:16:29 -05:00
|
|
|
if s1 != string(s2) {
|
|
|
|
t.Errorf("%s should be equal %s", s2, s1)
|
|
|
|
}
|
2016-01-03 16:26:46 -05:00
|
|
|
}
|
|
|
|
|
2016-01-09 14:05:21 -05:00
|
|
|
func TestDiffToHTML(t *testing.T) {
|
2019-06-24 16:23:52 -04:00
|
|
|
assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
|
2018-03-29 19:21:27 -04:00
|
|
|
{Type: dmp.DiffEqual, Text: "foo "},
|
|
|
|
{Type: dmp.DiffInsert, Text: "bar"},
|
|
|
|
{Type: dmp.DiffDelete, Text: " baz"},
|
|
|
|
{Type: dmp.DiffEqual, Text: " biz"},
|
2016-11-07 11:24:59 -05:00
|
|
|
}, DiffLineAdd))
|
2016-01-03 16:26:46 -05:00
|
|
|
|
2019-06-24 16:23:52 -04:00
|
|
|
assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
|
2018-03-29 19:21:27 -04:00
|
|
|
{Type: dmp.DiffEqual, Text: "foo "},
|
|
|
|
{Type: dmp.DiffDelete, Text: "bar"},
|
|
|
|
{Type: dmp.DiffInsert, Text: " baz"},
|
|
|
|
{Type: dmp.DiffEqual, Text: " biz"},
|
2016-11-07 11:24:59 -05:00
|
|
|
}, DiffLineDel))
|
2016-01-03 16:26:46 -05:00
|
|
|
}
|
2018-08-06 00:43:22 -04:00
|
|
|
|
2019-03-14 12:09:53 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-08-06 00:43:22 -04:00
|
|
|
func setupDefaultDiff() *Diff {
|
|
|
|
return &Diff{
|
|
|
|
Files: []*DiffFile{
|
|
|
|
{
|
|
|
|
Name: "README.md",
|
|
|
|
Sections: []*DiffSection{
|
|
|
|
{
|
|
|
|
Lines: []*DiffLine{
|
|
|
|
{
|
|
|
|
LeftIdx: 4,
|
|
|
|
RightIdx: 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestDiff_LoadComments(t *testing.T) {
|
2019-09-05 22:20:09 -04:00
|
|
|
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)
|
2018-08-06 00:43:22 -04:00
|
|
|
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())
|
2019-09-05 22:20:09 -04:00
|
|
|
assert.False(t, (&DiffLine{Type: DiffLineAdd, Comments: []*models.Comment{{Content: "bla"}}}).CanComment())
|
2018-08-06 00:43:22 -04:00
|
|
|
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) {
|
2019-09-05 22:20:09 -04:00
|
|
|
assert.Equal(t, "previous", (&DiffLine{Comments: []*models.Comment{{Line: -3}}}).GetCommentSide())
|
|
|
|
assert.Equal(t, "proposed", (&DiffLine{Comments: []*models.Comment{{Line: 3}}}).GetCommentSide())
|
2018-08-06 00:43:22 -04:00
|
|
|
}
|
2019-11-28 04:05:57 -05:00
|
|
|
|
|
|
|
func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) {
|
|
|
|
git.Debug = true
|
|
|
|
for _, behavior := range []string{"-w", "--ignore-space-at-eol", "-b", ""} {
|
|
|
|
diffs, err := GetDiffRangeWithWhitespaceBehavior("./testdata/academic-module", "559c156f8e0178b71cb44355428f24001b08fc68", "bd7063cc7c04689c4d082183d32a604ed27a24f9",
|
|
|
|
setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles, behavior)
|
|
|
|
assert.NoError(t, err, fmt.Sprintf("Error when diff with %s", behavior))
|
|
|
|
for _, f := range diffs.Files {
|
|
|
|
assert.True(t, len(f.Sections) > 0, fmt.Sprintf("%s should have sections", f.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|