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

Side by Side Diff: Lib/test/test_tarfile.py

Issue 23421: tarfile module does not correctly choose compression algorithms
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:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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")
36 40
37 md5_regtype = "65f477c818ad9e15f7feab0c6d37742f" 41 md5_regtype = "65f477c818ad9e15f7feab0c6d37742f"
38 md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6" 42 md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6"
39 43
40 44
41 class TarTest: 45 class TarTest:
42 tarname = tarname 46 tarname = tarname
47 clitarname = clitarname
43 suffix = '' 48 suffix = ''
44 open = io.FileIO 49 open = io.FileIO
45 taropen = tarfile.TarFile.taropen 50 taropen = tarfile.TarFile.taropen
46 51
47 @property 52 @property
48 def mode(self): 53 def mode(self):
49 return self.prefix + self.suffix 54 return self.prefix + self.suffix
50 55
51 @support.requires_gzip 56 @support.requires_gzip
52 class GzipTest: 57 class GzipTest:
53 tarname = gzipname 58 tarname = gzipname
59 clitarname = cligzipname
54 suffix = 'gz' 60 suffix = 'gz'
55 open = gzip.GzipFile if gzip else None 61 open = gzip.GzipFile if gzip else None
56 taropen = tarfile.TarFile.gzopen 62 taropen = tarfile.TarFile.gzopen
57 63
58 @support.requires_bz2 64 @support.requires_bz2
59 class Bz2Test: 65 class Bz2Test:
60 tarname = bz2name 66 tarname = bz2name
67 clitarname = clibz2name
61 suffix = 'bz2' 68 suffix = 'bz2'
62 open = bz2.BZ2File if bz2 else None 69 open = bz2.BZ2File if bz2 else None
63 taropen = tarfile.TarFile.bz2open 70 taropen = tarfile.TarFile.bz2open
64 71
65 @support.requires_lzma 72 @support.requires_lzma
66 class LzmaTest: 73 class LzmaTest:
67 tarname = xzname 74 tarname = xzname
75 clitarname = clixzname
68 suffix = 'xz' 76 suffix = 'xz'
69 open = lzma.LZMAFile if lzma else None 77 open = lzma.LZMAFile if lzma else None
70 taropen = tarfile.TarFile.xzopen 78 taropen = tarfile.TarFile.xzopen
71 79
72 80
73 class ReadTest(TarTest): 81 class ReadTest(TarTest):
74 82
75 prefix = "r:" 83 prefix = "r:"
76 84
77 def setUp(self): 85 def setUp(self):
(...skipping 1920 matching lines...) Expand 10 before | Expand all | Expand 10 after
1998 def test_create_command_dot_started_filename(self): 2006 def test_create_command_dot_started_filename(self):
1999 tar_name = os.path.join(TEMPDIR, ".testtar") 2007 tar_name = os.path.join(TEMPDIR, ".testtar")
2000 files = [support.findfile('tokenize_tests.txt')] 2008 files = [support.findfile('tokenize_tests.txt')]
2001 try: 2009 try:
2002 out = self.tarfilecmd('-c', tar_name, *files) 2010 out = self.tarfilecmd('-c', tar_name, *files)
2003 self.assertEqual(out, b'') 2011 self.assertEqual(out, b'')
2004 with tarfile.open(tar_name) as tar: 2012 with tarfile.open(tar_name) as tar:
2005 tar.getmembers() 2013 tar.getmembers()
2006 finally: 2014 finally:
2007 support.unlink(tar_name) 2015 support.unlink(tar_name)
2016
2017 def test_create_command_filetype(self):
2018 files = [support.findfile('tokenize_tests.txt'),
2019 support.findfile('tokenize_tests-no-coding-cookie-'
2020 'and-utf8-bom-sig-only.txt')]
2021
2022 for filetype in (TarTest, GzipTest, Bz2Test, LzmaTest):
storchaka 2015/02/09 19:33:45 Uncompressed tar files already tested in other tes
2023 for opt in '-c', '--create':
storchaka 2015/02/09 19:33:45 The long option already tested in other test. This
2024 try:
2025 out = self.tarfilecmd(opt, filetype.clitarname, *files)
storchaka 2015/02/09 19:33:45 Compression can be not available if required modul
2026 self.assertEqual(out, b'')
2027 with filetype.taropen(filetype.clitarname, "r") as tar:
2028 pass
2029 finally:
2030 support.unlink(filetype.clitarname)
2008 2031
2009 def test_extract_command(self): 2032 def test_extract_command(self):
2010 self.make_simple_tarfile(tmpname) 2033 self.make_simple_tarfile(tmpname)
2011 for opt in '-e', '--extract': 2034 for opt in '-e', '--extract':
2012 try: 2035 try:
2013 with support.temp_cwd(tarextdir): 2036 with support.temp_cwd(tarextdir):
2014 out = self.tarfilecmd(opt, tmpname) 2037 out = self.tarfilecmd(opt, tmpname)
2015 self.assertEqual(out, b'') 2038 self.assertEqual(out, b'')
2016 finally: 2039 finally:
2017 support.rmtree(tarextdir) 2040 support.rmtree(tarextdir)
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
2183 testtarnames.append(c.tarname) 2206 testtarnames.append(c.tarname)
2184 with c.open(c.tarname, "wb") as tar: 2207 with c.open(c.tarname, "wb") as tar:
2185 tar.write(data) 2208 tar.write(data)
2186 2209
2187 def tearDownModule(): 2210 def tearDownModule():
2188 if os.path.exists(TEMPDIR): 2211 if os.path.exists(TEMPDIR):
2189 support.rmtree(TEMPDIR) 2212 support.rmtree(TEMPDIR)
2190 2213
2191 if __name__ == "__main__": 2214 if __name__ == "__main__":
2192 unittest.main() 2215 unittest.main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

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