Show More
@@ -1,282 +1,287 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 | def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None): | |
|
28 | def sep(text): | |
|
29 | return text.replace('\\', '/') | |
|
30 | ||
|
31 | def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None, | |
|
32 | outfilter=lambda x: x): | |
|
29 | 33 | print ' runcommand', ' '.join(args) |
|
30 | 34 | sys.stdout.flush() |
|
31 | 35 | server.stdin.write('runcommand\n') |
|
32 | 36 | writeblock(server, '\0'.join(args)) |
|
33 | 37 | |
|
34 | 38 | if not input: |
|
35 | 39 | input = cStringIO.StringIO() |
|
36 | 40 | |
|
37 | 41 | while True: |
|
38 | 42 | ch, data = readchannel(server) |
|
39 | 43 | if ch == 'o': |
|
40 | output.write(data) | |
|
44 | output.write(outfilter(data)) | |
|
41 | 45 | output.flush() |
|
42 | 46 | elif ch == 'e': |
|
43 | 47 | error.write(data) |
|
44 | 48 | error.flush() |
|
45 | 49 | elif ch == 'I': |
|
46 | 50 | writeblock(server, input.read(data)) |
|
47 | 51 | elif ch == 'L': |
|
48 | 52 | writeblock(server, input.readline(data)) |
|
49 | 53 | elif ch == 'r': |
|
50 | 54 | return struct.unpack('>i', data)[0] |
|
51 | 55 | else: |
|
52 | 56 | print "unexpected channel %c: %r" % (ch, data) |
|
53 | 57 | if ch.isupper(): |
|
54 | 58 | return |
|
55 | 59 | |
|
56 | 60 | def check(func, repopath=None): |
|
57 | 61 | |
|
58 | 62 | print 'testing %s:' % func.__name__ |
|
59 | 63 | |
|
60 | 64 | sys.stdout.flush() |
|
61 | 65 | server = connect(repopath) |
|
62 | 66 | try: |
|
63 | 67 | return func(server) |
|
64 | 68 | finally: |
|
65 | 69 | server.stdin.close() |
|
66 | 70 | server.wait() |
|
67 | 71 | |
|
68 | 72 | def unknowncommand(server): |
|
69 | 73 | server.stdin.write('unknowncommand\n') |
|
70 | 74 | |
|
71 | 75 | def hellomessage(server): |
|
72 | 76 | ch, data = readchannel(server) |
|
73 | 77 | # escaping python tests output not supported |
|
74 | 78 | print '%c, %r' % (ch, re.sub('encoding: [a-zA-Z0-9-]+', 'encoding: ***', |
|
75 | 79 | data)) |
|
76 | 80 | |
|
77 | 81 | # run an arbitrary command to make sure the next thing the server sends |
|
78 | 82 | # isn't part of the hello message |
|
79 | 83 | runcommand(server, ['id']) |
|
80 | 84 | |
|
81 | 85 | def checkruncommand(server): |
|
82 | 86 | # hello block |
|
83 | 87 | readchannel(server) |
|
84 | 88 | |
|
85 | 89 | # no args |
|
86 | 90 | runcommand(server, []) |
|
87 | 91 | |
|
88 | 92 | # global options |
|
89 | 93 | runcommand(server, ['id', '--quiet']) |
|
90 | 94 | |
|
91 | 95 | # make sure global options don't stick through requests |
|
92 | 96 | runcommand(server, ['id']) |
|
93 | 97 | |
|
94 | 98 | # --config |
|
95 | 99 | runcommand(server, ['id', '--config', 'ui.quiet=True']) |
|
96 | 100 | |
|
97 | 101 | # make sure --config doesn't stick |
|
98 | 102 | runcommand(server, ['id']) |
|
99 | 103 | |
|
100 | 104 | def inputeof(server): |
|
101 | 105 | readchannel(server) |
|
102 | 106 | server.stdin.write('runcommand\n') |
|
103 | 107 | # close stdin while server is waiting for input |
|
104 | 108 | server.stdin.close() |
|
105 | 109 | |
|
106 | 110 | # server exits with 1 if the pipe closed while reading the command |
|
107 | 111 | print 'server exit code =', server.wait() |
|
108 | 112 | |
|
109 | 113 | def serverinput(server): |
|
110 | 114 | readchannel(server) |
|
111 | 115 | |
|
112 | 116 | patch = """ |
|
113 | 117 | # HG changeset patch |
|
114 | 118 | # User test |
|
115 | 119 | # Date 0 0 |
|
116 | 120 | # Node ID c103a3dec114d882c98382d684d8af798d09d857 |
|
117 | 121 | # Parent 0000000000000000000000000000000000000000 |
|
118 | 122 | 1 |
|
119 | 123 | |
|
120 | 124 | diff -r 000000000000 -r c103a3dec114 a |
|
121 | 125 | --- /dev/null Thu Jan 01 00:00:00 1970 +0000 |
|
122 | 126 | +++ b/a Thu Jan 01 00:00:00 1970 +0000 |
|
123 | 127 | @@ -0,0 +1,1 @@ |
|
124 | 128 | +1 |
|
125 | 129 | """ |
|
126 | 130 | |
|
127 | 131 | runcommand(server, ['import', '-'], input=cStringIO.StringIO(patch)) |
|
128 | 132 | runcommand(server, ['log']) |
|
129 | 133 | |
|
130 | 134 | def cwd(server): |
|
131 | 135 | """ check that --cwd doesn't persist between requests """ |
|
132 | 136 | readchannel(server) |
|
133 | 137 | os.mkdir('foo') |
|
134 | 138 | f = open('foo/bar', 'wb') |
|
135 | 139 | f.write('a') |
|
136 | 140 | f.close() |
|
137 | 141 | runcommand(server, ['--cwd', 'foo', 'st', 'bar']) |
|
138 | 142 | runcommand(server, ['st', 'foo/bar']) |
|
139 | 143 | os.remove('foo/bar') |
|
140 | 144 | |
|
141 | 145 | def localhgrc(server): |
|
142 | 146 | """ check that local configs for the cached repo aren't inherited when -R |
|
143 | 147 | is used """ |
|
144 | 148 | readchannel(server) |
|
145 | 149 | |
|
146 | 150 | # the cached repo local hgrc contains ui.foo=bar, so showconfig should |
|
147 | 151 | # show it |
|
148 | 152 | runcommand(server, ['showconfig']) |
|
149 | 153 | |
|
150 | 154 | # but not for this repo |
|
151 | 155 | runcommand(server, ['init', 'foo']) |
|
152 | 156 | runcommand(server, ['-R', 'foo', 'showconfig', 'ui', 'defaults']) |
|
153 | 157 | shutil.rmtree('foo') |
|
154 | 158 | |
|
155 | 159 | def hook(**args): |
|
156 | 160 | print 'hook talking' |
|
157 | 161 | print 'now try to read something: %r' % sys.stdin.read() |
|
158 | 162 | |
|
159 | 163 | def hookoutput(server): |
|
160 | 164 | readchannel(server) |
|
161 | 165 | runcommand(server, ['--config', |
|
162 | 166 | 'hooks.pre-identify=python:test-commandserver.hook', |
|
163 | 167 | 'id'], |
|
164 | 168 | input=cStringIO.StringIO('some input')) |
|
165 | 169 | |
|
166 | 170 | def outsidechanges(server): |
|
167 | 171 | readchannel(server) |
|
168 | 172 | f = open('a', 'ab') |
|
169 | 173 | f.write('a\n') |
|
170 | 174 | f.close() |
|
171 | 175 | runcommand(server, ['status']) |
|
172 | 176 | os.system('hg ci -Am2') |
|
173 | 177 | runcommand(server, ['tip']) |
|
174 | 178 | runcommand(server, ['status']) |
|
175 | 179 | |
|
176 | 180 | def bookmarks(server): |
|
177 | 181 | readchannel(server) |
|
178 | 182 | runcommand(server, ['bookmarks']) |
|
179 | 183 | |
|
180 | 184 | # changes .hg/bookmarks |
|
181 | 185 | os.system('hg bookmark -i bm1') |
|
182 | 186 | os.system('hg bookmark -i bm2') |
|
183 | 187 | runcommand(server, ['bookmarks']) |
|
184 | 188 | |
|
185 | 189 | # changes .hg/bookmarks.current |
|
186 | 190 | os.system('hg upd bm1 -q') |
|
187 | 191 | runcommand(server, ['bookmarks']) |
|
188 | 192 | |
|
189 | 193 | runcommand(server, ['bookmarks', 'bm3']) |
|
190 | 194 | f = open('a', 'ab') |
|
191 | 195 | f.write('a\n') |
|
192 | 196 | f.close() |
|
193 | 197 | runcommand(server, ['commit', '-Amm']) |
|
194 | 198 | runcommand(server, ['bookmarks']) |
|
195 | 199 | |
|
196 | 200 | def tagscache(server): |
|
197 | 201 | readchannel(server) |
|
198 | 202 | runcommand(server, ['id', '-t', '-r', '0']) |
|
199 | 203 | os.system('hg tag -r 0 foo') |
|
200 | 204 | runcommand(server, ['id', '-t', '-r', '0']) |
|
201 | 205 | |
|
202 | 206 | def setphase(server): |
|
203 | 207 | readchannel(server) |
|
204 | 208 | runcommand(server, ['phase', '-r', '.']) |
|
205 | 209 | os.system('hg phase -r . -p') |
|
206 | 210 | runcommand(server, ['phase', '-r', '.']) |
|
207 | 211 | |
|
208 | 212 | def rollback(server): |
|
209 | 213 | readchannel(server) |
|
210 | 214 | runcommand(server, ['phase', '-r', '.', '-p']) |
|
211 | 215 | f = open('a', 'ab') |
|
212 | 216 | f.write('a\n') |
|
213 | 217 | f.close() |
|
214 | 218 | runcommand(server, ['commit', '-Am.']) |
|
215 | 219 | runcommand(server, ['rollback']) |
|
216 | 220 | runcommand(server, ['phase', '-r', '.']) |
|
217 | 221 | |
|
218 | 222 | def branch(server): |
|
219 | 223 | readchannel(server) |
|
220 | 224 | runcommand(server, ['branch']) |
|
221 | 225 | os.system('hg branch foo') |
|
222 | 226 | runcommand(server, ['branch']) |
|
223 | 227 | os.system('hg branch default') |
|
224 | 228 | |
|
225 | 229 | def hgignore(server): |
|
226 | 230 | readchannel(server) |
|
227 | 231 | f = open('.hgignore', 'ab') |
|
228 | 232 | f.write('') |
|
229 | 233 | f.close() |
|
230 | 234 | runcommand(server, ['commit', '-Am.']) |
|
231 | 235 | f = open('ignored-file', 'ab') |
|
232 | 236 | f.write('') |
|
233 | 237 | f.close() |
|
234 | 238 | f = open('.hgignore', 'ab') |
|
235 | 239 | f.write('ignored-file') |
|
236 | 240 | f.close() |
|
237 | 241 | runcommand(server, ['status', '-i', '-u']) |
|
238 | 242 | |
|
239 | 243 | def phasecacheafterstrip(server): |
|
240 | 244 | readchannel(server) |
|
241 | 245 | |
|
242 | 246 | # create new head, 5:731265503d86 |
|
243 | 247 | runcommand(server, ['update', '-C', '0']) |
|
244 | 248 | f = open('a', 'ab') |
|
245 | 249 | f.write('a\n') |
|
246 | 250 | f.close() |
|
247 | 251 | runcommand(server, ['commit', '-Am.', 'a']) |
|
248 | 252 | runcommand(server, ['log', '-Gq']) |
|
249 | 253 | |
|
250 | 254 | # make it public; draft marker moves to 4:7966c8e3734d |
|
251 | 255 | runcommand(server, ['phase', '-p', '.']) |
|
252 |
|
|
|
256 | # load _phasecache.phaseroots | |
|
257 | runcommand(server, ['phase', '.'], outfilter=sep) | |
|
253 | 258 | |
|
254 | 259 | # strip 1::4 outside server |
|
255 | 260 | os.system('hg --config extensions.mq= strip 1') |
|
256 | 261 | |
|
257 | 262 | # shouldn't raise "7966c8e3734d: no node!" |
|
258 | 263 | runcommand(server, ['branches']) |
|
259 | 264 | |
|
260 | 265 | if __name__ == '__main__': |
|
261 | 266 | os.system('hg init') |
|
262 | 267 | |
|
263 | 268 | check(hellomessage) |
|
264 | 269 | check(unknowncommand) |
|
265 | 270 | check(checkruncommand) |
|
266 | 271 | check(inputeof) |
|
267 | 272 | check(serverinput) |
|
268 | 273 | check(cwd) |
|
269 | 274 | |
|
270 | 275 | hgrc = open('.hg/hgrc', 'a') |
|
271 | 276 | hgrc.write('[ui]\nfoo=bar\n') |
|
272 | 277 | hgrc.close() |
|
273 | 278 | check(localhgrc) |
|
274 | 279 | check(hookoutput) |
|
275 | 280 | check(outsidechanges) |
|
276 | 281 | check(bookmarks) |
|
277 | 282 | check(tagscache) |
|
278 | 283 | check(setphase) |
|
279 | 284 | check(rollback) |
|
280 | 285 | check(branch) |
|
281 | 286 | check(hgignore) |
|
282 | 287 | check(phasecacheafterstrip) |
General Comments 0
You need to be logged in to leave comments.
Login now