view tests/test-check-interfaces.py @ 47315:825d5a5907b4

exewrapper: avoid directly linking against python3X.dll Subsequent code calls `LoadLibrary()` to attempt to load the DLL, but because of this symbol reference, there is an attempt to load the DLL used during the build prior to `_main()` running. This causes the whole process to fail if the DLL isn't in the standard search path. That also means it will never load the DLL for HackableMercurial. (Maybe we should get rid of that for py3, since you can install python for a user without admin rights?) This could also be resolved by calling `GetProcAddress()` on the symbol and dereferencing it, but using the environment variable is consistent with the *.bat file since fc8a5c9ecee0. (The environment variable persists after the interpreter is initialized.) Far more concerning is somehow I've gotten my system into a state where setting the flag causes any output to the pager to be lost (as if it wasn't set at all) in MSYS, cmd.exe, WSL, and PowerShell using py3.9.0, but the environment variable works properly. I'm sure this flag worked on some versions of py3, so I'm not sure what's going on here. This is might be related to init config related changes in 3.8[1], since it works with 3.7.8, but fails with 3.8.1. Somebody who understands encoding issues better than I do should give some thought to if we need to make some changes to our encoding strategy on Windows with py3. With or without the flag/envvar, there is proper output if the command is directly paged by piping to `more.com` (in any environment) or `less` (in MSYS and WSL), or if paging is disabled with `--pager=no`. Legacy mode is required though when Mercurial decides to spin up a pager. [1] https://bugs.python.org/issue41941 Differential Revision: https://phab.mercurial-scm.org/D10756
author Matt Harbison <matt_harbison@yahoo.com>
date Tue, 11 May 2021 01:05:38 -0400
parents 119790e1c67c
children 04688c51f81f
line wrap: on
line source

# Test that certain objects conform to well-defined interfaces.

from __future__ import absolute_import, print_function

from mercurial import encoding

encoding.environ[b'HGREALINTERFACES'] = b'1'

import os
import subprocess
import sys

# Only run if tests are run in a repo
if subprocess.call(
    [sys.executable, '%s/hghave' % os.environ['TESTDIR'], 'test-repo']
):
    sys.exit(80)

from mercurial.interfaces import (
    dirstate as intdirstate,
    repository,
)
from mercurial.thirdparty.zope import interface as zi
from mercurial.thirdparty.zope.interface import verify as ziverify
from mercurial import (
    bundlerepo,
    dirstate,
    filelog,
    httppeer,
    localrepo,
    manifest,
    pycompat,
    revlog,
    sshpeer,
    statichttprepo,
    ui as uimod,
    unionrepo,
    vfs as vfsmod,
    wireprotoserver,
    wireprototypes,
    wireprotov1peer,
    wireprotov2server,
)

testdir = os.path.dirname(__file__)
rootdir = pycompat.fsencode(os.path.normpath(os.path.join(testdir, '..')))

sys.path[0:0] = [testdir]
import simplestorerepo

del sys.path[0]


def checkzobject(o, allowextra=False):
    """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)

    if allowextra:
        return

    # 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):
        print(
            'public attribute not declared in interfaces: %s.%s'
            % (o.__class__.__name__, attr)
        )


# Facilitates testing localpeer.
class dummyrepo(object):
    def __init__(self):
        self.ui = uimod.ui()
        self._wanted_sidedata = set()

    def filtered(self, name):
        pass

    def _restrictcapabilities(self, caps):
        pass


class dummyopener(object):
    handlers = []


# Facilitates testing sshpeer without requiring a server.
class badpeer(httppeer.httppeer):
    def __init__(self):
        super(badpeer, self).__init__(
            None, None, None, dummyopener(), None, None
        )
        self.badattribute = True

    def badmethod(self):
        pass


class dummypipe(object):
    def close(self):
        pass

    @property
    def closed(self):
        pass


def main():
    ui = uimod.ui()
    # Needed so we can open a local repo with obsstore without a warning.
    ui.setconfig(b'experimental', b'evolution.createmarkers', True)

    checkzobject(badpeer())

    ziverify.verifyClass(repository.ipeerbase, httppeer.httppeer)
    checkzobject(httppeer.httppeer(None, None, None, dummyopener(), None, None))

    ziverify.verifyClass(repository.ipeerv2, httppeer.httpv2peer)
    checkzobject(httppeer.httpv2peer(None, b'', b'', None, None, None))

    ziverify.verifyClass(repository.ipeerbase, localrepo.localpeer)
    checkzobject(localrepo.localpeer(dummyrepo()))

    ziverify.verifyClass(
        repository.ipeercommandexecutor, localrepo.localcommandexecutor
    )
    checkzobject(localrepo.localcommandexecutor(None))

    ziverify.verifyClass(
        repository.ipeercommandexecutor, wireprotov1peer.peerexecutor
    )
    checkzobject(wireprotov1peer.peerexecutor(None))

    ziverify.verifyClass(repository.ipeerbase, sshpeer.sshv1peer)
    checkzobject(
        sshpeer.sshv1peer(
            ui,
            b'ssh://localhost/foo',
            b'',
            dummypipe(),
            dummypipe(),
            None,
            None,
        )
    )

    ziverify.verifyClass(repository.ipeerbase, sshpeer.sshv2peer)
    checkzobject(
        sshpeer.sshv2peer(
            ui,
            b'ssh://localhost/foo',
            b'',
            dummypipe(),
            dummypipe(),
            None,
            None,
        )
    )

    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()))

    ziverify.verifyClass(
        repository.ilocalrepositorymain, localrepo.localrepository
    )
    ziverify.verifyClass(
        repository.ilocalrepositoryfilestorage, localrepo.revlogfilestorage
    )
    repo = localrepo.makelocalrepository(ui, rootdir)
    checkzobject(repo)

    ziverify.verifyClass(
        wireprototypes.baseprotocolhandler, wireprotoserver.sshv1protocolhandler
    )
    ziverify.verifyClass(
        wireprototypes.baseprotocolhandler, wireprotoserver.sshv2protocolhandler
    )
    ziverify.verifyClass(
        wireprototypes.baseprotocolhandler,
        wireprotoserver.httpv1protocolhandler,
    )
    ziverify.verifyClass(
        wireprototypes.baseprotocolhandler,
        wireprotov2server.httpv2protocolhandler,
    )

    sshv1 = wireprotoserver.sshv1protocolhandler(None, None, None)
    checkzobject(sshv1)
    sshv2 = wireprotoserver.sshv2protocolhandler(None, None, None)
    checkzobject(sshv2)

    httpv1 = wireprotoserver.httpv1protocolhandler(None, None, None)
    checkzobject(httpv1)
    httpv2 = wireprotov2server.httpv2protocolhandler(None, None)
    checkzobject(httpv2)

    ziverify.verifyClass(repository.ifilestorage, filelog.filelog)
    ziverify.verifyClass(repository.imanifestdict, manifest.manifestdict)
    ziverify.verifyClass(repository.imanifestdict, manifest.treemanifest)
    ziverify.verifyClass(
        repository.imanifestrevisionstored, manifest.manifestctx
    )
    ziverify.verifyClass(
        repository.imanifestrevisionwritable, manifest.memmanifestctx
    )
    ziverify.verifyClass(
        repository.imanifestrevisionstored, manifest.treemanifestctx
    )
    ziverify.verifyClass(
        repository.imanifestrevisionwritable, manifest.memtreemanifestctx
    )
    ziverify.verifyClass(repository.imanifestlog, manifest.manifestlog)
    ziverify.verifyClass(repository.imanifeststorage, manifest.manifestrevlog)

    ziverify.verifyClass(
        repository.irevisiondelta, simplestorerepo.simplestorerevisiondelta
    )
    ziverify.verifyClass(repository.ifilestorage, simplestorerepo.filestorage)
    ziverify.verifyClass(
        repository.iverifyproblem, simplestorerepo.simplefilestoreproblem
    )

    ziverify.verifyClass(intdirstate.idirstate, dirstate.dirstate)

    vfs = vfsmod.vfs(b'.')
    fl = filelog.filelog(vfs, b'dummy.i')
    checkzobject(fl, allowextra=True)

    # Conforms to imanifestlog.
    ml = manifest.manifestlog(
        vfs,
        repo,
        manifest.manifestrevlog(repo.nodeconstants, repo.svfs),
        repo.narrowmatch(),
    )
    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())

    mrl = manifest.manifestrevlog(repo.nodeconstants, vfs)
    checkzobject(mrl)

    ziverify.verifyClass(repository.irevisiondelta, revlog.revlogrevisiondelta)

    rd = revlog.revlogrevisiondelta(
        node=b'',
        p1node=b'',
        p2node=b'',
        basenode=b'',
        linknode=b'',
        flags=b'',
        baserevisionsize=None,
        revision=b'',
        sidedata=b'',
        delta=None,
        protocol_flags=b'',
    )
    checkzobject(rd)

    ziverify.verifyClass(repository.iverifyproblem, revlog.revlogproblem)
    checkzobject(revlog.revlogproblem())


main()