|
2 | 2 | import io
|
3 | 3 | import os
|
4 | 4 | import importlib.util
|
| 5 | +import pathlib |
5 | 6 | import posixpath
|
6 | 7 | import time
|
7 | 8 | import struct
|
|
13 | 14 | from random import randint, random, getrandbits
|
14 | 15 |
|
15 | 16 | from test.support import script_helper
|
16 |
| -from test.support import (TESTFN, findfile, unlink, rmtree, temp_dir, |
| 17 | +from test.support import (TESTFN, findfile, unlink, rmtree, temp_dir, temp_cwd, |
17 | 18 | requires_zlib, requires_bz2, requires_lzma,
|
18 | 19 | captured_stdout, check_warnings)
|
19 | 20 |
|
@@ -148,6 +149,12 @@ def test_open(self):
|
148 | 149 | for f in get_files(self):
|
149 | 150 | self.zip_open_test(f, self.compression)
|
150 | 151 |
|
| 152 | + def test_open_with_pathlike(self): |
| 153 | + path = pathlib.Path(TESTFN2) |
| 154 | + self.zip_open_test(path, self.compression) |
| 155 | + with zipfile.ZipFile(path, "r", self.compression) as zipfp: |
| 156 | + self.assertIsInstance(zipfp.filename, str) |
| 157 | + |
151 | 158 | def zip_random_open_test(self, f, compression):
|
152 | 159 | self.make_test_archive(f, compression)
|
153 | 160 |
|
@@ -906,49 +913,107 @@ def test_write_pyfile_bad_syntax(self):
|
906 | 913 | finally:
|
907 | 914 | rmtree(TESTFN2)
|
908 | 915 |
|
| 916 | + def test_write_pathlike(self): |
| 917 | + os.mkdir(TESTFN2) |
| 918 | + try: |
| 919 | + with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: |
| 920 | + fp.write("print(42)\n") |
| 921 | + |
| 922 | + with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: |
| 923 | + zipfp.writepy(pathlib.Path(TESTFN2) / "mod1.py") |
| 924 | + names = zipfp.namelist() |
| 925 | + self.assertCompiledIn('mod1.py', names) |
| 926 | + finally: |
| 927 | + rmtree(TESTFN2) |
| 928 | + |
909 | 929 |
|
910 | 930 | class ExtractTests(unittest.TestCase):
|
911 |
| - def test_extract(self): |
| 931 | + |
| 932 | + def make_test_file(self): |
912 | 933 | with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
|
913 | 934 | for fpath, fdata in SMALL_TEST_DATA:
|
914 | 935 | zipfp.writestr(fpath, fdata)
|
915 | 936 |
|
| 937 | + def test_extract(self): |
| 938 | + with temp_cwd(): |
| 939 | + self.make_test_file() |
| 940 | + with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 941 | + for fpath, fdata in SMALL_TEST_DATA: |
| 942 | + writtenfile = zipfp.extract(fpath) |
| 943 | + |
| 944 | + # make sure it was written to the right place |
| 945 | + correctfile = os.path.join(os.getcwd(), fpath) |
| 946 | + correctfile = os.path.normpath(correctfile) |
| 947 | + |
| 948 | + self.assertEqual(writtenfile, correctfile) |
| 949 | + |
| 950 | + # make sure correct data is in correct file |
| 951 | + with open(writtenfile, "rb") as f: |
| 952 | + self.assertEqual(fdata.encode(), f.read()) |
| 953 | + |
| 954 | + unlink(writtenfile) |
| 955 | + |
| 956 | + def _test_extract_with_target(self, target): |
| 957 | + self.make_test_file() |
916 | 958 | with zipfile.ZipFile(TESTFN2, "r") as zipfp:
|
917 | 959 | for fpath, fdata in SMALL_TEST_DATA:
|
918 |
| - writtenfile = zipfp.extract(fpath) |
| 960 | + writtenfile = zipfp.extract(fpath, target) |
919 | 961 |
|
920 | 962 | # make sure it was written to the right place
|
921 |
| - correctfile = os.path.join(os.getcwd(), fpath) |
| 963 | + correctfile = os.path.join(target, fpath) |
922 | 964 | correctfile = os.path.normpath(correctfile)
|
923 |
| - |
924 |
| - self.assertEqual(writtenfile, correctfile) |
| 965 | + self.assertTrue(os.path.samefile(writtenfile, correctfile), (writtenfile, target)) |
925 | 966 |
|
926 | 967 | # make sure correct data is in correct file
|
927 | 968 | with open(writtenfile, "rb") as f:
|
928 | 969 | self.assertEqual(fdata.encode(), f.read())
|
929 | 970 |
|
930 | 971 | unlink(writtenfile)
|
931 | 972 |
|
932 |
| - # remove the test file subdirectories |
933 |
| - rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 973 | + unlink(TESTFN2) |
| 974 | + |
| 975 | + def test_extract_with_target(self): |
| 976 | + with temp_dir() as extdir: |
| 977 | + self._test_extract_with_target(extdir) |
| 978 | + |
| 979 | + def test_extract_with_target_pathlike(self): |
| 980 | + with temp_dir() as extdir: |
| 981 | + self._test_extract_with_target(pathlib.Path(extdir)) |
934 | 982 |
|
935 | 983 | def test_extract_all(self):
|
936 |
| - with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: |
937 |
| - for fpath, fdata in SMALL_TEST_DATA: |
938 |
| - zipfp.writestr(fpath, fdata) |
| 984 | + with temp_cwd(): |
| 985 | + self.make_test_file() |
| 986 | + with zipfile.ZipFile(TESTFN2, "r") as zipfp: |
| 987 | + zipfp.extractall() |
| 988 | + for fpath, fdata in SMALL_TEST_DATA: |
| 989 | + outfile = os.path.join(os.getcwd(), fpath) |
| 990 | + |
| 991 | + with open(outfile, "rb") as f: |
| 992 | + self.assertEqual(fdata.encode(), f.read()) |
939 | 993 |
|
| 994 | + unlink(outfile) |
| 995 | + |
| 996 | + def _test_extract_all_with_target(self, target): |
| 997 | + self.make_test_file() |
940 | 998 | with zipfile.ZipFile(TESTFN2, "r") as zipfp:
|
941 |
| - zipfp.extractall() |
| 999 | + zipfp.extractall(target) |
942 | 1000 | for fpath, fdata in SMALL_TEST_DATA:
|
943 |
| - outfile = os.path.join(os.getcwd(), fpath) |
| 1001 | + outfile = os.path.join(target, fpath) |
944 | 1002 |
|
945 | 1003 | with open(outfile, "rb") as f:
|
946 | 1004 | self.assertEqual(fdata.encode(), f.read())
|
947 | 1005 |
|
948 | 1006 | unlink(outfile)
|
949 | 1007 |
|
950 |
| - # remove the test file subdirectories |
951 |
| - rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) |
| 1008 | + unlink(TESTFN2) |
| 1009 | + |
| 1010 | + def test_extract_all_with_target(self): |
| 1011 | + with temp_dir() as extdir: |
| 1012 | + self._test_extract_all_with_target(extdir) |
| 1013 | + |
| 1014 | + def test_extract_all_with_target_pathlike(self): |
| 1015 | + with temp_dir() as extdir: |
| 1016 | + self._test_extract_all_with_target(pathlib.Path(extdir)) |
952 | 1017 |
|
953 | 1018 | def check_file(self, filename, content):
|
954 | 1019 | self.assertTrue(os.path.isfile(filename))
|
@@ -1188,6 +1253,8 @@ def test_is_zip_erroneous_file(self):
|
1188 | 1253 | with open(TESTFN, "w") as fp:
|
1189 | 1254 | fp.write("this is not a legal zip file\n")
|
1190 | 1255 | self.assertFalse(zipfile.is_zipfile(TESTFN))
|
| 1256 | + # - passing a path-like object |
| 1257 | + self.assertFalse(zipfile.is_zipfile(pathlib.Path(TESTFN))) |
1191 | 1258 | # - passing a file object
|
1192 | 1259 | with open(TESTFN, "rb") as fp:
|
1193 | 1260 | self.assertFalse(zipfile.is_zipfile(fp))
|
@@ -2033,6 +2100,26 @@ def test_from_file(self):
|
2033 | 2100 | zi = zipfile.ZipInfo.from_file(__file__)
|
2034 | 2101 | self.assertEqual(posixpath.basename(zi.filename), 'test_zipfile.py')
|
2035 | 2102 | self.assertFalse(zi.is_dir())
|
| 2103 | + self.assertEqual(zi.file_size, os.path.getsize(__file__)) |
| 2104 | + |
| 2105 | + def test_from_file_pathlike(self): |
| 2106 | + zi = zipfile.ZipInfo.from_file(pathlib.Path(__file__)) |
| 2107 | + self.assertEqual(posixpath.basename(zi.filename), 'test_zipfile.py') |
| 2108 | + self.assertFalse(zi.is_dir()) |
| 2109 | + self.assertEqual(zi.file_size, os.path.getsize(__file__)) |
| 2110 | + |
| 2111 | + def test_from_file_bytes(self): |
| 2112 | + zi = zipfile.ZipInfo.from_file(os.fsencode(__file__), 'test') |
| 2113 | + self.assertEqual(posixpath.basename(zi.filename), 'test') |
| 2114 | + self.assertFalse(zi.is_dir()) |
| 2115 | + self.assertEqual(zi.file_size, os.path.getsize(__file__)) |
| 2116 | + |
| 2117 | + def test_from_file_fileno(self): |
| 2118 | + with open(__file__, 'rb') as f: |
| 2119 | + zi = zipfile.ZipInfo.from_file(f.fileno(), 'test') |
| 2120 | + self.assertEqual(posixpath.basename(zi.filename), 'test') |
| 2121 | + self.assertFalse(zi.is_dir()) |
| 2122 | + self.assertEqual(zi.file_size, os.path.getsize(__file__)) |
2036 | 2123 |
|
2037 | 2124 | def test_from_dir(self):
|
2038 | 2125 | dirpath = os.path.dirname(os.path.abspath(__file__))
|
|
0 commit comments