History

Alternative fix for HTML diff entity split (#13425)

* Alternative fix for HTML diff entity split This commit both reverts PR #13357 and uses the exiting implementation alredy used for spans to fix the same issue. That PR duplicates most of logic that is already present elsewhere and still was failing for some cases. This should be simpler as it uses the existing logic that already works for <span>s being split apart. Added both test cases as well. * Update gitdiff_test.go * fmt * entity can have uppercase letter, also add detailed comment per @zeripath

mrsdizzie committed 6 years ago

Commit d7e09831 parent dd882f62 files · +45 −114Browse files

Showing the changes to services/gitdiff/gitdiff_test.go (1 of 2 files). Show all

Unified · Split

services/gitdiff/gitdiff_test.go+15−30
@@ -15,7 +15,6 @@ import ( · 14 unmodified lines
15 "code.gitea.io/gitea/models"15 "code.gitea.io/gitea/models"
16 "code.gitea.io/gitea/modules/git"16 "code.gitea.io/gitea/modules/git"
17 "code.gitea.io/gitea/modules/setting"17 "code.gitea.io/gitea/modules/setting"
18 "github.com/sergi/go-diff/diffmatchpatch"
19 dmp "github.com/sergi/go-diff/diffmatchpatch"18 dmp "github.com/sergi/go-diff/diffmatchpatch"
20 "github.com/stretchr/testify/assert"19 "github.com/stretchr/testify/assert"
21 "gopkg.in/ini.v1"20 "gopkg.in/ini.v1"
@@ -27,35 +26,6 @@ func assertEqual(t *testing.T, s1 string, s2 template.HTML) { · 5 unmodified lines
27 }26 }
28}27}
29 28
30func TestUnsplitEntities(t *testing.T) {
31 left := "sh &#34;useradd -u 111 jenkins&#34;"
32 right := "sh &#39;useradd -u $(stat -c &#34;%u&#34; .gitignore) jenkins&#39;"
33 diffRecord := diffMatchPatch.DiffMain(left, right, true)
34 diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
35
36 // Now we need to clean up the split entities
37 diffRecord = unsplitEntities(diffRecord)
38 diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
39
40 leftRecombined := ""
41 rightRecombined := ""
42 for _, record := range diffRecord {
43 assert.False(t, unterminatedEntityRE.MatchString(record.Text), "")
44 switch record.Type {
45 case diffmatchpatch.DiffDelete:
46 leftRecombined += record.Text
47 case diffmatchpatch.DiffInsert:
48 rightRecombined += record.Text
49 default:
50 leftRecombined += record.Text
51 rightRecombined += record.Text
52 }
53 }
54
55 assert.EqualValues(t, left, leftRecombined)
56 assert.EqualValues(t, right, rightRecombined)
57}
58
59func TestDiffToHTML(t *testing.T) {29func TestDiffToHTML(t *testing.T) {
60 setting.Cfg = ini.Empty()30 setting.Cfg = ini.Empty()
61 assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML("", []dmp.Diff{31 assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML("", []dmp.Diff{
@@ -113,6 +83,21 @@ func TestDiffToHTML(t *testing.T) { · 51 unmodified lines
113 {Type: dmp.DiffEqual, Text: "<span class=\"sa\"></span><span class=\"s2\">&#34;</span><span class=\"s2\">// </span><span class=\"s2\">&#34;</span><span class=\"p\">,</span> <span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">argv</span>"},83 {Type: dmp.DiffEqual, Text: "<span class=\"sa\"></span><span class=\"s2\">&#34;</span><span class=\"s2\">// </span><span class=\"s2\">&#34;</span><span class=\"p\">,</span> <span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">argv</span>"},
114 {Type: dmp.DiffInsert, Text: "<span class=\"p\">)</span>"},84 {Type: dmp.DiffInsert, Text: "<span class=\"p\">)</span>"},
115 }, DiffLineAdd))85 }, DiffLineAdd))
86
87 assertEqual(t, "sh <span class=\"added-code\">&#39;useradd -u $(stat -c &#34;%u&#34; .gitignore) jenkins</span>&#39;", diffToHTML("", []dmp.Diff{
88 {Type: dmp.DiffEqual, Text: "sh &#3"},
89 {Type: dmp.DiffDelete, Text: "4;useradd -u 111 jenkins&#34"},
90 {Type: dmp.DiffInsert, Text: "9;useradd -u $(stat -c &#34;%u&#34; .gitignore) jenkins&#39"},
91 {Type: dmp.DiffEqual, Text: ";"},
92 }, DiffLineAdd))
93
94 assertEqual(t, "<span class=\"x\"> &lt;h<span class=\"added-code\">4 class=</span><span class=\"added-code\">&#34;release-list-title df ac&#34;</span>&gt;</span>", diffToHTML("", []dmp.Diff{
95 {Type: dmp.DiffEqual, Text: "<span class=\"x\"> &lt;h"},
96 {Type: dmp.DiffInsert, Text: "4 class=&#"},
97 {Type: dmp.DiffEqual, Text: "3"},
98 {Type: dmp.DiffInsert, Text: "4;release-list-title df ac&#34;"},
99 {Type: dmp.DiffEqual, Text: "&gt;</span>"},
100 }, DiffLineAdd))
116}101}
117 102
118func TestParsePatch_singlefile(t *testing.T) {103func TestParsePatch_singlefile(t *testing.T) {