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"
|
2020-11-21 17:41:24 -05:00
|
|
|
"strconv"
|
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
|
|
|
|
2022-01-19 18:26:57 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 05:37:59 -04:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-08-31 00:16:23 -04:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2021-02-14 09:51:00 -05:00
|
|
|
"code.gitea.io/gitea/modules/highlight"
|
2021-07-24 12:03:58 -04:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2019-03-14 12:09:53 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-07-24 12:03:58 -04:00
|
|
|
|
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"
|
2020-09-09 09:08:40 -04:00
|
|
|
"gopkg.in/ini.v1"
|
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) {
|
2021-02-14 09:51:00 -05:00
|
|
|
t.Errorf("Did not receive expected results:\nExpected: %s\nActual: %s", s1, s2)
|
2016-01-31 11:16:29 -05:00
|
|
|
}
|
2016-01-03 16:26:46 -05:00
|
|
|
}
|
|
|
|
|
2016-01-09 14:05:21 -05:00
|
|
|
func TestDiffToHTML(t *testing.T) {
|
2020-06-30 17:34:03 -04:00
|
|
|
setting.Cfg = ini.Empty()
|
|
|
|
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"},
|
2022-01-06 20:18:52 -05:00
|
|
|
}, DiffLineAdd).Content)
|
2016-01-03 16:26:46 -05:00
|
|
|
|
2020-06-30 17:34:03 -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"},
|
2022-01-06 20:18:52 -05:00
|
|
|
}, DiffLineDel).Content)
|
2020-07-16 09:58:54 -04:00
|
|
|
|
|
|
|
assertEqual(t, "<span class=\"k\">if</span> <span class=\"p\">!</span><span class=\"nx\">nohl</span> <span class=\"o\">&&</span> <span class=\"added-code\"><span class=\"p\">(</span></span><span class=\"nx\">lexer</span> <span class=\"o\">!=</span> <span class=\"kc\">nil</span><span class=\"added-code\"> <span class=\"o\">||</span> <span class=\"nx\">r</span><span class=\"p\">.</span><span class=\"nx\">GuessLanguage</span><span class=\"p\">)</span></span> <span class=\"p\">{</span>", diffToHTML("", []dmp.Diff{
|
|
|
|
{Type: dmp.DiffEqual, Text: "<span class=\"k\">if</span> <span class=\"p\">!</span><span class=\"nx\">nohl</span> <span class=\"o\">&&</span> <span class=\""},
|
|
|
|
{Type: dmp.DiffInsert, Text: "p\">(</span><span class=\""},
|
|
|
|
{Type: dmp.DiffEqual, Text: "nx\">lexer</span> <span class=\"o\">!=</span> <span class=\"kc\">nil"},
|
|
|
|
{Type: dmp.DiffInsert, Text: "</span> <span class=\"o\">||</span> <span class=\"nx\">r</span><span class=\"p\">.</span><span class=\"nx\">GuessLanguage</span><span class=\"p\">)"},
|
|
|
|
{Type: dmp.DiffEqual, Text: "</span> <span class=\"p\">{</span>"},
|
2022-01-06 20:18:52 -05:00
|
|
|
}, DiffLineAdd).Content)
|
2020-08-08 06:17:02 -04:00
|
|
|
|
2020-10-21 18:14:44 -04:00
|
|
|
assertEqual(t, "<span class=\"nx\">tagURL</span> <span class=\"o\">:=</span> <span class=\"removed-code\"><span class=\"nx\">fmt</span><span class=\"p\">.</span><span class=\"nf\">Sprintf</span><span class=\"p\">(</span><span class=\"s\">"## [%s](%s/%s/%s/%s?q=&type=all&state=closed&milestone=%d) - %s"</span><span class=\"p\">,</span> <span class=\"nx\">ge</span><span class=\"p\">.</span><span class=\"nx\">Milestone\"</span></span><span class=\"p\">,</span> <span class=\"nx\">ge</span><span class=\"p\">.</span><span class=\"nx\">BaseURL</span><span class=\"p\">,</span> <span class=\"nx\">ge</span><span class=\"p\">.</span><span class=\"nx\">Owner</span><span class=\"p\">,</span> <span class=\"nx\">ge</span><span class=\"p\">.</span><span class=\"nx\">Repo</span><span class=\"p\">,</span> <span class=\"removed-code\"><span class=\"nx\">from</span><span class=\"p\">,</span> <span class=\"nx\">milestoneID</span><span class=\"p\">,</span> <span class=\"nx\">time</span><span class=\"p\">.</span><span class=\"nf\">Now</span><span class=\"p\">(</span><span class=\"p\">)</span><span class=\"p\">.</span><span class=\"nf\">Format</span><span class=\"p\">(</span><span class=\"s\">"2006-01-02"</span><span class=\"p\">)</span></span><span class=\"p\">)</span>", diffToHTML("", []dmp.Diff{
|
2020-08-08 06:17:02 -04:00
|
|
|
{Type: dmp.DiffEqual, Text: "<span class=\"nx\">tagURL</span> <span class=\"o\">:=</span> <span class=\"n"},
|
|
|
|
{Type: dmp.DiffDelete, Text: "x\">fmt</span><span class=\"p\">.</span><span class=\"nf\">Sprintf</span><span class=\"p\">(</span><span class=\"s\">"## [%s](%s/%s/%s/%s?q=&type=all&state=closed&milestone=%d) - %s"</span><span class=\"p\">,</span> <span class=\"nx\">ge</span><span class=\"p\">.</span><span class=\"nx\">Milestone\""},
|
|
|
|
{Type: dmp.DiffInsert, Text: "f\">getGiteaTagURL</span><span class=\"p\">(</span><span class=\"nx\">client"},
|
|
|
|
{Type: dmp.DiffEqual, Text: "</span><span class=\"p\">,</span> <span class=\"nx\">ge</span><span class=\"p\">.</span><span class=\"nx\">BaseURL</span><span class=\"p\">,</span> <span class=\"nx\">ge</span><span class=\"p\">.</span><span class=\"nx\">Owner</span><span class=\"p\">,</span> <span class=\"nx\">ge</span><span class=\"p\">.</span><span class=\"nx\">Repo</span><span class=\"p\">,</span> <span class=\"nx\">"},
|
|
|
|
{Type: dmp.DiffDelete, Text: "from</span><span class=\"p\">,</span> <span class=\"nx\">milestoneID</span><span class=\"p\">,</span> <span class=\"nx\">time</span><span class=\"p\">.</span><span class=\"nf\">Now</span><span class=\"p\">(</span><span class=\"p\">)</span><span class=\"p\">.</span><span class=\"nf\">Format</span><span class=\"p\">(</span><span class=\"s\">"2006-01-02"</span><span class=\"p\">)"},
|
|
|
|
{Type: dmp.DiffInsert, Text: "ge</span><span class=\"p\">.</span><span class=\"nx\">Milestone</span><span class=\"p\">,</span> <span class=\"nx\">from</span><span class=\"p\">,</span> <span class=\"nx\">milestoneID"},
|
|
|
|
{Type: dmp.DiffEqual, Text: "</span><span class=\"p\">)</span>"},
|
2022-01-06 20:18:52 -05:00
|
|
|
}, DiffLineDel).Content)
|
2020-08-11 00:29:25 -04:00
|
|
|
|
2021-02-14 09:51:00 -05:00
|
|
|
assertEqual(t, "<span class=\"nx\">r</span><span class=\"p\">.</span><span class=\"nf\">WrapperRenderer</span><span class=\"p\">(</span><span class=\"nx\">w</span><span class=\"p\">,</span> <span class=\"removed-code\"><span class=\"nx\">language</span><span class=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs</span></span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>", diffToHTML("", []dmp.Diff{
|
2020-08-11 00:29:25 -04:00
|
|
|
{Type: dmp.DiffEqual, Text: "<span class=\"nx\">r</span><span class=\"p\">.</span><span class=\"nf\">WrapperRenderer</span><span class=\"p\">(</span><span class=\"nx\">w</span><span class=\"p\">,</span> <span class=\"nx\">"},
|
|
|
|
{Type: dmp.DiffDelete, Text: "language</span><span "},
|
|
|
|
{Type: dmp.DiffEqual, Text: "c"},
|
|
|
|
{Type: dmp.DiffDelete, Text: "lass=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs"},
|
|
|
|
{Type: dmp.DiffEqual, Text: "</span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>"},
|
2022-01-06 20:18:52 -05:00
|
|
|
}, DiffLineDel).Content)
|
2020-08-11 00:29:25 -04:00
|
|
|
|
2021-02-14 09:51:00 -05:00
|
|
|
assertEqual(t, "<span class=\"added-code\">language</span><span class=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs</span></span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>", diffToHTML("", []dmp.Diff{
|
2020-08-11 00:29:25 -04:00
|
|
|
{Type: dmp.DiffInsert, Text: "language</span><span "},
|
|
|
|
{Type: dmp.DiffEqual, Text: "c"},
|
|
|
|
{Type: dmp.DiffInsert, Text: "lass=\"p\">,</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"nx\">attrs"},
|
|
|
|
{Type: dmp.DiffEqual, Text: "</span><span class=\"p\">,</span> <span class=\"kc\">false</span><span class=\"p\">)</span>"},
|
2022-01-06 20:18:52 -05:00
|
|
|
}, DiffLineAdd).Content)
|
2020-10-18 13:51:38 -04:00
|
|
|
|
2021-02-14 09:51:00 -05:00
|
|
|
assertEqual(t, "<span class=\"k\">print</span><span class=\"added-code\"><span class=\"p\">(</span></span><span class=\"sa\"></span><span class=\"s2\">"</span><span class=\"s2\">// </span><span class=\"s2\">"</span><span class=\"p\">,</span> <span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">argv</span><span class=\"added-code\"><span class=\"p\">)</span></span>", diffToHTML("", []dmp.Diff{
|
2020-10-18 13:51:38 -04:00
|
|
|
{Type: dmp.DiffEqual, Text: "<span class=\"k\">print</span>"},
|
|
|
|
{Type: dmp.DiffInsert, Text: "<span"},
|
|
|
|
{Type: dmp.DiffEqual, Text: " "},
|
|
|
|
{Type: dmp.DiffInsert, Text: "class=\"p\">(</span>"},
|
|
|
|
{Type: dmp.DiffEqual, Text: "<span class=\"sa\"></span><span class=\"s2\">"</span><span class=\"s2\">// </span><span class=\"s2\">"</span><span class=\"p\">,</span> <span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">argv</span>"},
|
|
|
|
{Type: dmp.DiffInsert, Text: "<span class=\"p\">)</span>"},
|
2022-01-06 20:18:52 -05:00
|
|
|
}, DiffLineAdd).Content)
|
2020-11-04 19:35:05 -05:00
|
|
|
|
2021-02-14 09:51:00 -05:00
|
|
|
assertEqual(t, "sh <span class=\"added-code\">'useradd -u $(stat -c "%u" .gitignore) jenkins'</span>", diffToHTML("", []dmp.Diff{
|
2020-11-04 19:35:05 -05:00
|
|
|
{Type: dmp.DiffEqual, Text: "sh "},
|
|
|
|
{Type: dmp.DiffDelete, Text: "4;useradd -u 111 jenkins""},
|
|
|
|
{Type: dmp.DiffInsert, Text: "9;useradd -u $(stat -c "%u" .gitignore) jenkins'"},
|
|
|
|
{Type: dmp.DiffEqual, Text: ";"},
|
2022-01-06 20:18:52 -05:00
|
|
|
}, DiffLineAdd).Content)
|
2020-11-04 19:35:05 -05:00
|
|
|
|
2021-02-14 09:51:00 -05:00
|
|
|
assertEqual(t, "<span class=\"x\"> <h<span class=\"added-code\">4 class="release-list-title df ac"</span>></span>", diffToHTML("", []dmp.Diff{
|
2020-11-04 19:35:05 -05:00
|
|
|
{Type: dmp.DiffEqual, Text: "<span class=\"x\"> <h"},
|
|
|
|
{Type: dmp.DiffInsert, Text: "4 class=&#"},
|
|
|
|
{Type: dmp.DiffEqual, Text: "3"},
|
|
|
|
{Type: dmp.DiffInsert, Text: "4;release-list-title df ac""},
|
|
|
|
{Type: dmp.DiffEqual, Text: "></span>"},
|
2022-01-06 20:18:52 -05:00
|
|
|
}, DiffLineAdd).Content)
|
2016-01-03 16:26:46 -05:00
|
|
|
}
|
2018-08-06 00:43:22 -04:00
|
|
|
|
2021-11-20 08:50:00 -05:00
|
|
|
func TestParsePatch_skipTo(t *testing.T) {
|
|
|
|
type testcase struct {
|
|
|
|
name string
|
|
|
|
gitdiff string
|
|
|
|
wantErr bool
|
|
|
|
addition int
|
|
|
|
deletion int
|
|
|
|
oldFilename string
|
|
|
|
filename string
|
|
|
|
skipTo string
|
|
|
|
}
|
|
|
|
tests := []testcase{
|
|
|
|
{
|
|
|
|
name: "readme.md2readme.md",
|
|
|
|
gitdiff: `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
|
|
|
|
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
|
|
|
|
`,
|
|
|
|
addition: 4,
|
|
|
|
deletion: 1,
|
|
|
|
filename: "README.md",
|
|
|
|
oldFilename: "README.md",
|
|
|
|
skipTo: "README.md",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "A \\ B",
|
|
|
|
gitdiff: `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`,
|
|
|
|
addition: 4,
|
|
|
|
deletion: 1,
|
|
|
|
filename: "A \\ B",
|
|
|
|
oldFilename: "A \\ B",
|
|
|
|
skipTo: "A \\ B",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "A \\ B",
|
|
|
|
gitdiff: `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
|
|
|
|
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`,
|
|
|
|
addition: 4,
|
|
|
|
deletion: 1,
|
|
|
|
filename: "A \\ B",
|
|
|
|
oldFilename: "A \\ B",
|
|
|
|
skipTo: "A \\ B",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "readme.md2readme.md",
|
|
|
|
gitdiff: `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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
`,
|
|
|
|
addition: 4,
|
|
|
|
deletion: 1,
|
|
|
|
filename: "README.md",
|
|
|
|
oldFilename: "README.md",
|
|
|
|
skipTo: "README.md",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testcase := range tests {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
|
|
|
got, err := ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(testcase.gitdiff), testcase.skipTo)
|
|
|
|
if (err != nil) != testcase.wantErr {
|
|
|
|
t.Errorf("ParsePatch(%q) error = %v, wantErr %v", testcase.name, err, testcase.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
gotMarshaled, _ := json.MarshalIndent(got, "", " ")
|
|
|
|
if got.NumFiles != 1 {
|
|
|
|
t.Errorf("ParsePath(%q) did not receive 1 file:\n%s", testcase.name, string(gotMarshaled))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got.TotalAddition != testcase.addition {
|
|
|
|
t.Errorf("ParsePath(%q) does not have correct totalAddition %d, wanted %d", testcase.name, got.TotalAddition, testcase.addition)
|
|
|
|
}
|
|
|
|
if got.TotalDeletion != testcase.deletion {
|
|
|
|
t.Errorf("ParsePath(%q) did not have correct totalDeletion %d, wanted %d", testcase.name, got.TotalDeletion, testcase.deletion)
|
|
|
|
}
|
|
|
|
file := got.Files[0]
|
|
|
|
if file.Addition != testcase.addition {
|
|
|
|
t.Errorf("ParsePath(%q) does not have correct file addition %d, wanted %d", testcase.name, file.Addition, testcase.addition)
|
|
|
|
}
|
|
|
|
if file.Deletion != testcase.deletion {
|
|
|
|
t.Errorf("ParsePath(%q) did not have correct file deletion %d, wanted %d", testcase.name, file.Deletion, testcase.deletion)
|
|
|
|
}
|
|
|
|
if file.OldName != testcase.oldFilename {
|
|
|
|
t.Errorf("ParsePath(%q) did not have correct OldName %q, wanted %q", testcase.name, file.OldName, testcase.oldFilename)
|
|
|
|
}
|
|
|
|
if file.Name != testcase.filename {
|
|
|
|
t.Errorf("ParsePath(%q) did not have correct Name %q, wanted %q", testcase.name, file.Name, testcase.filename)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 09:08:40 -04:00
|
|
|
func TestParsePatch_singlefile(t *testing.T) {
|
|
|
|
type testcase struct {
|
|
|
|
name string
|
|
|
|
gitdiff string
|
|
|
|
wantErr bool
|
|
|
|
addition int
|
|
|
|
deletion int
|
|
|
|
oldFilename string
|
|
|
|
filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []testcase{
|
|
|
|
{
|
|
|
|
name: "readme.md2readme.md",
|
2020-10-14 00:49:33 -04:00
|
|
|
gitdiff: `diff --git "\\a/README.md" "\\b/README.md"
|
|
|
|
--- "\\a/README.md"
|
|
|
|
+++ "\\b/README.md"
|
2020-09-09 09:08:40 -04:00
|
|
|
@@ -1,3 +1,6 @@
|
|
|
|
# gitea-github-migrator
|
|
|
|
+
|
|
|
|
+ Build Status
|
|
|
|
- Latest Release
|
|
|
|
Docker Pulls
|
|
|
|
+ cut off
|
|
|
|
+ cut off
|
|
|
|
`,
|
2020-10-14 00:49:33 -04:00
|
|
|
addition: 4,
|
|
|
|
deletion: 1,
|
|
|
|
filename: "README.md",
|
|
|
|
oldFilename: "README.md",
|
2020-09-09 09:08:40 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "A \\ B",
|
|
|
|
gitdiff: `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`,
|
2020-10-14 00:49:33 -04:00
|
|
|
addition: 4,
|
|
|
|
deletion: 1,
|
|
|
|
filename: "A \\ B",
|
|
|
|
oldFilename: "A \\ B",
|
2020-09-09 09:08:40 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "really weird filename",
|
2020-10-14 00:49:33 -04:00
|
|
|
gitdiff: `diff --git "\\a/a b/file b/a a/file" "\\b/a b/file b/a a/file"
|
2020-09-09 09:08:40 -04:00
|
|
|
index d2186f1..f5c8ed2 100644
|
2020-11-21 17:41:24 -05:00
|
|
|
--- "\\a/a b/file b/a a/file" ` + `
|
|
|
|
+++ "\\b/a b/file b/a a/file" ` + `
|
2020-09-09 09:08:40 -04:00
|
|
|
@@ -1,3 +1,2 @@
|
|
|
|
Create a weird file.
|
2020-11-21 17:41:24 -05:00
|
|
|
` + `
|
2020-09-09 09:08:40 -04:00
|
|
|
-and what does diff do here?
|
|
|
|
\ No newline at end of file`,
|
|
|
|
addition: 0,
|
|
|
|
deletion: 1,
|
|
|
|
filename: "a b/file b/a a/file",
|
|
|
|
oldFilename: "a b/file b/a a/file",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete file with blanks",
|
2020-10-14 00:49:33 -04:00
|
|
|
gitdiff: `diff --git "\\a/file with blanks" "\\b/file with blanks"
|
2020-09-09 09:08:40 -04:00
|
|
|
deleted file mode 100644
|
|
|
|
index 898651a..0000000
|
2020-11-21 17:41:24 -05:00
|
|
|
--- "\\a/file with blanks" ` + `
|
2020-09-09 09:08:40 -04:00
|
|
|
+++ /dev/null
|
|
|
|
@@ -1,5 +0,0 @@
|
|
|
|
-a blank file
|
|
|
|
-
|
|
|
|
-has a couple o line
|
|
|
|
-
|
|
|
|
-the 5th line is the last
|
|
|
|
`,
|
2020-10-14 00:49:33 -04:00
|
|
|
addition: 0,
|
|
|
|
deletion: 5,
|
|
|
|
filename: "file with blanks",
|
|
|
|
oldFilename: "file with blanks",
|
2020-09-09 09:08:40 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rename a—as",
|
|
|
|
gitdiff: `diff --git "a/\360\243\220\265b\342\200\240vs" "b/a\342\200\224as"
|
|
|
|
similarity index 100%
|
|
|
|
rename from "\360\243\220\265b\342\200\240vs"
|
|
|
|
rename to "a\342\200\224as"
|
|
|
|
`,
|
|
|
|
addition: 0,
|
|
|
|
deletion: 0,
|
|
|
|
oldFilename: "𣐵b†vs",
|
|
|
|
filename: "a—as",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rename with spaces",
|
2020-10-14 00:49:33 -04:00
|
|
|
gitdiff: `diff --git "\\a/a b/file b/a a/file" "\\b/a b/a a/file b/b file"
|
2020-09-09 09:08:40 -04:00
|
|
|
similarity index 100%
|
|
|
|
rename from a b/file b/a a/file
|
|
|
|
rename to a b/a a/file b/b file
|
|
|
|
`,
|
|
|
|
oldFilename: "a b/file b/a a/file",
|
|
|
|
filename: "a b/a a/file b/b file",
|
|
|
|
},
|
2021-02-27 13:46:14 -05:00
|
|
|
{
|
|
|
|
name: "ambiguous deleted",
|
|
|
|
gitdiff: `diff --git a/b b/b b/b b/b
|
|
|
|
deleted file mode 100644
|
|
|
|
index 92e798b..0000000
|
|
|
|
--- a/b b/b` + "\t" + `
|
|
|
|
+++ /dev/null
|
|
|
|
@@ -1 +0,0 @@
|
|
|
|
-b b/b
|
|
|
|
`,
|
|
|
|
oldFilename: "b b/b",
|
|
|
|
filename: "b b/b",
|
|
|
|
addition: 0,
|
|
|
|
deletion: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ambiguous addition",
|
|
|
|
gitdiff: `diff --git a/b b/b b/b b/b
|
|
|
|
new file mode 100644
|
|
|
|
index 0000000..92e798b
|
|
|
|
--- /dev/null
|
|
|
|
+++ b/b b/b` + "\t" + `
|
|
|
|
@@ -0,0 +1 @@
|
|
|
|
+b b/b
|
|
|
|
`,
|
|
|
|
oldFilename: "b b/b",
|
|
|
|
filename: "b b/b",
|
|
|
|
addition: 1,
|
|
|
|
deletion: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rename",
|
|
|
|
gitdiff: `diff --git a/b b/b b/b b/b b/b b/b
|
|
|
|
similarity index 100%
|
|
|
|
rename from b b/b b/b b/b b/b
|
|
|
|
rename to b
|
|
|
|
`,
|
|
|
|
oldFilename: "b b/b b/b b/b b/b",
|
|
|
|
filename: "b",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ambiguous 1",
|
|
|
|
gitdiff: `diff --git a/b b/b b/b b/b b/b b/b
|
|
|
|
similarity index 100%
|
|
|
|
rename from b b/b b/b b/b b/b
|
|
|
|
rename to b
|
|
|
|
`,
|
|
|
|
oldFilename: "b b/b b/b b/b b/b",
|
|
|
|
filename: "b",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ambiguous 2",
|
|
|
|
gitdiff: `diff --git a/b b/b b/b b/b b/b b/b
|
|
|
|
similarity index 100%
|
|
|
|
rename from b b/b b/b b/b
|
|
|
|
rename to b b/b
|
|
|
|
`,
|
|
|
|
oldFilename: "b b/b b/b b/b",
|
|
|
|
filename: "b b/b",
|
|
|
|
},
|
2020-10-16 13:13:18 -04:00
|
|
|
{
|
|
|
|
name: "minuses-and-pluses",
|
|
|
|
gitdiff: `diff --git a/minuses-and-pluses b/minuses-and-pluses
|
|
|
|
index 6961180..9ba1a00 100644
|
|
|
|
--- a/minuses-and-pluses
|
|
|
|
+++ b/minuses-and-pluses
|
|
|
|
@@ -1,4 +1,4 @@
|
|
|
|
--- 1st line
|
|
|
|
-++ 2nd line
|
|
|
|
--- 3rd line
|
|
|
|
-++ 4th line
|
|
|
|
+++ 1st line
|
|
|
|
+-- 2nd line
|
|
|
|
+++ 3rd line
|
|
|
|
+-- 4th line
|
|
|
|
`,
|
|
|
|
oldFilename: "minuses-and-pluses",
|
|
|
|
filename: "minuses-and-pluses",
|
|
|
|
addition: 4,
|
|
|
|
deletion: 4,
|
|
|
|
},
|
2020-09-09 09:08:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range tests {
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
2021-11-20 08:50:00 -05:00
|
|
|
got, err := ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(testcase.gitdiff), "")
|
2020-09-09 09:08:40 -04:00
|
|
|
if (err != nil) != testcase.wantErr {
|
2021-02-27 13:46:14 -05:00
|
|
|
t.Errorf("ParsePatch(%q) error = %v, wantErr %v", testcase.name, err, testcase.wantErr)
|
2020-09-09 09:08:40 -04:00
|
|
|
return
|
|
|
|
}
|
2021-07-24 12:03:58 -04:00
|
|
|
|
2021-03-01 16:08:10 -05:00
|
|
|
gotMarshaled, _ := json.MarshalIndent(got, "", " ")
|
2020-09-09 09:08:40 -04:00
|
|
|
if got.NumFiles != 1 {
|
2021-02-27 13:46:14 -05:00
|
|
|
t.Errorf("ParsePath(%q) did not receive 1 file:\n%s", testcase.name, string(gotMarshaled))
|
2020-09-09 09:08:40 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if got.TotalAddition != testcase.addition {
|
2021-02-27 13:46:14 -05:00
|
|
|
t.Errorf("ParsePath(%q) does not have correct totalAddition %d, wanted %d", testcase.name, got.TotalAddition, testcase.addition)
|
2020-09-09 09:08:40 -04:00
|
|
|
}
|
|
|
|
if got.TotalDeletion != testcase.deletion {
|
2021-02-27 13:46:14 -05:00
|
|
|
t.Errorf("ParsePath(%q) did not have correct totalDeletion %d, wanted %d", testcase.name, got.TotalDeletion, testcase.deletion)
|
2020-09-09 09:08:40 -04:00
|
|
|
}
|
|
|
|
file := got.Files[0]
|
|
|
|
if file.Addition != testcase.addition {
|
2021-02-27 13:46:14 -05:00
|
|
|
t.Errorf("ParsePath(%q) does not have correct file addition %d, wanted %d", testcase.name, file.Addition, testcase.addition)
|
2020-09-09 09:08:40 -04:00
|
|
|
}
|
|
|
|
if file.Deletion != testcase.deletion {
|
2021-02-27 13:46:14 -05:00
|
|
|
t.Errorf("ParsePath(%q) did not have correct file deletion %d, wanted %d", testcase.name, file.Deletion, testcase.deletion)
|
2020-09-09 09:08:40 -04:00
|
|
|
}
|
|
|
|
if file.OldName != testcase.oldFilename {
|
2021-02-27 13:46:14 -05:00
|
|
|
t.Errorf("ParsePath(%q) did not have correct OldName %q, wanted %q", testcase.name, file.OldName, testcase.oldFilename)
|
2020-09-09 09:08:40 -04:00
|
|
|
}
|
|
|
|
if file.Name != testcase.filename {
|
2021-02-27 13:46:14 -05:00
|
|
|
t.Errorf("ParsePath(%q) did not have correct Name %q, wanted %q", testcase.name, file.Name, testcase.filename)
|
2020-09-09 09:08:40 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-21 17:41:24 -05:00
|
|
|
// Test max lines
|
|
|
|
diffBuilder := &strings.Builder{}
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
diff := `diff --git a/newfile2 b/newfile2
|
2020-11-21 17:41:24 -05:00
|
|
|
new file mode 100644
|
|
|
|
index 0000000..6bb8f39
|
|
|
|
--- /dev/null
|
|
|
|
+++ b/newfile2
|
|
|
|
@@ -0,0 +1,35 @@
|
|
|
|
`
|
|
|
|
diffBuilder.WriteString(diff)
|
|
|
|
|
|
|
|
for i := 0; i < 35; i++ {
|
|
|
|
diffBuilder.WriteString("+line" + strconv.Itoa(i) + "\n")
|
|
|
|
}
|
|
|
|
diff = diffBuilder.String()
|
2021-11-20 08:50:00 -05:00
|
|
|
result, err := ParsePatch(20, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff), "")
|
2020-11-21 17:41:24 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("There should not be an error: %v", err)
|
|
|
|
}
|
|
|
|
if !result.Files[0].IsIncomplete {
|
|
|
|
t.Errorf("Files should be incomplete! %v", result.Files[0])
|
|
|
|
}
|
2021-11-20 08:50:00 -05:00
|
|
|
result, err = ParsePatch(40, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff), "")
|
2020-11-21 17:41:24 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("There should not be an error: %v", err)
|
|
|
|
}
|
|
|
|
if result.Files[0].IsIncomplete {
|
|
|
|
t.Errorf("Files should not be incomplete! %v", result.Files[0])
|
|
|
|
}
|
2021-11-20 08:50:00 -05:00
|
|
|
result, err = ParsePatch(40, 5, setting.Git.MaxGitDiffFiles, strings.NewReader(diff), "")
|
2020-11-21 17:41:24 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("There should not be an error: %v", err)
|
|
|
|
}
|
|
|
|
if !result.Files[0].IsIncomplete {
|
|
|
|
t.Errorf("Files should be incomplete! %v", result.Files[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test max characters
|
|
|
|
diff = `diff --git a/newfile2 b/newfile2
|
|
|
|
new file mode 100644
|
|
|
|
index 0000000..6bb8f39
|
|
|
|
--- /dev/null
|
|
|
|
+++ b/newfile2
|
|
|
|
@@ -0,0 +1,35 @@
|
|
|
|
`
|
|
|
|
diffBuilder.Reset()
|
|
|
|
diffBuilder.WriteString(diff)
|
|
|
|
|
|
|
|
for i := 0; i < 33; i++ {
|
|
|
|
diffBuilder.WriteString("+line" + strconv.Itoa(i) + "\n")
|
|
|
|
}
|
|
|
|
diffBuilder.WriteString("+line33")
|
|
|
|
for i := 0; i < 512; i++ {
|
|
|
|
diffBuilder.WriteString("0123456789ABCDEF")
|
|
|
|
}
|
|
|
|
diffBuilder.WriteByte('\n')
|
|
|
|
diffBuilder.WriteString("+line" + strconv.Itoa(34) + "\n")
|
|
|
|
diffBuilder.WriteString("+line" + strconv.Itoa(35) + "\n")
|
|
|
|
diff = diffBuilder.String()
|
|
|
|
|
2021-11-20 08:50:00 -05:00
|
|
|
result, err = ParsePatch(20, 4096, setting.Git.MaxGitDiffFiles, strings.NewReader(diff), "")
|
2020-11-21 17:41:24 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("There should not be an error: %v", err)
|
|
|
|
}
|
|
|
|
if !result.Files[0].IsIncomplete {
|
|
|
|
t.Errorf("Files should be incomplete! %v", result.Files[0])
|
|
|
|
}
|
2021-11-20 08:50:00 -05:00
|
|
|
result, err = ParsePatch(40, 4096, setting.Git.MaxGitDiffFiles, strings.NewReader(diff), "")
|
2020-11-21 17:41:24 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("There should not be an error: %v", err)
|
|
|
|
}
|
|
|
|
if !result.Files[0].IsIncomplete {
|
|
|
|
t.Errorf("Files should be incomplete! %v", result.Files[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
diff = `diff --git "a/README.md" "b/README.md"
|
2019-03-14 12:09:53 -04:00
|
|
|
--- a/README.md
|
|
|
|
+++ b/README.md
|
|
|
|
@@ -1,3 +1,6 @@
|
|
|
|
# gitea-github-migrator
|
|
|
|
+
|
|
|
|
+ Build Status
|
|
|
|
- Latest Release
|
|
|
|
Docker Pulls
|
|
|
|
+ cut off
|
|
|
|
+ cut off`
|
2021-11-20 08:50:00 -05:00
|
|
|
result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff), "")
|
2019-03-14 12:09:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ParsePatch failed: %s", err)
|
|
|
|
}
|
|
|
|
println(result)
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
diff2 := `diff --git "a/A \\ B" "b/A \\ B"
|
2019-03-14 12:09:53 -04:00
|
|
|
--- "a/A \\ B"
|
|
|
|
+++ "b/A \\ B"
|
|
|
|
@@ -1,3 +1,6 @@
|
|
|
|
# gitea-github-migrator
|
|
|
|
+
|
|
|
|
+ Build Status
|
|
|
|
- Latest Release
|
|
|
|
Docker Pulls
|
|
|
|
+ cut off
|
|
|
|
+ cut off`
|
2021-11-20 08:50:00 -05:00
|
|
|
result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff2), "")
|
2019-03-14 12:09:53 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ParsePatch failed: %s", err)
|
|
|
|
}
|
|
|
|
println(result)
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
diff2a := `diff --git "a/A \\ B" b/A/B
|
2020-08-22 11:07:37 -04:00
|
|
|
--- "a/A \\ B"
|
|
|
|
+++ b/A/B
|
|
|
|
@@ -1,3 +1,6 @@
|
|
|
|
# gitea-github-migrator
|
|
|
|
+
|
|
|
|
+ Build Status
|
|
|
|
- Latest Release
|
|
|
|
Docker Pulls
|
|
|
|
+ cut off
|
|
|
|
+ cut off`
|
2021-11-20 08:50:00 -05:00
|
|
|
result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff2a), "")
|
2020-08-22 11:07:37 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ParsePatch failed: %s", err)
|
|
|
|
}
|
|
|
|
println(result)
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
diff3 := `diff --git a/README.md b/README.md
|
2019-03-14 12:09:53 -04:00
|
|
|
--- a/README.md
|
|
|
|
+++ b/README.md
|
|
|
|
@@ -1,3 +1,6 @@
|
|
|
|
# gitea-github-migrator
|
|
|
|
+
|
|
|
|
+ Build Status
|
|
|
|
- Latest Release
|
|
|
|
Docker Pulls
|
|
|
|
+ cut off
|
|
|
|
+ cut off`
|
2021-11-20 08:50:00 -05:00
|
|
|
result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff3), "")
|
2019-03-14 12:09:53 -04:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 12:46:10 -05:00
|
|
|
|
2018-08-06 00:43:22 -04:00
|
|
|
func TestDiff_LoadComments(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-09-05 22:20:09 -04:00
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2}).(*issues_model.Issue)
|
2021-11-24 04:49:20 -05:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2018-08-06 00:43:22 -04:00
|
|
|
diff := setupDefaultDiff()
|
2022-01-19 18:26:57 -05:00
|
|
|
assert.NoError(t, diff.LoadComments(db.DefaultContext, issue, user))
|
2018-08-06 00:43:22 -04:00
|
|
|
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())
|
2022-06-13 05:37:59 -04:00
|
|
|
assert.False(t, (&DiffLine{Type: DiffLineAdd, Comments: []*issues_model.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) {
|
2022-06-13 05:37:59 -04:00
|
|
|
assert.Equal(t, "previous", (&DiffLine{Comments: []*issues_model.Comment{{Line: -3}}}).GetCommentSide())
|
|
|
|
assert.Equal(t, "proposed", (&DiffLine{Comments: []*issues_model.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) {
|
2022-03-29 15:13:41 -04:00
|
|
|
gitRepo, err := git.OpenRepository(git.DefaultContext, "./testdata/academic-module")
|
2021-08-31 00:16:23 -04:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
2019-11-28 04:05:57 -05:00
|
|
|
for _, behavior := range []string{"-w", "--ignore-space-at-eol", "-b", ""} {
|
2021-11-21 11:51:08 -05:00
|
|
|
diffs, err := GetDiff(gitRepo,
|
|
|
|
&DiffOptions{
|
|
|
|
AfterCommitID: "bd7063cc7c04689c4d082183d32a604ed27a24f9",
|
|
|
|
BeforeCommitID: "559c156f8e0178b71cb44355428f24001b08fc68",
|
|
|
|
MaxLines: setting.Git.MaxGitDiffLines,
|
|
|
|
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
|
|
|
|
MaxFiles: setting.Git.MaxGitDiffFiles,
|
|
|
|
WhitespaceBehavior: behavior,
|
|
|
|
})
|
2019-11-28 04:05:57 -05:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 09:51:00 -05:00
|
|
|
|
|
|
|
func TestDiffToHTML_14231(t *testing.T) {
|
|
|
|
setting.Cfg = ini.Empty()
|
2021-11-17 15:37:00 -05:00
|
|
|
diffRecord := diffMatchPatch.DiffMain(highlight.Code("main.v", "", " run()\n"), highlight.Code("main.v", "", " run(db)\n"), true)
|
2021-02-14 09:51:00 -05:00
|
|
|
diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
|
|
|
|
|
2022-01-20 04:59:15 -05:00
|
|
|
expected := `<span class="line"><span class="cl"> <span class="n">run</span><span class="added-code"><span class="o">(</span><span class="n">db</span></span><span class="o">)</span>
|
|
|
|
</span></span>`
|
2021-02-14 09:51:00 -05:00
|
|
|
output := diffToHTML("main.v", diffRecord, DiffLineAdd)
|
|
|
|
|
2022-01-06 20:18:52 -05:00
|
|
|
assertEqual(t, expected, output.Content)
|
2021-02-14 09:51:00 -05:00
|
|
|
}
|
2021-11-07 12:52:50 -05:00
|
|
|
|
|
|
|
func TestNoCrashes(t *testing.T) {
|
|
|
|
type testcase struct {
|
|
|
|
gitdiff string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []testcase{
|
|
|
|
{
|
|
|
|
gitdiff: "diff --git \n--- a\t\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
gitdiff: "diff --git \"0\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testcase := range tests {
|
|
|
|
// It shouldn't crash, so don't care about the output.
|
2021-11-20 08:50:00 -05:00
|
|
|
ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(testcase.gitdiff), "")
|
2021-11-07 12:52:50 -05:00
|
|
|
}
|
|
|
|
}
|