-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix broken spans in diffs #14678
Fix broken spans in diffs #14678
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/git" | ||
"code.gitea.io/gitea/modules/highlight" | ||
"code.gitea.io/gitea/modules/setting" | ||
dmp "github.com/sergi/go-diff/diffmatchpatch" | ||
"github.com/stretchr/testify/assert" | ||
|
@@ -23,7 +24,7 @@ import ( | |
|
||
func assertEqual(t *testing.T, s1 string, s2 template.HTML) { | ||
if s1 != string(s2) { | ||
t.Errorf("%s should be equal %s", s2, s1) | ||
t.Errorf("Did not receive expected results:\nExpected: %s\nActual: %s", s1, s2) | ||
} | ||
} | ||
|
||
|
@@ -61,22 +62,22 @@ func TestDiffToHTML(t *testing.T) { | |
{Type: dmp.DiffEqual, Text: "</span><span class=\"p\">)</span>"}, | ||
}, DiffLineDel)) | ||
|
||
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><span class=\"removed-code\"><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{ | ||
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{ | ||
{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>"}, | ||
}, DiffLineDel)) | ||
|
||
assertEqual(t, "<span class=\"added-code\">language</span></span><span class=\"added-code\"><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{ | ||
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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This removes an unnecessary close and reopen of a added-code span |
||
{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>"}, | ||
}, DiffLineAdd)) | ||
|
||
assertEqual(t, "<span class=\"k\">print</span><span class=\"added-code\"></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{ | ||
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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This removes an unnecessary close and reopen of a removed-code span |
||
{Type: dmp.DiffEqual, Text: "<span class=\"k\">print</span>"}, | ||
{Type: dmp.DiffInsert, Text: "<span"}, | ||
{Type: dmp.DiffEqual, Text: " "}, | ||
|
@@ -85,14 +86,14 @@ func TestDiffToHTML(t *testing.T) { | |
{Type: dmp.DiffInsert, Text: "<span class=\"p\">)</span>"}, | ||
}, DiffLineAdd)) | ||
|
||
assertEqual(t, "sh <span class=\"added-code\">'useradd -u $(stat -c "%u" .gitignore) jenkins</span>'", diffToHTML("", []dmp.Diff{ | ||
assertEqual(t, "sh <span class=\"added-code\">'useradd -u $(stat -c "%u" .gitignore) jenkins'</span>", diffToHTML("", []dmp.Diff{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you look at the provided diff array and what was previously listed as expected you'll see that we actually expected something incorrect! |
||
{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: ";"}, | ||
}, DiffLineAdd)) | ||
|
||
assertEqual(t, "<span class=\"x\"> <h<span class=\"added-code\">4 class=</span><span class=\"added-code\">"release-list-title df ac"</span>></span>", diffToHTML("", []dmp.Diff{ | ||
assertEqual(t, "<span class=\"x\"> <h<span class=\"added-code\">4 class="release-list-title df ac"</span>></span>", diffToHTML("", []dmp.Diff{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This removes an unnecessary close and reopen of a addded-code span |
||
{Type: dmp.DiffEqual, Text: "<span class=\"x\"> <h"}, | ||
{Type: dmp.DiffInsert, Text: "4 class=&#"}, | ||
{Type: dmp.DiffEqual, Text: "3"}, | ||
|
@@ -462,3 +463,14 @@ func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) { | |
} | ||
} | ||
} | ||
|
||
func TestDiffToHTML_14231(t *testing.T) { | ||
setting.Cfg = ini.Empty() | ||
diffRecord := diffMatchPatch.DiffMain(highlight.Code("main.v", " run()\n"), highlight.Code("main.v", " run(db)\n"), true) | ||
diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord) | ||
|
||
expected := ` <span class="n">run</span><span class="added-code"><span class="o">(</span><span class="n">db</span></span><span class="o">)</span>` | ||
output := diffToHTML("main.v", diffRecord, DiffLineAdd) | ||
|
||
assertEqual(t, expected, output) | ||
} | ||
Comment on lines
+467
to
+476
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here we add a test case specific for the issue that this PR fixes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This removes an unnecessary close and reopen of a removed-code span