##// END OF EJS Templates
test-commandserver: flush stdout
Idan Kamara -
r16117:6ecf5fb2 stable
parent child Browse files
Show More
@@ -1,232 +1,234 b''
1 1 import sys, os, struct, subprocess, cStringIO, re, shutil
2 2
3 3 def connect(path=None):
4 4 cmdline = ['hg', 'serve', '--cmdserver', 'pipe']
5 5 if path:
6 6 cmdline += ['-R', path]
7 7
8 8 server = subprocess.Popen(cmdline, stdin=subprocess.PIPE,
9 9 stdout=subprocess.PIPE)
10 10
11 11 return server
12 12
13 13 def writeblock(server, data):
14 14 server.stdin.write(struct.pack('>I', len(data)))
15 15 server.stdin.write(data)
16 16 server.stdin.flush()
17 17
18 18 def readchannel(server):
19 19 data = server.stdout.read(5)
20 20 if not data:
21 21 raise EOFError()
22 22 channel, length = struct.unpack('>cI', data)
23 23 if channel in 'IL':
24 24 return channel, length
25 25 else:
26 26 return channel, server.stdout.read(length)
27 27
28 28 def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None):
29 29 print ' runcommand', ' '.join(args)
30 sys.stdout.flush()
30 31 server.stdin.write('runcommand\n')
31 32 writeblock(server, '\0'.join(args))
32 33
33 34 if not input:
34 35 input = cStringIO.StringIO()
35 36
36 37 while True:
37 38 ch, data = readchannel(server)
38 39 if ch == 'o':
39 40 output.write(data)
40 41 output.flush()
41 42 elif ch == 'e':
42 43 error.write(data)
43 44 error.flush()
44 45 elif ch == 'I':
45 46 writeblock(server, input.read(data))
46 47 elif ch == 'L':
47 48 writeblock(server, input.readline(data))
48 49 elif ch == 'r':
49 50 return struct.unpack('>i', data)[0]
50 51 else:
51 52 print "unexpected channel %c: %r" % (ch, data)
52 53 if ch.isupper():
53 54 return
54 55
55 56 def check(func, repopath=None):
56 57 print
57 58 print 'testing %s:' % func.__name__
58 59 print
60 sys.stdout.flush()
59 61 server = connect(repopath)
60 62 try:
61 63 return func(server)
62 64 finally:
63 65 server.stdin.close()
64 66 server.wait()
65 67
66 68 def unknowncommand(server):
67 69 server.stdin.write('unknowncommand\n')
68 70
69 71 def hellomessage(server):
70 72 ch, data = readchannel(server)
71 73 # escaping python tests output not supported
72 74 print '%c, %r' % (ch, re.sub('encoding: [a-zA-Z0-9-]+', 'encoding: ***', data))
73 75
74 76 # run an arbitrary command to make sure the next thing the server sends
75 77 # isn't part of the hello message
76 78 runcommand(server, ['id'])
77 79
78 80 def checkruncommand(server):
79 81 # hello block
80 82 readchannel(server)
81 83
82 84 # no args
83 85 runcommand(server, [])
84 86
85 87 # global options
86 88 runcommand(server, ['id', '--quiet'])
87 89
88 90 # make sure global options don't stick through requests
89 91 runcommand(server, ['id'])
90 92
91 93 # --config
92 94 runcommand(server, ['id', '--config', 'ui.quiet=True'])
93 95
94 96 # make sure --config doesn't stick
95 97 runcommand(server, ['id'])
96 98
97 99 def inputeof(server):
98 100 readchannel(server)
99 101 server.stdin.write('runcommand\n')
100 102 # close stdin while server is waiting for input
101 103 server.stdin.close()
102 104
103 105 # server exits with 1 if the pipe closed while reading the command
104 106 print 'server exit code =', server.wait()
105 107
106 108 def serverinput(server):
107 109 readchannel(server)
108 110
109 111 patch = """
110 112 # HG changeset patch
111 113 # User test
112 114 # Date 0 0
113 115 # Node ID c103a3dec114d882c98382d684d8af798d09d857
114 116 # Parent 0000000000000000000000000000000000000000
115 117 1
116 118
117 119 diff -r 000000000000 -r c103a3dec114 a
118 120 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
119 121 +++ b/a Thu Jan 01 00:00:00 1970 +0000
120 122 @@ -0,0 +1,1 @@
121 123 +1
122 124 """
123 125
124 126 runcommand(server, ['import', '-'], input=cStringIO.StringIO(patch))
125 127 runcommand(server, ['log'])
126 128
127 129 def cwd(server):
128 130 """ check that --cwd doesn't persist between requests """
129 131 readchannel(server)
130 132 os.mkdir('foo')
131 133 f = open('foo/bar', 'wb')
132 134 f.write('a')
133 135 f.close()
134 136 runcommand(server, ['--cwd', 'foo', 'st', 'bar'])
135 137 runcommand(server, ['st', 'foo/bar'])
136 138 os.remove('foo/bar')
137 139
138 140 def localhgrc(server):
139 141 """ check that local configs for the cached repo aren't inherited when -R
140 142 is used """
141 143 readchannel(server)
142 144
143 145 # the cached repo local hgrc contains ui.foo=bar, so showconfig should show it
144 146 runcommand(server, ['showconfig'])
145 147
146 148 # but not for this repo
147 149 runcommand(server, ['init', 'foo'])
148 150 runcommand(server, ['-R', 'foo', 'showconfig', 'ui', 'defaults'])
149 151 shutil.rmtree('foo')
150 152
151 153 def hook(**args):
152 154 print 'hook talking'
153 155 print 'now try to read something: %r' % sys.stdin.read()
154 156
155 157 def hookoutput(server):
156 158 readchannel(server)
157 159 runcommand(server, ['--config',
158 160 'hooks.pre-identify=python:test-commandserver.hook', 'id'],
159 161 input=cStringIO.StringIO('some input'))
160 162
161 163 def outsidechanges(server):
162 164 readchannel(server)
163 165 f = open('a', 'ab')
164 166 f.write('a\n')
165 167 f.close()
166 168 runcommand(server, ['status'])
167 169 os.system('hg ci -Am2')
168 170 runcommand(server, ['tip'])
169 171 runcommand(server, ['status'])
170 172
171 173 def bookmarks(server):
172 174 readchannel(server)
173 175 runcommand(server, ['bookmarks'])
174 176
175 177 # changes .hg/bookmarks
176 178 os.system('hg bookmark -i bm1')
177 179 os.system('hg bookmark -i bm2')
178 180 runcommand(server, ['bookmarks'])
179 181
180 182 # changes .hg/bookmarks.current
181 183 os.system('hg upd bm1 -q')
182 184 runcommand(server, ['bookmarks'])
183 185
184 186 runcommand(server, ['bookmarks', 'bm3'])
185 187 f = open('a', 'ab')
186 188 f.write('a\n')
187 189 f.close()
188 190 runcommand(server, ['commit', '-Amm'])
189 191 runcommand(server, ['bookmarks'])
190 192
191 193 def tagscache(server):
192 194 readchannel(server)
193 195 runcommand(server, ['id', '-t', '-r', '0'])
194 196 os.system('hg tag -r 0 foo')
195 197 runcommand(server, ['id', '-t', '-r', '0'])
196 198
197 199 def setphase(server):
198 200 readchannel(server)
199 201 runcommand(server, ['phase', '-r', '.'])
200 202 os.system('hg phase -r . -p')
201 203 runcommand(server, ['phase', '-r', '.'])
202 204
203 205 def rollback(server):
204 206 readchannel(server)
205 207 runcommand(server, ['phase', '-r', '.', '-p'])
206 208 f = open('a', 'ab')
207 209 f.write('a\n')
208 210 f.close()
209 211 runcommand(server, ['commit', '-Am.'])
210 212 runcommand(server, ['rollback'])
211 213 runcommand(server, ['phase', '-r', '.'])
212 214
213 215 if __name__ == '__main__':
214 216 os.system('hg init')
215 217
216 218 check(hellomessage)
217 219 check(unknowncommand)
218 220 check(checkruncommand)
219 221 check(inputeof)
220 222 check(serverinput)
221 223 check(cwd)
222 224
223 225 hgrc = open('.hg/hgrc', 'a')
224 226 hgrc.write('[ui]\nfoo=bar\n')
225 227 hgrc.close()
226 228 check(localhgrc)
227 229 check(hookoutput)
228 230 check(outsidechanges)
229 231 check(bookmarks)
230 232 check(tagscache)
231 233 check(setphase)
232 234 check(rollback)
@@ -1,147 +1,147 b''
1 1
2 2 testing hellomessage:
3 3
4 4 o, 'capabilities: getencoding runcommand\nencoding: ***'
5 5 runcommand id
6 6 000000000000 tip
7 abort: unknown command unknowncommand
8 7
9 8 testing unknowncommand:
10 9
10 abort: unknown command unknowncommand
11 11
12 12 testing checkruncommand:
13 13
14 14 runcommand
15 15 Mercurial Distributed SCM
16 16
17 17 basic commands:
18 18
19 19 add add the specified files on the next commit
20 20 annotate show changeset information by line for each file
21 21 clone make a copy of an existing repository
22 22 commit commit the specified files or all outstanding changes
23 23 diff diff repository (or selected files)
24 24 export dump the header and diffs for one or more changesets
25 25 forget forget the specified files on the next commit
26 26 init create a new repository in the given directory
27 27 log show revision history of entire repository or files
28 28 merge merge working directory with another revision
29 29 phase set or show the current phase name
30 30 pull pull changes from the specified source
31 31 push push changes to the specified destination
32 32 remove remove the specified files on the next commit
33 33 serve start stand-alone webserver
34 34 status show changed files in the working directory
35 35 summary summarize working directory state
36 36 update update working directory (or switch revisions)
37 37
38 38 use "hg help" for the full list of commands or "hg -v" for details
39 39 runcommand id --quiet
40 40 000000000000
41 41 runcommand id
42 42 000000000000 tip
43 43 runcommand id --config ui.quiet=True
44 44 000000000000
45 45 runcommand id
46 46 000000000000 tip
47 47
48 48 testing inputeof:
49 49
50 50 server exit code = 1
51 51
52 52 testing serverinput:
53 53
54 54 runcommand import -
55 55 applying patch from stdin
56 56 runcommand log
57 57 changeset: 0:eff892de26ec
58 58 tag: tip
59 59 user: test
60 60 date: Thu Jan 01 00:00:00 1970 +0000
61 61 summary: 1
62 62
63 63
64 64 testing cwd:
65 65
66 66 runcommand --cwd foo st bar
67 67 ? bar
68 68 runcommand st foo/bar
69 69 ? foo/bar
70 70
71 71 testing localhgrc:
72 72
73 73 runcommand showconfig
74 74 bundle.mainreporoot=$TESTTMP
75 75 defaults.backout=-d "0 0"
76 76 defaults.commit=-d "0 0"
77 77 defaults.tag=-d "0 0"
78 78 ui.slash=True
79 79 ui.foo=bar
80 80 runcommand init foo
81 81 runcommand -R foo showconfig ui defaults
82 82 defaults.backout=-d "0 0"
83 83 defaults.commit=-d "0 0"
84 84 defaults.tag=-d "0 0"
85 85 ui.slash=True
86 86
87 87 testing hookoutput:
88 88
89 89 runcommand --config hooks.pre-identify=python:test-commandserver.hook id
90 90 hook talking
91 91 now try to read something: 'some input'
92 92 eff892de26ec tip
93 93
94 94 testing outsidechanges:
95 95
96 96 runcommand status
97 97 M a
98 98 runcommand tip
99 99 changeset: 1:d3a0a68be6de
100 100 tag: tip
101 101 user: test
102 102 date: Thu Jan 01 00:00:00 1970 +0000
103 103 summary: 2
104 104
105 105 runcommand status
106 106
107 107 testing bookmarks:
108 108
109 109 runcommand bookmarks
110 110 no bookmarks set
111 111 runcommand bookmarks
112 112 bm1 1:d3a0a68be6de
113 113 bm2 1:d3a0a68be6de
114 114 runcommand bookmarks
115 115 * bm1 1:d3a0a68be6de
116 116 bm2 1:d3a0a68be6de
117 117 runcommand bookmarks bm3
118 118 runcommand commit -Amm
119 119 runcommand bookmarks
120 120 bm1 1:d3a0a68be6de
121 121 bm2 1:d3a0a68be6de
122 122 * bm3 2:aef17e88f5f0
123 123
124 124 testing tagscache:
125 125
126 126 runcommand id -t -r 0
127 127
128 128 runcommand id -t -r 0
129 129 foo
130 130
131 131 testing setphase:
132 132
133 133 runcommand phase -r .
134 134 3: draft
135 135 runcommand phase -r .
136 136 3: public
137 no phases changed
138 137
139 138 testing rollback:
140 139
141 140 runcommand phase -r . -p
141 no phases changed
142 142 runcommand commit -Am.
143 143 runcommand rollback
144 144 repository tip rolled back to revision 3 (undo commit)
145 145 working directory now based on revision 3
146 146 runcommand phase -r .
147 147 3: public
General Comments 0
You need to be logged in to leave comments. Login now