annotate tests/test-simplekeyvaluefile.py @ 47150:8d3c2f9d4af7

revlog: use a "radix" to address revlog Instead of pointing to the index directly and to derive the other file from that, we directly provide the radix and let the revlog determine the associated file path internally. This is more robust and will give us more flexibility for picking this file name in the future. Differential Revision: https://phab.mercurial-scm.org/D10576
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Mon, 03 May 2021 12:22:36 +0200
parents 2372284d9457
children 6000f5b25c9b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
1 from __future__ import absolute_import
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
2
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
3 import unittest
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
4 import silenttestrunner
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
5
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
6 from mercurial import (
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
7 error,
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
8 scmutil,
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
9 )
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
10
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
11
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
12 class mockfile(object):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
13 def __init__(self, name, fs):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
14 self.name = name
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
15 self.fs = fs
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
16
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
17 def __enter__(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
18 return self
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
19
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
20 def __exit__(self, *args, **kwargs):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
21 pass
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
22
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
23 def write(self, text):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
24 self.fs.contents[self.name] = text
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
25
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
26 def read(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
27 return self.fs.contents[self.name]
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
28
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
29
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
30 class mockvfs(object):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
31 def __init__(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
32 self.contents = {}
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
33
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
34 def read(self, path):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
35 return mockfile(path, self).read()
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
36
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
37 def readlines(self, path):
32269
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
38 # lines need to contain the trailing '\n' to mock the real readlines
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
39 return [l for l in mockfile(path, self).read().splitlines(True)]
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
40
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
41 def __call__(self, path, mode, atomictemp):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
42 return mockfile(path, self)
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
43
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
44
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
45 class testsimplekeyvaluefile(unittest.TestCase):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
46 def setUp(self):
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
47 self.vfs = mockvfs()
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
48
32269
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
49 def testbasicwritingiandreading(self):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
50 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
51 scmutil.simplekeyvaluefile(self.vfs, b'kvfile').write(dw)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
52 self.assertEqual(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
53 sorted(self.vfs.read(b'kvfile').split(b'\n')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
54 [b'', b'Key2=value2', b'key1=value1'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
55 )
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
56 dr = scmutil.simplekeyvaluefile(self.vfs, b'kvfile').read()
32269
ed2c44741190 scmutil: add simplekeyvaluefile reading test
Kostia Balytskyi <ikostia@fb.com>
parents: 31585
diff changeset
57 self.assertEqual(dr, dw)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
58
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
59 if not getattr(unittest.TestCase, 'assertRaisesRegex', False):
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
60 # 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: 32279
diff changeset
61 # the regex version.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
62 assertRaisesRegex = ( # camelcase-required
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
63 unittest.TestCase.assertRaisesRegexp
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
64 )
37715
1859b9a7ddef cleanup: polyfill assertRaisesRegex so we can avoid assertRaisesRegexp
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
65
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
66 def testinvalidkeys(self):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
67 d = {b'0key1': b'value1', b'Key2': b'value2'}
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
68 with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
69 error.ProgrammingError, 'keys must start with a letter.*'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
70 ):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
71 scmutil.simplekeyvaluefile(self.vfs, b'kvfile').write(d)
32279
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32270
diff changeset
72
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
73 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: 32279
diff changeset
74 with self.assertRaisesRegex(error.ProgrammingError, 'invalid key.*'):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
75 scmutil.simplekeyvaluefile(self.vfs, b'kvfile').write(d)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
76
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
77 def testinvalidvalues(self):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
78 d = {b'key1': b'value1', b'Key2': b'value2\n'}
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
79 with self.assertRaisesRegex(error.ProgrammingError, 'invalid val.*'):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
80 scmutil.simplekeyvaluefile(self.vfs, b'kvfile').write(d)
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
81
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
82 def testcorruptedfile(self):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
83 self.vfs.contents[b'badfile'] = b'ababagalamaga\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
84 with self.assertRaisesRegex(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
85 error.CorruptedState, 'dictionary.*element.*'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
86 ):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
87 scmutil.simplekeyvaluefile(self.vfs, b'badfile').read()
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
88
32270
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
89 def testfirstline(self):
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
90 dw = {b'key1': b'value1'}
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
91 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
92 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: 41759
diff changeset
93 dr = scmutil.simplekeyvaluefile(self.vfs, b'fl').read(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
94 firstlinenonkeyval=True
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
95 )
37939
6eca47f6319d tests: port test-simplekeyvaluefile.py to Python 3
Augie Fackler <augie@google.com>
parents: 37715
diff changeset
96 self.assertEqual(dr, {b'__firstline': b'1.0', b'key1': b'value1'})
32270
218ca8526ec0 scmutil: make simplekeyvaluefile able to have a non-key-value first line
Kostia Balytskyi <ikostia@fb.com>
parents: 32269
diff changeset
97
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
98
31553
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
99 if __name__ == "__main__":
56acc4250900 scmutil: add a simple key-value file helper
Kostia Balytskyi <ikostia@fb.com>
parents:
diff changeset
100 silenttestrunner.main(__name__)