##// END OF EJS Templates
tests: test-archive.t use sys.stdout.buffer for binary output in py3
timeless -
r29182:6c0b1d96 default
parent child Browse files
Show More
@@ -1,365 +1,369 b''
1 1 #require serve
2 2
3 3 $ hg init test
4 4 $ cd test
5 5 $ echo foo>foo
6 6 $ hg commit -Am 1 -d '1 0'
7 7 adding foo
8 8 $ echo bar>bar
9 9 $ hg commit -Am 2 -d '2 0'
10 10 adding bar
11 11 $ mkdir baz
12 12 $ echo bletch>baz/bletch
13 13 $ hg commit -Am 3 -d '1000000000 0'
14 14 adding baz/bletch
15 15 $ hg init subrepo
16 16 $ touch subrepo/sub
17 17 $ hg -q -R subrepo ci -Am "init subrepo"
18 18 $ echo "subrepo = subrepo" > .hgsub
19 19 $ hg add .hgsub
20 20 $ hg ci -m "add subrepo"
21 21 $ echo "[web]" >> .hg/hgrc
22 22 $ echo "name = test-archive" >> .hg/hgrc
23 23 $ echo "archivesubrepos = True" >> .hg/hgrc
24 24 $ cp .hg/hgrc .hg/hgrc-base
25 25 > test_archtype() {
26 26 > echo "allow_archive = $1" >> .hg/hgrc
27 27 > hg serve -p $HGPORT -d --pid-file=hg.pid -E errors.log
28 28 > cat hg.pid >> $DAEMON_PIDS
29 29 > echo % $1 allowed should give 200
30 30 > get-with-headers.py localhost:$HGPORT "archive/tip.$2" | head -n 1
31 31 > echo % $3 and $4 disallowed should both give 403
32 32 > get-with-headers.py localhost:$HGPORT "archive/tip.$3" | head -n 1
33 33 > get-with-headers.py localhost:$HGPORT "archive/tip.$4" | head -n 1
34 34 > killdaemons.py
35 35 > cat errors.log
36 36 > cp .hg/hgrc-base .hg/hgrc
37 37 > }
38 38
39 39 check http return codes
40 40
41 41 $ test_archtype gz tar.gz tar.bz2 zip
42 42 % gz allowed should give 200
43 43 200 Script output follows
44 44 % tar.bz2 and zip disallowed should both give 403
45 45 403 Archive type not allowed: bz2
46 46 403 Archive type not allowed: zip
47 47 $ test_archtype bz2 tar.bz2 zip tar.gz
48 48 % bz2 allowed should give 200
49 49 200 Script output follows
50 50 % zip and tar.gz disallowed should both give 403
51 51 403 Archive type not allowed: zip
52 52 403 Archive type not allowed: gz
53 53 $ test_archtype zip zip tar.gz tar.bz2
54 54 % zip allowed should give 200
55 55 200 Script output follows
56 56 % tar.gz and tar.bz2 disallowed should both give 403
57 57 403 Archive type not allowed: gz
58 58 403 Archive type not allowed: bz2
59 59
60 60 $ echo "allow_archive = gz bz2 zip" >> .hg/hgrc
61 61 $ hg serve -p $HGPORT -d --pid-file=hg.pid -E errors.log
62 62 $ cat hg.pid >> $DAEMON_PIDS
63 63
64 64 invalid arch type should give 404
65 65
66 66 $ get-with-headers.py localhost:$HGPORT "archive/tip.invalid" | head -n 1
67 67 404 Unsupported archive type: None
68 68
69 69 $ TIP=`hg id -v | cut -f1 -d' '`
70 70 $ QTIP=`hg id -q`
71 71 $ cat > getarchive.py <<EOF
72 72 > from __future__ import absolute_import
73 73 > import os
74 74 > import sys
75 75 > import urllib2
76 76 > try:
77 77 > # Set stdout to binary mode for win32 platforms
78 78 > import msvcrt
79 79 > msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
80 80 > except ImportError:
81 81 > pass
82 82 > if len(sys.argv) <= 3:
83 83 > node, archive = sys.argv[1:]
84 84 > requeststr = 'cmd=archive;node=%s;type=%s' % (node, archive)
85 85 > else:
86 86 > node, archive, file = sys.argv[1:]
87 87 > requeststr = 'cmd=archive;node=%s;type=%s;file=%s' % (node, archive, file)
88 88 > try:
89 > stdout = sys.stdout.buffer
90 > except AttributeError:
91 > stdout = sys.stdout
92 > try:
89 93 > f = urllib2.urlopen('http://127.0.0.1:%s/?%s'
90 94 > % (os.environ['HGPORT'], requeststr))
91 > sys.stdout.write(f.read())
95 > stdout.write(f.read())
92 96 > except urllib2.HTTPError, e:
93 97 > sys.stderr.write(str(e) + '\n')
94 98 > EOF
95 99 $ python getarchive.py "$TIP" gz | gunzip | tar tf - 2>/dev/null
96 100 test-archive-1701ef1f1510/.hg_archival.txt
97 101 test-archive-1701ef1f1510/.hgsub
98 102 test-archive-1701ef1f1510/.hgsubstate
99 103 test-archive-1701ef1f1510/bar
100 104 test-archive-1701ef1f1510/baz/bletch
101 105 test-archive-1701ef1f1510/foo
102 106 test-archive-1701ef1f1510/subrepo/sub
103 107 $ python getarchive.py "$TIP" bz2 | bunzip2 | tar tf - 2>/dev/null
104 108 test-archive-1701ef1f1510/.hg_archival.txt
105 109 test-archive-1701ef1f1510/.hgsub
106 110 test-archive-1701ef1f1510/.hgsubstate
107 111 test-archive-1701ef1f1510/bar
108 112 test-archive-1701ef1f1510/baz/bletch
109 113 test-archive-1701ef1f1510/foo
110 114 test-archive-1701ef1f1510/subrepo/sub
111 115 $ python getarchive.py "$TIP" zip > archive.zip
112 116 $ unzip -t archive.zip
113 117 Archive: archive.zip
114 118 testing: test-archive-1701ef1f1510/.hg_archival.txt OK
115 119 testing: test-archive-1701ef1f1510/.hgsub OK
116 120 testing: test-archive-1701ef1f1510/.hgsubstate OK
117 121 testing: test-archive-1701ef1f1510/bar OK
118 122 testing: test-archive-1701ef1f1510/baz/bletch OK
119 123 testing: test-archive-1701ef1f1510/foo OK
120 124 testing: test-archive-1701ef1f1510/subrepo/sub OK
121 125 No errors detected in compressed data of archive.zip.
122 126
123 127 test that we can download single directories and files
124 128
125 129 $ python getarchive.py "$TIP" gz baz | gunzip | tar tf - 2>/dev/null
126 130 test-archive-1701ef1f1510/baz/bletch
127 131 $ python getarchive.py "$TIP" gz foo | gunzip | tar tf - 2>/dev/null
128 132 test-archive-1701ef1f1510/foo
129 133
130 134 test that we detect file patterns that match no files
131 135
132 136 $ python getarchive.py "$TIP" gz foobar
133 137 HTTP Error 404: file(s) not found: foobar
134 138
135 139 test that we reject unsafe patterns
136 140
137 141 $ python getarchive.py "$TIP" gz relre:baz
138 142 HTTP Error 404: file(s) not found: relre:baz
139 143
140 144 $ killdaemons.py
141 145
142 146 $ hg archive -t tar test.tar
143 147 $ tar tf test.tar
144 148 test/.hg_archival.txt
145 149 test/.hgsub
146 150 test/.hgsubstate
147 151 test/bar
148 152 test/baz/bletch
149 153 test/foo
150 154
151 155 $ hg archive --debug -t tbz2 -X baz test.tar.bz2 --config progress.debug=true
152 156 archiving: 0/4 files (0.00%)
153 157 archiving: .hgsub 1/4 files (25.00%)
154 158 archiving: .hgsubstate 2/4 files (50.00%)
155 159 archiving: bar 3/4 files (75.00%)
156 160 archiving: foo 4/4 files (100.00%)
157 161 $ bunzip2 -dc test.tar.bz2 | tar tf - 2>/dev/null
158 162 test/.hg_archival.txt
159 163 test/.hgsub
160 164 test/.hgsubstate
161 165 test/bar
162 166 test/foo
163 167
164 168 $ hg archive -t tgz -p %b-%h test-%h.tar.gz
165 169 $ gzip -dc test-$QTIP.tar.gz | tar tf - 2>/dev/null
166 170 test-1701ef1f1510/.hg_archival.txt
167 171 test-1701ef1f1510/.hgsub
168 172 test-1701ef1f1510/.hgsubstate
169 173 test-1701ef1f1510/bar
170 174 test-1701ef1f1510/baz/bletch
171 175 test-1701ef1f1510/foo
172 176
173 177 $ hg archive autodetected_test.tar
174 178 $ tar tf autodetected_test.tar
175 179 autodetected_test/.hg_archival.txt
176 180 autodetected_test/.hgsub
177 181 autodetected_test/.hgsubstate
178 182 autodetected_test/bar
179 183 autodetected_test/baz/bletch
180 184 autodetected_test/foo
181 185
182 186 The '-t' should override autodetection
183 187
184 188 $ hg archive -t tar autodetect_override_test.zip
185 189 $ tar tf autodetect_override_test.zip
186 190 autodetect_override_test.zip/.hg_archival.txt
187 191 autodetect_override_test.zip/.hgsub
188 192 autodetect_override_test.zip/.hgsubstate
189 193 autodetect_override_test.zip/bar
190 194 autodetect_override_test.zip/baz/bletch
191 195 autodetect_override_test.zip/foo
192 196
193 197 $ for ext in tar tar.gz tgz tar.bz2 tbz2 zip; do
194 198 > hg archive auto_test.$ext
195 199 > if [ -d auto_test.$ext ]; then
196 200 > echo "extension $ext was not autodetected."
197 201 > fi
198 202 > done
199 203
200 204 $ cat > md5comp.py <<EOF
201 205 > from __future__ import print_function
202 206 > try:
203 207 > from hashlib import md5
204 208 > except ImportError:
205 209 > from md5 import md5
206 210 > import sys
207 211 > f1, f2 = sys.argv[1:3]
208 212 > h1 = md5(open(f1, 'rb').read()).hexdigest()
209 213 > h2 = md5(open(f2, 'rb').read()).hexdigest()
210 214 > print(h1 == h2 or "md5 differ: " + repr((h1, h2)))
211 215 > EOF
212 216
213 217 archive name is stored in the archive, so create similar archives and
214 218 rename them afterwards.
215 219
216 220 $ hg archive -t tgz tip.tar.gz
217 221 $ mv tip.tar.gz tip1.tar.gz
218 222 $ sleep 1
219 223 $ hg archive -t tgz tip.tar.gz
220 224 $ mv tip.tar.gz tip2.tar.gz
221 225 $ python md5comp.py tip1.tar.gz tip2.tar.gz
222 226 True
223 227
224 228 $ hg archive -t zip -p /illegal test.zip
225 229 abort: archive prefix contains illegal components
226 230 [255]
227 231 $ hg archive -t zip -p very/../bad test.zip
228 232
229 233 $ hg archive --config ui.archivemeta=false -t zip -r 2 test.zip
230 234 $ unzip -t test.zip
231 235 Archive: test.zip
232 236 testing: test/bar OK
233 237 testing: test/baz/bletch OK
234 238 testing: test/foo OK
235 239 No errors detected in compressed data of test.zip.
236 240
237 241 $ hg archive -t tar - | tar tf - 2>/dev/null
238 242 test-1701ef1f1510/.hg_archival.txt
239 243 test-1701ef1f1510/.hgsub
240 244 test-1701ef1f1510/.hgsubstate
241 245 test-1701ef1f1510/bar
242 246 test-1701ef1f1510/baz/bletch
243 247 test-1701ef1f1510/foo
244 248
245 249 $ hg archive -r 0 -t tar rev-%r.tar
246 250 $ [ -f rev-0.tar ]
247 251
248 252 test .hg_archival.txt
249 253
250 254 $ hg archive ../test-tags
251 255 $ cat ../test-tags/.hg_archival.txt
252 256 repo: daa7f7c60e0a224faa4ff77ca41b2760562af264
253 257 node: 1701ef1f151069b8747038e93b5186bb43a47504
254 258 branch: default
255 259 latesttag: null
256 260 latesttagdistance: 4
257 261 changessincelatesttag: 4
258 262 $ hg tag -r 2 mytag
259 263 $ hg tag -r 2 anothertag
260 264 $ hg archive -r 2 ../test-lasttag
261 265 $ cat ../test-lasttag/.hg_archival.txt
262 266 repo: daa7f7c60e0a224faa4ff77ca41b2760562af264
263 267 node: 2c0277f05ed49d1c8328fb9ba92fba7a5ebcb33e
264 268 branch: default
265 269 tag: anothertag
266 270 tag: mytag
267 271
268 272 $ hg archive -t bogus test.bogus
269 273 abort: unknown archive type 'bogus'
270 274 [255]
271 275
272 276 enable progress extension:
273 277
274 278 $ cp $HGRCPATH $HGRCPATH.no-progress
275 279 $ cat >> $HGRCPATH <<EOF
276 280 > [progress]
277 281 > assume-tty = 1
278 282 > format = topic bar number
279 283 > delay = 0
280 284 > refresh = 0
281 285 > width = 60
282 286 > EOF
283 287
284 288 $ hg archive ../with-progress
285 289 \r (no-eol) (esc)
286 290 archiving [ ] 0/6\r (no-eol) (esc)
287 291 archiving [======> ] 1/6\r (no-eol) (esc)
288 292 archiving [=============> ] 2/6\r (no-eol) (esc)
289 293 archiving [====================> ] 3/6\r (no-eol) (esc)
290 294 archiving [===========================> ] 4/6\r (no-eol) (esc)
291 295 archiving [==================================> ] 5/6\r (no-eol) (esc)
292 296 archiving [==========================================>] 6/6\r (no-eol) (esc)
293 297 \r (no-eol) (esc)
294 298
295 299 cleanup after progress extension test:
296 300
297 301 $ cp $HGRCPATH.no-progress $HGRCPATH
298 302
299 303 server errors
300 304
301 305 $ cat errors.log
302 306
303 307 empty repo
304 308
305 309 $ hg init ../empty
306 310 $ cd ../empty
307 311 $ hg archive ../test-empty
308 312 abort: no working directory: please specify a revision
309 313 [255]
310 314
311 315 old file -- date clamped to 1980
312 316
313 317 $ touch -t 197501010000 old
314 318 $ hg add old
315 319 $ hg commit -m old
316 320 $ hg archive ../old.zip
317 321 $ unzip -l ../old.zip
318 322 Archive: ../old.zip
319 323 \s*Length.* (re)
320 324 *-----* (glob)
321 325 *172*80*00:00*old/.hg_archival.txt (glob)
322 326 *0*80*00:00*old/old (glob)
323 327 *-----* (glob)
324 328 \s*172\s+2 files (re)
325 329
326 330 show an error when a provided pattern matches no files
327 331
328 332 $ hg archive -I file_that_does_not_exist.foo ../empty.zip
329 333 abort: no files match the archive pattern
330 334 [255]
331 335
332 336 $ hg archive -X * ../empty.zip
333 337 abort: no files match the archive pattern
334 338 [255]
335 339
336 340 $ cd ..
337 341
338 342 issue3600: check whether "hg archive" can create archive files which
339 343 are extracted with expected timestamp, even though TZ is not
340 344 configured as GMT.
341 345
342 346 $ mkdir issue3600
343 347 $ cd issue3600
344 348
345 349 $ hg init repo
346 350 $ echo a > repo/a
347 351 $ hg -R repo add repo/a
348 352 $ hg -R repo commit -m '#0' -d '456789012 21600'
349 353 $ cat > show_mtime.py <<EOF
350 354 > from __future__ import print_function
351 355 > import sys, os
352 356 > print(int(os.stat(sys.argv[1]).st_mtime))
353 357 > EOF
354 358
355 359 $ hg -R repo archive --prefix tar-extracted archive.tar
356 360 $ (TZ=UTC-3; export TZ; tar xf archive.tar)
357 361 $ python show_mtime.py tar-extracted/a
358 362 456789012
359 363
360 364 $ hg -R repo archive --prefix zip-extracted archive.zip
361 365 $ (TZ=UTC-3; export TZ; unzip -q archive.zip)
362 366 $ python show_mtime.py zip-extracted/a
363 367 456789012
364 368
365 369 $ cd ..
General Comments 0
You need to be logged in to leave comments. Login now