tests/test-check-interfaces.py
author Yuya Nishihara <yuya@tcha.org>
Sat, 02 Sep 2017 21:49:45 +0900
changeset 34058 4bf1889456f3
parent 33807 b70029f355a3
child 34307 afcbc6f64d27
permissions -rw-r--r--
test-editor-filename: fix portability of fake editor command - /bin/bash doesn't exist on FreeBSD - edit is executed by cmd.exe on Windows
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33807
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
# Test that certain objects conform to well-defined interfaces.
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
from __future__ import absolute_import, print_function
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
from mercurial import (
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
    httppeer,
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
    localrepo,
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
    sshpeer,
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
    ui as uimod,
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
)
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
def checkobject(o):
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    13
    """Verify a constructed object conforms to interface rules.
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    15
    An object must have __abstractmethods__ defined.
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    16
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
    All "public" attributes of the object (attributes not prefixed with
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    18
    an underscore) must be in __abstractmethods__ or appear on a base class
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    19
    with __abstractmethods__.
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    20
    """
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    21
    name = o.__class__.__name__
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    23
    allowed = set()
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
    for cls in o.__class__.__mro__:
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
        if not getattr(cls, '__abstractmethods__', set()):
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
            continue
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    27
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
        allowed |= cls.__abstractmethods__
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    29
        allowed |= {a for a in dir(cls) if not a.startswith('_')}
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
    if not allowed:
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
        print('%s does not have abstract methods' % name)
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
        return
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
    public = {a for a in dir(o) if not a.startswith('_')}
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
    for attr in sorted(public - allowed):
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
        print('public attributes not in abstract interface: %s.%s' % (
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
            name, attr))
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
# Facilitates testing localpeer.
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
class dummyrepo(object):
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
    def __init__(self):
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    44
        self.ui = uimod.ui()
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
    def filtered(self, name):
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    46
        pass
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
    def _restrictcapabilities(self, caps):
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
        pass
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
# Facilitates testing sshpeer without requiring an SSH server.
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
class testingsshpeer(sshpeer.sshpeer):
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
    def _validaterepo(self, *args, **kwargs):
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
        pass
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
class badpeer(httppeer.httppeer):
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
    def __init__(self):
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    57
        super(badpeer, self).__init__(uimod.ui(), 'http://localhost')
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    58
        self.badattribute = True
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    59
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    60
    def badmethod(self):
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    61
        pass
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    62
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    63
def main():
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    64
    ui = uimod.ui()
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    65
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    66
    checkobject(badpeer())
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    67
    checkobject(httppeer.httppeer(ui, 'http://localhost'))
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    68
    checkobject(localrepo.localpeer(dummyrepo()))
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    69
    checkobject(testingsshpeer(ui, 'ssh://localhost/foo'))
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    70
b70029f355a3 tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    71
main()