test-check-interfaces.py
287 lines
| 7.7 KiB
| text/x-python
|
PythonLexer
/ tests / test-check-interfaces.py
|
r33806 | # Test that certain objects conform to well-defined interfaces. | ||
from __future__ import absolute_import, print_function | ||||
|
r37828 | from mercurial import encoding | ||
|
r43346 | |||
|
r37828 | encoding.environ[b'HGREALINTERFACES'] = b'1' | ||
|
r37198 | import os | ||
|
r37832 | import subprocess | ||
import sys | ||||
# Only run if tests are run in a repo | ||||
|
r43346 | if subprocess.call( | ||
|
r46792 | [sys.executable, '%s/hghave' % os.environ['TESTDIR'], 'test-repo'] | ||
|
r43346 | ): | ||
|
r37832 | sys.exit(80) | ||
|
r37198 | |||
|
r43078 | from mercurial.interfaces import ( | ||
|
r43197 | dirstate as intdirstate, | ||
|
r43078 | repository, | ||
) | ||||
|
r43346 | from mercurial.thirdparty.zope import interface as zi | ||
from mercurial.thirdparty.zope.interface import verify as ziverify | ||||
|
r33806 | from mercurial import ( | ||
|
r34308 | bundlerepo, | ||
|
r43197 | dirstate, | ||
|
r37459 | filelog, | ||
|
r33806 | httppeer, | ||
localrepo, | ||||
|
r38549 | manifest, | ||
|
r37887 | pycompat, | ||
|
r39267 | revlog, | ||
|
r33806 | sshpeer, | ||
|
r34308 | statichttprepo, | ||
|
r33806 | ui as uimod, | ||
|
r34308 | unionrepo, | ||
|
r37459 | vfs as vfsmod, | ||
|
r37312 | wireprotoserver, | ||
wireprototypes, | ||||
|
r37648 | wireprotov1peer, | ||
|
r37563 | wireprotov2server, | ||
|
r33806 | ) | ||
|
r41194 | testdir = os.path.dirname(__file__) | ||
rootdir = pycompat.fsencode(os.path.normpath(os.path.join(testdir, '..'))) | ||||
sys.path[0:0] = [testdir] | ||||
import simplestorerepo | ||||
|
r43346 | |||
|
r41194 | del sys.path[0] | ||
|
r37198 | |||
|
r43346 | |||
|
r37459 | def checkzobject(o, allowextra=False): | ||
|
r37198 | """Verify an object with a zope interface.""" | ||
ifaces = zi.providedBy(o) | ||||
if not ifaces: | ||||
print('%r does not provide any zope interfaces' % o) | ||||
return | ||||
# Run zope.interface's built-in verification routine. This verifies that | ||||
# everything that is supposed to be present is present. | ||||
for iface in ifaces: | ||||
ziverify.verifyObject(iface, o) | ||||
|
r37459 | if allowextra: | ||
return | ||||
|
r37198 | # Now verify that the object provides no extra public attributes that | ||
# aren't declared as part of interfaces. | ||||
allowed = set() | ||||
for iface in ifaces: | ||||
allowed |= set(iface.names(all=True)) | ||||
public = {a for a in dir(o) if not a.startswith('_')} | ||||
for attr in sorted(public - allowed): | ||||
|
r43346 | print( | ||
'public attribute not declared in interfaces: %s.%s' | ||||
% (o.__class__.__name__, attr) | ||||
) | ||||
|
r37198 | |||
|
r33806 | # Facilitates testing localpeer. | ||
class dummyrepo(object): | ||||
def __init__(self): | ||||
self.ui = uimod.ui() | ||||
|
r43346 | |||
|
r33806 | def filtered(self, name): | ||
pass | ||||
|
r43346 | |||
|
r33806 | def _restrictcapabilities(self, caps): | ||
pass | ||||
|
r43346 | |||
|
r37024 | class dummyopener(object): | ||
handlers = [] | ||||
|
r43346 | |||
|
r37570 | # Facilitates testing sshpeer without requiring a server. | ||
|
r33806 | class badpeer(httppeer.httppeer): | ||
def __init__(self): | ||||
|
r43346 | super(badpeer, self).__init__( | ||
None, None, None, dummyopener(), None, None | ||||
) | ||||
|
r33806 | self.badattribute = True | ||
def badmethod(self): | ||||
pass | ||||
|
r43346 | |||
|
r36388 | class dummypipe(object): | ||
def close(self): | ||||
pass | ||||
|
r47416 | @property | ||
def closed(self): | ||||
pass | ||||
|
r43346 | |||
|
r33806 | def main(): | ||
ui = uimod.ui() | ||||
|
r37198 | # Needed so we can open a local repo with obsstore without a warning. | ||
|
r40189 | ui.setconfig(b'experimental', b'evolution.createmarkers', True) | ||
|
r33806 | |||
|
r37336 | checkzobject(badpeer()) | ||
|
r37653 | ziverify.verifyClass(repository.ipeerbase, httppeer.httppeer) | ||
|
r37570 | checkzobject(httppeer.httppeer(None, None, None, dummyopener(), None, None)) | ||
|
r37336 | |||
|
r40207 | ziverify.verifyClass(repository.ipeerv2, httppeer.httpv2peer) | ||
|
r37887 | checkzobject(httppeer.httpv2peer(None, b'', b'', None, None, None)) | ||
|
r37627 | |||
|
r43346 | ziverify.verifyClass(repository.ipeerbase, localrepo.localpeer) | ||
|
r37336 | checkzobject(localrepo.localpeer(dummyrepo())) | ||
|
r43346 | ziverify.verifyClass( | ||
repository.ipeercommandexecutor, localrepo.localcommandexecutor | ||||
) | ||||
|
r37648 | checkzobject(localrepo.localcommandexecutor(None)) | ||
|
r43346 | ziverify.verifyClass( | ||
repository.ipeercommandexecutor, wireprotov1peer.peerexecutor | ||||
) | ||||
|
r37648 | checkzobject(wireprotov1peer.peerexecutor(None)) | ||
|
r37653 | ziverify.verifyClass(repository.ipeerbase, sshpeer.sshv1peer) | ||
|
r43346 | checkzobject( | ||
sshpeer.sshv1peer( | ||||
ui, | ||||
b'ssh://localhost/foo', | ||||
b'', | ||||
dummypipe(), | ||||
dummypipe(), | ||||
None, | ||||
None, | ||||
) | ||||
) | ||||
|
r37336 | |||
|
r37653 | ziverify.verifyClass(repository.ipeerbase, sshpeer.sshv2peer) | ||
|
r43346 | checkzobject( | ||
sshpeer.sshv2peer( | ||||
ui, | ||||
b'ssh://localhost/foo', | ||||
b'', | ||||
dummypipe(), | ||||
dummypipe(), | ||||
None, | ||||
None, | ||||
) | ||||
) | ||||
|
r37336 | |||
ziverify.verifyClass(repository.ipeerbase, bundlerepo.bundlepeer) | ||||
checkzobject(bundlerepo.bundlepeer(dummyrepo())) | ||||
ziverify.verifyClass(repository.ipeerbase, statichttprepo.statichttppeer) | ||||
checkzobject(statichttprepo.statichttppeer(dummyrepo())) | ||||
ziverify.verifyClass(repository.ipeerbase, unionrepo.unionpeer) | ||||
checkzobject(unionrepo.unionpeer(dummyrepo())) | ||||
|
r33806 | |||
|
r43346 | ziverify.verifyClass( | ||
repository.ilocalrepositorymain, localrepo.localrepository | ||||
) | ||||
ziverify.verifyClass( | ||||
repository.ilocalrepositoryfilestorage, localrepo.revlogfilestorage | ||||
) | ||||
|
r39723 | repo = localrepo.makelocalrepository(ui, rootdir) | ||
|
r37198 | checkzobject(repo) | ||
|
r43346 | ziverify.verifyClass( | ||
wireprototypes.baseprotocolhandler, wireprotoserver.sshv1protocolhandler | ||||
) | ||||
ziverify.verifyClass( | ||||
wireprototypes.baseprotocolhandler, wireprotoserver.sshv2protocolhandler | ||||
) | ||||
ziverify.verifyClass( | ||||
wireprototypes.baseprotocolhandler, | ||||
wireprotoserver.httpv1protocolhandler, | ||||
) | ||||
ziverify.verifyClass( | ||||
wireprototypes.baseprotocolhandler, | ||||
wireprotov2server.httpv2protocolhandler, | ||||
) | ||||
|
r37312 | |||
sshv1 = wireprotoserver.sshv1protocolhandler(None, None, None) | ||||
checkzobject(sshv1) | ||||
sshv2 = wireprotoserver.sshv2protocolhandler(None, None, None) | ||||
checkzobject(sshv2) | ||||
httpv1 = wireprotoserver.httpv1protocolhandler(None, None, None) | ||||
checkzobject(httpv1) | ||||
|
r37563 | httpv2 = wireprotov2server.httpv2protocolhandler(None, None) | ||
|
r37312 | checkzobject(httpv2) | ||
|
r37459 | ziverify.verifyClass(repository.ifilestorage, filelog.filelog) | ||
|
r38549 | ziverify.verifyClass(repository.imanifestdict, manifest.manifestdict) | ||
|
r45155 | ziverify.verifyClass(repository.imanifestdict, manifest.treemanifest) | ||
|
r43346 | ziverify.verifyClass( | ||
repository.imanifestrevisionstored, manifest.manifestctx | ||||
) | ||||
ziverify.verifyClass( | ||||
repository.imanifestrevisionwritable, manifest.memmanifestctx | ||||
) | ||||
ziverify.verifyClass( | ||||
repository.imanifestrevisionstored, manifest.treemanifestctx | ||||
) | ||||
ziverify.verifyClass( | ||||
repository.imanifestrevisionwritable, manifest.memtreemanifestctx | ||||
) | ||||
|
r38549 | ziverify.verifyClass(repository.imanifestlog, manifest.manifestlog) | ||
|
r39350 | ziverify.verifyClass(repository.imanifeststorage, manifest.manifestrevlog) | ||
|
r37459 | |||
|
r43346 | ziverify.verifyClass( | ||
repository.irevisiondelta, simplestorerepo.simplestorerevisiondelta | ||||
) | ||||
|
r41194 | ziverify.verifyClass(repository.ifilestorage, simplestorerepo.filestorage) | ||
|
r43346 | ziverify.verifyClass( | ||
repository.iverifyproblem, simplestorerepo.simplefilestoreproblem | ||||
) | ||||
|
r41194 | |||
|
r43197 | ziverify.verifyClass(intdirstate.idirstate, dirstate.dirstate) | ||
|
r37887 | vfs = vfsmod.vfs(b'.') | ||
fl = filelog.filelog(vfs, b'dummy.i') | ||||
|
r37459 | checkzobject(fl, allowextra=True) | ||
|
r38549 | # Conforms to imanifestlog. | ||
|
r43346 | ml = manifest.manifestlog( | ||
vfs, repo, manifest.manifestrevlog(repo.svfs), repo.narrowmatch() | ||||
) | ||||
|
r38549 | checkzobject(ml) | ||
checkzobject(repo.manifestlog) | ||||
# Conforms to imanifestrevision. | ||||
mctx = ml[repo[0].manifestnode()] | ||||
checkzobject(mctx) | ||||
# Conforms to imanifestrevisionwritable. | ||||
checkzobject(mctx.copy()) | ||||
# Conforms to imanifestdict. | ||||
checkzobject(mctx.read()) | ||||
|
r39350 | mrl = manifest.manifestrevlog(vfs) | ||
checkzobject(mrl) | ||||
|
r43346 | ziverify.verifyClass(repository.irevisiondelta, revlog.revlogrevisiondelta) | ||
|
r39266 | |||
|
r39267 | rd = revlog.revlogrevisiondelta( | ||
|
r39266 | node=b'', | ||
p1node=b'', | ||||
p2node=b'', | ||||
basenode=b'', | ||||
linknode=b'', | ||||
flags=b'', | ||||
baserevisionsize=None, | ||||
revision=b'', | ||||
|
r43346 | delta=None, | ||
) | ||||
|
r39266 | checkzobject(rd) | ||
|
r43346 | ziverify.verifyClass(repository.iverifyproblem, revlog.revlogproblem) | ||
|
r39878 | checkzobject(revlog.revlogproblem()) | ||
|
r43346 | |||
|
r33806 | main() | ||