##// END OF EJS Templates
tests: byteify an `error.Abort` message in test-wireproto.py...
Matt Harbison -
r53416:5cf81d8d default
parent child Browse files
Show More
@@ -1,122 +1,122
1 import sys
1 import sys
2
2
3 from mercurial import (
3 from mercurial import (
4 error,
4 error,
5 pycompat,
5 pycompat,
6 ui as uimod,
6 ui as uimod,
7 util,
7 util,
8 wireprototypes,
8 wireprototypes,
9 wireprotov1peer,
9 wireprotov1peer,
10 wireprotov1server,
10 wireprotov1server,
11 )
11 )
12 from mercurial.utils import stringutil
12 from mercurial.utils import stringutil
13
13
14 stringio = util.stringio
14 stringio = util.stringio
15
15
16
16
17 class proto:
17 class proto:
18 def __init__(self, args):
18 def __init__(self, args):
19 self.args = args
19 self.args = args
20 self.name = 'dummyproto'
20 self.name = 'dummyproto'
21
21
22 def getargs(self, spec):
22 def getargs(self, spec):
23 args = self.args
23 args = self.args
24 args.setdefault(b'*', {})
24 args.setdefault(b'*', {})
25 names = spec.split()
25 names = spec.split()
26 return [args[n] for n in names]
26 return [args[n] for n in names]
27
27
28 def checkperm(self, perm):
28 def checkperm(self, perm):
29 pass
29 pass
30
30
31
31
32 wireprototypes.TRANSPORTS['dummyproto'] = {
32 wireprototypes.TRANSPORTS['dummyproto'] = {
33 'transport': 'dummy',
33 'transport': 'dummy',
34 'version': 1,
34 'version': 1,
35 }
35 }
36
36
37
37
38 class clientpeer(wireprotov1peer.wirepeer):
38 class clientpeer(wireprotov1peer.wirepeer):
39 def __init__(self, serverrepo, ui):
39 def __init__(self, serverrepo, ui):
40 self.serverrepo = serverrepo
40 self.serverrepo = serverrepo
41 self.ui = ui
41 self.ui = ui
42
42
43 def url(self):
43 def url(self):
44 return b'test'
44 return b'test'
45
45
46 def local(self):
46 def local(self):
47 return None
47 return None
48
48
49 def peer(self):
49 def peer(self):
50 return self
50 return self
51
51
52 def canpush(self):
52 def canpush(self):
53 return True
53 return True
54
54
55 def close(self):
55 def close(self):
56 pass
56 pass
57
57
58 def capabilities(self):
58 def capabilities(self):
59 return [b'batch']
59 return [b'batch']
60
60
61 def _call(self, cmd, **args):
61 def _call(self, cmd, **args):
62 args = pycompat.byteskwargs(args)
62 args = pycompat.byteskwargs(args)
63 res = wireprotov1server.dispatch(self.serverrepo, proto(args), cmd)
63 res = wireprotov1server.dispatch(self.serverrepo, proto(args), cmd)
64 if isinstance(res, wireprototypes.bytesresponse):
64 if isinstance(res, wireprototypes.bytesresponse):
65 return res.data
65 return res.data
66 elif isinstance(res, bytes):
66 elif isinstance(res, bytes):
67 return res
67 return res
68 else:
68 else:
69 raise error.Abort('dummy client does not support response type')
69 raise error.Abort(b'dummy client does not support response type')
70
70
71 def _callstream(self, cmd, **args):
71 def _callstream(self, cmd, **args):
72 return stringio(self._call(cmd, **args))
72 return stringio(self._call(cmd, **args))
73
73
74 @wireprotov1peer.batchable
74 @wireprotov1peer.batchable
75 def greet(self, name):
75 def greet(self, name):
76 return {b'name': mangle(name)}, unmangle
76 return {b'name': mangle(name)}, unmangle
77
77
78
78
79 class serverrepo:
79 class serverrepo:
80 def __init__(self, ui):
80 def __init__(self, ui):
81 self.ui = ui
81 self.ui = ui
82
82
83 def greet(self, name):
83 def greet(self, name):
84 return b"Hello, " + name
84 return b"Hello, " + name
85
85
86 def filtered(self, name):
86 def filtered(self, name):
87 return self
87 return self
88
88
89
89
90 def mangle(s):
90 def mangle(s):
91 return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s))
91 return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s))
92
92
93
93
94 def unmangle(s):
94 def unmangle(s):
95 return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s))
95 return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s))
96
96
97
97
98 def greet(repo, proto, name):
98 def greet(repo, proto, name):
99 return mangle(repo.greet(unmangle(name)))
99 return mangle(repo.greet(unmangle(name)))
100
100
101
101
102 wireprotov1server.commands[b'greet'] = (greet, b'name')
102 wireprotov1server.commands[b'greet'] = (greet, b'name')
103
103
104 srv = serverrepo(uimod.ui())
104 srv = serverrepo(uimod.ui())
105 clt = clientpeer(srv, uimod.ui())
105 clt = clientpeer(srv, uimod.ui())
106
106
107
107
108 def printb(data, end=b'\n'):
108 def printb(data, end=b'\n'):
109 out = getattr(sys.stdout, 'buffer', sys.stdout)
109 out = getattr(sys.stdout, 'buffer', sys.stdout)
110 out.write(data + end)
110 out.write(data + end)
111 out.flush()
111 out.flush()
112
112
113
113
114 printb(clt.greet(b"Foobar"))
114 printb(clt.greet(b"Foobar"))
115
115
116 with clt.commandexecutor() as e:
116 with clt.commandexecutor() as e:
117 fgreet1 = e.callcommand(b'greet', {b'name': b'Fo, =;:<o'})
117 fgreet1 = e.callcommand(b'greet', {b'name': b'Fo, =;:<o'})
118 fgreet2 = e.callcommand(b'greet', {b'name': b'Bar'})
118 fgreet2 = e.callcommand(b'greet', {b'name': b'Bar'})
119
119
120 printb(
120 printb(
121 stringutil.pprint([f.result() for f in (fgreet1, fgreet2)], bprefix=True)
121 stringutil.pprint([f.result() for f in (fgreet1, fgreet2)], bprefix=True)
122 )
122 )
General Comments 0
You need to be logged in to leave comments. Login now