1 | n/a | # Copyright (C) 2003 Python Software Foundation |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | import unittest.mock |
---|
5 | n/a | import shutil |
---|
6 | n/a | import tempfile |
---|
7 | n/a | import sys |
---|
8 | n/a | import stat |
---|
9 | n/a | import os |
---|
10 | n/a | import os.path |
---|
11 | n/a | import errno |
---|
12 | n/a | import functools |
---|
13 | n/a | import subprocess |
---|
14 | n/a | from contextlib import ExitStack |
---|
15 | n/a | from shutil import (make_archive, |
---|
16 | n/a | register_archive_format, unregister_archive_format, |
---|
17 | n/a | get_archive_formats, Error, unpack_archive, |
---|
18 | n/a | register_unpack_format, RegistryError, |
---|
19 | n/a | unregister_unpack_format, get_unpack_formats, |
---|
20 | n/a | SameFileError) |
---|
21 | n/a | import tarfile |
---|
22 | n/a | import zipfile |
---|
23 | n/a | |
---|
24 | n/a | from test import support |
---|
25 | n/a | from test.support import (TESTFN, check_warnings, captured_stdout, |
---|
26 | n/a | android_not_root) |
---|
27 | n/a | |
---|
28 | n/a | TESTFN2 = TESTFN + "2" |
---|
29 | n/a | |
---|
30 | n/a | try: |
---|
31 | n/a | import grp |
---|
32 | n/a | import pwd |
---|
33 | n/a | UID_GID_SUPPORT = True |
---|
34 | n/a | except ImportError: |
---|
35 | n/a | UID_GID_SUPPORT = False |
---|
36 | n/a | |
---|
37 | n/a | def _fake_rename(*args, **kwargs): |
---|
38 | n/a | # Pretend the destination path is on a different filesystem. |
---|
39 | n/a | raise OSError(getattr(errno, 'EXDEV', 18), "Invalid cross-device link") |
---|
40 | n/a | |
---|
41 | n/a | def mock_rename(func): |
---|
42 | n/a | @functools.wraps(func) |
---|
43 | n/a | def wrap(*args, **kwargs): |
---|
44 | n/a | try: |
---|
45 | n/a | builtin_rename = os.rename |
---|
46 | n/a | os.rename = _fake_rename |
---|
47 | n/a | return func(*args, **kwargs) |
---|
48 | n/a | finally: |
---|
49 | n/a | os.rename = builtin_rename |
---|
50 | n/a | return wrap |
---|
51 | n/a | |
---|
52 | n/a | def write_file(path, content, binary=False): |
---|
53 | n/a | """Write *content* to a file located at *path*. |
---|
54 | n/a | |
---|
55 | n/a | If *path* is a tuple instead of a string, os.path.join will be used to |
---|
56 | n/a | make a path. If *binary* is true, the file will be opened in binary |
---|
57 | n/a | mode. |
---|
58 | n/a | """ |
---|
59 | n/a | if isinstance(path, tuple): |
---|
60 | n/a | path = os.path.join(*path) |
---|
61 | n/a | with open(path, 'wb' if binary else 'w') as fp: |
---|
62 | n/a | fp.write(content) |
---|
63 | n/a | |
---|
64 | n/a | def read_file(path, binary=False): |
---|
65 | n/a | """Return contents from a file located at *path*. |
---|
66 | n/a | |
---|
67 | n/a | If *path* is a tuple instead of a string, os.path.join will be used to |
---|
68 | n/a | make a path. If *binary* is true, the file will be opened in binary |
---|
69 | n/a | mode. |
---|
70 | n/a | """ |
---|
71 | n/a | if isinstance(path, tuple): |
---|
72 | n/a | path = os.path.join(*path) |
---|
73 | n/a | with open(path, 'rb' if binary else 'r') as fp: |
---|
74 | n/a | return fp.read() |
---|
75 | n/a | |
---|
76 | n/a | def rlistdir(path): |
---|
77 | n/a | res = [] |
---|
78 | n/a | for name in sorted(os.listdir(path)): |
---|
79 | n/a | p = os.path.join(path, name) |
---|
80 | n/a | if os.path.isdir(p) and not os.path.islink(p): |
---|
81 | n/a | res.append(name + '/') |
---|
82 | n/a | for n in rlistdir(p): |
---|
83 | n/a | res.append(name + '/' + n) |
---|
84 | n/a | else: |
---|
85 | n/a | res.append(name) |
---|
86 | n/a | return res |
---|
87 | n/a | |
---|
88 | n/a | |
---|
89 | n/a | class TestShutil(unittest.TestCase): |
---|
90 | n/a | |
---|
91 | n/a | def setUp(self): |
---|
92 | n/a | super(TestShutil, self).setUp() |
---|
93 | n/a | self.tempdirs = [] |
---|
94 | n/a | |
---|
95 | n/a | def tearDown(self): |
---|
96 | n/a | super(TestShutil, self).tearDown() |
---|
97 | n/a | while self.tempdirs: |
---|
98 | n/a | d = self.tempdirs.pop() |
---|
99 | n/a | shutil.rmtree(d, os.name in ('nt', 'cygwin')) |
---|
100 | n/a | |
---|
101 | n/a | |
---|
102 | n/a | def mkdtemp(self): |
---|
103 | n/a | """Create a temporary directory that will be cleaned up. |
---|
104 | n/a | |
---|
105 | n/a | Returns the path of the directory. |
---|
106 | n/a | """ |
---|
107 | n/a | d = tempfile.mkdtemp() |
---|
108 | n/a | self.tempdirs.append(d) |
---|
109 | n/a | return d |
---|
110 | n/a | |
---|
111 | n/a | def test_rmtree_works_on_bytes(self): |
---|
112 | n/a | tmp = self.mkdtemp() |
---|
113 | n/a | victim = os.path.join(tmp, 'killme') |
---|
114 | n/a | os.mkdir(victim) |
---|
115 | n/a | write_file(os.path.join(victim, 'somefile'), 'foo') |
---|
116 | n/a | victim = os.fsencode(victim) |
---|
117 | n/a | self.assertIsInstance(victim, bytes) |
---|
118 | n/a | shutil.rmtree(victim) |
---|
119 | n/a | |
---|
120 | n/a | @support.skip_unless_symlink |
---|
121 | n/a | def test_rmtree_fails_on_symlink(self): |
---|
122 | n/a | tmp = self.mkdtemp() |
---|
123 | n/a | dir_ = os.path.join(tmp, 'dir') |
---|
124 | n/a | os.mkdir(dir_) |
---|
125 | n/a | link = os.path.join(tmp, 'link') |
---|
126 | n/a | os.symlink(dir_, link) |
---|
127 | n/a | self.assertRaises(OSError, shutil.rmtree, link) |
---|
128 | n/a | self.assertTrue(os.path.exists(dir_)) |
---|
129 | n/a | self.assertTrue(os.path.lexists(link)) |
---|
130 | n/a | errors = [] |
---|
131 | n/a | def onerror(*args): |
---|
132 | n/a | errors.append(args) |
---|
133 | n/a | shutil.rmtree(link, onerror=onerror) |
---|
134 | n/a | self.assertEqual(len(errors), 1) |
---|
135 | n/a | self.assertIs(errors[0][0], os.path.islink) |
---|
136 | n/a | self.assertEqual(errors[0][1], link) |
---|
137 | n/a | self.assertIsInstance(errors[0][2][1], OSError) |
---|
138 | n/a | |
---|
139 | n/a | @support.skip_unless_symlink |
---|
140 | n/a | def test_rmtree_works_on_symlinks(self): |
---|
141 | n/a | tmp = self.mkdtemp() |
---|
142 | n/a | dir1 = os.path.join(tmp, 'dir1') |
---|
143 | n/a | dir2 = os.path.join(dir1, 'dir2') |
---|
144 | n/a | dir3 = os.path.join(tmp, 'dir3') |
---|
145 | n/a | for d in dir1, dir2, dir3: |
---|
146 | n/a | os.mkdir(d) |
---|
147 | n/a | file1 = os.path.join(tmp, 'file1') |
---|
148 | n/a | write_file(file1, 'foo') |
---|
149 | n/a | link1 = os.path.join(dir1, 'link1') |
---|
150 | n/a | os.symlink(dir2, link1) |
---|
151 | n/a | link2 = os.path.join(dir1, 'link2') |
---|
152 | n/a | os.symlink(dir3, link2) |
---|
153 | n/a | link3 = os.path.join(dir1, 'link3') |
---|
154 | n/a | os.symlink(file1, link3) |
---|
155 | n/a | # make sure symlinks are removed but not followed |
---|
156 | n/a | shutil.rmtree(dir1) |
---|
157 | n/a | self.assertFalse(os.path.exists(dir1)) |
---|
158 | n/a | self.assertTrue(os.path.exists(dir3)) |
---|
159 | n/a | self.assertTrue(os.path.exists(file1)) |
---|
160 | n/a | |
---|
161 | n/a | def test_rmtree_errors(self): |
---|
162 | n/a | # filename is guaranteed not to exist |
---|
163 | n/a | filename = tempfile.mktemp() |
---|
164 | n/a | self.assertRaises(FileNotFoundError, shutil.rmtree, filename) |
---|
165 | n/a | # test that ignore_errors option is honored |
---|
166 | n/a | shutil.rmtree(filename, ignore_errors=True) |
---|
167 | n/a | |
---|
168 | n/a | # existing file |
---|
169 | n/a | tmpdir = self.mkdtemp() |
---|
170 | n/a | write_file((tmpdir, "tstfile"), "") |
---|
171 | n/a | filename = os.path.join(tmpdir, "tstfile") |
---|
172 | n/a | with self.assertRaises(NotADirectoryError) as cm: |
---|
173 | n/a | shutil.rmtree(filename) |
---|
174 | n/a | # The reason for this rather odd construct is that Windows sprinkles |
---|
175 | n/a | # a \*.* at the end of file names. But only sometimes on some buildbots |
---|
176 | n/a | possible_args = [filename, os.path.join(filename, '*.*')] |
---|
177 | n/a | self.assertIn(cm.exception.filename, possible_args) |
---|
178 | n/a | self.assertTrue(os.path.exists(filename)) |
---|
179 | n/a | # test that ignore_errors option is honored |
---|
180 | n/a | shutil.rmtree(filename, ignore_errors=True) |
---|
181 | n/a | self.assertTrue(os.path.exists(filename)) |
---|
182 | n/a | errors = [] |
---|
183 | n/a | def onerror(*args): |
---|
184 | n/a | errors.append(args) |
---|
185 | n/a | shutil.rmtree(filename, onerror=onerror) |
---|
186 | n/a | self.assertEqual(len(errors), 2) |
---|
187 | n/a | self.assertIs(errors[0][0], os.listdir) |
---|
188 | n/a | self.assertEqual(errors[0][1], filename) |
---|
189 | n/a | self.assertIsInstance(errors[0][2][1], NotADirectoryError) |
---|
190 | n/a | self.assertIn(errors[0][2][1].filename, possible_args) |
---|
191 | n/a | self.assertIs(errors[1][0], os.rmdir) |
---|
192 | n/a | self.assertEqual(errors[1][1], filename) |
---|
193 | n/a | self.assertIsInstance(errors[1][2][1], NotADirectoryError) |
---|
194 | n/a | self.assertIn(errors[1][2][1].filename, possible_args) |
---|
195 | n/a | |
---|
196 | n/a | |
---|
197 | n/a | @unittest.skipUnless(hasattr(os, 'chmod'), 'requires os.chmod()') |
---|
198 | n/a | @unittest.skipIf(sys.platform[:6] == 'cygwin', |
---|
199 | n/a | "This test can't be run on Cygwin (issue #1071513).") |
---|
200 | n/a | @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, |
---|
201 | n/a | "This test can't be run reliably as root (issue #1076467).") |
---|
202 | n/a | def test_on_error(self): |
---|
203 | n/a | self.errorState = 0 |
---|
204 | n/a | os.mkdir(TESTFN) |
---|
205 | n/a | self.addCleanup(shutil.rmtree, TESTFN) |
---|
206 | n/a | |
---|
207 | n/a | self.child_file_path = os.path.join(TESTFN, 'a') |
---|
208 | n/a | self.child_dir_path = os.path.join(TESTFN, 'b') |
---|
209 | n/a | support.create_empty_file(self.child_file_path) |
---|
210 | n/a | os.mkdir(self.child_dir_path) |
---|
211 | n/a | old_dir_mode = os.stat(TESTFN).st_mode |
---|
212 | n/a | old_child_file_mode = os.stat(self.child_file_path).st_mode |
---|
213 | n/a | old_child_dir_mode = os.stat(self.child_dir_path).st_mode |
---|
214 | n/a | # Make unwritable. |
---|
215 | n/a | new_mode = stat.S_IREAD|stat.S_IEXEC |
---|
216 | n/a | os.chmod(self.child_file_path, new_mode) |
---|
217 | n/a | os.chmod(self.child_dir_path, new_mode) |
---|
218 | n/a | os.chmod(TESTFN, new_mode) |
---|
219 | n/a | |
---|
220 | n/a | self.addCleanup(os.chmod, TESTFN, old_dir_mode) |
---|
221 | n/a | self.addCleanup(os.chmod, self.child_file_path, old_child_file_mode) |
---|
222 | n/a | self.addCleanup(os.chmod, self.child_dir_path, old_child_dir_mode) |
---|
223 | n/a | |
---|
224 | n/a | shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror) |
---|
225 | n/a | # Test whether onerror has actually been called. |
---|
226 | n/a | self.assertEqual(self.errorState, 3, |
---|
227 | n/a | "Expected call to onerror function did not happen.") |
---|
228 | n/a | |
---|
229 | n/a | def check_args_to_onerror(self, func, arg, exc): |
---|
230 | n/a | # test_rmtree_errors deliberately runs rmtree |
---|
231 | n/a | # on a directory that is chmod 500, which will fail. |
---|
232 | n/a | # This function is run when shutil.rmtree fails. |
---|
233 | n/a | # 99.9% of the time it initially fails to remove |
---|
234 | n/a | # a file in the directory, so the first time through |
---|
235 | n/a | # func is os.remove. |
---|
236 | n/a | # However, some Linux machines running ZFS on |
---|
237 | n/a | # FUSE experienced a failure earlier in the process |
---|
238 | n/a | # at os.listdir. The first failure may legally |
---|
239 | n/a | # be either. |
---|
240 | n/a | if self.errorState < 2: |
---|
241 | n/a | if func is os.unlink: |
---|
242 | n/a | self.assertEqual(arg, self.child_file_path) |
---|
243 | n/a | elif func is os.rmdir: |
---|
244 | n/a | self.assertEqual(arg, self.child_dir_path) |
---|
245 | n/a | else: |
---|
246 | n/a | self.assertIs(func, os.listdir) |
---|
247 | n/a | self.assertIn(arg, [TESTFN, self.child_dir_path]) |
---|
248 | n/a | self.assertTrue(issubclass(exc[0], OSError)) |
---|
249 | n/a | self.errorState += 1 |
---|
250 | n/a | else: |
---|
251 | n/a | self.assertEqual(func, os.rmdir) |
---|
252 | n/a | self.assertEqual(arg, TESTFN) |
---|
253 | n/a | self.assertTrue(issubclass(exc[0], OSError)) |
---|
254 | n/a | self.errorState = 3 |
---|
255 | n/a | |
---|
256 | n/a | def test_rmtree_does_not_choke_on_failing_lstat(self): |
---|
257 | n/a | try: |
---|
258 | n/a | orig_lstat = os.lstat |
---|
259 | n/a | def raiser(fn, *args, **kwargs): |
---|
260 | n/a | if fn != TESTFN: |
---|
261 | n/a | raise OSError() |
---|
262 | n/a | else: |
---|
263 | n/a | return orig_lstat(fn) |
---|
264 | n/a | os.lstat = raiser |
---|
265 | n/a | |
---|
266 | n/a | os.mkdir(TESTFN) |
---|
267 | n/a | write_file((TESTFN, 'foo'), 'foo') |
---|
268 | n/a | shutil.rmtree(TESTFN) |
---|
269 | n/a | finally: |
---|
270 | n/a | os.lstat = orig_lstat |
---|
271 | n/a | |
---|
272 | n/a | @unittest.skipUnless(hasattr(os, 'chmod'), 'requires os.chmod') |
---|
273 | n/a | @support.skip_unless_symlink |
---|
274 | n/a | def test_copymode_follow_symlinks(self): |
---|
275 | n/a | tmp_dir = self.mkdtemp() |
---|
276 | n/a | src = os.path.join(tmp_dir, 'foo') |
---|
277 | n/a | dst = os.path.join(tmp_dir, 'bar') |
---|
278 | n/a | src_link = os.path.join(tmp_dir, 'baz') |
---|
279 | n/a | dst_link = os.path.join(tmp_dir, 'quux') |
---|
280 | n/a | write_file(src, 'foo') |
---|
281 | n/a | write_file(dst, 'foo') |
---|
282 | n/a | os.symlink(src, src_link) |
---|
283 | n/a | os.symlink(dst, dst_link) |
---|
284 | n/a | os.chmod(src, stat.S_IRWXU|stat.S_IRWXG) |
---|
285 | n/a | # file to file |
---|
286 | n/a | os.chmod(dst, stat.S_IRWXO) |
---|
287 | n/a | self.assertNotEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
---|
288 | n/a | shutil.copymode(src, dst) |
---|
289 | n/a | self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
---|
290 | n/a | # On Windows, os.chmod does not follow symlinks (issue #15411) |
---|
291 | n/a | if os.name != 'nt': |
---|
292 | n/a | # follow src link |
---|
293 | n/a | os.chmod(dst, stat.S_IRWXO) |
---|
294 | n/a | shutil.copymode(src_link, dst) |
---|
295 | n/a | self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
---|
296 | n/a | # follow dst link |
---|
297 | n/a | os.chmod(dst, stat.S_IRWXO) |
---|
298 | n/a | shutil.copymode(src, dst_link) |
---|
299 | n/a | self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
---|
300 | n/a | # follow both links |
---|
301 | n/a | os.chmod(dst, stat.S_IRWXO) |
---|
302 | n/a | shutil.copymode(src_link, dst_link) |
---|
303 | n/a | self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
---|
304 | n/a | |
---|
305 | n/a | @unittest.skipUnless(hasattr(os, 'lchmod'), 'requires os.lchmod') |
---|
306 | n/a | @support.skip_unless_symlink |
---|
307 | n/a | def test_copymode_symlink_to_symlink(self): |
---|
308 | n/a | tmp_dir = self.mkdtemp() |
---|
309 | n/a | src = os.path.join(tmp_dir, 'foo') |
---|
310 | n/a | dst = os.path.join(tmp_dir, 'bar') |
---|
311 | n/a | src_link = os.path.join(tmp_dir, 'baz') |
---|
312 | n/a | dst_link = os.path.join(tmp_dir, 'quux') |
---|
313 | n/a | write_file(src, 'foo') |
---|
314 | n/a | write_file(dst, 'foo') |
---|
315 | n/a | os.symlink(src, src_link) |
---|
316 | n/a | os.symlink(dst, dst_link) |
---|
317 | n/a | os.chmod(src, stat.S_IRWXU|stat.S_IRWXG) |
---|
318 | n/a | os.chmod(dst, stat.S_IRWXU) |
---|
319 | n/a | os.lchmod(src_link, stat.S_IRWXO|stat.S_IRWXG) |
---|
320 | n/a | # link to link |
---|
321 | n/a | os.lchmod(dst_link, stat.S_IRWXO) |
---|
322 | n/a | shutil.copymode(src_link, dst_link, follow_symlinks=False) |
---|
323 | n/a | self.assertEqual(os.lstat(src_link).st_mode, |
---|
324 | n/a | os.lstat(dst_link).st_mode) |
---|
325 | n/a | self.assertNotEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
---|
326 | n/a | # src link - use chmod |
---|
327 | n/a | os.lchmod(dst_link, stat.S_IRWXO) |
---|
328 | n/a | shutil.copymode(src_link, dst, follow_symlinks=False) |
---|
329 | n/a | self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
---|
330 | n/a | # dst link - use chmod |
---|
331 | n/a | os.lchmod(dst_link, stat.S_IRWXO) |
---|
332 | n/a | shutil.copymode(src, dst_link, follow_symlinks=False) |
---|
333 | n/a | self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
---|
334 | n/a | |
---|
335 | n/a | @unittest.skipIf(hasattr(os, 'lchmod'), 'requires os.lchmod to be missing') |
---|
336 | n/a | @support.skip_unless_symlink |
---|
337 | n/a | def test_copymode_symlink_to_symlink_wo_lchmod(self): |
---|
338 | n/a | tmp_dir = self.mkdtemp() |
---|
339 | n/a | src = os.path.join(tmp_dir, 'foo') |
---|
340 | n/a | dst = os.path.join(tmp_dir, 'bar') |
---|
341 | n/a | src_link = os.path.join(tmp_dir, 'baz') |
---|
342 | n/a | dst_link = os.path.join(tmp_dir, 'quux') |
---|
343 | n/a | write_file(src, 'foo') |
---|
344 | n/a | write_file(dst, 'foo') |
---|
345 | n/a | os.symlink(src, src_link) |
---|
346 | n/a | os.symlink(dst, dst_link) |
---|
347 | n/a | shutil.copymode(src_link, dst_link, follow_symlinks=False) # silent fail |
---|
348 | n/a | |
---|
349 | n/a | @support.skip_unless_symlink |
---|
350 | n/a | def test_copystat_symlinks(self): |
---|
351 | n/a | tmp_dir = self.mkdtemp() |
---|
352 | n/a | src = os.path.join(tmp_dir, 'foo') |
---|
353 | n/a | dst = os.path.join(tmp_dir, 'bar') |
---|
354 | n/a | src_link = os.path.join(tmp_dir, 'baz') |
---|
355 | n/a | dst_link = os.path.join(tmp_dir, 'qux') |
---|
356 | n/a | write_file(src, 'foo') |
---|
357 | n/a | src_stat = os.stat(src) |
---|
358 | n/a | os.utime(src, (src_stat.st_atime, |
---|
359 | n/a | src_stat.st_mtime - 42.0)) # ensure different mtimes |
---|
360 | n/a | write_file(dst, 'bar') |
---|
361 | n/a | self.assertNotEqual(os.stat(src).st_mtime, os.stat(dst).st_mtime) |
---|
362 | n/a | os.symlink(src, src_link) |
---|
363 | n/a | os.symlink(dst, dst_link) |
---|
364 | n/a | if hasattr(os, 'lchmod'): |
---|
365 | n/a | os.lchmod(src_link, stat.S_IRWXO) |
---|
366 | n/a | if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'): |
---|
367 | n/a | os.lchflags(src_link, stat.UF_NODUMP) |
---|
368 | n/a | src_link_stat = os.lstat(src_link) |
---|
369 | n/a | # follow |
---|
370 | n/a | if hasattr(os, 'lchmod'): |
---|
371 | n/a | shutil.copystat(src_link, dst_link, follow_symlinks=True) |
---|
372 | n/a | self.assertNotEqual(src_link_stat.st_mode, os.stat(dst).st_mode) |
---|
373 | n/a | # don't follow |
---|
374 | n/a | shutil.copystat(src_link, dst_link, follow_symlinks=False) |
---|
375 | n/a | dst_link_stat = os.lstat(dst_link) |
---|
376 | n/a | if os.utime in os.supports_follow_symlinks: |
---|
377 | n/a | for attr in 'st_atime', 'st_mtime': |
---|
378 | n/a | # The modification times may be truncated in the new file. |
---|
379 | n/a | self.assertLessEqual(getattr(src_link_stat, attr), |
---|
380 | n/a | getattr(dst_link_stat, attr) + 1) |
---|
381 | n/a | if hasattr(os, 'lchmod'): |
---|
382 | n/a | self.assertEqual(src_link_stat.st_mode, dst_link_stat.st_mode) |
---|
383 | n/a | if hasattr(os, 'lchflags') and hasattr(src_link_stat, 'st_flags'): |
---|
384 | n/a | self.assertEqual(src_link_stat.st_flags, dst_link_stat.st_flags) |
---|
385 | n/a | # tell to follow but dst is not a link |
---|
386 | n/a | shutil.copystat(src_link, dst, follow_symlinks=False) |
---|
387 | n/a | self.assertTrue(abs(os.stat(src).st_mtime - os.stat(dst).st_mtime) < |
---|
388 | n/a | 00000.1) |
---|
389 | n/a | |
---|
390 | n/a | @unittest.skipUnless(hasattr(os, 'chflags') and |
---|
391 | n/a | hasattr(errno, 'EOPNOTSUPP') and |
---|
392 | n/a | hasattr(errno, 'ENOTSUP'), |
---|
393 | n/a | "requires os.chflags, EOPNOTSUPP & ENOTSUP") |
---|
394 | n/a | def test_copystat_handles_harmless_chflags_errors(self): |
---|
395 | n/a | tmpdir = self.mkdtemp() |
---|
396 | n/a | file1 = os.path.join(tmpdir, 'file1') |
---|
397 | n/a | file2 = os.path.join(tmpdir, 'file2') |
---|
398 | n/a | write_file(file1, 'xxx') |
---|
399 | n/a | write_file(file2, 'xxx') |
---|
400 | n/a | |
---|
401 | n/a | def make_chflags_raiser(err): |
---|
402 | n/a | ex = OSError() |
---|
403 | n/a | |
---|
404 | n/a | def _chflags_raiser(path, flags, *, follow_symlinks=True): |
---|
405 | n/a | ex.errno = err |
---|
406 | n/a | raise ex |
---|
407 | n/a | return _chflags_raiser |
---|
408 | n/a | old_chflags = os.chflags |
---|
409 | n/a | try: |
---|
410 | n/a | for err in errno.EOPNOTSUPP, errno.ENOTSUP: |
---|
411 | n/a | os.chflags = make_chflags_raiser(err) |
---|
412 | n/a | shutil.copystat(file1, file2) |
---|
413 | n/a | # assert others errors break it |
---|
414 | n/a | os.chflags = make_chflags_raiser(errno.EOPNOTSUPP + errno.ENOTSUP) |
---|
415 | n/a | self.assertRaises(OSError, shutil.copystat, file1, file2) |
---|
416 | n/a | finally: |
---|
417 | n/a | os.chflags = old_chflags |
---|
418 | n/a | |
---|
419 | n/a | @support.skip_unless_xattr |
---|
420 | n/a | def test_copyxattr(self): |
---|
421 | n/a | tmp_dir = self.mkdtemp() |
---|
422 | n/a | src = os.path.join(tmp_dir, 'foo') |
---|
423 | n/a | write_file(src, 'foo') |
---|
424 | n/a | dst = os.path.join(tmp_dir, 'bar') |
---|
425 | n/a | write_file(dst, 'bar') |
---|
426 | n/a | |
---|
427 | n/a | # no xattr == no problem |
---|
428 | n/a | shutil._copyxattr(src, dst) |
---|
429 | n/a | # common case |
---|
430 | n/a | os.setxattr(src, 'user.foo', b'42') |
---|
431 | n/a | os.setxattr(src, 'user.bar', b'43') |
---|
432 | n/a | shutil._copyxattr(src, dst) |
---|
433 | n/a | self.assertEqual(sorted(os.listxattr(src)), sorted(os.listxattr(dst))) |
---|
434 | n/a | self.assertEqual( |
---|
435 | n/a | os.getxattr(src, 'user.foo'), |
---|
436 | n/a | os.getxattr(dst, 'user.foo')) |
---|
437 | n/a | # check errors don't affect other attrs |
---|
438 | n/a | os.remove(dst) |
---|
439 | n/a | write_file(dst, 'bar') |
---|
440 | n/a | os_error = OSError(errno.EPERM, 'EPERM') |
---|
441 | n/a | |
---|
442 | n/a | def _raise_on_user_foo(fname, attr, val, **kwargs): |
---|
443 | n/a | if attr == 'user.foo': |
---|
444 | n/a | raise os_error |
---|
445 | n/a | else: |
---|
446 | n/a | orig_setxattr(fname, attr, val, **kwargs) |
---|
447 | n/a | try: |
---|
448 | n/a | orig_setxattr = os.setxattr |
---|
449 | n/a | os.setxattr = _raise_on_user_foo |
---|
450 | n/a | shutil._copyxattr(src, dst) |
---|
451 | n/a | self.assertIn('user.bar', os.listxattr(dst)) |
---|
452 | n/a | finally: |
---|
453 | n/a | os.setxattr = orig_setxattr |
---|
454 | n/a | # the source filesystem not supporting xattrs should be ok, too. |
---|
455 | n/a | def _raise_on_src(fname, *, follow_symlinks=True): |
---|
456 | n/a | if fname == src: |
---|
457 | n/a | raise OSError(errno.ENOTSUP, 'Operation not supported') |
---|
458 | n/a | return orig_listxattr(fname, follow_symlinks=follow_symlinks) |
---|
459 | n/a | try: |
---|
460 | n/a | orig_listxattr = os.listxattr |
---|
461 | n/a | os.listxattr = _raise_on_src |
---|
462 | n/a | shutil._copyxattr(src, dst) |
---|
463 | n/a | finally: |
---|
464 | n/a | os.listxattr = orig_listxattr |
---|
465 | n/a | |
---|
466 | n/a | # test that shutil.copystat copies xattrs |
---|
467 | n/a | src = os.path.join(tmp_dir, 'the_original') |
---|
468 | n/a | write_file(src, src) |
---|
469 | n/a | os.setxattr(src, 'user.the_value', b'fiddly') |
---|
470 | n/a | dst = os.path.join(tmp_dir, 'the_copy') |
---|
471 | n/a | write_file(dst, dst) |
---|
472 | n/a | shutil.copystat(src, dst) |
---|
473 | n/a | self.assertEqual(os.getxattr(dst, 'user.the_value'), b'fiddly') |
---|
474 | n/a | |
---|
475 | n/a | @support.skip_unless_symlink |
---|
476 | n/a | @support.skip_unless_xattr |
---|
477 | n/a | @unittest.skipUnless(hasattr(os, 'geteuid') and os.geteuid() == 0, |
---|
478 | n/a | 'root privileges required') |
---|
479 | n/a | def test_copyxattr_symlinks(self): |
---|
480 | n/a | # On Linux, it's only possible to access non-user xattr for symlinks; |
---|
481 | n/a | # which in turn require root privileges. This test should be expanded |
---|
482 | n/a | # as soon as other platforms gain support for extended attributes. |
---|
483 | n/a | tmp_dir = self.mkdtemp() |
---|
484 | n/a | src = os.path.join(tmp_dir, 'foo') |
---|
485 | n/a | src_link = os.path.join(tmp_dir, 'baz') |
---|
486 | n/a | write_file(src, 'foo') |
---|
487 | n/a | os.symlink(src, src_link) |
---|
488 | n/a | os.setxattr(src, 'trusted.foo', b'42') |
---|
489 | n/a | os.setxattr(src_link, 'trusted.foo', b'43', follow_symlinks=False) |
---|
490 | n/a | dst = os.path.join(tmp_dir, 'bar') |
---|
491 | n/a | dst_link = os.path.join(tmp_dir, 'qux') |
---|
492 | n/a | write_file(dst, 'bar') |
---|
493 | n/a | os.symlink(dst, dst_link) |
---|
494 | n/a | shutil._copyxattr(src_link, dst_link, follow_symlinks=False) |
---|
495 | n/a | self.assertEqual(os.getxattr(dst_link, 'trusted.foo', follow_symlinks=False), b'43') |
---|
496 | n/a | self.assertRaises(OSError, os.getxattr, dst, 'trusted.foo') |
---|
497 | n/a | shutil._copyxattr(src_link, dst, follow_symlinks=False) |
---|
498 | n/a | self.assertEqual(os.getxattr(dst, 'trusted.foo'), b'43') |
---|
499 | n/a | |
---|
500 | n/a | @support.skip_unless_symlink |
---|
501 | n/a | def test_copy_symlinks(self): |
---|
502 | n/a | tmp_dir = self.mkdtemp() |
---|
503 | n/a | src = os.path.join(tmp_dir, 'foo') |
---|
504 | n/a | dst = os.path.join(tmp_dir, 'bar') |
---|
505 | n/a | src_link = os.path.join(tmp_dir, 'baz') |
---|
506 | n/a | write_file(src, 'foo') |
---|
507 | n/a | os.symlink(src, src_link) |
---|
508 | n/a | if hasattr(os, 'lchmod'): |
---|
509 | n/a | os.lchmod(src_link, stat.S_IRWXU | stat.S_IRWXO) |
---|
510 | n/a | # don't follow |
---|
511 | n/a | shutil.copy(src_link, dst, follow_symlinks=True) |
---|
512 | n/a | self.assertFalse(os.path.islink(dst)) |
---|
513 | n/a | self.assertEqual(read_file(src), read_file(dst)) |
---|
514 | n/a | os.remove(dst) |
---|
515 | n/a | # follow |
---|
516 | n/a | shutil.copy(src_link, dst, follow_symlinks=False) |
---|
517 | n/a | self.assertTrue(os.path.islink(dst)) |
---|
518 | n/a | self.assertEqual(os.readlink(dst), os.readlink(src_link)) |
---|
519 | n/a | if hasattr(os, 'lchmod'): |
---|
520 | n/a | self.assertEqual(os.lstat(src_link).st_mode, |
---|
521 | n/a | os.lstat(dst).st_mode) |
---|
522 | n/a | |
---|
523 | n/a | @support.skip_unless_symlink |
---|
524 | n/a | def test_copy2_symlinks(self): |
---|
525 | n/a | tmp_dir = self.mkdtemp() |
---|
526 | n/a | src = os.path.join(tmp_dir, 'foo') |
---|
527 | n/a | dst = os.path.join(tmp_dir, 'bar') |
---|
528 | n/a | src_link = os.path.join(tmp_dir, 'baz') |
---|
529 | n/a | write_file(src, 'foo') |
---|
530 | n/a | os.symlink(src, src_link) |
---|
531 | n/a | if hasattr(os, 'lchmod'): |
---|
532 | n/a | os.lchmod(src_link, stat.S_IRWXU | stat.S_IRWXO) |
---|
533 | n/a | if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'): |
---|
534 | n/a | os.lchflags(src_link, stat.UF_NODUMP) |
---|
535 | n/a | src_stat = os.stat(src) |
---|
536 | n/a | src_link_stat = os.lstat(src_link) |
---|
537 | n/a | # follow |
---|
538 | n/a | shutil.copy2(src_link, dst, follow_symlinks=True) |
---|
539 | n/a | self.assertFalse(os.path.islink(dst)) |
---|
540 | n/a | self.assertEqual(read_file(src), read_file(dst)) |
---|
541 | n/a | os.remove(dst) |
---|
542 | n/a | # don't follow |
---|
543 | n/a | shutil.copy2(src_link, dst, follow_symlinks=False) |
---|
544 | n/a | self.assertTrue(os.path.islink(dst)) |
---|
545 | n/a | self.assertEqual(os.readlink(dst), os.readlink(src_link)) |
---|
546 | n/a | dst_stat = os.lstat(dst) |
---|
547 | n/a | if os.utime in os.supports_follow_symlinks: |
---|
548 | n/a | for attr in 'st_atime', 'st_mtime': |
---|
549 | n/a | # The modification times may be truncated in the new file. |
---|
550 | n/a | self.assertLessEqual(getattr(src_link_stat, attr), |
---|
551 | n/a | getattr(dst_stat, attr) + 1) |
---|
552 | n/a | if hasattr(os, 'lchmod'): |
---|
553 | n/a | self.assertEqual(src_link_stat.st_mode, dst_stat.st_mode) |
---|
554 | n/a | self.assertNotEqual(src_stat.st_mode, dst_stat.st_mode) |
---|
555 | n/a | if hasattr(os, 'lchflags') and hasattr(src_link_stat, 'st_flags'): |
---|
556 | n/a | self.assertEqual(src_link_stat.st_flags, dst_stat.st_flags) |
---|
557 | n/a | |
---|
558 | n/a | @support.skip_unless_xattr |
---|
559 | n/a | def test_copy2_xattr(self): |
---|
560 | n/a | tmp_dir = self.mkdtemp() |
---|
561 | n/a | src = os.path.join(tmp_dir, 'foo') |
---|
562 | n/a | dst = os.path.join(tmp_dir, 'bar') |
---|
563 | n/a | write_file(src, 'foo') |
---|
564 | n/a | os.setxattr(src, 'user.foo', b'42') |
---|
565 | n/a | shutil.copy2(src, dst) |
---|
566 | n/a | self.assertEqual( |
---|
567 | n/a | os.getxattr(src, 'user.foo'), |
---|
568 | n/a | os.getxattr(dst, 'user.foo')) |
---|
569 | n/a | os.remove(dst) |
---|
570 | n/a | |
---|
571 | n/a | @support.skip_unless_symlink |
---|
572 | n/a | def test_copyfile_symlinks(self): |
---|
573 | n/a | tmp_dir = self.mkdtemp() |
---|
574 | n/a | src = os.path.join(tmp_dir, 'src') |
---|
575 | n/a | dst = os.path.join(tmp_dir, 'dst') |
---|
576 | n/a | dst_link = os.path.join(tmp_dir, 'dst_link') |
---|
577 | n/a | link = os.path.join(tmp_dir, 'link') |
---|
578 | n/a | write_file(src, 'foo') |
---|
579 | n/a | os.symlink(src, link) |
---|
580 | n/a | # don't follow |
---|
581 | n/a | shutil.copyfile(link, dst_link, follow_symlinks=False) |
---|
582 | n/a | self.assertTrue(os.path.islink(dst_link)) |
---|
583 | n/a | self.assertEqual(os.readlink(link), os.readlink(dst_link)) |
---|
584 | n/a | # follow |
---|
585 | n/a | shutil.copyfile(link, dst) |
---|
586 | n/a | self.assertFalse(os.path.islink(dst)) |
---|
587 | n/a | |
---|
588 | n/a | def test_rmtree_uses_safe_fd_version_if_available(self): |
---|
589 | n/a | _use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <= |
---|
590 | n/a | os.supports_dir_fd and |
---|
591 | n/a | os.listdir in os.supports_fd and |
---|
592 | n/a | os.stat in os.supports_follow_symlinks) |
---|
593 | n/a | if _use_fd_functions: |
---|
594 | n/a | self.assertTrue(shutil._use_fd_functions) |
---|
595 | n/a | self.assertTrue(shutil.rmtree.avoids_symlink_attacks) |
---|
596 | n/a | tmp_dir = self.mkdtemp() |
---|
597 | n/a | d = os.path.join(tmp_dir, 'a') |
---|
598 | n/a | os.mkdir(d) |
---|
599 | n/a | try: |
---|
600 | n/a | real_rmtree = shutil._rmtree_safe_fd |
---|
601 | n/a | class Called(Exception): pass |
---|
602 | n/a | def _raiser(*args, **kwargs): |
---|
603 | n/a | raise Called |
---|
604 | n/a | shutil._rmtree_safe_fd = _raiser |
---|
605 | n/a | self.assertRaises(Called, shutil.rmtree, d) |
---|
606 | n/a | finally: |
---|
607 | n/a | shutil._rmtree_safe_fd = real_rmtree |
---|
608 | n/a | else: |
---|
609 | n/a | self.assertFalse(shutil._use_fd_functions) |
---|
610 | n/a | self.assertFalse(shutil.rmtree.avoids_symlink_attacks) |
---|
611 | n/a | |
---|
612 | n/a | def test_rmtree_dont_delete_file(self): |
---|
613 | n/a | # When called on a file instead of a directory, don't delete it. |
---|
614 | n/a | handle, path = tempfile.mkstemp() |
---|
615 | n/a | os.close(handle) |
---|
616 | n/a | self.assertRaises(NotADirectoryError, shutil.rmtree, path) |
---|
617 | n/a | os.remove(path) |
---|
618 | n/a | |
---|
619 | n/a | def test_copytree_simple(self): |
---|
620 | n/a | src_dir = tempfile.mkdtemp() |
---|
621 | n/a | dst_dir = os.path.join(tempfile.mkdtemp(), 'destination') |
---|
622 | n/a | self.addCleanup(shutil.rmtree, src_dir) |
---|
623 | n/a | self.addCleanup(shutil.rmtree, os.path.dirname(dst_dir)) |
---|
624 | n/a | write_file((src_dir, 'test.txt'), '123') |
---|
625 | n/a | os.mkdir(os.path.join(src_dir, 'test_dir')) |
---|
626 | n/a | write_file((src_dir, 'test_dir', 'test.txt'), '456') |
---|
627 | n/a | |
---|
628 | n/a | shutil.copytree(src_dir, dst_dir) |
---|
629 | n/a | self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt'))) |
---|
630 | n/a | self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir'))) |
---|
631 | n/a | self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir', |
---|
632 | n/a | 'test.txt'))) |
---|
633 | n/a | actual = read_file((dst_dir, 'test.txt')) |
---|
634 | n/a | self.assertEqual(actual, '123') |
---|
635 | n/a | actual = read_file((dst_dir, 'test_dir', 'test.txt')) |
---|
636 | n/a | self.assertEqual(actual, '456') |
---|
637 | n/a | |
---|
638 | n/a | @support.skip_unless_symlink |
---|
639 | n/a | def test_copytree_symlinks(self): |
---|
640 | n/a | tmp_dir = self.mkdtemp() |
---|
641 | n/a | src_dir = os.path.join(tmp_dir, 'src') |
---|
642 | n/a | dst_dir = os.path.join(tmp_dir, 'dst') |
---|
643 | n/a | sub_dir = os.path.join(src_dir, 'sub') |
---|
644 | n/a | os.mkdir(src_dir) |
---|
645 | n/a | os.mkdir(sub_dir) |
---|
646 | n/a | write_file((src_dir, 'file.txt'), 'foo') |
---|
647 | n/a | src_link = os.path.join(sub_dir, 'link') |
---|
648 | n/a | dst_link = os.path.join(dst_dir, 'sub/link') |
---|
649 | n/a | os.symlink(os.path.join(src_dir, 'file.txt'), |
---|
650 | n/a | src_link) |
---|
651 | n/a | if hasattr(os, 'lchmod'): |
---|
652 | n/a | os.lchmod(src_link, stat.S_IRWXU | stat.S_IRWXO) |
---|
653 | n/a | if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'): |
---|
654 | n/a | os.lchflags(src_link, stat.UF_NODUMP) |
---|
655 | n/a | src_stat = os.lstat(src_link) |
---|
656 | n/a | shutil.copytree(src_dir, dst_dir, symlinks=True) |
---|
657 | n/a | self.assertTrue(os.path.islink(os.path.join(dst_dir, 'sub', 'link'))) |
---|
658 | n/a | self.assertEqual(os.readlink(os.path.join(dst_dir, 'sub', 'link')), |
---|
659 | n/a | os.path.join(src_dir, 'file.txt')) |
---|
660 | n/a | dst_stat = os.lstat(dst_link) |
---|
661 | n/a | if hasattr(os, 'lchmod'): |
---|
662 | n/a | self.assertEqual(dst_stat.st_mode, src_stat.st_mode) |
---|
663 | n/a | if hasattr(os, 'lchflags'): |
---|
664 | n/a | self.assertEqual(dst_stat.st_flags, src_stat.st_flags) |
---|
665 | n/a | |
---|
666 | n/a | def test_copytree_with_exclude(self): |
---|
667 | n/a | # creating data |
---|
668 | n/a | join = os.path.join |
---|
669 | n/a | exists = os.path.exists |
---|
670 | n/a | src_dir = tempfile.mkdtemp() |
---|
671 | n/a | try: |
---|
672 | n/a | dst_dir = join(tempfile.mkdtemp(), 'destination') |
---|
673 | n/a | write_file((src_dir, 'test.txt'), '123') |
---|
674 | n/a | write_file((src_dir, 'test.tmp'), '123') |
---|
675 | n/a | os.mkdir(join(src_dir, 'test_dir')) |
---|
676 | n/a | write_file((src_dir, 'test_dir', 'test.txt'), '456') |
---|
677 | n/a | os.mkdir(join(src_dir, 'test_dir2')) |
---|
678 | n/a | write_file((src_dir, 'test_dir2', 'test.txt'), '456') |
---|
679 | n/a | os.mkdir(join(src_dir, 'test_dir2', 'subdir')) |
---|
680 | n/a | os.mkdir(join(src_dir, 'test_dir2', 'subdir2')) |
---|
681 | n/a | write_file((src_dir, 'test_dir2', 'subdir', 'test.txt'), '456') |
---|
682 | n/a | write_file((src_dir, 'test_dir2', 'subdir2', 'test.py'), '456') |
---|
683 | n/a | |
---|
684 | n/a | # testing glob-like patterns |
---|
685 | n/a | try: |
---|
686 | n/a | patterns = shutil.ignore_patterns('*.tmp', 'test_dir2') |
---|
687 | n/a | shutil.copytree(src_dir, dst_dir, ignore=patterns) |
---|
688 | n/a | # checking the result: some elements should not be copied |
---|
689 | n/a | self.assertTrue(exists(join(dst_dir, 'test.txt'))) |
---|
690 | n/a | self.assertFalse(exists(join(dst_dir, 'test.tmp'))) |
---|
691 | n/a | self.assertFalse(exists(join(dst_dir, 'test_dir2'))) |
---|
692 | n/a | finally: |
---|
693 | n/a | shutil.rmtree(dst_dir) |
---|
694 | n/a | try: |
---|
695 | n/a | patterns = shutil.ignore_patterns('*.tmp', 'subdir*') |
---|
696 | n/a | shutil.copytree(src_dir, dst_dir, ignore=patterns) |
---|
697 | n/a | # checking the result: some elements should not be copied |
---|
698 | n/a | self.assertFalse(exists(join(dst_dir, 'test.tmp'))) |
---|
699 | n/a | self.assertFalse(exists(join(dst_dir, 'test_dir2', 'subdir2'))) |
---|
700 | n/a | self.assertFalse(exists(join(dst_dir, 'test_dir2', 'subdir'))) |
---|
701 | n/a | finally: |
---|
702 | n/a | shutil.rmtree(dst_dir) |
---|
703 | n/a | |
---|
704 | n/a | # testing callable-style |
---|
705 | n/a | try: |
---|
706 | n/a | def _filter(src, names): |
---|
707 | n/a | res = [] |
---|
708 | n/a | for name in names: |
---|
709 | n/a | path = os.path.join(src, name) |
---|
710 | n/a | |
---|
711 | n/a | if (os.path.isdir(path) and |
---|
712 | n/a | path.split()[-1] == 'subdir'): |
---|
713 | n/a | res.append(name) |
---|
714 | n/a | elif os.path.splitext(path)[-1] in ('.py'): |
---|
715 | n/a | res.append(name) |
---|
716 | n/a | return res |
---|
717 | n/a | |
---|
718 | n/a | shutil.copytree(src_dir, dst_dir, ignore=_filter) |
---|
719 | n/a | |
---|
720 | n/a | # checking the result: some elements should not be copied |
---|
721 | n/a | self.assertFalse(exists(join(dst_dir, 'test_dir2', 'subdir2', |
---|
722 | n/a | 'test.py'))) |
---|
723 | n/a | self.assertFalse(exists(join(dst_dir, 'test_dir2', 'subdir'))) |
---|
724 | n/a | |
---|
725 | n/a | finally: |
---|
726 | n/a | shutil.rmtree(dst_dir) |
---|
727 | n/a | finally: |
---|
728 | n/a | shutil.rmtree(src_dir) |
---|
729 | n/a | shutil.rmtree(os.path.dirname(dst_dir)) |
---|
730 | n/a | |
---|
731 | n/a | def test_copytree_retains_permissions(self): |
---|
732 | n/a | tmp_dir = tempfile.mkdtemp() |
---|
733 | n/a | src_dir = os.path.join(tmp_dir, 'source') |
---|
734 | n/a | os.mkdir(src_dir) |
---|
735 | n/a | dst_dir = os.path.join(tmp_dir, 'destination') |
---|
736 | n/a | self.addCleanup(shutil.rmtree, tmp_dir) |
---|
737 | n/a | |
---|
738 | n/a | os.chmod(src_dir, 0o777) |
---|
739 | n/a | write_file((src_dir, 'permissive.txt'), '123') |
---|
740 | n/a | os.chmod(os.path.join(src_dir, 'permissive.txt'), 0o777) |
---|
741 | n/a | write_file((src_dir, 'restrictive.txt'), '456') |
---|
742 | n/a | os.chmod(os.path.join(src_dir, 'restrictive.txt'), 0o600) |
---|
743 | n/a | restrictive_subdir = tempfile.mkdtemp(dir=src_dir) |
---|
744 | n/a | os.chmod(restrictive_subdir, 0o600) |
---|
745 | n/a | |
---|
746 | n/a | shutil.copytree(src_dir, dst_dir) |
---|
747 | n/a | self.assertEqual(os.stat(src_dir).st_mode, os.stat(dst_dir).st_mode) |
---|
748 | n/a | self.assertEqual(os.stat(os.path.join(src_dir, 'permissive.txt')).st_mode, |
---|
749 | n/a | os.stat(os.path.join(dst_dir, 'permissive.txt')).st_mode) |
---|
750 | n/a | self.assertEqual(os.stat(os.path.join(src_dir, 'restrictive.txt')).st_mode, |
---|
751 | n/a | os.stat(os.path.join(dst_dir, 'restrictive.txt')).st_mode) |
---|
752 | n/a | restrictive_subdir_dst = os.path.join(dst_dir, |
---|
753 | n/a | os.path.split(restrictive_subdir)[1]) |
---|
754 | n/a | self.assertEqual(os.stat(restrictive_subdir).st_mode, |
---|
755 | n/a | os.stat(restrictive_subdir_dst).st_mode) |
---|
756 | n/a | |
---|
757 | n/a | @unittest.mock.patch('os.chmod') |
---|
758 | n/a | def test_copytree_winerror(self, mock_patch): |
---|
759 | n/a | # When copying to VFAT, copystat() raises OSError. On Windows, the |
---|
760 | n/a | # exception object has a meaningful 'winerror' attribute, but not |
---|
761 | n/a | # on other operating systems. Do not assume 'winerror' is set. |
---|
762 | n/a | src_dir = tempfile.mkdtemp() |
---|
763 | n/a | dst_dir = os.path.join(tempfile.mkdtemp(), 'destination') |
---|
764 | n/a | self.addCleanup(shutil.rmtree, src_dir) |
---|
765 | n/a | self.addCleanup(shutil.rmtree, os.path.dirname(dst_dir)) |
---|
766 | n/a | |
---|
767 | n/a | mock_patch.side_effect = PermissionError('ka-boom') |
---|
768 | n/a | with self.assertRaises(shutil.Error): |
---|
769 | n/a | shutil.copytree(src_dir, dst_dir) |
---|
770 | n/a | |
---|
771 | n/a | @unittest.skipIf(os.name == 'nt', 'temporarily disabled on Windows') |
---|
772 | n/a | @unittest.skipUnless(hasattr(os, 'link'), 'requires os.link') |
---|
773 | n/a | @unittest.skipIf(android_not_root, "hard links not allowed, non root user") |
---|
774 | n/a | def test_dont_copy_file_onto_link_to_itself(self): |
---|
775 | n/a | # bug 851123. |
---|
776 | n/a | os.mkdir(TESTFN) |
---|
777 | n/a | src = os.path.join(TESTFN, 'cheese') |
---|
778 | n/a | dst = os.path.join(TESTFN, 'shop') |
---|
779 | n/a | try: |
---|
780 | n/a | with open(src, 'w') as f: |
---|
781 | n/a | f.write('cheddar') |
---|
782 | n/a | os.link(src, dst) |
---|
783 | n/a | self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst) |
---|
784 | n/a | with open(src, 'r') as f: |
---|
785 | n/a | self.assertEqual(f.read(), 'cheddar') |
---|
786 | n/a | os.remove(dst) |
---|
787 | n/a | finally: |
---|
788 | n/a | shutil.rmtree(TESTFN, ignore_errors=True) |
---|
789 | n/a | |
---|
790 | n/a | @support.skip_unless_symlink |
---|
791 | n/a | def test_dont_copy_file_onto_symlink_to_itself(self): |
---|
792 | n/a | # bug 851123. |
---|
793 | n/a | os.mkdir(TESTFN) |
---|
794 | n/a | src = os.path.join(TESTFN, 'cheese') |
---|
795 | n/a | dst = os.path.join(TESTFN, 'shop') |
---|
796 | n/a | try: |
---|
797 | n/a | with open(src, 'w') as f: |
---|
798 | n/a | f.write('cheddar') |
---|
799 | n/a | # Using `src` here would mean we end up with a symlink pointing |
---|
800 | n/a | # to TESTFN/TESTFN/cheese, while it should point at |
---|
801 | n/a | # TESTFN/cheese. |
---|
802 | n/a | os.symlink('cheese', dst) |
---|
803 | n/a | self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst) |
---|
804 | n/a | with open(src, 'r') as f: |
---|
805 | n/a | self.assertEqual(f.read(), 'cheddar') |
---|
806 | n/a | os.remove(dst) |
---|
807 | n/a | finally: |
---|
808 | n/a | shutil.rmtree(TESTFN, ignore_errors=True) |
---|
809 | n/a | |
---|
810 | n/a | @support.skip_unless_symlink |
---|
811 | n/a | def test_rmtree_on_symlink(self): |
---|
812 | n/a | # bug 1669. |
---|
813 | n/a | os.mkdir(TESTFN) |
---|
814 | n/a | try: |
---|
815 | n/a | src = os.path.join(TESTFN, 'cheese') |
---|
816 | n/a | dst = os.path.join(TESTFN, 'shop') |
---|
817 | n/a | os.mkdir(src) |
---|
818 | n/a | os.symlink(src, dst) |
---|
819 | n/a | self.assertRaises(OSError, shutil.rmtree, dst) |
---|
820 | n/a | shutil.rmtree(dst, ignore_errors=True) |
---|
821 | n/a | finally: |
---|
822 | n/a | shutil.rmtree(TESTFN, ignore_errors=True) |
---|
823 | n/a | |
---|
824 | n/a | # Issue #3002: copyfile and copytree block indefinitely on named pipes |
---|
825 | n/a | @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') |
---|
826 | n/a | @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") |
---|
827 | n/a | def test_copyfile_named_pipe(self): |
---|
828 | n/a | os.mkfifo(TESTFN) |
---|
829 | n/a | try: |
---|
830 | n/a | self.assertRaises(shutil.SpecialFileError, |
---|
831 | n/a | shutil.copyfile, TESTFN, TESTFN2) |
---|
832 | n/a | self.assertRaises(shutil.SpecialFileError, |
---|
833 | n/a | shutil.copyfile, __file__, TESTFN) |
---|
834 | n/a | finally: |
---|
835 | n/a | os.remove(TESTFN) |
---|
836 | n/a | |
---|
837 | n/a | @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") |
---|
838 | n/a | @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') |
---|
839 | n/a | @support.skip_unless_symlink |
---|
840 | n/a | def test_copytree_named_pipe(self): |
---|
841 | n/a | os.mkdir(TESTFN) |
---|
842 | n/a | try: |
---|
843 | n/a | subdir = os.path.join(TESTFN, "subdir") |
---|
844 | n/a | os.mkdir(subdir) |
---|
845 | n/a | pipe = os.path.join(subdir, "mypipe") |
---|
846 | n/a | os.mkfifo(pipe) |
---|
847 | n/a | try: |
---|
848 | n/a | shutil.copytree(TESTFN, TESTFN2) |
---|
849 | n/a | except shutil.Error as e: |
---|
850 | n/a | errors = e.args[0] |
---|
851 | n/a | self.assertEqual(len(errors), 1) |
---|
852 | n/a | src, dst, error_msg = errors[0] |
---|
853 | n/a | self.assertEqual("`%s` is a named pipe" % pipe, error_msg) |
---|
854 | n/a | else: |
---|
855 | n/a | self.fail("shutil.Error should have been raised") |
---|
856 | n/a | finally: |
---|
857 | n/a | shutil.rmtree(TESTFN, ignore_errors=True) |
---|
858 | n/a | shutil.rmtree(TESTFN2, ignore_errors=True) |
---|
859 | n/a | |
---|
860 | n/a | def test_copytree_special_func(self): |
---|
861 | n/a | |
---|
862 | n/a | src_dir = self.mkdtemp() |
---|
863 | n/a | dst_dir = os.path.join(self.mkdtemp(), 'destination') |
---|
864 | n/a | write_file((src_dir, 'test.txt'), '123') |
---|
865 | n/a | os.mkdir(os.path.join(src_dir, 'test_dir')) |
---|
866 | n/a | write_file((src_dir, 'test_dir', 'test.txt'), '456') |
---|
867 | n/a | |
---|
868 | n/a | copied = [] |
---|
869 | n/a | def _copy(src, dst): |
---|
870 | n/a | copied.append((src, dst)) |
---|
871 | n/a | |
---|
872 | n/a | shutil.copytree(src_dir, dst_dir, copy_function=_copy) |
---|
873 | n/a | self.assertEqual(len(copied), 2) |
---|
874 | n/a | |
---|
875 | n/a | @support.skip_unless_symlink |
---|
876 | n/a | def test_copytree_dangling_symlinks(self): |
---|
877 | n/a | |
---|
878 | n/a | # a dangling symlink raises an error at the end |
---|
879 | n/a | src_dir = self.mkdtemp() |
---|
880 | n/a | dst_dir = os.path.join(self.mkdtemp(), 'destination') |
---|
881 | n/a | os.symlink('IDONTEXIST', os.path.join(src_dir, 'test.txt')) |
---|
882 | n/a | os.mkdir(os.path.join(src_dir, 'test_dir')) |
---|
883 | n/a | write_file((src_dir, 'test_dir', 'test.txt'), '456') |
---|
884 | n/a | self.assertRaises(Error, shutil.copytree, src_dir, dst_dir) |
---|
885 | n/a | |
---|
886 | n/a | # a dangling symlink is ignored with the proper flag |
---|
887 | n/a | dst_dir = os.path.join(self.mkdtemp(), 'destination2') |
---|
888 | n/a | shutil.copytree(src_dir, dst_dir, ignore_dangling_symlinks=True) |
---|
889 | n/a | self.assertNotIn('test.txt', os.listdir(dst_dir)) |
---|
890 | n/a | |
---|
891 | n/a | # a dangling symlink is copied if symlinks=True |
---|
892 | n/a | dst_dir = os.path.join(self.mkdtemp(), 'destination3') |
---|
893 | n/a | shutil.copytree(src_dir, dst_dir, symlinks=True) |
---|
894 | n/a | self.assertIn('test.txt', os.listdir(dst_dir)) |
---|
895 | n/a | |
---|
896 | n/a | @support.skip_unless_symlink |
---|
897 | n/a | def test_copytree_symlink_dir(self): |
---|
898 | n/a | src_dir = self.mkdtemp() |
---|
899 | n/a | dst_dir = os.path.join(self.mkdtemp(), 'destination') |
---|
900 | n/a | os.mkdir(os.path.join(src_dir, 'real_dir')) |
---|
901 | n/a | with open(os.path.join(src_dir, 'real_dir', 'test.txt'), 'w'): |
---|
902 | n/a | pass |
---|
903 | n/a | os.symlink(os.path.join(src_dir, 'real_dir'), |
---|
904 | n/a | os.path.join(src_dir, 'link_to_dir'), |
---|
905 | n/a | target_is_directory=True) |
---|
906 | n/a | |
---|
907 | n/a | shutil.copytree(src_dir, dst_dir, symlinks=False) |
---|
908 | n/a | self.assertFalse(os.path.islink(os.path.join(dst_dir, 'link_to_dir'))) |
---|
909 | n/a | self.assertIn('test.txt', os.listdir(os.path.join(dst_dir, 'link_to_dir'))) |
---|
910 | n/a | |
---|
911 | n/a | dst_dir = os.path.join(self.mkdtemp(), 'destination2') |
---|
912 | n/a | shutil.copytree(src_dir, dst_dir, symlinks=True) |
---|
913 | n/a | self.assertTrue(os.path.islink(os.path.join(dst_dir, 'link_to_dir'))) |
---|
914 | n/a | self.assertIn('test.txt', os.listdir(os.path.join(dst_dir, 'link_to_dir'))) |
---|
915 | n/a | |
---|
916 | n/a | def _copy_file(self, method): |
---|
917 | n/a | fname = 'test.txt' |
---|
918 | n/a | tmpdir = self.mkdtemp() |
---|
919 | n/a | write_file((tmpdir, fname), 'xxx') |
---|
920 | n/a | file1 = os.path.join(tmpdir, fname) |
---|
921 | n/a | tmpdir2 = self.mkdtemp() |
---|
922 | n/a | method(file1, tmpdir2) |
---|
923 | n/a | file2 = os.path.join(tmpdir2, fname) |
---|
924 | n/a | return (file1, file2) |
---|
925 | n/a | |
---|
926 | n/a | @unittest.skipUnless(hasattr(os, 'chmod'), 'requires os.chmod') |
---|
927 | n/a | def test_copy(self): |
---|
928 | n/a | # Ensure that the copied file exists and has the same mode bits. |
---|
929 | n/a | file1, file2 = self._copy_file(shutil.copy) |
---|
930 | n/a | self.assertTrue(os.path.exists(file2)) |
---|
931 | n/a | self.assertEqual(os.stat(file1).st_mode, os.stat(file2).st_mode) |
---|
932 | n/a | |
---|
933 | n/a | @unittest.skipUnless(hasattr(os, 'chmod'), 'requires os.chmod') |
---|
934 | n/a | @unittest.skipUnless(hasattr(os, 'utime'), 'requires os.utime') |
---|
935 | n/a | def test_copy2(self): |
---|
936 | n/a | # Ensure that the copied file exists and has the same mode and |
---|
937 | n/a | # modification time bits. |
---|
938 | n/a | file1, file2 = self._copy_file(shutil.copy2) |
---|
939 | n/a | self.assertTrue(os.path.exists(file2)) |
---|
940 | n/a | file1_stat = os.stat(file1) |
---|
941 | n/a | file2_stat = os.stat(file2) |
---|
942 | n/a | self.assertEqual(file1_stat.st_mode, file2_stat.st_mode) |
---|
943 | n/a | for attr in 'st_atime', 'st_mtime': |
---|
944 | n/a | # The modification times may be truncated in the new file. |
---|
945 | n/a | self.assertLessEqual(getattr(file1_stat, attr), |
---|
946 | n/a | getattr(file2_stat, attr) + 1) |
---|
947 | n/a | if hasattr(os, 'chflags') and hasattr(file1_stat, 'st_flags'): |
---|
948 | n/a | self.assertEqual(getattr(file1_stat, 'st_flags'), |
---|
949 | n/a | getattr(file2_stat, 'st_flags')) |
---|
950 | n/a | |
---|
951 | n/a | @support.requires_zlib |
---|
952 | n/a | def test_make_tarball(self): |
---|
953 | n/a | # creating something to tar |
---|
954 | n/a | root_dir, base_dir = self._create_files('') |
---|
955 | n/a | |
---|
956 | n/a | tmpdir2 = self.mkdtemp() |
---|
957 | n/a | # force shutil to create the directory |
---|
958 | n/a | os.rmdir(tmpdir2) |
---|
959 | n/a | # working with relative paths |
---|
960 | n/a | work_dir = os.path.dirname(tmpdir2) |
---|
961 | n/a | rel_base_name = os.path.join(os.path.basename(tmpdir2), 'archive') |
---|
962 | n/a | |
---|
963 | n/a | with support.change_cwd(work_dir): |
---|
964 | n/a | base_name = os.path.abspath(rel_base_name) |
---|
965 | n/a | tarball = make_archive(rel_base_name, 'gztar', root_dir, '.') |
---|
966 | n/a | |
---|
967 | n/a | # check if the compressed tarball was created |
---|
968 | n/a | self.assertEqual(tarball, base_name + '.tar.gz') |
---|
969 | n/a | self.assertTrue(os.path.isfile(tarball)) |
---|
970 | n/a | self.assertTrue(tarfile.is_tarfile(tarball)) |
---|
971 | n/a | with tarfile.open(tarball, 'r:gz') as tf: |
---|
972 | n/a | self.assertCountEqual(tf.getnames(), |
---|
973 | n/a | ['.', './sub', './sub2', |
---|
974 | n/a | './file1', './file2', './sub/file3']) |
---|
975 | n/a | |
---|
976 | n/a | # trying an uncompressed one |
---|
977 | n/a | with support.change_cwd(work_dir): |
---|
978 | n/a | tarball = make_archive(rel_base_name, 'tar', root_dir, '.') |
---|
979 | n/a | self.assertEqual(tarball, base_name + '.tar') |
---|
980 | n/a | self.assertTrue(os.path.isfile(tarball)) |
---|
981 | n/a | self.assertTrue(tarfile.is_tarfile(tarball)) |
---|
982 | n/a | with tarfile.open(tarball, 'r') as tf: |
---|
983 | n/a | self.assertCountEqual(tf.getnames(), |
---|
984 | n/a | ['.', './sub', './sub2', |
---|
985 | n/a | './file1', './file2', './sub/file3']) |
---|
986 | n/a | |
---|
987 | n/a | def _tarinfo(self, path): |
---|
988 | n/a | with tarfile.open(path) as tar: |
---|
989 | n/a | names = tar.getnames() |
---|
990 | n/a | names.sort() |
---|
991 | n/a | return tuple(names) |
---|
992 | n/a | |
---|
993 | n/a | def _create_files(self, base_dir='dist'): |
---|
994 | n/a | # creating something to tar |
---|
995 | n/a | root_dir = self.mkdtemp() |
---|
996 | n/a | dist = os.path.join(root_dir, base_dir) |
---|
997 | n/a | os.makedirs(dist, exist_ok=True) |
---|
998 | n/a | write_file((dist, 'file1'), 'xxx') |
---|
999 | n/a | write_file((dist, 'file2'), 'xxx') |
---|
1000 | n/a | os.mkdir(os.path.join(dist, 'sub')) |
---|
1001 | n/a | write_file((dist, 'sub', 'file3'), 'xxx') |
---|
1002 | n/a | os.mkdir(os.path.join(dist, 'sub2')) |
---|
1003 | n/a | if base_dir: |
---|
1004 | n/a | write_file((root_dir, 'outer'), 'xxx') |
---|
1005 | n/a | return root_dir, base_dir |
---|
1006 | n/a | |
---|
1007 | n/a | @support.requires_zlib |
---|
1008 | n/a | @unittest.skipUnless(shutil.which('tar'), |
---|
1009 | n/a | 'Need the tar command to run') |
---|
1010 | n/a | def test_tarfile_vs_tar(self): |
---|
1011 | n/a | root_dir, base_dir = self._create_files() |
---|
1012 | n/a | base_name = os.path.join(self.mkdtemp(), 'archive') |
---|
1013 | n/a | tarball = make_archive(base_name, 'gztar', root_dir, base_dir) |
---|
1014 | n/a | |
---|
1015 | n/a | # check if the compressed tarball was created |
---|
1016 | n/a | self.assertEqual(tarball, base_name + '.tar.gz') |
---|
1017 | n/a | self.assertTrue(os.path.isfile(tarball)) |
---|
1018 | n/a | |
---|
1019 | n/a | # now create another tarball using `tar` |
---|
1020 | n/a | tarball2 = os.path.join(root_dir, 'archive2.tar') |
---|
1021 | n/a | tar_cmd = ['tar', '-cf', 'archive2.tar', base_dir] |
---|
1022 | n/a | subprocess.check_call(tar_cmd, cwd=root_dir, |
---|
1023 | n/a | stdout=subprocess.DEVNULL) |
---|
1024 | n/a | |
---|
1025 | n/a | self.assertTrue(os.path.isfile(tarball2)) |
---|
1026 | n/a | # let's compare both tarballs |
---|
1027 | n/a | self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2)) |
---|
1028 | n/a | |
---|
1029 | n/a | # trying an uncompressed one |
---|
1030 | n/a | tarball = make_archive(base_name, 'tar', root_dir, base_dir) |
---|
1031 | n/a | self.assertEqual(tarball, base_name + '.tar') |
---|
1032 | n/a | self.assertTrue(os.path.isfile(tarball)) |
---|
1033 | n/a | |
---|
1034 | n/a | # now for a dry_run |
---|
1035 | n/a | tarball = make_archive(base_name, 'tar', root_dir, base_dir, |
---|
1036 | n/a | dry_run=True) |
---|
1037 | n/a | self.assertEqual(tarball, base_name + '.tar') |
---|
1038 | n/a | self.assertTrue(os.path.isfile(tarball)) |
---|
1039 | n/a | |
---|
1040 | n/a | @support.requires_zlib |
---|
1041 | n/a | def test_make_zipfile(self): |
---|
1042 | n/a | # creating something to zip |
---|
1043 | n/a | root_dir, base_dir = self._create_files() |
---|
1044 | n/a | |
---|
1045 | n/a | tmpdir2 = self.mkdtemp() |
---|
1046 | n/a | # force shutil to create the directory |
---|
1047 | n/a | os.rmdir(tmpdir2) |
---|
1048 | n/a | # working with relative paths |
---|
1049 | n/a | work_dir = os.path.dirname(tmpdir2) |
---|
1050 | n/a | rel_base_name = os.path.join(os.path.basename(tmpdir2), 'archive') |
---|
1051 | n/a | |
---|
1052 | n/a | with support.change_cwd(work_dir): |
---|
1053 | n/a | base_name = os.path.abspath(rel_base_name) |
---|
1054 | n/a | res = make_archive(rel_base_name, 'zip', root_dir) |
---|
1055 | n/a | |
---|
1056 | n/a | self.assertEqual(res, base_name + '.zip') |
---|
1057 | n/a | self.assertTrue(os.path.isfile(res)) |
---|
1058 | n/a | self.assertTrue(zipfile.is_zipfile(res)) |
---|
1059 | n/a | with zipfile.ZipFile(res) as zf: |
---|
1060 | n/a | self.assertCountEqual(zf.namelist(), |
---|
1061 | n/a | ['dist/', 'dist/sub/', 'dist/sub2/', |
---|
1062 | n/a | 'dist/file1', 'dist/file2', 'dist/sub/file3', |
---|
1063 | n/a | 'outer']) |
---|
1064 | n/a | |
---|
1065 | n/a | with support.change_cwd(work_dir): |
---|
1066 | n/a | base_name = os.path.abspath(rel_base_name) |
---|
1067 | n/a | res = make_archive(rel_base_name, 'zip', root_dir, base_dir) |
---|
1068 | n/a | |
---|
1069 | n/a | self.assertEqual(res, base_name + '.zip') |
---|
1070 | n/a | self.assertTrue(os.path.isfile(res)) |
---|
1071 | n/a | self.assertTrue(zipfile.is_zipfile(res)) |
---|
1072 | n/a | with zipfile.ZipFile(res) as zf: |
---|
1073 | n/a | self.assertCountEqual(zf.namelist(), |
---|
1074 | n/a | ['dist/', 'dist/sub/', 'dist/sub2/', |
---|
1075 | n/a | 'dist/file1', 'dist/file2', 'dist/sub/file3']) |
---|
1076 | n/a | |
---|
1077 | n/a | @support.requires_zlib |
---|
1078 | n/a | @unittest.skipUnless(shutil.which('zip'), |
---|
1079 | n/a | 'Need the zip command to run') |
---|
1080 | n/a | def test_zipfile_vs_zip(self): |
---|
1081 | n/a | root_dir, base_dir = self._create_files() |
---|
1082 | n/a | base_name = os.path.join(self.mkdtemp(), 'archive') |
---|
1083 | n/a | archive = make_archive(base_name, 'zip', root_dir, base_dir) |
---|
1084 | n/a | |
---|
1085 | n/a | # check if ZIP file was created |
---|
1086 | n/a | self.assertEqual(archive, base_name + '.zip') |
---|
1087 | n/a | self.assertTrue(os.path.isfile(archive)) |
---|
1088 | n/a | |
---|
1089 | n/a | # now create another ZIP file using `zip` |
---|
1090 | n/a | archive2 = os.path.join(root_dir, 'archive2.zip') |
---|
1091 | n/a | zip_cmd = ['zip', '-q', '-r', 'archive2.zip', base_dir] |
---|
1092 | n/a | subprocess.check_call(zip_cmd, cwd=root_dir, |
---|
1093 | n/a | stdout=subprocess.DEVNULL) |
---|
1094 | n/a | |
---|
1095 | n/a | self.assertTrue(os.path.isfile(archive2)) |
---|
1096 | n/a | # let's compare both ZIP files |
---|
1097 | n/a | with zipfile.ZipFile(archive) as zf: |
---|
1098 | n/a | names = zf.namelist() |
---|
1099 | n/a | with zipfile.ZipFile(archive2) as zf: |
---|
1100 | n/a | names2 = zf.namelist() |
---|
1101 | n/a | self.assertEqual(sorted(names), sorted(names2)) |
---|
1102 | n/a | |
---|
1103 | n/a | @support.requires_zlib |
---|
1104 | n/a | @unittest.skipUnless(shutil.which('unzip'), |
---|
1105 | n/a | 'Need the unzip command to run') |
---|
1106 | n/a | def test_unzip_zipfile(self): |
---|
1107 | n/a | root_dir, base_dir = self._create_files() |
---|
1108 | n/a | base_name = os.path.join(self.mkdtemp(), 'archive') |
---|
1109 | n/a | archive = make_archive(base_name, 'zip', root_dir, base_dir) |
---|
1110 | n/a | |
---|
1111 | n/a | # check if ZIP file was created |
---|
1112 | n/a | self.assertEqual(archive, base_name + '.zip') |
---|
1113 | n/a | self.assertTrue(os.path.isfile(archive)) |
---|
1114 | n/a | |
---|
1115 | n/a | # now check the ZIP file using `unzip -t` |
---|
1116 | n/a | zip_cmd = ['unzip', '-t', archive] |
---|
1117 | n/a | with support.change_cwd(root_dir): |
---|
1118 | n/a | try: |
---|
1119 | n/a | subprocess.check_output(zip_cmd, stderr=subprocess.STDOUT) |
---|
1120 | n/a | except subprocess.CalledProcessError as exc: |
---|
1121 | n/a | details = exc.output.decode(errors="replace") |
---|
1122 | n/a | msg = "{}\n\n**Unzip Output**\n{}" |
---|
1123 | n/a | self.fail(msg.format(exc, details)) |
---|
1124 | n/a | |
---|
1125 | n/a | def test_make_archive(self): |
---|
1126 | n/a | tmpdir = self.mkdtemp() |
---|
1127 | n/a | base_name = os.path.join(tmpdir, 'archive') |
---|
1128 | n/a | self.assertRaises(ValueError, make_archive, base_name, 'xxx') |
---|
1129 | n/a | |
---|
1130 | n/a | @support.requires_zlib |
---|
1131 | n/a | def test_make_archive_owner_group(self): |
---|
1132 | n/a | # testing make_archive with owner and group, with various combinations |
---|
1133 | n/a | # this works even if there's not gid/uid support |
---|
1134 | n/a | if UID_GID_SUPPORT: |
---|
1135 | n/a | group = grp.getgrgid(0)[0] |
---|
1136 | n/a | owner = pwd.getpwuid(0)[0] |
---|
1137 | n/a | else: |
---|
1138 | n/a | group = owner = 'root' |
---|
1139 | n/a | |
---|
1140 | n/a | root_dir, base_dir = self._create_files() |
---|
1141 | n/a | base_name = os.path.join(self.mkdtemp(), 'archive') |
---|
1142 | n/a | res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner, |
---|
1143 | n/a | group=group) |
---|
1144 | n/a | self.assertTrue(os.path.isfile(res)) |
---|
1145 | n/a | |
---|
1146 | n/a | res = make_archive(base_name, 'zip', root_dir, base_dir) |
---|
1147 | n/a | self.assertTrue(os.path.isfile(res)) |
---|
1148 | n/a | |
---|
1149 | n/a | res = make_archive(base_name, 'tar', root_dir, base_dir, |
---|
1150 | n/a | owner=owner, group=group) |
---|
1151 | n/a | self.assertTrue(os.path.isfile(res)) |
---|
1152 | n/a | |
---|
1153 | n/a | res = make_archive(base_name, 'tar', root_dir, base_dir, |
---|
1154 | n/a | owner='kjhkjhkjg', group='oihohoh') |
---|
1155 | n/a | self.assertTrue(os.path.isfile(res)) |
---|
1156 | n/a | |
---|
1157 | n/a | |
---|
1158 | n/a | @support.requires_zlib |
---|
1159 | n/a | @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") |
---|
1160 | n/a | def test_tarfile_root_owner(self): |
---|
1161 | n/a | root_dir, base_dir = self._create_files() |
---|
1162 | n/a | base_name = os.path.join(self.mkdtemp(), 'archive') |
---|
1163 | n/a | group = grp.getgrgid(0)[0] |
---|
1164 | n/a | owner = pwd.getpwuid(0)[0] |
---|
1165 | n/a | with support.change_cwd(root_dir): |
---|
1166 | n/a | archive_name = make_archive(base_name, 'gztar', root_dir, 'dist', |
---|
1167 | n/a | owner=owner, group=group) |
---|
1168 | n/a | |
---|
1169 | n/a | # check if the compressed tarball was created |
---|
1170 | n/a | self.assertTrue(os.path.isfile(archive_name)) |
---|
1171 | n/a | |
---|
1172 | n/a | # now checks the rights |
---|
1173 | n/a | archive = tarfile.open(archive_name) |
---|
1174 | n/a | try: |
---|
1175 | n/a | for member in archive.getmembers(): |
---|
1176 | n/a | self.assertEqual(member.uid, 0) |
---|
1177 | n/a | self.assertEqual(member.gid, 0) |
---|
1178 | n/a | finally: |
---|
1179 | n/a | archive.close() |
---|
1180 | n/a | |
---|
1181 | n/a | def test_make_archive_cwd(self): |
---|
1182 | n/a | current_dir = os.getcwd() |
---|
1183 | n/a | def _breaks(*args, **kw): |
---|
1184 | n/a | raise RuntimeError() |
---|
1185 | n/a | |
---|
1186 | n/a | register_archive_format('xxx', _breaks, [], 'xxx file') |
---|
1187 | n/a | try: |
---|
1188 | n/a | try: |
---|
1189 | n/a | make_archive('xxx', 'xxx', root_dir=self.mkdtemp()) |
---|
1190 | n/a | except Exception: |
---|
1191 | n/a | pass |
---|
1192 | n/a | self.assertEqual(os.getcwd(), current_dir) |
---|
1193 | n/a | finally: |
---|
1194 | n/a | unregister_archive_format('xxx') |
---|
1195 | n/a | |
---|
1196 | n/a | def test_make_tarfile_in_curdir(self): |
---|
1197 | n/a | # Issue #21280 |
---|
1198 | n/a | root_dir = self.mkdtemp() |
---|
1199 | n/a | with support.change_cwd(root_dir): |
---|
1200 | n/a | self.assertEqual(make_archive('test', 'tar'), 'test.tar') |
---|
1201 | n/a | self.assertTrue(os.path.isfile('test.tar')) |
---|
1202 | n/a | |
---|
1203 | n/a | @support.requires_zlib |
---|
1204 | n/a | def test_make_zipfile_in_curdir(self): |
---|
1205 | n/a | # Issue #21280 |
---|
1206 | n/a | root_dir = self.mkdtemp() |
---|
1207 | n/a | with support.change_cwd(root_dir): |
---|
1208 | n/a | self.assertEqual(make_archive('test', 'zip'), 'test.zip') |
---|
1209 | n/a | self.assertTrue(os.path.isfile('test.zip')) |
---|
1210 | n/a | |
---|
1211 | n/a | def test_register_archive_format(self): |
---|
1212 | n/a | |
---|
1213 | n/a | self.assertRaises(TypeError, register_archive_format, 'xxx', 1) |
---|
1214 | n/a | self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x, |
---|
1215 | n/a | 1) |
---|
1216 | n/a | self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x, |
---|
1217 | n/a | [(1, 2), (1, 2, 3)]) |
---|
1218 | n/a | |
---|
1219 | n/a | register_archive_format('xxx', lambda: x, [(1, 2)], 'xxx file') |
---|
1220 | n/a | formats = [name for name, params in get_archive_formats()] |
---|
1221 | n/a | self.assertIn('xxx', formats) |
---|
1222 | n/a | |
---|
1223 | n/a | unregister_archive_format('xxx') |
---|
1224 | n/a | formats = [name for name, params in get_archive_formats()] |
---|
1225 | n/a | self.assertNotIn('xxx', formats) |
---|
1226 | n/a | |
---|
1227 | n/a | def check_unpack_archive(self, format): |
---|
1228 | n/a | root_dir, base_dir = self._create_files() |
---|
1229 | n/a | expected = rlistdir(root_dir) |
---|
1230 | n/a | expected.remove('outer') |
---|
1231 | n/a | |
---|
1232 | n/a | base_name = os.path.join(self.mkdtemp(), 'archive') |
---|
1233 | n/a | filename = make_archive(base_name, format, root_dir, base_dir) |
---|
1234 | n/a | |
---|
1235 | n/a | # let's try to unpack it now |
---|
1236 | n/a | tmpdir2 = self.mkdtemp() |
---|
1237 | n/a | unpack_archive(filename, tmpdir2) |
---|
1238 | n/a | self.assertEqual(rlistdir(tmpdir2), expected) |
---|
1239 | n/a | |
---|
1240 | n/a | # and again, this time with the format specified |
---|
1241 | n/a | tmpdir3 = self.mkdtemp() |
---|
1242 | n/a | unpack_archive(filename, tmpdir3, format=format) |
---|
1243 | n/a | self.assertEqual(rlistdir(tmpdir3), expected) |
---|
1244 | n/a | |
---|
1245 | n/a | self.assertRaises(shutil.ReadError, unpack_archive, TESTFN) |
---|
1246 | n/a | self.assertRaises(ValueError, unpack_archive, TESTFN, format='xxx') |
---|
1247 | n/a | |
---|
1248 | n/a | def test_unpack_archive_tar(self): |
---|
1249 | n/a | self.check_unpack_archive('tar') |
---|
1250 | n/a | |
---|
1251 | n/a | @support.requires_zlib |
---|
1252 | n/a | def test_unpack_archive_gztar(self): |
---|
1253 | n/a | self.check_unpack_archive('gztar') |
---|
1254 | n/a | |
---|
1255 | n/a | @support.requires_bz2 |
---|
1256 | n/a | def test_unpack_archive_bztar(self): |
---|
1257 | n/a | self.check_unpack_archive('bztar') |
---|
1258 | n/a | |
---|
1259 | n/a | @support.requires_lzma |
---|
1260 | n/a | def test_unpack_archive_xztar(self): |
---|
1261 | n/a | self.check_unpack_archive('xztar') |
---|
1262 | n/a | |
---|
1263 | n/a | @support.requires_zlib |
---|
1264 | n/a | def test_unpack_archive_zip(self): |
---|
1265 | n/a | self.check_unpack_archive('zip') |
---|
1266 | n/a | |
---|
1267 | n/a | def test_unpack_registry(self): |
---|
1268 | n/a | |
---|
1269 | n/a | formats = get_unpack_formats() |
---|
1270 | n/a | |
---|
1271 | n/a | def _boo(filename, extract_dir, extra): |
---|
1272 | n/a | self.assertEqual(extra, 1) |
---|
1273 | n/a | self.assertEqual(filename, 'stuff.boo') |
---|
1274 | n/a | self.assertEqual(extract_dir, 'xx') |
---|
1275 | n/a | |
---|
1276 | n/a | register_unpack_format('Boo', ['.boo', '.b2'], _boo, [('extra', 1)]) |
---|
1277 | n/a | unpack_archive('stuff.boo', 'xx') |
---|
1278 | n/a | |
---|
1279 | n/a | # trying to register a .boo unpacker again |
---|
1280 | n/a | self.assertRaises(RegistryError, register_unpack_format, 'Boo2', |
---|
1281 | n/a | ['.boo'], _boo) |
---|
1282 | n/a | |
---|
1283 | n/a | # should work now |
---|
1284 | n/a | unregister_unpack_format('Boo') |
---|
1285 | n/a | register_unpack_format('Boo2', ['.boo'], _boo) |
---|
1286 | n/a | self.assertIn(('Boo2', ['.boo'], ''), get_unpack_formats()) |
---|
1287 | n/a | self.assertNotIn(('Boo', ['.boo'], ''), get_unpack_formats()) |
---|
1288 | n/a | |
---|
1289 | n/a | # let's leave a clean state |
---|
1290 | n/a | unregister_unpack_format('Boo2') |
---|
1291 | n/a | self.assertEqual(get_unpack_formats(), formats) |
---|
1292 | n/a | |
---|
1293 | n/a | @unittest.skipUnless(hasattr(shutil, 'disk_usage'), |
---|
1294 | n/a | "disk_usage not available on this platform") |
---|
1295 | n/a | def test_disk_usage(self): |
---|
1296 | n/a | usage = shutil.disk_usage(os.getcwd()) |
---|
1297 | n/a | self.assertGreater(usage.total, 0) |
---|
1298 | n/a | self.assertGreater(usage.used, 0) |
---|
1299 | n/a | self.assertGreaterEqual(usage.free, 0) |
---|
1300 | n/a | self.assertGreaterEqual(usage.total, usage.used) |
---|
1301 | n/a | self.assertGreater(usage.total, usage.free) |
---|
1302 | n/a | |
---|
1303 | n/a | @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") |
---|
1304 | n/a | @unittest.skipUnless(hasattr(os, 'chown'), 'requires os.chown') |
---|
1305 | n/a | def test_chown(self): |
---|
1306 | n/a | |
---|
1307 | n/a | # cleaned-up automatically by TestShutil.tearDown method |
---|
1308 | n/a | dirname = self.mkdtemp() |
---|
1309 | n/a | filename = tempfile.mktemp(dir=dirname) |
---|
1310 | n/a | write_file(filename, 'testing chown function') |
---|
1311 | n/a | |
---|
1312 | n/a | with self.assertRaises(ValueError): |
---|
1313 | n/a | shutil.chown(filename) |
---|
1314 | n/a | |
---|
1315 | n/a | with self.assertRaises(LookupError): |
---|
1316 | n/a | shutil.chown(filename, user='non-existing username') |
---|
1317 | n/a | |
---|
1318 | n/a | with self.assertRaises(LookupError): |
---|
1319 | n/a | shutil.chown(filename, group='non-existing groupname') |
---|
1320 | n/a | |
---|
1321 | n/a | with self.assertRaises(TypeError): |
---|
1322 | n/a | shutil.chown(filename, b'spam') |
---|
1323 | n/a | |
---|
1324 | n/a | with self.assertRaises(TypeError): |
---|
1325 | n/a | shutil.chown(filename, 3.14) |
---|
1326 | n/a | |
---|
1327 | n/a | uid = os.getuid() |
---|
1328 | n/a | gid = os.getgid() |
---|
1329 | n/a | |
---|
1330 | n/a | def check_chown(path, uid=None, gid=None): |
---|
1331 | n/a | s = os.stat(filename) |
---|
1332 | n/a | if uid is not None: |
---|
1333 | n/a | self.assertEqual(uid, s.st_uid) |
---|
1334 | n/a | if gid is not None: |
---|
1335 | n/a | self.assertEqual(gid, s.st_gid) |
---|
1336 | n/a | |
---|
1337 | n/a | shutil.chown(filename, uid, gid) |
---|
1338 | n/a | check_chown(filename, uid, gid) |
---|
1339 | n/a | shutil.chown(filename, uid) |
---|
1340 | n/a | check_chown(filename, uid) |
---|
1341 | n/a | shutil.chown(filename, user=uid) |
---|
1342 | n/a | check_chown(filename, uid) |
---|
1343 | n/a | shutil.chown(filename, group=gid) |
---|
1344 | n/a | check_chown(filename, gid=gid) |
---|
1345 | n/a | |
---|
1346 | n/a | shutil.chown(dirname, uid, gid) |
---|
1347 | n/a | check_chown(dirname, uid, gid) |
---|
1348 | n/a | shutil.chown(dirname, uid) |
---|
1349 | n/a | check_chown(dirname, uid) |
---|
1350 | n/a | shutil.chown(dirname, user=uid) |
---|
1351 | n/a | check_chown(dirname, uid) |
---|
1352 | n/a | shutil.chown(dirname, group=gid) |
---|
1353 | n/a | check_chown(dirname, gid=gid) |
---|
1354 | n/a | |
---|
1355 | n/a | user = pwd.getpwuid(uid)[0] |
---|
1356 | n/a | group = grp.getgrgid(gid)[0] |
---|
1357 | n/a | shutil.chown(filename, user, group) |
---|
1358 | n/a | check_chown(filename, uid, gid) |
---|
1359 | n/a | shutil.chown(dirname, user, group) |
---|
1360 | n/a | check_chown(dirname, uid, gid) |
---|
1361 | n/a | |
---|
1362 | n/a | def test_copy_return_value(self): |
---|
1363 | n/a | # copy and copy2 both return their destination path. |
---|
1364 | n/a | for fn in (shutil.copy, shutil.copy2): |
---|
1365 | n/a | src_dir = self.mkdtemp() |
---|
1366 | n/a | dst_dir = self.mkdtemp() |
---|
1367 | n/a | src = os.path.join(src_dir, 'foo') |
---|
1368 | n/a | write_file(src, 'foo') |
---|
1369 | n/a | rv = fn(src, dst_dir) |
---|
1370 | n/a | self.assertEqual(rv, os.path.join(dst_dir, 'foo')) |
---|
1371 | n/a | rv = fn(src, os.path.join(dst_dir, 'bar')) |
---|
1372 | n/a | self.assertEqual(rv, os.path.join(dst_dir, 'bar')) |
---|
1373 | n/a | |
---|
1374 | n/a | def test_copyfile_return_value(self): |
---|
1375 | n/a | # copytree returns its destination path. |
---|
1376 | n/a | src_dir = self.mkdtemp() |
---|
1377 | n/a | dst_dir = self.mkdtemp() |
---|
1378 | n/a | dst_file = os.path.join(dst_dir, 'bar') |
---|
1379 | n/a | src_file = os.path.join(src_dir, 'foo') |
---|
1380 | n/a | write_file(src_file, 'foo') |
---|
1381 | n/a | rv = shutil.copyfile(src_file, dst_file) |
---|
1382 | n/a | self.assertTrue(os.path.exists(rv)) |
---|
1383 | n/a | self.assertEqual(read_file(src_file), read_file(dst_file)) |
---|
1384 | n/a | |
---|
1385 | n/a | def test_copyfile_same_file(self): |
---|
1386 | n/a | # copyfile() should raise SameFileError if the source and destination |
---|
1387 | n/a | # are the same. |
---|
1388 | n/a | src_dir = self.mkdtemp() |
---|
1389 | n/a | src_file = os.path.join(src_dir, 'foo') |
---|
1390 | n/a | write_file(src_file, 'foo') |
---|
1391 | n/a | self.assertRaises(SameFileError, shutil.copyfile, src_file, src_file) |
---|
1392 | n/a | # But Error should work too, to stay backward compatible. |
---|
1393 | n/a | self.assertRaises(Error, shutil.copyfile, src_file, src_file) |
---|
1394 | n/a | |
---|
1395 | n/a | def test_copytree_return_value(self): |
---|
1396 | n/a | # copytree returns its destination path. |
---|
1397 | n/a | src_dir = self.mkdtemp() |
---|
1398 | n/a | dst_dir = src_dir + "dest" |
---|
1399 | n/a | self.addCleanup(shutil.rmtree, dst_dir, True) |
---|
1400 | n/a | src = os.path.join(src_dir, 'foo') |
---|
1401 | n/a | write_file(src, 'foo') |
---|
1402 | n/a | rv = shutil.copytree(src_dir, dst_dir) |
---|
1403 | n/a | self.assertEqual(['foo'], os.listdir(rv)) |
---|
1404 | n/a | |
---|
1405 | n/a | |
---|
1406 | n/a | class TestWhich(unittest.TestCase): |
---|
1407 | n/a | |
---|
1408 | n/a | def setUp(self): |
---|
1409 | n/a | self.temp_dir = tempfile.mkdtemp(prefix="Tmp") |
---|
1410 | n/a | self.addCleanup(shutil.rmtree, self.temp_dir, True) |
---|
1411 | n/a | # Give the temp_file an ".exe" suffix for all. |
---|
1412 | n/a | # It's needed on Windows and not harmful on other platforms. |
---|
1413 | n/a | self.temp_file = tempfile.NamedTemporaryFile(dir=self.temp_dir, |
---|
1414 | n/a | prefix="Tmp", |
---|
1415 | n/a | suffix=".Exe") |
---|
1416 | n/a | os.chmod(self.temp_file.name, stat.S_IXUSR) |
---|
1417 | n/a | self.addCleanup(self.temp_file.close) |
---|
1418 | n/a | self.dir, self.file = os.path.split(self.temp_file.name) |
---|
1419 | n/a | |
---|
1420 | n/a | def test_basic(self): |
---|
1421 | n/a | # Given an EXE in a directory, it should be returned. |
---|
1422 | n/a | rv = shutil.which(self.file, path=self.dir) |
---|
1423 | n/a | self.assertEqual(rv, self.temp_file.name) |
---|
1424 | n/a | |
---|
1425 | n/a | def test_absolute_cmd(self): |
---|
1426 | n/a | # When given the fully qualified path to an executable that exists, |
---|
1427 | n/a | # it should be returned. |
---|
1428 | n/a | rv = shutil.which(self.temp_file.name, path=self.temp_dir) |
---|
1429 | n/a | self.assertEqual(rv, self.temp_file.name) |
---|
1430 | n/a | |
---|
1431 | n/a | def test_relative_cmd(self): |
---|
1432 | n/a | # When given the relative path with a directory part to an executable |
---|
1433 | n/a | # that exists, it should be returned. |
---|
1434 | n/a | base_dir, tail_dir = os.path.split(self.dir) |
---|
1435 | n/a | relpath = os.path.join(tail_dir, self.file) |
---|
1436 | n/a | with support.change_cwd(path=base_dir): |
---|
1437 | n/a | rv = shutil.which(relpath, path=self.temp_dir) |
---|
1438 | n/a | self.assertEqual(rv, relpath) |
---|
1439 | n/a | # But it shouldn't be searched in PATH directories (issue #16957). |
---|
1440 | n/a | with support.change_cwd(path=self.dir): |
---|
1441 | n/a | rv = shutil.which(relpath, path=base_dir) |
---|
1442 | n/a | self.assertIsNone(rv) |
---|
1443 | n/a | |
---|
1444 | n/a | def test_cwd(self): |
---|
1445 | n/a | # Issue #16957 |
---|
1446 | n/a | base_dir = os.path.dirname(self.dir) |
---|
1447 | n/a | with support.change_cwd(path=self.dir): |
---|
1448 | n/a | rv = shutil.which(self.file, path=base_dir) |
---|
1449 | n/a | if sys.platform == "win32": |
---|
1450 | n/a | # Windows: current directory implicitly on PATH |
---|
1451 | n/a | self.assertEqual(rv, os.path.join(os.curdir, self.file)) |
---|
1452 | n/a | else: |
---|
1453 | n/a | # Other platforms: shouldn't match in the current directory. |
---|
1454 | n/a | self.assertIsNone(rv) |
---|
1455 | n/a | |
---|
1456 | n/a | @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, |
---|
1457 | n/a | 'non-root user required') |
---|
1458 | n/a | def test_non_matching_mode(self): |
---|
1459 | n/a | # Set the file read-only and ask for writeable files. |
---|
1460 | n/a | os.chmod(self.temp_file.name, stat.S_IREAD) |
---|
1461 | n/a | if os.access(self.temp_file.name, os.W_OK): |
---|
1462 | n/a | self.skipTest("can't set the file read-only") |
---|
1463 | n/a | rv = shutil.which(self.file, path=self.dir, mode=os.W_OK) |
---|
1464 | n/a | self.assertIsNone(rv) |
---|
1465 | n/a | |
---|
1466 | n/a | def test_relative_path(self): |
---|
1467 | n/a | base_dir, tail_dir = os.path.split(self.dir) |
---|
1468 | n/a | with support.change_cwd(path=base_dir): |
---|
1469 | n/a | rv = shutil.which(self.file, path=tail_dir) |
---|
1470 | n/a | self.assertEqual(rv, os.path.join(tail_dir, self.file)) |
---|
1471 | n/a | |
---|
1472 | n/a | def test_nonexistent_file(self): |
---|
1473 | n/a | # Return None when no matching executable file is found on the path. |
---|
1474 | n/a | rv = shutil.which("foo.exe", path=self.dir) |
---|
1475 | n/a | self.assertIsNone(rv) |
---|
1476 | n/a | |
---|
1477 | n/a | @unittest.skipUnless(sys.platform == "win32", |
---|
1478 | n/a | "pathext check is Windows-only") |
---|
1479 | n/a | def test_pathext_checking(self): |
---|
1480 | n/a | # Ask for the file without the ".exe" extension, then ensure that |
---|
1481 | n/a | # it gets found properly with the extension. |
---|
1482 | n/a | rv = shutil.which(self.file[:-4], path=self.dir) |
---|
1483 | n/a | self.assertEqual(rv, self.temp_file.name[:-4] + ".EXE") |
---|
1484 | n/a | |
---|
1485 | n/a | def test_environ_path(self): |
---|
1486 | n/a | with support.EnvironmentVarGuard() as env: |
---|
1487 | n/a | env['PATH'] = self.dir |
---|
1488 | n/a | rv = shutil.which(self.file) |
---|
1489 | n/a | self.assertEqual(rv, self.temp_file.name) |
---|
1490 | n/a | |
---|
1491 | n/a | def test_empty_path(self): |
---|
1492 | n/a | base_dir = os.path.dirname(self.dir) |
---|
1493 | n/a | with support.change_cwd(path=self.dir), \ |
---|
1494 | n/a | support.EnvironmentVarGuard() as env: |
---|
1495 | n/a | env['PATH'] = self.dir |
---|
1496 | n/a | rv = shutil.which(self.file, path='') |
---|
1497 | n/a | self.assertIsNone(rv) |
---|
1498 | n/a | |
---|
1499 | n/a | def test_empty_path_no_PATH(self): |
---|
1500 | n/a | with support.EnvironmentVarGuard() as env: |
---|
1501 | n/a | env.pop('PATH', None) |
---|
1502 | n/a | rv = shutil.which(self.file) |
---|
1503 | n/a | self.assertIsNone(rv) |
---|
1504 | n/a | |
---|
1505 | n/a | |
---|
1506 | n/a | class TestMove(unittest.TestCase): |
---|
1507 | n/a | |
---|
1508 | n/a | def setUp(self): |
---|
1509 | n/a | filename = "foo" |
---|
1510 | n/a | self.src_dir = tempfile.mkdtemp() |
---|
1511 | n/a | self.dst_dir = tempfile.mkdtemp() |
---|
1512 | n/a | self.src_file = os.path.join(self.src_dir, filename) |
---|
1513 | n/a | self.dst_file = os.path.join(self.dst_dir, filename) |
---|
1514 | n/a | with open(self.src_file, "wb") as f: |
---|
1515 | n/a | f.write(b"spam") |
---|
1516 | n/a | |
---|
1517 | n/a | def tearDown(self): |
---|
1518 | n/a | for d in (self.src_dir, self.dst_dir): |
---|
1519 | n/a | try: |
---|
1520 | n/a | if d: |
---|
1521 | n/a | shutil.rmtree(d) |
---|
1522 | n/a | except: |
---|
1523 | n/a | pass |
---|
1524 | n/a | |
---|
1525 | n/a | def _check_move_file(self, src, dst, real_dst): |
---|
1526 | n/a | with open(src, "rb") as f: |
---|
1527 | n/a | contents = f.read() |
---|
1528 | n/a | shutil.move(src, dst) |
---|
1529 | n/a | with open(real_dst, "rb") as f: |
---|
1530 | n/a | self.assertEqual(contents, f.read()) |
---|
1531 | n/a | self.assertFalse(os.path.exists(src)) |
---|
1532 | n/a | |
---|
1533 | n/a | def _check_move_dir(self, src, dst, real_dst): |
---|
1534 | n/a | contents = sorted(os.listdir(src)) |
---|
1535 | n/a | shutil.move(src, dst) |
---|
1536 | n/a | self.assertEqual(contents, sorted(os.listdir(real_dst))) |
---|
1537 | n/a | self.assertFalse(os.path.exists(src)) |
---|
1538 | n/a | |
---|
1539 | n/a | def test_move_file(self): |
---|
1540 | n/a | # Move a file to another location on the same filesystem. |
---|
1541 | n/a | self._check_move_file(self.src_file, self.dst_file, self.dst_file) |
---|
1542 | n/a | |
---|
1543 | n/a | def test_move_file_to_dir(self): |
---|
1544 | n/a | # Move a file inside an existing dir on the same filesystem. |
---|
1545 | n/a | self._check_move_file(self.src_file, self.dst_dir, self.dst_file) |
---|
1546 | n/a | |
---|
1547 | n/a | @mock_rename |
---|
1548 | n/a | def test_move_file_other_fs(self): |
---|
1549 | n/a | # Move a file to an existing dir on another filesystem. |
---|
1550 | n/a | self.test_move_file() |
---|
1551 | n/a | |
---|
1552 | n/a | @mock_rename |
---|
1553 | n/a | def test_move_file_to_dir_other_fs(self): |
---|
1554 | n/a | # Move a file to another location on another filesystem. |
---|
1555 | n/a | self.test_move_file_to_dir() |
---|
1556 | n/a | |
---|
1557 | n/a | def test_move_dir(self): |
---|
1558 | n/a | # Move a dir to another location on the same filesystem. |
---|
1559 | n/a | dst_dir = tempfile.mktemp() |
---|
1560 | n/a | try: |
---|
1561 | n/a | self._check_move_dir(self.src_dir, dst_dir, dst_dir) |
---|
1562 | n/a | finally: |
---|
1563 | n/a | try: |
---|
1564 | n/a | shutil.rmtree(dst_dir) |
---|
1565 | n/a | except: |
---|
1566 | n/a | pass |
---|
1567 | n/a | |
---|
1568 | n/a | @mock_rename |
---|
1569 | n/a | def test_move_dir_other_fs(self): |
---|
1570 | n/a | # Move a dir to another location on another filesystem. |
---|
1571 | n/a | self.test_move_dir() |
---|
1572 | n/a | |
---|
1573 | n/a | def test_move_dir_to_dir(self): |
---|
1574 | n/a | # Move a dir inside an existing dir on the same filesystem. |
---|
1575 | n/a | self._check_move_dir(self.src_dir, self.dst_dir, |
---|
1576 | n/a | os.path.join(self.dst_dir, os.path.basename(self.src_dir))) |
---|
1577 | n/a | |
---|
1578 | n/a | @mock_rename |
---|
1579 | n/a | def test_move_dir_to_dir_other_fs(self): |
---|
1580 | n/a | # Move a dir inside an existing dir on another filesystem. |
---|
1581 | n/a | self.test_move_dir_to_dir() |
---|
1582 | n/a | |
---|
1583 | n/a | def test_move_dir_sep_to_dir(self): |
---|
1584 | n/a | self._check_move_dir(self.src_dir + os.path.sep, self.dst_dir, |
---|
1585 | n/a | os.path.join(self.dst_dir, os.path.basename(self.src_dir))) |
---|
1586 | n/a | |
---|
1587 | n/a | @unittest.skipUnless(os.path.altsep, 'requires os.path.altsep') |
---|
1588 | n/a | def test_move_dir_altsep_to_dir(self): |
---|
1589 | n/a | self._check_move_dir(self.src_dir + os.path.altsep, self.dst_dir, |
---|
1590 | n/a | os.path.join(self.dst_dir, os.path.basename(self.src_dir))) |
---|
1591 | n/a | |
---|
1592 | n/a | def test_existing_file_inside_dest_dir(self): |
---|
1593 | n/a | # A file with the same name inside the destination dir already exists. |
---|
1594 | n/a | with open(self.dst_file, "wb"): |
---|
1595 | n/a | pass |
---|
1596 | n/a | self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir) |
---|
1597 | n/a | |
---|
1598 | n/a | def test_dont_move_dir_in_itself(self): |
---|
1599 | n/a | # Moving a dir inside itself raises an Error. |
---|
1600 | n/a | dst = os.path.join(self.src_dir, "bar") |
---|
1601 | n/a | self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst) |
---|
1602 | n/a | |
---|
1603 | n/a | def test_destinsrc_false_negative(self): |
---|
1604 | n/a | os.mkdir(TESTFN) |
---|
1605 | n/a | try: |
---|
1606 | n/a | for src, dst in [('srcdir', 'srcdir/dest')]: |
---|
1607 | n/a | src = os.path.join(TESTFN, src) |
---|
1608 | n/a | dst = os.path.join(TESTFN, dst) |
---|
1609 | n/a | self.assertTrue(shutil._destinsrc(src, dst), |
---|
1610 | n/a | msg='_destinsrc() wrongly concluded that ' |
---|
1611 | n/a | 'dst (%s) is not in src (%s)' % (dst, src)) |
---|
1612 | n/a | finally: |
---|
1613 | n/a | shutil.rmtree(TESTFN, ignore_errors=True) |
---|
1614 | n/a | |
---|
1615 | n/a | def test_destinsrc_false_positive(self): |
---|
1616 | n/a | os.mkdir(TESTFN) |
---|
1617 | n/a | try: |
---|
1618 | n/a | for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]: |
---|
1619 | n/a | src = os.path.join(TESTFN, src) |
---|
1620 | n/a | dst = os.path.join(TESTFN, dst) |
---|
1621 | n/a | self.assertFalse(shutil._destinsrc(src, dst), |
---|
1622 | n/a | msg='_destinsrc() wrongly concluded that ' |
---|
1623 | n/a | 'dst (%s) is in src (%s)' % (dst, src)) |
---|
1624 | n/a | finally: |
---|
1625 | n/a | shutil.rmtree(TESTFN, ignore_errors=True) |
---|
1626 | n/a | |
---|
1627 | n/a | @support.skip_unless_symlink |
---|
1628 | n/a | @mock_rename |
---|
1629 | n/a | def test_move_file_symlink(self): |
---|
1630 | n/a | dst = os.path.join(self.src_dir, 'bar') |
---|
1631 | n/a | os.symlink(self.src_file, dst) |
---|
1632 | n/a | shutil.move(dst, self.dst_file) |
---|
1633 | n/a | self.assertTrue(os.path.islink(self.dst_file)) |
---|
1634 | n/a | self.assertTrue(os.path.samefile(self.src_file, self.dst_file)) |
---|
1635 | n/a | |
---|
1636 | n/a | @support.skip_unless_symlink |
---|
1637 | n/a | @mock_rename |
---|
1638 | n/a | def test_move_file_symlink_to_dir(self): |
---|
1639 | n/a | filename = "bar" |
---|
1640 | n/a | dst = os.path.join(self.src_dir, filename) |
---|
1641 | n/a | os.symlink(self.src_file, dst) |
---|
1642 | n/a | shutil.move(dst, self.dst_dir) |
---|
1643 | n/a | final_link = os.path.join(self.dst_dir, filename) |
---|
1644 | n/a | self.assertTrue(os.path.islink(final_link)) |
---|
1645 | n/a | self.assertTrue(os.path.samefile(self.src_file, final_link)) |
---|
1646 | n/a | |
---|
1647 | n/a | @support.skip_unless_symlink |
---|
1648 | n/a | @mock_rename |
---|
1649 | n/a | def test_move_dangling_symlink(self): |
---|
1650 | n/a | src = os.path.join(self.src_dir, 'baz') |
---|
1651 | n/a | dst = os.path.join(self.src_dir, 'bar') |
---|
1652 | n/a | os.symlink(src, dst) |
---|
1653 | n/a | dst_link = os.path.join(self.dst_dir, 'quux') |
---|
1654 | n/a | shutil.move(dst, dst_link) |
---|
1655 | n/a | self.assertTrue(os.path.islink(dst_link)) |
---|
1656 | n/a | # On Windows, os.path.realpath does not follow symlinks (issue #9949) |
---|
1657 | n/a | if os.name == 'nt': |
---|
1658 | n/a | self.assertEqual(os.path.realpath(src), os.readlink(dst_link)) |
---|
1659 | n/a | else: |
---|
1660 | n/a | self.assertEqual(os.path.realpath(src), os.path.realpath(dst_link)) |
---|
1661 | n/a | |
---|
1662 | n/a | @support.skip_unless_symlink |
---|
1663 | n/a | @mock_rename |
---|
1664 | n/a | def test_move_dir_symlink(self): |
---|
1665 | n/a | src = os.path.join(self.src_dir, 'baz') |
---|
1666 | n/a | dst = os.path.join(self.src_dir, 'bar') |
---|
1667 | n/a | os.mkdir(src) |
---|
1668 | n/a | os.symlink(src, dst) |
---|
1669 | n/a | dst_link = os.path.join(self.dst_dir, 'quux') |
---|
1670 | n/a | shutil.move(dst, dst_link) |
---|
1671 | n/a | self.assertTrue(os.path.islink(dst_link)) |
---|
1672 | n/a | self.assertTrue(os.path.samefile(src, dst_link)) |
---|
1673 | n/a | |
---|
1674 | n/a | def test_move_return_value(self): |
---|
1675 | n/a | rv = shutil.move(self.src_file, self.dst_dir) |
---|
1676 | n/a | self.assertEqual(rv, |
---|
1677 | n/a | os.path.join(self.dst_dir, os.path.basename(self.src_file))) |
---|
1678 | n/a | |
---|
1679 | n/a | def test_move_as_rename_return_value(self): |
---|
1680 | n/a | rv = shutil.move(self.src_file, os.path.join(self.dst_dir, 'bar')) |
---|
1681 | n/a | self.assertEqual(rv, os.path.join(self.dst_dir, 'bar')) |
---|
1682 | n/a | |
---|
1683 | n/a | @mock_rename |
---|
1684 | n/a | def test_move_file_special_function(self): |
---|
1685 | n/a | moved = [] |
---|
1686 | n/a | def _copy(src, dst): |
---|
1687 | n/a | moved.append((src, dst)) |
---|
1688 | n/a | shutil.move(self.src_file, self.dst_dir, copy_function=_copy) |
---|
1689 | n/a | self.assertEqual(len(moved), 1) |
---|
1690 | n/a | |
---|
1691 | n/a | @mock_rename |
---|
1692 | n/a | def test_move_dir_special_function(self): |
---|
1693 | n/a | moved = [] |
---|
1694 | n/a | def _copy(src, dst): |
---|
1695 | n/a | moved.append((src, dst)) |
---|
1696 | n/a | support.create_empty_file(os.path.join(self.src_dir, 'child')) |
---|
1697 | n/a | support.create_empty_file(os.path.join(self.src_dir, 'child1')) |
---|
1698 | n/a | shutil.move(self.src_dir, self.dst_dir, copy_function=_copy) |
---|
1699 | n/a | self.assertEqual(len(moved), 3) |
---|
1700 | n/a | |
---|
1701 | n/a | |
---|
1702 | n/a | class TestCopyFile(unittest.TestCase): |
---|
1703 | n/a | |
---|
1704 | n/a | _delete = False |
---|
1705 | n/a | |
---|
1706 | n/a | class Faux(object): |
---|
1707 | n/a | _entered = False |
---|
1708 | n/a | _exited_with = None |
---|
1709 | n/a | _raised = False |
---|
1710 | n/a | def __init__(self, raise_in_exit=False, suppress_at_exit=True): |
---|
1711 | n/a | self._raise_in_exit = raise_in_exit |
---|
1712 | n/a | self._suppress_at_exit = suppress_at_exit |
---|
1713 | n/a | def read(self, *args): |
---|
1714 | n/a | return '' |
---|
1715 | n/a | def __enter__(self): |
---|
1716 | n/a | self._entered = True |
---|
1717 | n/a | def __exit__(self, exc_type, exc_val, exc_tb): |
---|
1718 | n/a | self._exited_with = exc_type, exc_val, exc_tb |
---|
1719 | n/a | if self._raise_in_exit: |
---|
1720 | n/a | self._raised = True |
---|
1721 | n/a | raise OSError("Cannot close") |
---|
1722 | n/a | return self._suppress_at_exit |
---|
1723 | n/a | |
---|
1724 | n/a | def tearDown(self): |
---|
1725 | n/a | if self._delete: |
---|
1726 | n/a | del shutil.open |
---|
1727 | n/a | |
---|
1728 | n/a | def _set_shutil_open(self, func): |
---|
1729 | n/a | shutil.open = func |
---|
1730 | n/a | self._delete = True |
---|
1731 | n/a | |
---|
1732 | n/a | def test_w_source_open_fails(self): |
---|
1733 | n/a | def _open(filename, mode='r'): |
---|
1734 | n/a | if filename == 'srcfile': |
---|
1735 | n/a | raise OSError('Cannot open "srcfile"') |
---|
1736 | n/a | assert 0 # shouldn't reach here. |
---|
1737 | n/a | |
---|
1738 | n/a | self._set_shutil_open(_open) |
---|
1739 | n/a | |
---|
1740 | n/a | self.assertRaises(OSError, shutil.copyfile, 'srcfile', 'destfile') |
---|
1741 | n/a | |
---|
1742 | n/a | def test_w_dest_open_fails(self): |
---|
1743 | n/a | |
---|
1744 | n/a | srcfile = self.Faux() |
---|
1745 | n/a | |
---|
1746 | n/a | def _open(filename, mode='r'): |
---|
1747 | n/a | if filename == 'srcfile': |
---|
1748 | n/a | return srcfile |
---|
1749 | n/a | if filename == 'destfile': |
---|
1750 | n/a | raise OSError('Cannot open "destfile"') |
---|
1751 | n/a | assert 0 # shouldn't reach here. |
---|
1752 | n/a | |
---|
1753 | n/a | self._set_shutil_open(_open) |
---|
1754 | n/a | |
---|
1755 | n/a | shutil.copyfile('srcfile', 'destfile') |
---|
1756 | n/a | self.assertTrue(srcfile._entered) |
---|
1757 | n/a | self.assertTrue(srcfile._exited_with[0] is OSError) |
---|
1758 | n/a | self.assertEqual(srcfile._exited_with[1].args, |
---|
1759 | n/a | ('Cannot open "destfile"',)) |
---|
1760 | n/a | |
---|
1761 | n/a | def test_w_dest_close_fails(self): |
---|
1762 | n/a | |
---|
1763 | n/a | srcfile = self.Faux() |
---|
1764 | n/a | destfile = self.Faux(True) |
---|
1765 | n/a | |
---|
1766 | n/a | def _open(filename, mode='r'): |
---|
1767 | n/a | if filename == 'srcfile': |
---|
1768 | n/a | return srcfile |
---|
1769 | n/a | if filename == 'destfile': |
---|
1770 | n/a | return destfile |
---|
1771 | n/a | assert 0 # shouldn't reach here. |
---|
1772 | n/a | |
---|
1773 | n/a | self._set_shutil_open(_open) |
---|
1774 | n/a | |
---|
1775 | n/a | shutil.copyfile('srcfile', 'destfile') |
---|
1776 | n/a | self.assertTrue(srcfile._entered) |
---|
1777 | n/a | self.assertTrue(destfile._entered) |
---|
1778 | n/a | self.assertTrue(destfile._raised) |
---|
1779 | n/a | self.assertTrue(srcfile._exited_with[0] is OSError) |
---|
1780 | n/a | self.assertEqual(srcfile._exited_with[1].args, |
---|
1781 | n/a | ('Cannot close',)) |
---|
1782 | n/a | |
---|
1783 | n/a | def test_w_source_close_fails(self): |
---|
1784 | n/a | |
---|
1785 | n/a | srcfile = self.Faux(True) |
---|
1786 | n/a | destfile = self.Faux() |
---|
1787 | n/a | |
---|
1788 | n/a | def _open(filename, mode='r'): |
---|
1789 | n/a | if filename == 'srcfile': |
---|
1790 | n/a | return srcfile |
---|
1791 | n/a | if filename == 'destfile': |
---|
1792 | n/a | return destfile |
---|
1793 | n/a | assert 0 # shouldn't reach here. |
---|
1794 | n/a | |
---|
1795 | n/a | self._set_shutil_open(_open) |
---|
1796 | n/a | |
---|
1797 | n/a | self.assertRaises(OSError, |
---|
1798 | n/a | shutil.copyfile, 'srcfile', 'destfile') |
---|
1799 | n/a | self.assertTrue(srcfile._entered) |
---|
1800 | n/a | self.assertTrue(destfile._entered) |
---|
1801 | n/a | self.assertFalse(destfile._raised) |
---|
1802 | n/a | self.assertTrue(srcfile._exited_with[0] is None) |
---|
1803 | n/a | self.assertTrue(srcfile._raised) |
---|
1804 | n/a | |
---|
1805 | n/a | def test_move_dir_caseinsensitive(self): |
---|
1806 | n/a | # Renames a folder to the same name |
---|
1807 | n/a | # but a different case. |
---|
1808 | n/a | |
---|
1809 | n/a | self.src_dir = tempfile.mkdtemp() |
---|
1810 | n/a | self.addCleanup(shutil.rmtree, self.src_dir, True) |
---|
1811 | n/a | dst_dir = os.path.join( |
---|
1812 | n/a | os.path.dirname(self.src_dir), |
---|
1813 | n/a | os.path.basename(self.src_dir).upper()) |
---|
1814 | n/a | self.assertNotEqual(self.src_dir, dst_dir) |
---|
1815 | n/a | |
---|
1816 | n/a | try: |
---|
1817 | n/a | shutil.move(self.src_dir, dst_dir) |
---|
1818 | n/a | self.assertTrue(os.path.isdir(dst_dir)) |
---|
1819 | n/a | finally: |
---|
1820 | n/a | os.rmdir(dst_dir) |
---|
1821 | n/a | |
---|
1822 | n/a | class TermsizeTests(unittest.TestCase): |
---|
1823 | n/a | def test_does_not_crash(self): |
---|
1824 | n/a | """Check if get_terminal_size() returns a meaningful value. |
---|
1825 | n/a | |
---|
1826 | n/a | There's no easy portable way to actually check the size of the |
---|
1827 | n/a | terminal, so let's check if it returns something sensible instead. |
---|
1828 | n/a | """ |
---|
1829 | n/a | size = shutil.get_terminal_size() |
---|
1830 | n/a | self.assertGreaterEqual(size.columns, 0) |
---|
1831 | n/a | self.assertGreaterEqual(size.lines, 0) |
---|
1832 | n/a | |
---|
1833 | n/a | def test_os_environ_first(self): |
---|
1834 | n/a | "Check if environment variables have precedence" |
---|
1835 | n/a | |
---|
1836 | n/a | with support.EnvironmentVarGuard() as env: |
---|
1837 | n/a | env['COLUMNS'] = '777' |
---|
1838 | n/a | del env['LINES'] |
---|
1839 | n/a | size = shutil.get_terminal_size() |
---|
1840 | n/a | self.assertEqual(size.columns, 777) |
---|
1841 | n/a | |
---|
1842 | n/a | with support.EnvironmentVarGuard() as env: |
---|
1843 | n/a | del env['COLUMNS'] |
---|
1844 | n/a | env['LINES'] = '888' |
---|
1845 | n/a | size = shutil.get_terminal_size() |
---|
1846 | n/a | self.assertEqual(size.lines, 888) |
---|
1847 | n/a | |
---|
1848 | n/a | def test_bad_environ(self): |
---|
1849 | n/a | with support.EnvironmentVarGuard() as env: |
---|
1850 | n/a | env['COLUMNS'] = 'xxx' |
---|
1851 | n/a | env['LINES'] = 'yyy' |
---|
1852 | n/a | size = shutil.get_terminal_size() |
---|
1853 | n/a | self.assertGreaterEqual(size.columns, 0) |
---|
1854 | n/a | self.assertGreaterEqual(size.lines, 0) |
---|
1855 | n/a | |
---|
1856 | n/a | @unittest.skipUnless(os.isatty(sys.__stdout__.fileno()), "not on tty") |
---|
1857 | n/a | @unittest.skipUnless(hasattr(os, 'get_terminal_size'), |
---|
1858 | n/a | 'need os.get_terminal_size()') |
---|
1859 | n/a | def test_stty_match(self): |
---|
1860 | n/a | """Check if stty returns the same results ignoring env |
---|
1861 | n/a | |
---|
1862 | n/a | This test will fail if stdin and stdout are connected to |
---|
1863 | n/a | different terminals with different sizes. Nevertheless, such |
---|
1864 | n/a | situations should be pretty rare. |
---|
1865 | n/a | """ |
---|
1866 | n/a | try: |
---|
1867 | n/a | size = subprocess.check_output(['stty', 'size']).decode().split() |
---|
1868 | n/a | except (FileNotFoundError, PermissionError, |
---|
1869 | n/a | subprocess.CalledProcessError): |
---|
1870 | n/a | self.skipTest("stty invocation failed") |
---|
1871 | n/a | expected = (int(size[1]), int(size[0])) # reversed order |
---|
1872 | n/a | |
---|
1873 | n/a | with support.EnvironmentVarGuard() as env: |
---|
1874 | n/a | del env['LINES'] |
---|
1875 | n/a | del env['COLUMNS'] |
---|
1876 | n/a | actual = shutil.get_terminal_size() |
---|
1877 | n/a | |
---|
1878 | n/a | self.assertEqual(expected, actual) |
---|
1879 | n/a | |
---|
1880 | n/a | def test_fallback(self): |
---|
1881 | n/a | with support.EnvironmentVarGuard() as env: |
---|
1882 | n/a | del env['LINES'] |
---|
1883 | n/a | del env['COLUMNS'] |
---|
1884 | n/a | |
---|
1885 | n/a | # sys.__stdout__ has no fileno() |
---|
1886 | n/a | with support.swap_attr(sys, '__stdout__', None): |
---|
1887 | n/a | size = shutil.get_terminal_size(fallback=(10, 20)) |
---|
1888 | n/a | self.assertEqual(size.columns, 10) |
---|
1889 | n/a | self.assertEqual(size.lines, 20) |
---|
1890 | n/a | |
---|
1891 | n/a | # sys.__stdout__ is not a terminal on Unix |
---|
1892 | n/a | # or fileno() not in (0, 1, 2) on Windows |
---|
1893 | n/a | with open(os.devnull, 'w') as f, \ |
---|
1894 | n/a | support.swap_attr(sys, '__stdout__', f): |
---|
1895 | n/a | size = shutil.get_terminal_size(fallback=(30, 40)) |
---|
1896 | n/a | self.assertEqual(size.columns, 30) |
---|
1897 | n/a | self.assertEqual(size.lines, 40) |
---|
1898 | n/a | |
---|
1899 | n/a | |
---|
1900 | n/a | class PublicAPITests(unittest.TestCase): |
---|
1901 | n/a | """Ensures that the correct values are exposed in the public API.""" |
---|
1902 | n/a | |
---|
1903 | n/a | def test_module_all_attribute(self): |
---|
1904 | n/a | self.assertTrue(hasattr(shutil, '__all__')) |
---|
1905 | n/a | target_api = ['copyfileobj', 'copyfile', 'copymode', 'copystat', |
---|
1906 | n/a | 'copy', 'copy2', 'copytree', 'move', 'rmtree', 'Error', |
---|
1907 | n/a | 'SpecialFileError', 'ExecError', 'make_archive', |
---|
1908 | n/a | 'get_archive_formats', 'register_archive_format', |
---|
1909 | n/a | 'unregister_archive_format', 'get_unpack_formats', |
---|
1910 | n/a | 'register_unpack_format', 'unregister_unpack_format', |
---|
1911 | n/a | 'unpack_archive', 'ignore_patterns', 'chown', 'which', |
---|
1912 | n/a | 'get_terminal_size', 'SameFileError'] |
---|
1913 | n/a | if hasattr(os, 'statvfs') or os.name == 'nt': |
---|
1914 | n/a | target_api.append('disk_usage') |
---|
1915 | n/a | self.assertEqual(set(shutil.__all__), set(target_api)) |
---|
1916 | n/a | |
---|
1917 | n/a | |
---|
1918 | n/a | if __name__ == '__main__': |
---|
1919 | n/a | unittest.main() |
---|