More passing tests on Python 3.

This commit is contained in:
Itamar Turner-Trauring 2021-01-27 12:06:25 -05:00
parent 9f5e1381bf
commit c1391159fa
3 changed files with 18 additions and 5 deletions

View File

@ -634,7 +634,10 @@ class WebMixin(TimezoneMixin):
getattr(res.value, "response", ""), getattr(res.value, "response", ""),
which)) which))
if response_substring: if response_substring:
self.failUnlessIn(response_substring, unicode(res.value.response, "utf-8"), response = res.value.response
if isinstance(response, bytes):
response = unicode(response, "utf-8")
self.failUnlessIn(response_substring, response,
"'%s' not in '%s' for test '%s'" % \ "'%s' not in '%s' for test '%s'" % \
(response_substring, res.value.response, (response_substring, res.value.response,
which)) which))
@ -2209,11 +2212,14 @@ class Web(WebMixin, WebErrorMixin, testutil.StallMixin, testutil.ReallyEqualMixi
def _got_json(data): def _got_json(data):
self.failUnlessReallyEqual(data["finished"], True) self.failUnlessReallyEqual(data["finished"], True)
size = data["size"] size = data["size"]
self.failUnless(size > 1000) # Match calculation of text value size below:
self.failUnless(
size.get("size-directories", 0) + size.get("size-mutable-files", 0) +
size.get("size-immutable-files", 0) > 1000)
d.addCallback(_got_json) d.addCallback(_got_json)
d.addCallback(self.get_operation_results, "126", "text") d.addCallback(self.get_operation_results, "126", "text")
def _got_text(res): def _got_text(res):
mo = re.search(r'^size: (\d+)$', res, re.M) mo = re.search(br'^size: (\d+)$', res, re.M)
self.failUnless(mo, res) self.failUnless(mo, res)
size = int(mo.group(1)) size = int(mo.group(1))
# with directories, the size varies. # with directories, the size varies.
@ -3311,6 +3317,7 @@ class Web(WebMixin, WebErrorMixin, testutil.StallMixin, testutil.ReallyEqualMixi
d.addCallback(_check_json) d.addCallback(_check_json)
d.addCallback(self.get_operation_results, "124", "html") d.addCallback(self.get_operation_results, "124", "html")
def _check_html(res): def _check_html(res):
res = unicode(res, "utf-8")
self.failUnlessIn("Objects Checked: <span>11</span>", res) self.failUnlessIn("Objects Checked: <span>11</span>", res)
self.failUnlessIn("Objects Healthy (before repair): <span>11</span>", res) self.failUnlessIn("Objects Healthy (before repair): <span>11</span>", res)

View File

@ -83,7 +83,13 @@ class MultiFormatResource(resource.Resource, object):
if isinstance(t, bytes): if isinstance(t, bytes):
t = unicode(t, "ascii") t = unicode(t, "ascii")
renderer = self._get_renderer(t) renderer = self._get_renderer(t)
return renderer(req) result = renderer(req)
# On Python 3, json.dumps() returns Unicode for example, but
# twisted.web expects bytes. Instead of updating every single render
# method, just handle Unicode one time here.
if isinstance(result, unicode):
result = result.encode("utf-8")
return result
def _get_renderer(self, fmt): def _get_renderer(self, fmt):
""" """

View File

@ -1290,7 +1290,7 @@ class DeepSizeResults(MultiFormatResource):
+ stats.get("size-mutable-files", 0) + stats.get("size-mutable-files", 0)
+ stats.get("size-directories", 0)) + stats.get("size-directories", 0))
output += "size: %d\n" % total output += "size: %d\n" % total
return output return output.encode("utf-8")
render_TEXT = render_HTML render_TEXT = render_HTML
def render_JSON(self, req): def render_JSON(self, req):