test-check-interfaces.py
266 lines
| 7.0 KiB
| text/x-python
|
PythonLexer
/ tests / test-check-interfaces.py
Gregory Szorc
|
r33806 | # Test that certain objects conform to well-defined interfaces. | ||
Gregory Szorc
|
r37828 | from mercurial import encoding | ||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r37828 | encoding.environ[b'HGREALINTERFACES'] = b'1' | ||
Gregory Szorc
|
r37198 | import os | ||
Martin von Zweigbergk
|
r37832 | import subprocess | ||
import sys | ||||
Pulkit Goyal
|
r43078 | from mercurial.interfaces import ( | ||
Augie Fackler
|
r43197 | dirstate as intdirstate, | ||
Pulkit Goyal
|
r43078 | repository, | ||
) | ||||
Augie Fackler
|
r43346 | from mercurial.thirdparty.zope import interface as zi | ||
from mercurial.thirdparty.zope.interface import verify as ziverify | ||||
Gregory Szorc
|
r33806 | from mercurial import ( | ||
Gregory Szorc
|
r34308 | bundlerepo, | ||
Augie Fackler
|
r43197 | dirstate, | ||
Gregory Szorc
|
r37459 | filelog, | ||
Gregory Szorc
|
r33806 | httppeer, | ||
localrepo, | ||||
Gregory Szorc
|
r38549 | manifest, | ||
Augie Fackler
|
r37887 | pycompat, | ||
Gregory Szorc
|
r39267 | revlog, | ||
Gregory Szorc
|
r33806 | sshpeer, | ||
Gregory Szorc
|
r34308 | statichttprepo, | ||
Gregory Szorc
|
r33806 | ui as uimod, | ||
Gregory Szorc
|
r34308 | unionrepo, | ||
Gregory Szorc
|
r37459 | vfs as vfsmod, | ||
Gregory Szorc
|
r37312 | wireprotoserver, | ||
wireprototypes, | ||||
Gregory Szorc
|
r37648 | wireprotov1peer, | ||
Gregory Szorc
|
r33806 | ) | ||
Augie Fackler
|
r41194 | testdir = os.path.dirname(__file__) | ||
sys.path[0:0] = [testdir] | ||||
import simplestorerepo | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r41194 | del sys.path[0] | ||
Gregory Szorc
|
r37198 | |||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r37459 | def checkzobject(o, allowextra=False): | ||
Gregory Szorc
|
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) | ||||
Gregory Szorc
|
r37459 | if allowextra: | ||
return | ||||
Gregory Szorc
|
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): | ||||
Augie Fackler
|
r43346 | print( | ||
'public attribute not declared in interfaces: %s.%s' | ||||
% (o.__class__.__name__, attr) | ||||
) | ||||
Gregory Szorc
|
r37198 | |||
Gregory Szorc
|
r33806 | # Facilitates testing localpeer. | ||
Gregory Szorc
|
r49801 | class dummyrepo: | ||
Gregory Szorc
|
r33806 | def __init__(self): | ||
self.ui = uimod.ui() | ||||
Raphaël Gomès
|
r47447 | self._wanted_sidedata = set() | ||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r33806 | def filtered(self, name): | ||
pass | ||||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r33806 | def _restrictcapabilities(self, caps): | ||
pass | ||||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r49801 | class dummyopener: | ||
Gregory Szorc
|
r37024 | handlers = [] | ||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r37570 | # Facilitates testing sshpeer without requiring a server. | ||
Gregory Szorc
|
r33806 | class badpeer(httppeer.httppeer): | ||
def __init__(self): | ||||
Augie Fackler
|
r43346 | super(badpeer, self).__init__( | ||
None, None, None, dummyopener(), None, None | ||||
) | ||||
Gregory Szorc
|
r33806 | self.badattribute = True | ||
def badmethod(self): | ||||
pass | ||||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r49801 | class dummypipe: | ||
Gregory Szorc
|
r36388 | def close(self): | ||
pass | ||||
Valentin Gatien-Baron
|
r47416 | @property | ||
def closed(self): | ||||
pass | ||||
Augie Fackler
|
r43346 | |||
Arseniy Alekseyev
|
r52812 | def init_test_repo(): | ||
testtmp_dir = os.path.normpath(os.environ['TESTTMP']) | ||||
test_repo_dir = os.path.join(testtmp_dir, "test-repo") | ||||
subprocess.run(["hg", "init", test_repo_dir]) | ||||
subprocess.run(["hg", "--cwd", test_repo_dir, "debugbuilddag", "+3<3+1"]) | ||||
return test_repo_dir | ||||
Gregory Szorc
|
r33806 | def main(): | ||
ui = uimod.ui() | ||||
Gregory Szorc
|
r37198 | # Needed so we can open a local repo with obsstore without a warning. | ||
Gregory Szorc
|
r40189 | ui.setconfig(b'experimental', b'evolution.createmarkers', True) | ||
Gregory Szorc
|
r33806 | |||
Gregory Szorc
|
r37336 | checkzobject(badpeer()) | ||
Gregory Szorc
|
r37653 | ziverify.verifyClass(repository.ipeerbase, httppeer.httppeer) | ||
Gregory Szorc
|
r37570 | checkzobject(httppeer.httppeer(None, None, None, dummyopener(), None, None)) | ||
Gregory Szorc
|
r37336 | |||
Augie Fackler
|
r43346 | ziverify.verifyClass(repository.ipeerbase, localrepo.localpeer) | ||
Gregory Szorc
|
r37336 | checkzobject(localrepo.localpeer(dummyrepo())) | ||
Augie Fackler
|
r43346 | ziverify.verifyClass( | ||
repository.ipeercommandexecutor, localrepo.localcommandexecutor | ||||
) | ||||
Gregory Szorc
|
r37648 | checkzobject(localrepo.localcommandexecutor(None)) | ||
Augie Fackler
|
r43346 | ziverify.verifyClass( | ||
repository.ipeercommandexecutor, wireprotov1peer.peerexecutor | ||||
) | ||||
Gregory Szorc
|
r37648 | checkzobject(wireprotov1peer.peerexecutor(None)) | ||
Gregory Szorc
|
r37653 | ziverify.verifyClass(repository.ipeerbase, sshpeer.sshv1peer) | ||
Augie Fackler
|
r43346 | checkzobject( | ||
sshpeer.sshv1peer( | ||||
ui, | ||||
b'ssh://localhost/foo', | ||||
b'', | ||||
dummypipe(), | ||||
dummypipe(), | ||||
None, | ||||
None, | ||||
) | ||||
) | ||||
Gregory Szorc
|
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())) | ||||
Gregory Szorc
|
r33806 | |||
Augie Fackler
|
r43346 | ziverify.verifyClass( | ||
repository.ilocalrepositorymain, localrepo.localrepository | ||||
) | ||||
ziverify.verifyClass( | ||||
repository.ilocalrepositoryfilestorage, localrepo.revlogfilestorage | ||||
) | ||||
Arseniy Alekseyev
|
r52812 | test_repo_dir = init_test_repo() | ||
repo = localrepo.makelocalrepository(ui, pycompat.fsencode(test_repo_dir)) | ||||
Gregory Szorc
|
r37198 | checkzobject(repo) | ||
Augie Fackler
|
r43346 | ziverify.verifyClass( | ||
wireprototypes.baseprotocolhandler, wireprotoserver.sshv1protocolhandler | ||||
) | ||||
ziverify.verifyClass( | ||||
wireprototypes.baseprotocolhandler, | ||||
wireprotoserver.httpv1protocolhandler, | ||||
) | ||||
Gregory Szorc
|
r37312 | |||
sshv1 = wireprotoserver.sshv1protocolhandler(None, None, None) | ||||
checkzobject(sshv1) | ||||
httpv1 = wireprotoserver.httpv1protocolhandler(None, None, None) | ||||
checkzobject(httpv1) | ||||
Gregory Szorc
|
r37459 | ziverify.verifyClass(repository.ifilestorage, filelog.filelog) | ||
Gregory Szorc
|
r38549 | ziverify.verifyClass(repository.imanifestdict, manifest.manifestdict) | ||
Augie Fackler
|
r45155 | ziverify.verifyClass(repository.imanifestdict, manifest.treemanifest) | ||
Augie Fackler
|
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 | ||||
) | ||||
Gregory Szorc
|
r38549 | ziverify.verifyClass(repository.imanifestlog, manifest.manifestlog) | ||
Gregory Szorc
|
r39350 | ziverify.verifyClass(repository.imanifeststorage, manifest.manifestrevlog) | ||
Gregory Szorc
|
r37459 | |||
Augie Fackler
|
r43346 | ziverify.verifyClass( | ||
repository.irevisiondelta, simplestorerepo.simplestorerevisiondelta | ||||
) | ||||
Augie Fackler
|
r41194 | ziverify.verifyClass(repository.ifilestorage, simplestorerepo.filestorage) | ||
Augie Fackler
|
r43346 | ziverify.verifyClass( | ||
repository.iverifyproblem, simplestorerepo.simplefilestoreproblem | ||||
) | ||||
Augie Fackler
|
r41194 | |||
Augie Fackler
|
r43197 | ziverify.verifyClass(intdirstate.idirstate, dirstate.dirstate) | ||
Augie Fackler
|
r37887 | vfs = vfsmod.vfs(b'.') | ||
fl = filelog.filelog(vfs, b'dummy.i') | ||||
Gregory Szorc
|
r37459 | checkzobject(fl, allowextra=True) | ||
Gregory Szorc
|
r38549 | # Conforms to imanifestlog. | ||
Augie Fackler
|
r43346 | ml = manifest.manifestlog( | ||
Joerg Sonnenberger
|
r47538 | vfs, | ||
repo, | ||||
manifest.manifestrevlog(repo.nodeconstants, repo.svfs), | ||||
repo.narrowmatch(), | ||||
Augie Fackler
|
r43346 | ) | ||
Gregory Szorc
|
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()) | ||||
Joerg Sonnenberger
|
r47538 | mrl = manifest.manifestrevlog(repo.nodeconstants, vfs) | ||
Gregory Szorc
|
r39350 | checkzobject(mrl) | ||
Augie Fackler
|
r43346 | ziverify.verifyClass(repository.irevisiondelta, revlog.revlogrevisiondelta) | ||
Gregory Szorc
|
r39266 | |||
Gregory Szorc
|
r39267 | rd = revlog.revlogrevisiondelta( | ||
Gregory Szorc
|
r39266 | node=b'', | ||
p1node=b'', | ||||
p2node=b'', | ||||
basenode=b'', | ||||
linknode=b'', | ||||
flags=b'', | ||||
baserevisionsize=None, | ||||
revision=b'', | ||||
Raphaël Gomès
|
r47445 | sidedata=b'', | ||
Augie Fackler
|
r43346 | delta=None, | ||
Raphaël Gomès
|
r47843 | protocol_flags=b'', | ||
Augie Fackler
|
r43346 | ) | ||
Gregory Szorc
|
r39266 | checkzobject(rd) | ||
Augie Fackler
|
r43346 | ziverify.verifyClass(repository.iverifyproblem, revlog.revlogproblem) | ||
Gregory Szorc
|
r39878 | checkzobject(revlog.revlogproblem()) | ||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r33806 | main() | ||