blob: ee83cbebd7e32e2dc7cd0bdc87f4210c7279787b [file] [log] [blame]
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001import sys
2import os
3import shutil
Brett Cannon455ea532003-06-12 08:01:06 +00004import tempfile
Georg Brandl38c6a222006-05-10 16:26:03 +00005import StringIO
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00006
7import unittest
8import tarfile
9
10from test import test_support
11
12# Check for our compression modules.
13try:
14 import gzip
Neal Norwitzae323192003-04-14 01:18:32 +000015 gzip.GzipFile
16except (ImportError, AttributeError):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000017 gzip = None
18try:
19 import bz2
20except ImportError:
21 bz2 = None
22
23def path(path):
24 return test_support.findfile(path)
25
Brett Cannon455ea532003-06-12 08:01:06 +000026testtar = path("testtar.tar")
27tempdir = os.path.join(tempfile.gettempdir(), "testtar" + os.extsep + "dir")
28tempname = test_support.TESTFN
Georg Brandl38c6a222006-05-10 16:26:03 +000029membercount = 12
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000030
31def tarname(comp=""):
32 if not comp:
33 return testtar
Brett Cannon43e559a2003-06-12 19:16:58 +000034 return os.path.join(tempdir, "%s%s%s" % (testtar, os.extsep, comp))
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000035
36def dirname():
37 if not os.path.exists(tempdir):
38 os.mkdir(tempdir)
39 return tempdir
40
41def tmpname():
42 return tempname
43
44
45class BaseTest(unittest.TestCase):
46 comp = ''
47 mode = 'r'
48 sep = ':'
49
50 def setUp(self):
51 mode = self.mode + self.sep + self.comp
52 self.tar = tarfile.open(tarname(self.comp), mode)
53
54 def tearDown(self):
55 self.tar.close()
56
57class ReadTest(BaseTest):
58
59 def test(self):
60 """Test member extraction.
61 """
62 members = 0
63 for tarinfo in self.tar:
64 members += 1
65 if not tarinfo.isreg():
66 continue
67 f = self.tar.extractfile(tarinfo)
68 self.assert_(len(f.read()) == tarinfo.size,
69 "size read does not match expected size")
70 f.close()
71
72 self.assert_(members == membercount,
73 "could not find all members")
74
75 def test_sparse(self):
76 """Test sparse member extraction.
77 """
78 if self.sep != "|":
79 f1 = self.tar.extractfile("S-SPARSE")
Jack Jansen149a8992003-03-07 13:27:53 +000080 f2 = self.tar.extractfile("S-SPARSE-WITH-NULLS")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000081 self.assert_(f1.read() == f2.read(),
82 "_FileObject failed on sparse file member")
83
84 def test_readlines(self):
85 """Test readlines() method of _FileObject.
86 """
87 if self.sep != "|":
Jack Jansen149a8992003-03-07 13:27:53 +000088 filename = "0-REGTYPE-TEXT"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000089 self.tar.extract(filename, dirname())
Tim Peters02494762006-05-26 14:02:05 +000090 f = open(os.path.join(dirname(), filename), "rU")
91 lines1 = f.readlines()
92 f.close()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000093 lines2 = self.tar.extractfile(filename).readlines()
94 self.assert_(lines1 == lines2,
95 "_FileObject.readline() does not work correctly")
96
Martin v. Löwisdf241532005-03-03 08:17:42 +000097 def test_iter(self):
98 # Test iteration over ExFileObject.
99 if self.sep != "|":
100 filename = "0-REGTYPE-TEXT"
101 self.tar.extract(filename, dirname())
Tim Peters02494762006-05-26 14:02:05 +0000102 f = open(os.path.join(dirname(), filename), "rU")
103 lines1 = f.readlines()
104 f.close()
Martin v. Löwisdf241532005-03-03 08:17:42 +0000105 lines2 = [line for line in self.tar.extractfile(filename)]
106 self.assert_(lines1 == lines2,
107 "ExFileObject iteration does not work correctly")
108
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000109 def test_seek(self):
110 """Test seek() method of _FileObject, incl. random reading.
111 """
112 if self.sep != "|":
Jack Jansen149a8992003-03-07 13:27:53 +0000113 filename = "0-REGTYPE"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000114 self.tar.extract(filename, dirname())
Tim Peters02494762006-05-26 14:02:05 +0000115 f = open(os.path.join(dirname(), filename), "rb")
116 data = f.read()
117 f.close()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000118
119 tarinfo = self.tar.getmember(filename)
120 fobj = self.tar.extractfile(tarinfo)
121
122 text = fobj.read()
123 fobj.seek(0)
124 self.assert_(0 == fobj.tell(),
125 "seek() to file's start failed")
126 fobj.seek(2048, 0)
127 self.assert_(2048 == fobj.tell(),
128 "seek() to absolute position failed")
129 fobj.seek(-1024, 1)
130 self.assert_(1024 == fobj.tell(),
131 "seek() to negative relative position failed")
132 fobj.seek(1024, 1)
133 self.assert_(2048 == fobj.tell(),
134 "seek() to positive relative position failed")
135 s = fobj.read(10)
136 self.assert_(s == data[2048:2058],
137 "read() after seek failed")
138 fobj.seek(0, 2)
139 self.assert_(tarinfo.size == fobj.tell(),
140 "seek() to file's end failed")
141 self.assert_(fobj.read() == "",
142 "read() at file's end did not return empty string")
143 fobj.seek(-tarinfo.size, 2)
144 self.assert_(0 == fobj.tell(),
145 "relative seek() to file's start failed")
146 fobj.seek(512)
147 s1 = fobj.readlines()
148 fobj.seek(512)
149 s2 = fobj.readlines()
150 self.assert_(s1 == s2,
151 "readlines() after seek failed")
152 fobj.close()
153
Neal Norwitzf3396542005-10-28 05:52:22 +0000154 def test_old_dirtype(self):
155 """Test old style dirtype member (bug #1336623).
156 """
157 # Old tars create directory members using a REGTYPE
158 # header with a "/" appended to the filename field.
159
160 # Create an old tar style directory entry.
161 filename = tmpname()
162 tarinfo = tarfile.TarInfo("directory/")
163 tarinfo.type = tarfile.REGTYPE
164
Tim Petersb1f32512006-05-26 13:39:17 +0000165 fobj = open(filename, "w")
Neal Norwitzf3396542005-10-28 05:52:22 +0000166 fobj.write(tarinfo.tobuf())
167 fobj.close()
168
169 try:
170 # Test if it is still a directory entry when
171 # read back.
172 tar = tarfile.open(filename)
173 tarinfo = tar.getmembers()[0]
174 tar.close()
175
176 self.assert_(tarinfo.type == tarfile.DIRTYPE)
177 self.assert_(tarinfo.name.endswith("/"))
178 finally:
179 try:
180 os.unlink(filename)
181 except:
182 pass
183
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000184class ReadStreamTest(ReadTest):
185 sep = "|"
186
187 def test(self):
188 """Test member extraction, and for StreamError when
189 seeking backwards.
190 """
191 ReadTest.test(self)
192 tarinfo = self.tar.getmembers()[0]
193 f = self.tar.extractfile(tarinfo)
194 self.assertRaises(tarfile.StreamError, f.read)
195
196 def test_stream(self):
197 """Compare the normal tar and the stream tar.
198 """
199 stream = self.tar
200 tar = tarfile.open(tarname(), 'r')
201
202 while 1:
203 t1 = tar.next()
204 t2 = stream.next()
205 if t1 is None:
206 break
207 self.assert_(t2 is not None, "stream.next() failed.")
208
209 if t2.islnk() or t2.issym():
210 self.assertRaises(tarfile.StreamError, stream.extractfile, t2)
211 continue
212 v1 = tar.extractfile(t1)
213 v2 = stream.extractfile(t2)
214 if v1 is None:
215 continue
216 self.assert_(v2 is not None, "stream.extractfile() failed")
217 self.assert_(v1.read() == v2.read(), "stream extraction failed")
218
Tim Peters02494762006-05-26 14:02:05 +0000219 tar.close()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000220 stream.close()
221
Georg Brandl49c8f4c2006-05-15 19:30:35 +0000222class ReadDetectTest(ReadTest):
223
224 def setUp(self):
225 self.tar = tarfile.open(tarname(self.comp), self.mode)
226
227class ReadDetectFileobjTest(ReadTest):
228
229 def setUp(self):
230 name = tarname(self.comp)
Tim Peters12087ba2006-05-15 20:44:10 +0000231 self.tar = tarfile.open(name, mode=self.mode,
232 fileobj=open(name, "rb"))
Georg Brandl49c8f4c2006-05-15 19:30:35 +0000233
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000234class ReadAsteriskTest(ReadTest):
235
236 def setUp(self):
237 mode = self.mode + self.sep + "*"
238 self.tar = tarfile.open(tarname(self.comp), mode)
239
240class ReadStreamAsteriskTest(ReadStreamTest):
241
242 def setUp(self):
243 mode = self.mode + self.sep + "*"
244 self.tar = tarfile.open(tarname(self.comp), mode)
245
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000246class WriteTest(BaseTest):
247 mode = 'w'
248
249 def setUp(self):
250 mode = self.mode + self.sep + self.comp
251 self.src = tarfile.open(tarname(self.comp), 'r')
Martin v. Löwisc234a522004-08-22 21:28:33 +0000252 self.dstname = tmpname()
253 self.dst = tarfile.open(self.dstname, mode)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000254
255 def tearDown(self):
256 self.src.close()
257 self.dst.close()
258
259 def test_posix(self):
260 self.dst.posix = 1
261 self._test()
262
263 def test_nonposix(self):
264 self.dst.posix = 0
265 self._test()
266
Martin v. Löwisc234a522004-08-22 21:28:33 +0000267 def test_small(self):
268 self.dst.add(os.path.join(os.path.dirname(__file__),"cfgparser.1"))
269 self.dst.close()
270 self.assertNotEqual(os.stat(self.dstname).st_size, 0)
271
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000272 def _test(self):
273 for tarinfo in self.src:
274 if not tarinfo.isreg():
275 continue
276 f = self.src.extractfile(tarinfo)
Georg Brandl38c6a222006-05-10 16:26:03 +0000277 if self.dst.posix and len(tarinfo.name) > tarfile.LENGTH_NAME and "/" not in tarinfo.name:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000278 self.assertRaises(ValueError, self.dst.addfile,
279 tarinfo, f)
280 else:
281 self.dst.addfile(tarinfo, f)
282
Georg Brandla32e0a02006-10-24 16:54:16 +0000283
284class Write100Test(BaseTest):
285 # The name field in a tar header stores strings of at most 100 chars.
286 # If a string is shorter than 100 chars it has to be padded with '\0',
287 # which implies that a string of exactly 100 chars is stored without
288 # a trailing '\0'.
289
290 def setUp(self):
291 self.name = "01234567890123456789012345678901234567890123456789"
292 self.name += "01234567890123456789012345678901234567890123456789"
293
294 self.tar = tarfile.open(tmpname(), "w")
295 t = tarfile.TarInfo(self.name)
296 self.tar.addfile(t)
297 self.tar.close()
298
299 self.tar = tarfile.open(tmpname())
300
301 def tearDown(self):
302 self.tar.close()
303
304 def test(self):
305 self.assertEqual(self.tar.getnames()[0], self.name,
306 "failed to store 100 char filename")
307
308
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000309class WriteSize0Test(BaseTest):
310 mode = 'w'
311
312 def setUp(self):
313 self.tmpdir = dirname()
314 self.dstname = tmpname()
315 self.dst = tarfile.open(self.dstname, "w")
316
317 def tearDown(self):
318 self.dst.close()
319
320 def test_file(self):
321 path = os.path.join(self.tmpdir, "file")
Tim Peters02494762006-05-26 14:02:05 +0000322 f = open(path, "w")
323 f.close()
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000324 tarinfo = self.dst.gettarinfo(path)
325 self.assertEqual(tarinfo.size, 0)
Tim Peters02494762006-05-26 14:02:05 +0000326 f = open(path, "w")
327 f.write("aaa")
328 f.close()
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000329 tarinfo = self.dst.gettarinfo(path)
330 self.assertEqual(tarinfo.size, 3)
331
332 def test_directory(self):
333 path = os.path.join(self.tmpdir, "directory")
Tim Peters4ccc0b72006-05-15 21:32:25 +0000334 if os.path.exists(path):
335 # This shouldn't be necessary, but is <wink> if a previous
336 # run was killed in mid-stream.
337 shutil.rmtree(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000338 os.mkdir(path)
339 tarinfo = self.dst.gettarinfo(path)
340 self.assertEqual(tarinfo.size, 0)
341
342 def test_symlink(self):
343 if hasattr(os, "symlink"):
344 path = os.path.join(self.tmpdir, "symlink")
345 os.symlink("link_target", path)
346 tarinfo = self.dst.gettarinfo(path)
347 self.assertEqual(tarinfo.size, 0)
348
349
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000350class WriteStreamTest(WriteTest):
351 sep = '|'
352
Neal Norwitz8a519392006-08-21 17:59:46 +0000353 def test_padding(self):
354 self.dst.close()
355
356 if self.comp == "gz":
357 f = gzip.GzipFile(self.dstname)
358 s = f.read()
359 f.close()
360 elif self.comp == "bz2":
361 f = bz2.BZ2Decompressor()
362 s = file(self.dstname).read()
363 s = f.decompress(s)
364 self.assertEqual(len(f.unused_data), 0, "trailing data")
365 else:
366 f = file(self.dstname)
367 s = f.read()
368 f.close()
369
370 self.assertEqual(s.count("\0"), tarfile.RECORDSIZE,
371 "incorrect zero padding")
372
373
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000374class WriteGNULongTest(unittest.TestCase):
375 """This testcase checks for correct creation of GNU Longname
376 and Longlink extensions.
377
378 It creates a tarfile and adds empty members with either
379 long names, long linknames or both and compares the size
380 of the tarfile with the expected size.
381
382 It checks for SF bug #812325 in TarFile._create_gnulong().
383
384 While I was writing this testcase, I noticed a second bug
385 in the same method:
386 Long{names,links} weren't null-terminated which lead to
387 bad tarfiles when their length was a multiple of 512. This
388 is tested as well.
389 """
390
391 def setUp(self):
392 self.tar = tarfile.open(tmpname(), "w")
393 self.tar.posix = False
394
395 def tearDown(self):
396 self.tar.close()
397
398 def _length(self, s):
399 blocks, remainder = divmod(len(s) + 1, 512)
400 if remainder:
401 blocks += 1
402 return blocks * 512
403
404 def _calc_size(self, name, link=None):
405 # initial tar header
406 count = 512
407
408 if len(name) > tarfile.LENGTH_NAME:
409 # gnu longname extended header + longname
410 count += 512
411 count += self._length(name)
412
413 if link is not None and len(link) > tarfile.LENGTH_LINK:
414 # gnu longlink extended header + longlink
415 count += 512
416 count += self._length(link)
417
418 return count
419
420 def _test(self, name, link=None):
421 tarinfo = tarfile.TarInfo(name)
422 if link:
423 tarinfo.linkname = link
424 tarinfo.type = tarfile.LNKTYPE
425
426 self.tar.addfile(tarinfo)
427
428 v1 = self._calc_size(name, link)
429 v2 = self.tar.offset
430 self.assertEqual(v1, v2, "GNU longname/longlink creation failed")
431
432 def test_longname_1023(self):
433 self._test(("longnam/" * 127) + "longnam")
434
435 def test_longname_1024(self):
436 self._test(("longnam/" * 127) + "longname")
437
438 def test_longname_1025(self):
439 self._test(("longnam/" * 127) + "longname_")
440
441 def test_longlink_1023(self):
442 self._test("name", ("longlnk/" * 127) + "longlnk")
443
444 def test_longlink_1024(self):
445 self._test("name", ("longlnk/" * 127) + "longlink")
446
447 def test_longlink_1025(self):
448 self._test("name", ("longlnk/" * 127) + "longlink_")
449
450 def test_longnamelink_1023(self):
451 self._test(("longnam/" * 127) + "longnam",
452 ("longlnk/" * 127) + "longlnk")
453
454 def test_longnamelink_1024(self):
455 self._test(("longnam/" * 127) + "longname",
456 ("longlnk/" * 127) + "longlink")
457
458 def test_longnamelink_1025(self):
459 self._test(("longnam/" * 127) + "longname_",
460 ("longlnk/" * 127) + "longlink_")
461
Georg Brandl38c6a222006-05-10 16:26:03 +0000462class ReadGNULongTest(unittest.TestCase):
463
464 def setUp(self):
465 self.tar = tarfile.open(tarname())
466
467 def tearDown(self):
468 self.tar.close()
469
470 def test_1471427(self):
471 """Test reading of longname (bug #1471427).
472 """
473 name = "test/" * 20 + "0-REGTYPE"
474 try:
475 tarinfo = self.tar.getmember(name)
476 except KeyError:
477 tarinfo = None
478 self.assert_(tarinfo is not None, "longname not found")
479 self.assert_(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype")
480
481 def test_read_name(self):
482 name = ("0-LONGNAME-" * 10)[:101]
483 try:
484 tarinfo = self.tar.getmember(name)
485 except KeyError:
486 tarinfo = None
487 self.assert_(tarinfo is not None, "longname not found")
488
489 def test_read_link(self):
490 link = ("1-LONGLINK-" * 10)[:101]
491 name = ("0-LONGNAME-" * 10)[:101]
492 try:
493 tarinfo = self.tar.getmember(link)
494 except KeyError:
495 tarinfo = None
496 self.assert_(tarinfo is not None, "longlink not found")
497 self.assert_(tarinfo.linkname == name, "linkname wrong")
498
499 def test_truncated_longname(self):
Tim Peters02494762006-05-26 14:02:05 +0000500 f = open(tarname())
501 fobj = StringIO.StringIO(f.read(1024))
502 f.close()
Georg Brandl38c6a222006-05-10 16:26:03 +0000503 tar = tarfile.open(name="foo.tar", fileobj=fobj)
504 self.assert_(len(tar.getmembers()) == 0, "")
Tim Peters02494762006-05-26 14:02:05 +0000505 tar.close()
Georg Brandl38c6a222006-05-10 16:26:03 +0000506
507
Neal Norwitza4f651a2004-07-20 22:07:44 +0000508class ExtractHardlinkTest(BaseTest):
509
510 def test_hardlink(self):
511 """Test hardlink extraction (bug #857297)
512 """
513 # Prevent errors from being caught
514 self.tar.errorlevel = 1
515
516 self.tar.extract("0-REGTYPE", dirname())
517 try:
518 # Extract 1-LNKTYPE which is a hardlink to 0-REGTYPE
519 self.tar.extract("1-LNKTYPE", dirname())
520 except EnvironmentError, e:
521 import errno
522 if e.errno == errno.ENOENT:
523 self.fail("hardlink not extracted properly")
524
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000525class CreateHardlinkTest(BaseTest):
526 """Test the creation of LNKTYPE (hardlink) members in an archive.
527 In this respect tarfile.py mimics the behaviour of GNU tar: If
528 a file has a st_nlink > 1, it will be added a REGTYPE member
529 only the first time.
530 """
531
532 def setUp(self):
533 self.tar = tarfile.open(tmpname(), "w")
534
535 self.foo = os.path.join(dirname(), "foo")
536 self.bar = os.path.join(dirname(), "bar")
537
538 if os.path.exists(self.foo):
539 os.remove(self.foo)
540 if os.path.exists(self.bar):
541 os.remove(self.bar)
542
Tim Peters02494762006-05-26 14:02:05 +0000543 f = open(self.foo, "w")
544 f.write("foo")
545 f.close()
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000546 self.tar.add(self.foo)
547
548 def test_add_twice(self):
549 # If st_nlink == 1 then the same file will be added as
550 # REGTYPE every time.
551 tarinfo = self.tar.gettarinfo(self.foo)
552 self.assertEqual(tarinfo.type, tarfile.REGTYPE,
553 "add file as regular failed")
554
555 def test_add_hardlink(self):
556 # If st_nlink > 1 then the same file will be added as
557 # LNKTYPE.
558 os.link(self.foo, self.bar)
559 tarinfo = self.tar.gettarinfo(self.foo)
560 self.assertEqual(tarinfo.type, tarfile.LNKTYPE,
561 "add file as hardlink failed")
562
563 tarinfo = self.tar.gettarinfo(self.bar)
564 self.assertEqual(tarinfo.type, tarfile.LNKTYPE,
565 "add file as hardlink failed")
566
567 def test_dereference_hardlink(self):
568 self.tar.dereference = True
569 os.link(self.foo, self.bar)
570 tarinfo = self.tar.gettarinfo(self.bar)
571 self.assertEqual(tarinfo.type, tarfile.REGTYPE,
572 "dereferencing hardlink failed")
573
Neal Norwitza4f651a2004-07-20 22:07:44 +0000574
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000575# Gzip TestCases
576class ReadTestGzip(ReadTest):
577 comp = "gz"
578class ReadStreamTestGzip(ReadStreamTest):
579 comp = "gz"
580class WriteTestGzip(WriteTest):
581 comp = "gz"
582class WriteStreamTestGzip(WriteStreamTest):
583 comp = "gz"
Georg Brandl49c8f4c2006-05-15 19:30:35 +0000584class ReadDetectTestGzip(ReadDetectTest):
585 comp = "gz"
586class ReadDetectFileobjTestGzip(ReadDetectFileobjTest):
587 comp = "gz"
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000588class ReadAsteriskTestGzip(ReadAsteriskTest):
589 comp = "gz"
590class ReadStreamAsteriskTestGzip(ReadStreamAsteriskTest):
591 comp = "gz"
592
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +0000593# Filemode test cases
594
595class FileModeTest(unittest.TestCase):
596 def test_modes(self):
597 self.assertEqual(tarfile.filemode(0755), '-rwxr-xr-x')
598 self.assertEqual(tarfile.filemode(07111), '---s--s--t')
599
Tim Peters8ceefc52004-10-25 03:19:41 +0000600
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000601if bz2:
602 # Bzip2 TestCases
603 class ReadTestBzip2(ReadTestGzip):
604 comp = "bz2"
605 class ReadStreamTestBzip2(ReadStreamTestGzip):
606 comp = "bz2"
607 class WriteTestBzip2(WriteTest):
608 comp = "bz2"
609 class WriteStreamTestBzip2(WriteStreamTestGzip):
610 comp = "bz2"
Georg Brandl49c8f4c2006-05-15 19:30:35 +0000611 class ReadDetectTestBzip2(ReadDetectTest):
612 comp = "bz2"
613 class ReadDetectFileobjTestBzip2(ReadDetectFileobjTest):
614 comp = "bz2"
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000615 class ReadAsteriskTestBzip2(ReadAsteriskTest):
616 comp = "bz2"
617 class ReadStreamAsteriskTestBzip2(ReadStreamAsteriskTest):
618 comp = "bz2"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000619
620# If importing gzip failed, discard the Gzip TestCases.
621if not gzip:
622 del ReadTestGzip
623 del ReadStreamTestGzip
624 del WriteTestGzip
625 del WriteStreamTestGzip
626
Neal Norwitz996acf12003-02-17 14:51:41 +0000627def test_main():
Tim Peters02494762006-05-26 14:02:05 +0000628 # Create archive.
629 f = open(tarname(), "rb")
630 fguts = f.read()
631 f.close()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000632 if gzip:
633 # create testtar.tar.gz
Tim Peters02494762006-05-26 14:02:05 +0000634 tar = gzip.open(tarname("gz"), "wb")
635 tar.write(fguts)
636 tar.close()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000637 if bz2:
638 # create testtar.tar.bz2
Tim Peters02494762006-05-26 14:02:05 +0000639 tar = bz2.BZ2File(tarname("bz2"), "wb")
640 tar.write(fguts)
641 tar.close()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000642
Walter Dörwald21d3a322003-05-01 17:45:56 +0000643 tests = [
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +0000644 FileModeTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000645 ReadTest,
646 ReadStreamTest,
Georg Brandl49c8f4c2006-05-15 19:30:35 +0000647 ReadDetectTest,
648 ReadDetectFileobjTest,
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000649 ReadAsteriskTest,
650 ReadStreamAsteriskTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000651 WriteTest,
Georg Brandla32e0a02006-10-24 16:54:16 +0000652 Write100Test,
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000653 WriteSize0Test,
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000654 WriteStreamTest,
655 WriteGNULongTest,
Georg Brandl38c6a222006-05-10 16:26:03 +0000656 ReadGNULongTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000657 ]
658
Neal Norwitza4f651a2004-07-20 22:07:44 +0000659 if hasattr(os, "link"):
660 tests.append(ExtractHardlinkTest)
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000661 tests.append(CreateHardlinkTest)
Neal Norwitza4f651a2004-07-20 22:07:44 +0000662
Walter Dörwald21d3a322003-05-01 17:45:56 +0000663 if gzip:
664 tests.extend([
665 ReadTestGzip, ReadStreamTestGzip,
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000666 WriteTestGzip, WriteStreamTestGzip,
Georg Brandl49c8f4c2006-05-15 19:30:35 +0000667 ReadDetectTestGzip, ReadDetectFileobjTestGzip,
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000668 ReadAsteriskTestGzip, ReadStreamAsteriskTestGzip
Walter Dörwald21d3a322003-05-01 17:45:56 +0000669 ])
670
671 if bz2:
672 tests.extend([
673 ReadTestBzip2, ReadStreamTestBzip2,
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000674 WriteTestBzip2, WriteStreamTestBzip2,
Georg Brandl49c8f4c2006-05-15 19:30:35 +0000675 ReadDetectTestBzip2, ReadDetectFileobjTestBzip2,
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000676 ReadAsteriskTestBzip2, ReadStreamAsteriskTestBzip2
Walter Dörwald21d3a322003-05-01 17:45:56 +0000677 ])
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000678 try:
Walter Dörwald21d3a322003-05-01 17:45:56 +0000679 test_support.run_unittest(*tests)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000680 finally:
681 if gzip:
682 os.remove(tarname("gz"))
683 if bz2:
Tim Peters4e306172006-05-27 14:13:13 +0000684 os.remove(tarname("bz2"))
Brett Cannon455ea532003-06-12 08:01:06 +0000685 if os.path.exists(dirname()):
686 shutil.rmtree(dirname())
687 if os.path.exists(tmpname()):
688 os.remove(tmpname())
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000689
Neal Norwitz996acf12003-02-17 14:51:41 +0000690if __name__ == "__main__":
691 test_main()