sapling/tests/test-check-interfaces.py
Jun Wu 9dc21f8d0b codemod: import from the edenscm package
Summary:
D13853115 adds `edenscm/` to `sys.path` and code still uses `import mercurial`.
That has nasty problems if both `import mercurial` and
`import edenscm.mercurial` are used, because Python would think `mercurial.foo`
and `edenscm.mercurial.foo` are different modules so code like
`try: ... except mercurial.error.Foo: ...`, or `isinstance(x, mercurial.foo.Bar)`
would fail to handle the `edenscm.mercurial` version. There are also some
module-level states (ex. `extensions._extensions`) that would cause trouble if
they have multiple versions in a single process.

Change imports to use the `edenscm` so ideally the `mercurial` is no longer
imported at all. Add checks in extensions.py to catch unexpected extensions
importing modules from the old (wrong) locations when running tests.

Reviewed By: phillco

Differential Revision: D13868981

fbshipit-source-id: f4e2513766957fd81d85407994f7521a08e4de48
2019-01-29 17:25:32 -08:00

85 lines
2.1 KiB
Python

# Test that certain objects conform to well-defined interfaces.
from __future__ import absolute_import, print_function
from edenscm.mercurial import (
bundlerepo,
httppeer,
localrepo,
sshpeer,
statichttprepo,
ui as uimod,
unionrepo,
)
def checkobject(o):
"""Verify a constructed object conforms to interface rules.
An object must have __abstractmethods__ defined.
All "public" attributes of the object (attributes not prefixed with
an underscore) must be in __abstractmethods__ or appear on a base class
with __abstractmethods__.
"""
name = o.__class__.__name__
allowed = set()
for cls in o.__class__.__mro__:
if not getattr(cls, "__abstractmethods__", set()):
continue
allowed |= cls.__abstractmethods__
allowed |= {a for a in dir(cls) if not a.startswith("_")}
if not allowed:
print("%s does not have abstract methods" % name)
return
public = {a for a in dir(o) if not a.startswith("_")}
for attr in sorted(public - allowed):
print("public attributes not in abstract interface: %s.%s" % (name, attr))
# Facilitates testing localpeer.
class dummyrepo(object):
def __init__(self):
self.ui = uimod.ui()
def filtered(self, name):
pass
def _restrictcapabilities(self, caps):
pass
# Facilitates testing sshpeer without requiring an SSH server.
class testingsshpeer(sshpeer.sshpeer):
def _validaterepo(self, *args, **kwargs):
pass
class badpeer(httppeer.httppeer):
def __init__(self):
super(badpeer, self).__init__(uimod.ui(), "http://localhost")
self.badattribute = True
def badmethod(self):
pass
def main():
ui = uimod.ui()
checkobject(badpeer())
checkobject(httppeer.httppeer(ui, "http://localhost"))
checkobject(localrepo.localpeer(dummyrepo()))
checkobject(testingsshpeer(ui, "ssh://localhost/foo"))
checkobject(bundlerepo.bundlepeer(dummyrepo()))
checkobject(statichttprepo.statichttppeer(dummyrepo()))
checkobject(unionrepo.unionpeer(dummyrepo()))
main()