tests/test-rust-discovery.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48946 642e31cb55f0
child 51245 0b81440e2a73
permissions -rw-r--r--
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     1
import unittest
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     2
42670
a7f1a0b3f461 module-policy: update rust extension import to use the new module policy
Raphaël Gomès <rgomes@octobus.net>
parents: 42180
diff changeset
     3
from mercurial import policy
a7f1a0b3f461 module-policy: update rust extension import to use the new module policy
Raphaël Gomès <rgomes@octobus.net>
parents: 42180
diff changeset
     4
a7f1a0b3f461 module-policy: update rust extension import to use the new module policy
Raphaël Gomès <rgomes@octobus.net>
parents: 42180
diff changeset
     5
PartialDiscovery = policy.importrust('discovery', member='PartialDiscovery')
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     6
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     7
try:
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     8
    from mercurial.cext import parsers as cparsers
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     9
except ImportError:
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    10
    cparsers = None
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    11
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    12
# picked from test-parse-index2, copied rather than imported
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    13
# so that it stays stable even if test-parse-index2 changes or disappears.
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    14
data_non_inlined = (
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    15
    b'\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01D\x19'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    16
    b'\x00\x07e\x12\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    17
    b'\xff\xff\xff\xff\xd1\xf4\xbb\xb0\xbe\xfc\x13\xbd\x8c\xd3\x9d'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    18
    b'\x0f\xcd\xd9;\x8c\x07\x8cJ/\x00\x00\x00\x00\x00\x00\x00\x00\x00'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    19
    b'\x00\x00\x00\x00\x00\x00\x01D\x19\x00\x00\x00\x00\x00\xdf\x00'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    20
    b'\x00\x01q\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\xff'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    21
    b'\xff\xff\xff\xc1\x12\xb9\x04\x96\xa4Z1t\x91\xdfsJ\x90\xf0\x9bh'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    22
    b'\x07l&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    23
    b'\x00\x01D\xf8\x00\x00\x00\x00\x01\x1b\x00\x00\x01\xb8\x00\x00'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    24
    b'\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\xff\xff\xff\xff\x02\n'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    25
    b'\x0e\xc6&\xa1\x92\xae6\x0b\x02i\xfe-\xe5\xbao\x05\xd1\xe7\x00'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    26
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01F'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    27
    b'\x13\x00\x00\x00\x00\x01\xec\x00\x00\x03\x06\x00\x00\x00\x01'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    28
    b'\x00\x00\x00\x03\x00\x00\x00\x02\xff\xff\xff\xff\x12\xcb\xeby1'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    29
    b'\xb6\r\x98B\xcb\x07\xbd`\x8f\x92\xd9\xc4\x84\xbdK\x00\x00\x00'
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    30
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x00'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    31
)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    32
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    33
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
    34
class fakechangelog:
42759
791791a1fd4e tests: split joint repo/changelog fake into one for each type
Augie Fackler <augie@google.com>
parents: 42741
diff changeset
    35
    def __init__(self, idx):
791791a1fd4e tests: split joint repo/changelog fake into one for each type
Augie Fackler <augie@google.com>
parents: 42741
diff changeset
    36
        self.index = idx
791791a1fd4e tests: split joint repo/changelog fake into one for each type
Augie Fackler <augie@google.com>
parents: 42741
diff changeset
    37
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    38
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
    39
class fakerepo:
42736
b6f3f704a561 rust-discovery: read the index from a repo passed at init
Georges Racinet <georges.racinet@octobus.net>
parents: 42735
diff changeset
    40
    def __init__(self, idx):
b6f3f704a561 rust-discovery: read the index from a repo passed at init
Georges Racinet <georges.racinet@octobus.net>
parents: 42735
diff changeset
    41
        """Just make so that self.changelog.index is the given idx."""
42759
791791a1fd4e tests: split joint repo/changelog fake into one for each type
Augie Fackler <augie@google.com>
parents: 42741
diff changeset
    42
        self.changelog = fakechangelog(idx)
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    43
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    44
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    45
@unittest.skipIf(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    46
    PartialDiscovery is None or cparsers is None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    47
    "rustext or the C Extension parsers module "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    48
    "discovery relies on is not available",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    49
)
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    50
class rustdiscoverytest(unittest.TestCase):
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    51
    """Test the correctness of binding to Rust code.
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    52
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    53
    This test is merely for the binding to Rust itself: extraction of
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    54
    Python variable, giving back the results etc.
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    55
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    56
    It is not meant to test the algorithmic correctness of the provided
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    57
    methods. Hence the very simple embedded index data is good enough.
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    58
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    59
    Algorithmic correctness is asserted by the Rust unit tests.
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    60
    """
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    61
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    62
    def parseindex(self):
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    63
        return cparsers.parse_index2(data_non_inlined, False)[0]
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    64
42736
b6f3f704a561 rust-discovery: read the index from a repo passed at init
Georges Racinet <georges.racinet@octobus.net>
parents: 42735
diff changeset
    65
    def repo(self):
