Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(10494)

Delta Between Two Patch Sets: Lib/test/test_tarfile.py

Issue 23421: tarfile module does not correctly choose compression algorithms
Left Patch Set: Created 4 years, 5 months ago
Right Patch Set: Created 4 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import sys 1 import sys
2 import os 2 import os
3 import io 3 import io
4 from hashlib import md5 4 from hashlib import md5
5 5
6 import unittest 6 import unittest
7 import tarfile 7 import tarfile
8 8
9 from test import support, script_helper 9 from test import support, script_helper
10 10
(...skipping 15 matching lines...) Expand all
26 return md5(data).hexdigest() 26 return md5(data).hexdigest()
27 27
28 TEMPDIR = os.path.abspath(support.TESTFN) + "-tardir" 28 TEMPDIR = os.path.abspath(support.TESTFN) + "-tardir"
29 tarextdir = TEMPDIR + '-extract-test' 29 tarextdir = TEMPDIR + '-extract-test'
30 tarname = support.findfile("testtar.tar") 30 tarname = support.findfile("testtar.tar")
31 gzipname = os.path.join(TEMPDIR, "testtar.tar.gz") 31 gzipname = os.path.join(TEMPDIR, "testtar.tar.gz")
32 bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2") 32 bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2")
33 xzname = os.path.join(TEMPDIR, "testtar.tar.xz") 33 xzname = os.path.join(TEMPDIR, "testtar.tar.xz")
34 tmpname = os.path.join(TEMPDIR, "tmp.tar") 34 tmpname = os.path.join(TEMPDIR, "tmp.tar")
35 dotlessname = os.path.join(TEMPDIR, "testtar") 35 dotlessname = os.path.join(TEMPDIR, "testtar")
36 clitarname = os.path.join(TEMPDIR, "clitar.tar")
37 cligzipname = os.path.join(TEMPDIR, "clitar.tar.gz")
38 clibz2name = os.path.join(TEMPDIR, "clitar.tar.bz2")
39 clixzname = os.path.join(TEMPDIR, "clitar.tar.xz")
40 36
41 md5_regtype = "65f477c818ad9e15f7feab0c6d37742f" 37 md5_regtype = "65f477c818ad9e15f7feab0c6d37742f"
42 md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6" 38 md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6"
43 39
44 40
45 class TarTest: 41 class TarTest:
46 tarname = tarname 42 tarname = tarname
47 clitarname = clitarname
48 suffix = '' 43 suffix = ''
49 open = io.FileIO 44 open = io.FileIO
50 taropen = tarfile.TarFile.taropen 45 taropen = tarfile.TarFile.taropen
51 46
52 @property 47 @property
53 def mode(self): 48 def mode(self):
54 return self.prefix + self.suffix 49 return self.prefix + self.suffix
55 50
56 @support.requires_gzip 51 @support.requires_gzip
57 class GzipTest: 52 class GzipTest:
58 tarname = gzipname 53 tarname = gzipname
59 clitarname = cligzipname
60 suffix = 'gz' 54 suffix = 'gz'
61 open = gzip.GzipFile if gzip else None 55 open = gzip.GzipFile if gzip else None
62 taropen = tarfile.TarFile.gzopen 56 taropen = tarfile.TarFile.gzopen
63 57
64 @support.requires_bz2 58 @support.requires_bz2
65 class Bz2Test: 59 class Bz2Test:
66 tarname = bz2name 60 tarname = bz2name
67 clitarname = clibz2name
68 suffix = 'bz2' 61 suffix = 'bz2'
69 open = bz2.BZ2File if bz2 else None 62 open = bz2.BZ2File if bz2 else None
70 taropen = tarfile.TarFile.bz2open 63 taropen = tarfile.TarFile.bz2open
71 64
72 @support.requires_lzma 65 @support.requires_lzma
73 class LzmaTest: 66 class LzmaTest:
74 tarname = xzname 67 tarname = xzname
75 clitarname = clixzname
76 suffix = 'xz' 68 suffix = 'xz'
77 open = lzma.LZMAFile if lzma else None 69 open = lzma.LZMAFile if lzma else None
78 taropen = tarfile.TarFile.xzopen 70 taropen = tarfile.TarFile.xzopen
79 71
80 72
81 class ReadTest(TarTest): 73 class ReadTest(TarTest):
82 74
83 prefix = "r:" 75 prefix = "r:"
84 76
85 def setUp(self): 77 def setUp(self):
(...skipping 1926 matching lines...) Expand 10 before | Expand all | Expand 10 after
2012 with tarfile.open(tar_name) as tar: 2004 with tarfile.open(tar_name) as tar:
2013 tar.getmembers() 2005 tar.getmembers()
2014 finally: 2006 finally:
2015 support.unlink(tar_name) 2007 support.unlink(tar_name)
2016 2008
2017 def test_create_command_filetype(self): 2009 def test_create_command_filetype(self):
2018 files = [support.findfile('tokenize_tests.txt'), 2010 files = [support.findfile('tokenize_tests.txt'),
2019 support.findfile('tokenize_tests-no-coding-cookie-' 2011 support.findfile('tokenize_tests-no-coding-cookie-'
2020 'and-utf8-bom-sig-only.txt')] 2012 'and-utf8-bom-sig-only.txt')]
2021 2013
2022 for filetype in (TarTest, GzipTest, Bz2Test, LzmaTest): 2014 for filetype in (GzipTest, Bz2Test, LzmaTest):
storchaka 2015/02/09 19:33:45 Uncompressed tar files already tested in other tes
2023 for opt in '-c', '--create': 2015 if not filetype.open:
storchaka 2015/02/09 19:33:45 The long option already tested in other test. This
2024 try: 2016 continue
2025 out = self.tarfilecmd(opt, filetype.clitarname, *files) 2017 try:
storchaka 2015/02/09 19:33:45 Compression can be not available if required modul
2026 self.assertEqual(out, b'') 2018 filename = tmpname + '.' + filetype.suffix
2027 with filetype.taropen(filetype.clitarname, "r") as tar: 2019 out = self.tarfilecmd('-c', filename, *files)
2028 pass 2020 with filetype.taropen(filename, 'r') as tar:
2029 finally: 2021 pass
2030 support.unlink(filetype.clitarname) 2022 finally:
2023 support.unlink(filename)
2031 2024
2032 def test_extract_command(self): 2025 def test_extract_command(self):
2033 self.make_simple_tarfile(tmpname) 2026 self.make_simple_tarfile(tmpname)
2034 for opt in '-e', '--extract': 2027 for opt in '-e', '--extract':
2035 try: 2028 try:
2036 with support.temp_cwd(tarextdir): 2029 with support.temp_cwd(tarextdir):
2037 out = self.tarfilecmd(opt, tmpname) 2030 out = self.tarfilecmd(opt, tmpname)
2038 self.assertEqual(out, b'') 2031 self.assertEqual(out, b'')
2039 finally: 2032 finally:
2040 support.rmtree(tarextdir) 2033 support.rmtree(tarextdir)
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
2206 testtarnames.append(c.tarname) 2199 testtarnames.append(c.tarname)
2207 with c.open(c.tarname, "wb") as tar: 2200 with c.open(c.tarname, "wb") as tar:
2208 tar.write(data) 2201 tar.write(data)
2209 2202
2210 def tearDownModule(): 2203 def tearDownModule():
2211 if os.path.exists(TEMPDIR): 2204 if os.path.exists(TEMPDIR):
2212 support.rmtree(TEMPDIR) 2205 support.rmtree(TEMPDIR)
2213 2206
2214 if __name__ == "__main__": 2207 if __name__ == "__main__":
2215 unittest.main() 2208 unittest.main()
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+