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