b6f3f704a561 rust-discovery: read the index from a repo passed at init
Georges Racinet <georges.racinet@octobus.net>
parents: 42735
diff changeset
    66
        return fakerepo(self.parseindex())
b6f3f704a561 rust-discovery: read the index from a repo passed at init
Georges Racinet <georges.racinet@octobus.net>
parents: 42735
diff changeset
    67
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    68
    def testindex(self):
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    69
        idx = self.parseindex()
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    70
        # checking our assumptions about the index binary data:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    71
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    72
            {i: (r[5], r[6]) for i, r in enumerate(idx)},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    73
            {0: (-1, -1), 1: (0, -1), 2: (1, -1), 3: (2, -1)},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
    74
        )
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    75
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    76
    def testaddcommonsmissings(self):
42736
b6f3f704a561 rust-discovery: read the index from a repo passed at init
Georges Racinet <georges.racinet@octobus.net>
parents: 42735
diff changeset
    77
        disco = PartialDiscovery(self.repo(), [3], True)
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    78
        self.assertFalse(disco.hasinfo())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    79
        self.assertFalse(disco.iscomplete())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    80
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    81
        disco.addcommons([1])
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    82
        self.assertTrue(disco.hasinfo())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    83
        self.assertFalse(disco.iscomplete())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    84
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    85
        disco.addmissings([2])
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    86
        self.assertTrue(disco.hasinfo())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    87
        self.assertTrue(disco.iscomplete())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    88
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    89
        self.assertEqual(disco.commonheads(), {1})
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    90
42180
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
    91
    def testaddmissingsstats(self):
42736
b6f3f704a561 rust-discovery: read the index from a repo passed at init
Georges Racinet <georges.racinet@octobus.net>
parents: 42735
diff changeset
    92
        disco = PartialDiscovery(self.repo(), [3], True)
42180
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
    93
        self.assertIsNone(disco.stats()['undecided'], None)
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
    94
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
    95
        disco.addmissings([2])
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
    96
        self.assertEqual(disco.stats()['undecided'], 2)
1b0be75cb61f rust-discovery: implementing and exposing stats()
Georges Racinet <georges.racinet@octobus.net>
parents: 42179
diff changeset
    97
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    98
    def testaddinfocommonfirst(self):
42736
b6f3f704a561 rust-discovery: read the index from a repo passed at init
Georges Racinet <georges.racinet@octobus.net>
parents: 42735
diff changeset
    99
        disco = PartialDiscovery(self.repo(), [3], True)
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   100
        disco.addinfo([(1, True), (2, False)])
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   101
        self.assertTrue(disco.hasinfo())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   102
        self.assertTrue(disco.iscomplete())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   103
        self.assertEqual(disco.commonheads(), {1})
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   104
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   105
    def testaddinfomissingfirst(self):
42736
b6f3f704a561 rust-discovery: read the index from a repo passed at init
Georges Racinet <georges.racinet@octobus.net>
parents: 42735
diff changeset
   106
        disco = PartialDiscovery(self.repo(), [3], True)
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   107
        disco.addinfo([(2, False), (1, True)])
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   108
        self.assertTrue(disco.hasinfo())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   109
        self.assertTrue(disco.iscomplete())
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   110
        self.assertEqual(disco.commonheads(), {1})
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   111
42741
4e7bd6180b53 rust-discovery: optionally don't randomize at all, for tests
Georges Racinet <georges.racinet@octobus.net>
parents: 42736
diff changeset
   112
    def testinitnorandom(self):
4e7bd6180b53 rust-discovery: optionally don't randomize at all, for tests
Georges Racinet <georges.racinet@octobus.net>
parents: 42736
diff changeset
   113
        PartialDiscovery(self.repo(), [3], True, randomize=False)
4e7bd6180b53 rust-discovery: optionally don't randomize at all, for tests
Georges Racinet <georges.racinet@octobus.net>
parents: 42736
diff changeset
   114
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
   115
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   116
if __name__ == '__main__':
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   117
    import silenttestrunner
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42759
diff changeset
   118
42179
13b64247f48f rust-discovery: cpython bindings for the core logic
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   119
    silenttestrunner.main(__name__)