Merge pull request #1037 from tahoe-lafs/3671.more-test-utilities-python-3

Port even more test utilities to Python 3

Fixes ticket:3671
This commit is contained in:
Itamar Turner-Trauring 2021-04-07 09:36:40 -04:00 committed by GitHub
commit 8b4e92e1d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 62 additions and 12 deletions

0
newsfragments/3671.minor Normal file
View File

View File

@ -1,3 +1,13 @@
"""
This module is only necessary on Python 2. Once Python 2 code is dropped, it
can be deleted.
"""
from future.utils import PY3
if PY3:
raise RuntimeError("Just use subprocess.Popen")
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
## Copyright (C) 2021 Valentin Lab ## Copyright (C) 2021 Valentin Lab

View File

@ -1,8 +1,15 @@
"""
Ported to Python 3.
"""
from __future__ import print_function from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from future.utils import PY2, bchr, binary_type from future.utils import PY2, bchr, binary_type
from future.builtins import str as future_str from future.builtins import str as future_str
from past.builtins import unicode if PY2:
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, dict, list, object, range, str, max, min # noqa: F401
import os import os
import time import time
@ -24,7 +31,7 @@ from allmydata.util.encodingutil import unicode_platform, get_filesystem_encodin
def skip_if_cannot_represent_filename(u): def skip_if_cannot_represent_filename(u):
precondition(isinstance(u, unicode)) precondition(isinstance(u, str))
enc = get_filesystem_encoding() enc = get_filesystem_encoding()
if not unicode_platform(): if not unicode_platform():
@ -44,7 +51,7 @@ def _getvalue(io):
def maybe_unicode_to_argv(o): def maybe_unicode_to_argv(o):
"""Convert object to argv form if necessary.""" """Convert object to argv form if necessary."""
if isinstance(o, unicode): if isinstance(o, str):
return unicode_to_argv(o) return unicode_to_argv(o)
return o return o
@ -181,7 +188,7 @@ class DevNullDictionary(dict):
return return
def insecurerandstr(n): def insecurerandstr(n):
return b''.join(map(bchr, map(randrange, [0]*n, [256]*n))) return b''.join(map(bchr, list(map(randrange, [0]*n, [256]*n))))
def flip_bit(good, which): def flip_bit(good, which):
"""Flip the low-order bit of good[which].""" """Flip the low-order bit of good[which]."""
@ -211,9 +218,9 @@ class ReallyEqualMixin(object):
# type. They're equal, and _logically_ the same type, but have # type. They're equal, and _logically_ the same type, but have
# different types in practice. # different types in practice.
if a.__class__ == future_str: if a.__class__ == future_str:
a = unicode(a) a = str(a)
if b.__class__ == future_str: if b.__class__ == future_str:
b = unicode(b) b = str(b)
self.assertEqual(type(a), type(b), "a :: %r (%s), b :: %r (%s), %r" % (a, type(a), b, type(b), msg)) self.assertEqual(type(a), type(b), "a :: %r (%s), b :: %r (%s), %r" % (a, type(a), b, type(b), msg))
@ -297,7 +304,7 @@ class ShouldFailMixin(object):
of the message wrapped by this Failure, or the test will fail. of the message wrapped by this Failure, or the test will fail.
""" """
assert substring is None or isinstance(substring, (bytes, unicode)) assert substring is None or isinstance(substring, (bytes, str))
d = defer.maybeDeferred(callable, *args, **kwargs) d = defer.maybeDeferred(callable, *args, **kwargs)
def done(res): def done(res):
if isinstance(res, failure.Failure): if isinstance(res, failure.Failure):

View File

