tests/test-hashutil.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48946 642e31cb55f0
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.

# Tests to ensure that sha1dc.sha1 is exactly a drop-in for
# hashlib.sha1 for our needs.

import hashlib
import unittest

import silenttestrunner

try:
    from mercurial.thirdparty import sha1dc
except ImportError:
    sha1dc = None


class hashertestsbase:
    def test_basic_hash(self):
        h = self.hasher()
        h.update(b'foo')
        self.assertEqual(
            '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', h.hexdigest()
        )
        h.update(b'bar')
        self.assertEqual(
            '8843d7f92416211de9ebb963ff4ce28125932878', h.hexdigest()
        )

    def test_copy_hasher(self):
        h = self.hasher()
        h.update(b'foo')
        h2 = h.copy()
        h.update(b'baz')
        h2.update(b'bar')
        self.assertEqual(
            '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
        )
        self.assertEqual(
            '8843d7f92416211de9ebb963ff4ce28125932878', h2.hexdigest()
        )

    def test_init_hasher(self):
        h = self.hasher(b'initial string')
        self.assertEqual(
            b'\xc9y|n\x1f3S\xa4:\xbaJ\xca,\xc1\x1a\x9e\xb8\xd8\xdd\x86',
            h.digest(),
        )

    def test_bytes_like_types(self):
        h = self.hasher()
        h.update(bytearray(b'foo'))
        h.update(memoryview(b'baz'))
        self.assertEqual(
            '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
        )

        h = self.hasher(bytearray(b'foo'))
        h.update(b'baz')
        self.assertEqual(
            '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
        )

        h = self.hasher(memoryview(b'foo'))
        h.update(b'baz')
        self.assertEqual(
            '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
        )


class hashlibtests(unittest.TestCase, hashertestsbase):
    hasher = hashlib.sha1


if sha1dc:

    class sha1dctests(unittest.TestCase, hashertestsbase):
        hasher = sha1dc.sha1


if __name__ == '__main__':
    silenttestrunner.main(__name__)