[#8375] better py2/3 unified_diff fix with a test
diff --git a/Allura/allura/controllers/repository.py b/Allura/allura/controllers/repository.py
index 5191413..77a4d0f 100644
--- a/Allura/allura/controllers/repository.py
+++ b/Allura/allura/controllers/repository.py
@@ -900,8 +900,9 @@
return dict(a=a, b=b, diff=diff)
# could consider making Blob.__iter__ do unicode conversion?
- la = [h.really_unicode(line) for line in a]
- lb = [h.really_unicode(line) for line in b]
+ # py2 unified_diff can handle some unicode but not consistently, so best to do ensure_str (can drop it on py3)
+ la = [six.ensure_str(h.really_unicode(line)) for line in a]
+ lb = [six.ensure_str(h.really_unicode(line)) for line in b]
adesc = 'a' + h.really_unicode(apath)
bdesc = 'b' + h.really_unicode(b.path())
@@ -919,6 +920,8 @@
hd = HtmlSideBySideDiff()
diff = hd.make_table(la, lb, adesc, bdesc)
else:
+ # py2 unified_diff can handle some unicode but not consistently, so best to do str() and ensure_str()
+ # (can drop it on py3)
diff = str('').join(difflib.unified_diff(la, lb, six.ensure_str(adesc), six.ensure_str(bdesc)))
return dict(a=a, b=b, diff=diff)
diff --git a/Allura/allura/model/stats.py b/Allura/allura/model/stats.py
index 80966b8..6daddaa 100644
--- a/Allura/allura/model/stats.py
+++ b/Allura/allura/model/stats.py
@@ -371,9 +371,10 @@
if oldblob is None:
lines = len(listnew)
elif newblob and newblob.has_html_view:
+ # py2 unified_diff can handle some unicode but not consistently, so best to do ensure_str (can drop it on py3)
diff = difflib.unified_diff(
- [h.really_unicode(line) for line in listold],
- [h.really_unicode(line) for line in listnew],
+ [six.ensure_str(h.really_unicode(line)) for line in listold],
+ [six.ensure_str(h.really_unicode(line)) for line in listnew],
six.ensure_str('old' + oldblob.path()),
six.ensure_str('new' + newblob.path()))
lines = len(
diff --git a/ForgeGit/forgegit/tests/functional/test_controllers.py b/ForgeGit/forgegit/tests/functional/test_controllers.py
index c5c679a..b966fc6 100644
--- a/ForgeGit/forgegit/tests/functional/test_controllers.py
+++ b/ForgeGit/forgegit/tests/functional/test_controllers.py
@@ -25,6 +25,7 @@
import os
import shutil
import tempfile
+import textwrap
from datadiff.tools import assert_equal as dd_assert_equal
from nose.tools import assert_equal, assert_in, assert_not_in, assert_not_equal
@@ -321,6 +322,45 @@
assert 'readme' in resp, resp.showbrowser()
assert '+++' in resp, resp.showbrowser()
+ def test_diff_weirdchars(self):
+ self._setup_weird_chars_repo()
+ ci = self._get_ci(repo='/p/test/weird-chars/')
+ resp = self.app.get(h.urlquote(ci + 'tree/привіт.txt') + '?diff=407950e8fba4dbc108ffbce0128ed1085c52cfd7')
+ diffhtml = six.text_type(resp.html.select_one('.diffbrowser'))
+ assert_in(textwrap.dedent('''\
+ <span class="gd">--- a/привіт.txt</span>
+ <span class="gi">+++ b/привіт.txt</span>
+ <span class="gu">@@ -1 +1,2 @@</span>
+ Привіт!
+ <span class="gi">+Which means Hello!</span>'''),
+ diffhtml)
+
+ resp = self.app.get(h.urlquote(ci + 'tree/привіт.txt') + '?diff=407950e8fba4dbc108ffbce0128ed1085c52cfd7&diformat=sidebyside')
+ diffhtml = six.text_type(resp.html.select_one('.diffbrowser'))
+ assert_in(textwrap.dedent('''\
+ <thead>
+ <th class="lineno"></th>
+ <th>a/привіт.txt</th>
+ <th class="lineno"></th>
+ <th>b/привіт.txt</th>
+ </thead>
+ <tr>
+ <td class="lineno">1</td>
+ <td><pre>Привіт!
+ </pre></td>
+ <td class="lineno">1</td>
+ <td><pre>Привіт!
+ </pre></td>
+ </tr>
+ <tr>
+ <td class="lineno"></td>
+ <td><pre>
+ </pre></td>
+ <td class="lineno">2</td>
+ <td class="diff-add"><pre>Which means Hello!
+ </pre></td>'''),
+ diffhtml)
+
def test_diff_view_mode(self):
ci = self._get_ci()
fn = 'tree/README?diff=df30427c488aeab84b2352bdf88a3b19223f9d7a'