annotate tests/test-commandserver.py @ 14770:95a8c0f5dd3f stable

tests: add basic commandserver test
author Idan Kamara <idankk86@gmail.com>
date Wed, 29 Jun 2011 15:49:35 +0300
parents
children 1b872599f39f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14770
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
1 import sys, os, struct, subprocess, cStringIO, re
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
2
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
3 def connect(path=None):
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
4 cmdline = ['hg', 'serve', '--cmdserver', 'pipe']
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
5 if path:
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
6 cmdline += ['-R', path]
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
7
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
8 server = subprocess.Popen(cmdline, stdin=subprocess.PIPE,
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
9 stdout=subprocess.PIPE)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
10
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
11 return server
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
12
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
13 def writeblock(server, data):
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
14 server.stdin.write(struct.pack('>I', len(data)))
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
15 server.stdin.write(data)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
16 server.stdin.flush()
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
17
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
18 def readchannel(server):
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
19 data = server.stdout.read(5)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
20 if not data:
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
21 raise EOFError()
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
22 channel, length = struct.unpack('>cI', data)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
23 if channel in 'IL':
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
24 return channel, length
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
25 else:
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
26 return channel, server.stdout.read(length)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
27
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
28 def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None):
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
29 server.stdin.write('runcommand\n')
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
30 writeblock(server, '\0'.join(args))
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
31
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
32 if not input:
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
33 input = cStringIO.StringIO()
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
34
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
35 while True:
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
36 ch, data = readchannel(server)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
37 if ch == 'o':
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
38 output.write(data)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
39 output.flush()
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
40 elif ch == 'e':
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
41 error.write(data)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
42 error.flush()
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
43 elif ch == 'I':
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
44 writeblock(server, input.read(data))
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
45 elif ch == 'L':
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
46 writeblock(server, input.readline(data))
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
47 elif ch == 'r':
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
48 return struct.unpack('>i', data)[0]
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
49 else:
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
50 print "unexpected channel %c: %r" % (ch, data)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
51 if ch.isupper():
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
52 return
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
53
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
54 def check(func, repopath=None):
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
55 server = connect(repopath)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
56 try:
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
57 return func(server)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
58 finally:
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
59 server.stdin.close()
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
60 server.wait()
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
61
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
62 def unknowncommand(server):
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
63 server.stdin.write('unknowncommand\n')
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
64
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
65 def hellomessage(server):
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
66 ch, data = readchannel(server)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
67 # escaping python tests output not supported
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
68 print '%c, %r' % (ch, re.sub('encoding: [a-zA-Z0-9-]+', 'encoding: ***', data))
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
69
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
70 # run an arbitrary command to make sure the next thing the server sends
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
71 # isn't part of the hello message
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
72 runcommand(server, ['id'])
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
73
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
74 def checkruncommand(server):
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
75 # hello block
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
76 readchannel(server)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
77
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
78 # no args
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
79 runcommand(server, [])
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
80
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
81 # global options
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
82 runcommand(server, ['id', '--quiet'])
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
83
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
84 # make sure global options don't stick through requests
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
85 runcommand(server, ['id'])
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
86
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
87 # --config
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
88 runcommand(server, ['id', '--config', 'ui.quiet=True'])
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
89
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
90 # make sure --config doesn't stick
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
91 runcommand(server, ['id'])
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
92
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
93 def inputeof(server):
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
94 readchannel(server)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
95 server.stdin.write('runcommand\n')
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
96 # close stdin while server is waiting for input
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
97 server.stdin.close()
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
98
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
99 # server exits with 1 if the pipe closed while reading the command
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
100 print 'server exit code =', server.wait()
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
101
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
102 def serverinput(server):
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
103 readchannel(server)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
104
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
105 patch = """
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
106 # HG changeset patch
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
107 # User test
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
108 # Date 0 0
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
109 # Node ID c103a3dec114d882c98382d684d8af798d09d857
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
110 # Parent 0000000000000000000000000000000000000000
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
111 1
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
112
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
113 diff -r 000000000000 -r c103a3dec114 a
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
114 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
115 +++ b/a Thu Jan 01 00:00:00 1970 +0000
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
116 @@ -0,0 +1,1 @@
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
117 +1
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
118 """
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
119
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
120 runcommand(server, ['import', '-'], input=cStringIO.StringIO(patch))
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
121 runcommand(server, ['log'])
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
122
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
123 if __name__ == '__main__':
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
124 os.system('hg init')
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
125
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
126 check(hellomessage)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
127 check(unknowncommand)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
128 check(checkruncommand)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
129 check(inputeof)
95a8c0f5dd3f tests: add basic commandserver test
Idan Kamara <idankk86@gmail.com>
parents:
diff changeset
130 check(serverinput)