@ -1,12 +1,21 @@
""" """
Tools aimed at the interaction between tests and Eliot. Tools aimed at the interaction between tests and Eliot.
Ported to Python 3.
""" """
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
# Python 2 compatibility # Python 2 compatibility
# Can't use `builtins.str` because it's not JSON encodable: # Can't use `builtins.str` because it's not JSON encodable:
# `exceptions.TypeError: <class 'future.types.newstr.newstr'> is not JSON-encodeable` # `exceptions.TypeError: <class 'future.types.newstr.newstr'> is not JSON-encodeable`
from past.builtins import unicode as str from past.builtins import unicode as str
from future.utils import PY2 from future.utils import PY2
if PY2:
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, max, min # noqa: F401
from six import ensure_text from six import ensure_text
__all__ = [ __all__ = [

View File

@ -1 +0,0 @@

View File

@ -1,6 +1,16 @@
""" """
Hypothesis strategies use for testing Tahoe-LAFS. Hypothesis strategies use for testing Tahoe-LAFS.
Ported to Python 3.
""" """
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future.utils import PY2
if PY2:
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
from hypothesis.strategies import ( from hypothesis.strategies import (
one_of, one_of,

View File

@ -1,3 +1,14 @@
"""
Ported to Python 3.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future.utils import PY2
if PY2:
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
import re import re

View File

@ -1394,8 +1394,8 @@ class Web(WebMixin, WebErrorMixin, testutil.StallMixin, testutil.ReallyEqualMixi
def _got(res_and_status_and_headers): def _got(res_and_status_and_headers):
(res, status, headers) = res_and_status_and_headers (res, status, headers) = res_and_status_and_headers
self.failUnlessReallyEqual(res, "") self.failUnlessReallyEqual(res, "")
self.failUnlessReallyEqual(headers.getRawHeaders("content-length")[0], self.failUnlessReallyEqual(int(headers.getRawHeaders("content-length")[0]),
str(len(self.BAR_CONTENTS))) len(self.BAR_CONTENTS))
self.failUnlessReallyEqual(headers.getRawHeaders("content-type"), self.failUnlessReallyEqual(headers.getRawHeaders("content-type"),
["text/plain"]) ["text/plain"])
d.addCallback(_got) d.addCallback(_got)
@ -3015,8 +3015,8 @@ class Web(WebMixin, WebErrorMixin, testutil.StallMixin, testutil.ReallyEqualMixi
def _got_headers(res_and_status_and_headers): def _got_headers(res_and_status_and_headers):
(res, status, headers) = res_and_status_and_headers (res, status, headers) = res_and_status_and_headers
self.failUnlessReallyEqual(res, "") self.failUnlessReallyEqual(res, "")
self.failUnlessReallyEqual(headers.getRawHeaders("content-length")[0], self.failUnlessReallyEqual(int(headers.getRawHeaders("content-length")[0]),
str(len(NEW2_CONTENTS))) len(NEW2_CONTENTS))
self.failUnlessReallyEqual(headers.getRawHeaders("content-type"), self.failUnlessReallyEqual(headers.getRawHeaders("content-type"),
["text/plain"]) ["text/plain"])
d.addCallback(_got_headers) d.addCallback(_got_headers)

View File

@ -100,14 +100,18 @@ PORTED_MODULES = [
"allmydata.test.cli", "allmydata.test.cli",
"allmydata.test.cli_node_api", "allmydata.test.cli_node_api",
"allmydata.test.common", "allmydata.test.common",
"allmydata.test.common_util",
"allmydata.test.common_web", "allmydata.test.common_web",
"allmydata.test.eliotutil",
"allmydata.test.no_network", "allmydata.test.no_network",
"allmydata.test.matchers", "allmydata.test.matchers",
"allmydata.test.mutable", "allmydata.test.mutable",
"allmydata.test.mutable.util", "allmydata.test.mutable.util",
"allmydata.test.python3_tests", "allmydata.test.python3_tests",
"allmydata.test.storage_plugin", "allmydata.test.storage_plugin",
"allmydata.test.strategies",
"allmydata.test.web", "allmydata.test.web",
"allmydata.test.web.common",
"allmydata.test.web.matchers", "allmydata.test.web.matchers",
"allmydata.testing", "allmydata.testing",
"allmydata.testing.web", "allmydata.testing.web",