Fix the test to really assert what it means to

This commit is contained in:
Jean-Paul Calderone 2019-08-13 16:55:25 -04:00
parent 6d14a2d719
commit 43e19e6e51

View File

@ -1,31 +1,71 @@
""" """
Tests to check for Python2 regressions Tests to check for Python2 regressions
""" """
from twisted.trial import unittest
from inspect import isclass
from twisted.python.modules import getModule from twisted.python.modules import getModule
class PythonTwoRegressions(unittest.TestCase): from testtools import (
""" TestCase,
A test class to hold Python2 regression tests. )
""" from testtools.matchers import (
Equals,
)
def is_new_style(self, cls): BLACKLIST = {
"""check for being a new-style class""" "allmydata.test.check_load",
# another test could be: issubclass(value, type) "allmydata.watchdog._watchdog_541",
has_class_attr = hasattr(cls, '__class__') }
dict_or_slots = '__dict__' in dir(cls) or hasattr(cls, '__slots__')
return has_class_attr and dict_or_slots
def test_old_style_class(self):
def is_new_style(cls):
""" """
Check if all classes are new-style classes :return bool: ``True`` if and only if the given class is "new style".
""" """
# All new-style classes are instances of type. By definition.
return isinstance(cls, type)
def defined_here(cls, where):
"""
:return bool: ``True`` if and only if the given class was defined in a
module with the given name.
:note: Classes can lie about where they are defined. Try not to do that.
"""
return cls.__module__ == where
class PythonTwoRegressions(TestCase):
"""
Regression tests for Python 2 behaviors related to Python 3 porting.
"""
def test_new_style_classes(self):
"""
All classes in Tahoe-LAFS are new-style.
"""
newstyle = set()
classic = set()
for mod in getModule("allmydata").walkModules(): for mod in getModule("allmydata").walkModules():
if mod.name in BLACKLIST:
continue
# iterAttributes will only work on loaded modules. So, load it.
try:
mod.load()
except Exception as e:
print(mod.name, ":", e)
continue
for attr in mod.iterAttributes(): for attr in mod.iterAttributes():
value = attr.load() value = attr.load()
if isinstance(value, str): if isclass(value) and defined_here(value, mod.name):
# apparently strings are note a new-style class (in Python 2.7) if is_new_style(value):
# so we skip testing them newstyle.add(value)
return else:
self.assertTrue(self.is_new_style(value), classic.add(value)
"{} does not seem to be a new-style class".format(attr.name)) print(newstyle)
self.assertThat(
classic,
Equals(set()),
"Expected to find no classic classes.",
)