Show More
@@ -0,0 +1,72 b'' | |||||
|
1 | from __future__ import absolute_import | |||
|
2 | ||||
|
3 | import unittest | |||
|
4 | import silenttestrunner | |||
|
5 | ||||
|
6 | from mercurial import ( | |||
|
7 | error, | |||
|
8 | scmutil, | |||
|
9 | ) | |||
|
10 | ||||
|
11 | class mockfile(object): | |||
|
12 | def __init__(self, name, fs): | |||
|
13 | self.name = name | |||
|
14 | self.fs = fs | |||
|
15 | ||||
|
16 | def __enter__(self): | |||
|
17 | return self | |||
|
18 | ||||
|
19 | def __exit__(self, *args, **kwargs): | |||
|
20 | pass | |||
|
21 | ||||
|
22 | def write(self, text): | |||
|
23 | self.fs.contents[self.name] = text | |||
|
24 | ||||
|
25 | def read(self): | |||
|
26 | return self.fs.contents[self.name] | |||
|
27 | ||||
|
28 | class mockvfs(object): | |||
|
29 | def __init__(self): | |||
|
30 | self.contents = {} | |||
|
31 | ||||
|
32 | def read(self, path): | |||
|
33 | return mockfile(path, self).read() | |||
|
34 | ||||
|
35 | def readlines(self, path): | |||
|
36 | return mockfile(path, self).read().split('\n') | |||
|
37 | ||||
|
38 | def __call__(self, path, mode, atomictemp): | |||
|
39 | return mockfile(path, self) | |||
|
40 | ||||
|
41 | class testsimplekeyvaluefile(unittest.TestCase): | |||
|
42 | def setUp(self): | |||
|
43 | self.vfs = mockvfs() | |||
|
44 | ||||
|
45 | def testbasicwriting(self): | |||
|
46 | d = {'key1': 'value1', 'Key2': 'value2'} | |||
|
47 | scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d) | |||
|
48 | self.assertEqual(sorted(self.vfs.read('kvfile').split('\n')), | |||
|
49 | ['', 'Key2=value2', 'key1=value1']) | |||
|
50 | ||||
|
51 | def testinvalidkeys(self): | |||
|
52 | d = {'0key1': 'value1', 'Key2': 'value2'} | |||
|
53 | with self.assertRaisesRegexp(error.ProgrammingError, | |||
|
54 | "keys must start with a letter.*"): | |||
|
55 | scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d) | |||
|
56 | d = {'key1@': 'value1', 'Key2': 'value2'} | |||
|
57 | with self.assertRaisesRegexp(error.ProgrammingError, "invalid key.*"): | |||
|
58 | scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d) | |||
|
59 | ||||
|
60 | def testinvalidvalues(self): | |||
|
61 | d = {'key1': 'value1', 'Key2': 'value2\n'} | |||
|
62 | with self.assertRaisesRegexp(error.ProgrammingError, "invalid val.*"): | |||
|
63 | scmutil.simplekeyvaluefile(self.vfs, 'kvfile').write(d) | |||
|
64 | ||||
|
65 | def testcorruptedfile(self): | |||
|
66 | self.vfs.contents['badfile'] = 'ababagalamaga\n' | |||
|
67 | with self.assertRaisesRegexp(error.CorruptedState, | |||
|
68 | "dictionary.*element.*"): | |||
|
69 | scmutil.simplekeyvaluefile(self.vfs, 'badfile').read() | |||
|
70 | ||||
|
71 | if __name__ == "__main__": | |||
|
72 | silenttestrunner.main(__name__) |
@@ -965,3 +965,41 b' def gddeltaconfig(ui):' | |||||
965 | """ |
|
965 | """ | |
966 | # experimental config: format.generaldelta |
|
966 | # experimental config: format.generaldelta | |
967 | return ui.configbool('format', 'generaldelta', False) |
|
967 | return ui.configbool('format', 'generaldelta', False) | |
|
968 | ||||
|
969 | class simplekeyvaluefile(object): | |||
|
970 | """A simple file with key=value lines | |||
|
971 | ||||
|
972 | Keys must be alphanumerics and start with a letter, values must not | |||
|
973 | contain '\n' characters""" | |||
|
974 | ||||
|
975 | def __init__(self, vfs, path, keys=None): | |||
|
976 | self.vfs = vfs | |||
|
977 | self.path = path | |||
|
978 | ||||
|
979 | def read(self): | |||
|
980 | lines = self.vfs.readlines(self.path) | |||
|
981 | try: | |||
|
982 | d = dict(line[:-1].split('=', 1) for line in lines if line) | |||
|
983 | except ValueError as e: | |||
|
984 | raise error.CorruptedState(str(e)) | |||
|
985 | return d | |||
|
986 | ||||
|
987 | def write(self, data): | |||
|
988 | """Write key=>value mapping to a file | |||
|
989 | data is a dict. Keys must be alphanumerical and start with a letter. | |||
|
990 | Values must not contain newline characters.""" | |||
|
991 | lines = [] | |||
|
992 | for k, v in data.items(): | |||
|
993 | if not k[0].isalpha(): | |||
|
994 | e = "keys must start with a letter in a key-value file" | |||
|
995 | raise error.ProgrammingError(e) | |||
|
996 | if not k.isalnum(): | |||
|
997 | e = "invalid key name in a simple key-value file" | |||
|
998 | raise error.ProgrammingError(e) | |||
|
999 | if '\n' in v: | |||
|
1000 | e = "invalid value in a simple key-value file" | |||
|
1001 | raise error.ProgrammingError(e) | |||
|
1002 | lines.append("%s=%s\n" % (k, v)) | |||
|
1003 | with self.vfs(self.path, mode='wb', atomictemp=True) as fp: | |||
|
1004 | fp.write(''.join(lines)) | |||
|
1005 |
General Comments 0
You need to be logged in to leave comments.
Login now