tests/test-simplekeyvaluefile.py
author Gregory Szorc <gregory.szorc@gmail.com>
Mon, 21 Feb 2022 13:08:28 -0700
changeset 49037 642e31cb55f0
parent 48966 6000f5b25c9b
permissions -rw-r--r--
py3: use class X: instead of class X(object): The inheritance from object is implied in Python 3. So this should be equivalent. This change was generated via an automated search and replace. So there may have been some accidental changes. Differential Revision: https://phab.mercurial-scm.org/D12352
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31559
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     1
import unittest
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     2
import silenttestrunner
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     3
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     4
from mercurial import (
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     5
    error,
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     6
    scmutil,
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     7
)
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
     8
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
     9
49037
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48966
diff changeset
    10
class mockfile:
31559
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    11
    def __init__(self, name, fs):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    12
        self.name = name
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    13
        self.fs = fs
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    14
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    15
    def __enter__(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    16
        return self
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    17
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    18
    def __exit__(self, *args, **kwargs):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    19
        pass
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    20
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    21
    def write(self, text):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    22
        self.fs.contents[self.name] = text
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    23
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    24
    def read(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    25
        return self.fs.contents[self.name]
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    26
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    27
49037
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48966
diff changeset
    28
class mockvfs:
31559
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    29
    def __init__(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    30
        self.contents = {}
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    31
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    32
    def read(self, path):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    33
        return mockfile(path, self).read()
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    34
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    35
    def readlines(self, path):
32309
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31591
diff changeset
    36
        # lines need to contain the trailing '\n' to mock the real readlines
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31591
diff changeset
    37
        return [l for l in mockfile(path, self).read().splitlines(True)]
31559
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    38
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    39
    def __call__(self, path, mode, atomictemp):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    40
        return mockfile(path, self)
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    41
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    42
31559
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    43
class testsimplekeyvaluefile(unittest.TestCase):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    44
    def setUp(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    45
        self.vfs = mockvfs()
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    46
32309
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31591
diff changeset
    47
    def testbasicwritingiandreading(self):
37983
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    48
        dw = {b'key1': b'value1', b'Key2': b'value2'}
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    49
        scmutil.simplekeyvaluefile(self.vfs, b'kvfile').write(dw)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    50
        self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    51
            sorted(self.vfs.read(b'kvfile').split(b'\n')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    52
            [b'', b'Key2=value2', b'key1=value1'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    53
        )
37983
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    54
        dr = scmutil.simplekeyvaluefile(self.vfs, b'kvfile').read()
32309
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31591
diff changeset
    55
        self.assertEqual(dr, dw)
31559
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    56
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32319
diff changeset
    57
    if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32319
diff changeset
    58
        # Python 3.7 deprecates the regex*p* version, but 2.7 lacks
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32319
diff changeset
    59
        # the regex version.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    60
        assertRaisesRegex = (  # camelcase-required
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    61
            unittest.TestCase.assertRaisesRegexp
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    62
        )
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32319
diff changeset
    63
31559
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    64
    def testinvalidkeys(self):
37983
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    65
        d = {b'0key1': b'value1', b'Key2': b'value2'}
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    66
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    67
            error.ProgrammingError, 'keys must start with a letter.*'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    68
        ):
37983
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    69
            scmutil.simplekeyvaluefile(self.vfs, b'kvfile').write(d)
32319
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32310
diff changeset
    70
37983
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    71
        d = {b'key1@': b'value1', b'Key2': b'value2'}
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32319
diff changeset
    72
        with self.assertRaisesRegex(error.ProgrammingError, 'invalid key.*'):
37983
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    73
            scmutil.simplekeyvaluefile(self.vfs, b'kvfile').write(d)
31559
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    74
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    75
    def testinvalidvalues(self):
37983
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    76
        d = {b'key1': b'value1', b'Key2': b'value2\n'}
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    77
        with self.assertRaisesRegex(error.ProgrammingError, 'invalid val.*'):
37983
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    78
            scmutil.simplekeyvaluefile(self.vfs, b'kvfile').write(d)
31559
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    79
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    80
    def testcorruptedfile(self):
37983
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    81
        self.vfs.contents[b'badfile'] = b'ababagalamaga\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    82
        with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    83
            error.CorruptedState, 'dictionary.*element.*'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    84
        ):
37983
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    85
            scmutil.simplekeyvaluefile(self.vfs, b'badfile').read()
31559
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    86
32310
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32309
diff changeset
    87
    def testfirstline(self):
37983
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    88
        dw = {b'key1': b'value1'}
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    89
        scmutil.simplekeyvaluefile(self.vfs, b'fl').write(dw, firstline=b'1.0')
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    90
        self.assertEqual(self.vfs.read(b'fl'), b'1.0\nkey1=value1\n')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    91
        dr = scmutil.simplekeyvaluefile(self.vfs, b'fl').read(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    92
            firstlinenonkeyval=True
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    93
        )
37983
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
    94
        self.assertEqual(dr, {b'__firstline': b'1.0', b'key1': b'value1'})
32310
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32309
diff changeset
    95
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    96
31559
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    97
if __name__ == "__main__":
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
    98
    silenttestrunner.main(__name__)