tests/testlib/ext-sidedata.py
author Yuya Nishihara <yuya@tcha.org>
Mon, 13 Jul 2020 21:14:20 +0900
changeset 45102 efcc87d37f4d
parent 43134 75ad8af9c95e
child 45373 d7dcc75a3eae
permissions -rw-r--r--
dispatch: handle late KeyboardInterrupt occurred in run() User can press Ctrl+C while flushing streams in dispatch.run(). In such case, I think exiting with 255 is better than printing Python traceback and exiting with 1.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43040
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     1
# ext-sidedata.py - small extension to test the sidedata logic
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     2
#
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     3
# Copyright 2019 Pierre-Yves David <pierre-yves.david@octobus.net)
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     4
#
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     7
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     8
from __future__ import absolute_import
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     9
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    10
import hashlib
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    11
import struct
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    12
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    13
from mercurial import (
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    14
    extensions,
43134
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    15
    localrepo,
43042
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    16
    node,
43040
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    17
    revlog,
43134
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    18
    upgrade,
43040
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    19
)
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    20
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43042
diff changeset
    21
from mercurial.revlogutils import sidedata
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43042
diff changeset
    22
43040
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    23
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43042
diff changeset
    24
def wrapaddrevision(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43042
diff changeset
    25
    orig, self, text, transaction, link, p1, p2, *args, **kwargs
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43042
diff changeset
    26
):
43040
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    27
    if kwargs.get('sidedata') is None:
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    28
        kwargs['sidedata'] = {}
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    29
    sd = kwargs['sidedata']
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    30
    ## let's store some arbitrary data just for testing
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    31
    # text length
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    32
    sd[sidedata.SD_TEST1] = struct.pack('>I', len(text))
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    33
    # and sha2 hashes
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    34
    sha256 = hashlib.sha256(text).digest()
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    35
    sd[sidedata.SD_TEST2] = struct.pack('>32s', sha256)
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    36
    return orig(self, text, transaction, link, p1, p2, *args, **kwargs)
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    37
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43042
diff changeset
    38
43042
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    39
def wraprevision(orig, self, nodeorrev, *args, **kwargs):
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    40
    text = orig(self, nodeorrev, *args, **kwargs)
43134
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    41
    if getattr(self, 'sidedatanocheck', False):
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    42
        return text
43042
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    43
    if nodeorrev != node.nullrev and nodeorrev != node.nullid:
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    44
        sd = self.sidedata(nodeorrev)
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    45
        if len(text) != struct.unpack('>I', sd[sidedata.SD_TEST1])[0]:
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    46
            raise RuntimeError('text size mismatch')
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    47
        expected = sd[sidedata.SD_TEST2]
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    48
        got = hashlib.sha256(text).digest()
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    49
        if got != expected:
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    50
            raise RuntimeError('sha256 mismatch')
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    51
    return text
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    52
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43042
diff changeset
    53
43134
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    54
def wrapgetsidedatacompanion(orig, srcrepo, dstrepo):
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    55
    sidedatacompanion = orig(srcrepo, dstrepo)
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    56
    addedreqs = dstrepo.requirements - srcrepo.requirements
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    57
    if localrepo.SIDEDATA_REQUIREMENT in addedreqs:
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    58
        assert sidedatacompanion is None  # deal with composition later
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    59
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    60
        def sidedatacompanion(revlog, rev):
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    61
            update = {}
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    62
            revlog.sidedatanocheck = True
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    63
            try:
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    64
                text = revlog.revision(rev)
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    65
            finally:
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    66
                del revlog.sidedatanocheck
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    67
            ## let's store some arbitrary data just for testing
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    68
            # text length
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    69
            update[sidedata.SD_TEST1] = struct.pack('>I', len(text))
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    70
            # and sha2 hashes
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    71
            sha256 = hashlib.sha256(text).digest()
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    72
            update[sidedata.SD_TEST2] = struct.pack('>32s', sha256)
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    73
            return False, (), update
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    74
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    75
    return sidedatacompanion
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    76
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    77
43040
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    78
def extsetup(ui):
ba4072c0a911 sidedata: test we can successfully write sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    79
    extensions.wrapfunction(revlog.revlog, 'addrevision', wrapaddrevision)
43042
03e769278ef3 sidedata: check that the sidedata safely roundtrip
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43040
diff changeset
    80
    extensions.wrapfunction(revlog.revlog, 'revision', wraprevision)
43134
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    81
    extensions.wrapfunction(
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    82
        upgrade, 'getsidedatacompanion', wrapgetsidedatacompanion
75ad8af9c95e upgrade: allow upgrade to repository using sidedata
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    83
    )