annotate Lib/test/test_shutil.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 409448d455e3
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27360
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
1 # Copyright (C) 2003 Python Software Foundation
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
2
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
3 import unittest
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
4 import shutil
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
5 import tempfile
34019
bdd14ab6c5cf Bug #1071513: don't test on Cygwin, as chmod doesn't work reliably there
Johannes Gijsbers <jlg@dds.nl>
parents: 33870
diff changeset
6 import sys
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
7 import stat
32386
686e5d38be42 shutil.move() will raise an exception when trying to move a directory into
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
8 import os
686e5d38be42 shutil.move() will raise an exception when trying to move a directory into
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
9 import os.path
27360
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
10 from test import test_support
33124
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
11 from test.test_support import TESTFN
53652
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
12 TESTFN2 = TESTFN + "2"
27360
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
13
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
14 class TestShutil(unittest.TestCase):
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
15 def test_rmtree_errors(self):
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
16 # filename is guaranteed not to exist
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
17 filename = tempfile.mktemp()
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
18 self.assertRaises(OSError, shutil.rmtree, filename)
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
19
34119
60d482e8f845 SF bug #1076467: don't run test_on_error as root, as the permission
Johannes Gijsbers <jlg@dds.nl>
parents: 34019
diff changeset
20 # See bug #1071513 for why we don't run this on cygwin
60d482e8f845 SF bug #1076467: don't run test_on_error as root, as the permission
Johannes Gijsbers <jlg@dds.nl>
parents: 34019
diff changeset
21 # and bug #1076467 for why we don't run this as root.
60d482e8f845 SF bug #1076467: don't run test_on_error as root, as the permission
Johannes Gijsbers <jlg@dds.nl>
parents: 34019
diff changeset
22 if (hasattr(os, 'chmod') and sys.platform[:6] != 'cygwin'
34149
a659b0cecb92 Use os.geteuid() for checking whether we are root, as suggested by
Johannes Gijsbers <jlg@dds.nl>
parents: 34119
diff changeset
23 and not (hasattr(os, 'geteuid') and os.geteuid() == 0)):
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
24 def test_on_error(self):
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
25 self.errorState = 0
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
26 os.mkdir(TESTFN)
33870
80ca1a8b2321 test_on_error(): Rewrite so it works on WinXP too. Unsure about 95/98/ME.
Tim Peters <tim.peters@gmail.com>
parents: 33865
diff changeset
27 self.childpath = os.path.join(TESTFN, 'a')
80ca1a8b2321 test_on_error(): Rewrite so it works on WinXP too. Unsure about 95/98/ME.
Tim Peters <tim.peters@gmail.com>
parents: 33865
diff changeset
28 f = open(self.childpath, 'w')
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
29 f.close()
33870
80ca1a8b2321 test_on_error(): Rewrite so it works on WinXP too. Unsure about 95/98/ME.
Tim Peters <tim.peters@gmail.com>
parents: 33865
diff changeset
30 old_dir_mode = os.stat(TESTFN).st_mode
80ca1a8b2321 test_on_error(): Rewrite so it works on WinXP too. Unsure about 95/98/ME.
Tim Peters <tim.peters@gmail.com>
parents: 33865
diff changeset
31 old_child_mode = os.stat(self.childpath).st_mode
80ca1a8b2321 test_on_error(): Rewrite so it works on WinXP too. Unsure about 95/98/ME.
Tim Peters <tim.peters@gmail.com>
parents: 33865
diff changeset
32 # Make unwritable.
80ca1a8b2321 test_on_error(): Rewrite so it works on WinXP too. Unsure about 95/98/ME.
Tim Peters <tim.peters@gmail.com>
parents: 33865
diff changeset
33 os.chmod(self.childpath, stat.S_IREAD)
80ca1a8b2321 test_on_error(): Rewrite so it works on WinXP too. Unsure about 95/98/ME.
Tim Peters <tim.peters@gmail.com>
parents: 33865
diff changeset
34 os.chmod(TESTFN, stat.S_IREAD)
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
35
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
36 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
34019
bdd14ab6c5cf Bug #1071513: don't test on Cygwin, as chmod doesn't work reliably there
Johannes Gijsbers <jlg@dds.nl>
parents: 33870
diff changeset
37 # Test whether onerror has actually been called.
34119
60d482e8f845 SF bug #1076467: don't run test_on_error as root, as the permission
Johannes Gijsbers <jlg@dds.nl>
parents: 34019
diff changeset
38 self.assertEqual(self.errorState, 2,
60d482e8f845 SF bug #1076467: don't run test_on_error as root, as the permission
Johannes Gijsbers <jlg@dds.nl>
parents: 34019
diff changeset
39 "Expected call to onerror function did not happen.")
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
40
33870
80ca1a8b2321 test_on_error(): Rewrite so it works on WinXP too. Unsure about 95/98/ME.
Tim Peters <tim.peters@gmail.com>
parents: 33865
diff changeset
41 # Make writable again.
80ca1a8b2321 test_on_error(): Rewrite so it works on WinXP too. Unsure about 95/98/ME.
Tim Peters <tim.peters@gmail.com>
parents: 33865
diff changeset
42 os.chmod(TESTFN, old_dir_mode)
80ca1a8b2321 test_on_error(): Rewrite so it works on WinXP too. Unsure about 95/98/ME.
Tim Peters <tim.peters@gmail.com>
parents: 33865
diff changeset
43 os.chmod(self.childpath, old_child_mode)
80ca1a8b2321 test_on_error(): Rewrite so it works on WinXP too. Unsure about 95/98/ME.
Tim Peters <tim.peters@gmail.com>
parents: 33865
diff changeset
44
80ca1a8b2321 test_on_error(): Rewrite so it works on WinXP too. Unsure about 95/98/ME.
Tim Peters <tim.peters@gmail.com>
parents: 33865
diff changeset
45 # Clean up.
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
46 shutil.rmtree(TESTFN)
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
47
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
48 def check_args_to_onerror(self, func, arg, exc):
53623
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
49 # test_rmtree_errors deliberately runs rmtree
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
50 # on a directory that is chmod 400, which will fail.
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
51 # This function is run when shutil.rmtree fails.
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
52 # 99.9% of the time it initially fails to remove
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
53 # a file in the directory, so the first time through
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
54 # func is os.remove.
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
55 # However, some Linux machines running ZFS on
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
56 # FUSE experienced a failure earlier in the process
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
57 # at os.listdir. The first failure may legally
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
58 # be either.
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
59 if self.errorState == 0:
53623
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
60 if func is os.remove:
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
61 self.assertEqual(arg, self.childpath)
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
62 else:
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
63 self.assertIs(func, os.listdir,
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
64 "func must be either os.remove or os.listdir")
27c6b63d1db3 fix test_shutil on ZFS #5676
Benjamin Peterson <benjamin@python.org>
parents: 51446
diff changeset
65 self.assertEqual(arg, TESTFN)
38112
be49645e3984 Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents: 34149
diff changeset
66 self.failUnless(issubclass(exc[0], OSError))
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
67 self.errorState = 1
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
68 else:
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
69 self.assertEqual(func, os.rmdir)
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33461
diff changeset
70 self.assertEqual(arg, TESTFN)
38112
be49645e3984 Implement os.{chdir,rename,rmdir,remove} using Win32 directly.
Martin v. Löwis <martin@v.loewis.de>
parents: 34149
diff changeset
71 self.failUnless(issubclass(exc[0], OSError))
34019
bdd14ab6c5cf Bug #1071513: don't test on Cygwin, as chmod doesn't work reliably there
Johannes Gijsbers <jlg@dds.nl>
parents: 33870
diff changeset
72 self.errorState = 2
27360
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
73
33461
c8825a74e421 Document not-completely-obvious behavior in a test.
Johannes Gijsbers <jlg@dds.nl>
parents: 33125
diff changeset
74 def test_rmtree_dont_delete_file(self):
c8825a74e421 Document not-completely-obvious behavior in a test.
Johannes Gijsbers <jlg@dds.nl>
parents: 33125
diff changeset
75 # When called on a file instead of a directory, don't delete it.
c8825a74e421 Document not-completely-obvious behavior in a test.
Johannes Gijsbers <jlg@dds.nl>
parents: 33125
diff changeset
76 handle, path = tempfile.mkstemp()
c8825a74e421 Document not-completely-obvious behavior in a test.
Johannes Gijsbers <jlg@dds.nl>
parents: 33125
diff changeset
77 os.fdopen(handle).close()
c8825a74e421 Document not-completely-obvious behavior in a test.
Johannes Gijsbers <jlg@dds.nl>
parents: 33125
diff changeset
78 self.assertRaises(OSError, shutil.rmtree, path)
c8825a74e421 Document not-completely-obvious behavior in a test.
Johannes Gijsbers <jlg@dds.nl>
parents: 33125
diff changeset
79 os.remove(path)
c8825a74e421 Document not-completely-obvious behavior in a test.
Johannes Gijsbers <jlg@dds.nl>
parents: 33125
diff changeset
80
39425
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
81 def test_copytree_simple(self):
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
82 def write_data(path, data):
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
83 f = open(path, "w")
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
84 f.write(data)
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
85 f.close()
39322
cf046e22d7dd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39307
diff changeset
86
39425
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
87 def read_data(path):
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
88 f = open(path)
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
89 data = f.read()
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
90 f.close()
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
91 return data
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
92
39307
9377c6604542 Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents: 38112
diff changeset
93 src_dir = tempfile.mkdtemp()
9377c6604542 Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents: 38112
diff changeset
94 dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
39425
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
95
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
96 write_data(os.path.join(src_dir, 'test.txt'), '123')
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
97
39307
9377c6604542 Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents: 38112
diff changeset
98 os.mkdir(os.path.join(src_dir, 'test_dir'))
39425
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
99 write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
100
39307
9377c6604542 Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents: 38112
diff changeset
101 try:
9377c6604542 Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents: 38112
diff changeset
102 shutil.copytree(src_dir, dst_dir)
9377c6604542 Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents: 38112
diff changeset
103 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
9377c6604542 Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents: 38112
diff changeset
104 self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir')))
39425
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
105 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir',
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
106 'test.txt')))
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
107 actual = read_data(os.path.join(dst_dir, 'test.txt'))
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
108 self.assertEqual(actual, '123')
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
109 actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt'))
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
110 self.assertEqual(actual, '456')
39307
9377c6604542 Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents: 38112
diff changeset
111 finally:
39425
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
112 for path in (
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
113 os.path.join(src_dir, 'test.txt'),
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
114 os.path.join(dst_dir, 'test.txt'),
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
115 os.path.join(src_dir, 'test_dir', 'test.txt'),
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
116 os.path.join(dst_dir, 'test_dir', 'test.txt'),
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
117 ):
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
118 if os.path.exists(path):
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
119 os.remove(path)
43924
74f22954f189 Another fix for test_shutil. Martin pointed out that it breaks some build bots
Christian Heimes <christian@cheimes.de>
parents: 43920
diff changeset
120 for path in (src_dir,
74f22954f189 Another fix for test_shutil. Martin pointed out that it breaks some build bots
Christian Heimes <christian@cheimes.de>
parents: 43920
diff changeset
121 os.path.abspath(os.path.join(dst_dir, os.path.pardir))
74f22954f189 Another fix for test_shutil. Martin pointed out that it breaks some build bots
Christian Heimes <christian@cheimes.de>
parents: 43920
diff changeset
122 ):
39425
3c2bf79526be test_copytree_simple(): This was leaving behind two new temp
Tim Peters <tim.peters@gmail.com>
parents: 39322
diff changeset
123 if os.path.exists(path):
43920
fdf14170337a Fixed bug #1470
Christian Heimes <christian@cheimes.de>
parents: 39425
diff changeset
124 shutil.rmtree(path)
39322
cf046e22d7dd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39307
diff changeset
125
47888
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
126 def test_copytree_with_exclude(self):
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
127
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
128 def write_data(path, data):
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
129 f = open(path, "w")
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
130 f.write(data)
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
131 f.close()
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
132
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
133 def read_data(path):
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
134 f = open(path)
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
135 data = f.read()
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
136 f.close()
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
137 return data
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
138
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
139 # creating data
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
140 join = os.path.join
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
141 exists = os.path.exists
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
142 src_dir = tempfile.mkdtemp()
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
143 dst_dir = join(tempfile.mkdtemp(), 'destination')
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
144 write_data(join(src_dir, 'test.txt'), '123')
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
145 write_data(join(src_dir, 'test.tmp'), '123')
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
146 os.mkdir(join(src_dir, 'test_dir'))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
147 write_data(join(src_dir, 'test_dir', 'test.txt'), '456')
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
148 os.mkdir(join(src_dir, 'test_dir2'))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
149 write_data(join(src_dir, 'test_dir2', 'test.txt'), '456')
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
150 os.mkdir(join(src_dir, 'test_dir2', 'subdir'))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
151 os.mkdir(join(src_dir, 'test_dir2', 'subdir2'))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
152 write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'), '456')
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
153 write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'), '456')
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
154
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
155
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
156 # testing glob-like patterns
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
157 try:
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
158 patterns = shutil.ignore_patterns('*.tmp', 'test_dir2')
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
159 shutil.copytree(src_dir, dst_dir, ignore=patterns)
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
160 # checking the result: some elements should not be copied
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
161 self.assert_(exists(join(dst_dir, 'test.txt')))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
162 self.assert_(not exists(join(dst_dir, 'test.tmp')))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
163 self.assert_(not exists(join(dst_dir, 'test_dir2')))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
164 finally:
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
165 if os.path.exists(dst_dir):
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
166 shutil.rmtree(dst_dir)
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
167 try:
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
168 patterns = shutil.ignore_patterns('*.tmp', 'subdir*')
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
169 shutil.copytree(src_dir, dst_dir, ignore=patterns)
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
170 # checking the result: some elements should not be copied
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
171 self.assert_(not exists(join(dst_dir, 'test.tmp')))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
172 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir2')))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
173 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir')))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
174 finally:
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
175 if os.path.exists(dst_dir):
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
176 shutil.rmtree(dst_dir)
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
177
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
178 # testing callable-style
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
179 try:
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
180 def _filter(src, names):
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
181 res = []
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
182 for name in names:
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
183 path = os.path.join(src, name)
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
184
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
185 if (os.path.isdir(path) and
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
186 path.split()[-1] == 'subdir'):
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
187 res.append(name)
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
188 elif os.path.splitext(path)[-1] in ('.py'):
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
189 res.append(name)
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
190 return res
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
191
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
192 shutil.copytree(src_dir, dst_dir, ignore=_filter)
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
193
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
194 # checking the result: some elements should not be copied
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
195 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir2',
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
196 'test.py')))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
197 self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir')))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
198
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
199 finally:
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
200 if os.path.exists(dst_dir):
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
201 shutil.rmtree(dst_dir)
39322
cf046e22d7dd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39307
diff changeset
202
33124
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
203 if hasattr(os, "symlink"):
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
204 def test_dont_copy_file_onto_link_to_itself(self):
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
205 # bug 851123.
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
206 os.mkdir(TESTFN)
33125
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
207 src = os.path.join(TESTFN, 'cheese')
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
208 dst = os.path.join(TESTFN, 'shop')
33124
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
209 try:
33125
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
210 f = open(src, 'w')
33124
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
211 f.write('cheddar')
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
212 f.close()
33125
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
213
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
214 os.link(src, dst)
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
215 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
216 self.assertEqual(open(src,'r').read(), 'cheddar')
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
217 os.remove(dst)
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
218
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
219 # Using `src` here would mean we end up with a symlink pointing
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
220 # to TESTFN/TESTFN/cheese, while it should point at
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
221 # TESTFN/cheese.
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
222 os.symlink('cheese', dst)
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
223 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
224 self.assertEqual(open(src,'r').read(), 'cheddar')
3a7f82e60cb2 Unwrap too-smart loop: we can't use `src` for both hard and symbolic links.
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
225 os.remove(dst)
33124
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
226 finally:
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
227 try:
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
228 shutil.rmtree(TESTFN)
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
229 except OSError:
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32654
diff changeset
230 pass
32386
686e5d38be42 shutil.move() will raise an exception when trying to move a directory into
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
231
44823
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
232 def test_rmtree_on_symlink(self):
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
233 # bug 1669.
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
234 os.mkdir(TESTFN)
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
235 try:
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
236 src = os.path.join(TESTFN, 'cheese')
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
237 dst = os.path.join(TESTFN, 'shop')
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
238 os.mkdir(src)
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
239 os.symlink(src, dst)
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
240 self.assertRaises(OSError, shutil.rmtree, dst)
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
241 finally:
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
242 shutil.rmtree(TESTFN, ignore_errors=True)
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
243
53652
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
244 if hasattr(os, "mkfifo"):
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
245 # Issue #3002: copyfile and copytree block indefinitely on named pipes
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
246 def test_copyfile_named_pipe(self):
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
247 os.mkfifo(TESTFN)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
248 try:
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
249 self.assertRaises(shutil.SpecialFileError,
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
250 shutil.copyfile, TESTFN, TESTFN2)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
251 self.assertRaises(shutil.SpecialFileError,
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
252 shutil.copyfile, __file__, TESTFN)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
253 finally:
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
254 os.remove(TESTFN)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
255
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
256 def test_copytree_named_pipe(self):
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
257 os.mkdir(TESTFN)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
258 try:
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
259 subdir = os.path.join(TESTFN, "subdir")
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
260 os.mkdir(subdir)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
261 pipe = os.path.join(subdir, "mypipe")
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
262 os.mkfifo(pipe)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
263 try:
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
264 shutil.copytree(TESTFN, TESTFN2)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
265 except shutil.Error as e:
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
266 errors = e.args[0]
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
267 self.assertEqual(len(errors), 1)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
268 src, dst, error_msg = errors[0]
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
269 self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
270 else:
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
271 self.fail("shutil.Error should have been raised")
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
272 finally:
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
273 shutil.rmtree(TESTFN, ignore_errors=True)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
274 shutil.rmtree(TESTFN2, ignore_errors=True)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 53623
diff changeset
275
44823
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
276
45858
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
277 class TestMove(unittest.TestCase):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
278
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
279 def setUp(self):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
280 filename = "foo"
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
281 self.src_dir = tempfile.mkdtemp()
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
282 self.dst_dir = tempfile.mkdtemp()
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
283 self.src_file = os.path.join(self.src_dir, filename)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
284 self.dst_file = os.path.join(self.dst_dir, filename)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
285 # Try to create a dir in the current directory, hoping that it is
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
286 # not located on the same filesystem as the system tmp dir.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
287 try:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
288 self.dir_other_fs = tempfile.mkdtemp(
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
289 dir=os.path.dirname(__file__))
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
290 self.file_other_fs = os.path.join(self.dir_other_fs,
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
291 filename)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
292 except OSError:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
293 self.dir_other_fs = None
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
294 with open(self.src_file, "wb") as f:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
295 f.write("spam")
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
296
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
297 def tearDown(self):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
298 for d in (self.src_dir, self.dst_dir, self.dir_other_fs):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
299 try:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
300 if d:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
301 shutil.rmtree(d)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
302 except:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
303 pass
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
304
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
305 def _check_move_file(self, src, dst, real_dst):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
306 contents = open(src, "rb").read()
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
307 shutil.move(src, dst)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
308 self.assertEqual(contents, open(real_dst, "rb").read())
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
309 self.assertFalse(os.path.exists(src))
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
310
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
311 def _check_move_dir(self, src, dst, real_dst):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
312 contents = sorted(os.listdir(src))
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
313 shutil.move(src, dst)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
314 self.assertEqual(contents, sorted(os.listdir(real_dst)))
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
315 self.assertFalse(os.path.exists(src))
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
316
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
317 def test_move_file(self):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
318 # Move a file to another location on the same filesystem.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
319 self._check_move_file(self.src_file, self.dst_file, self.dst_file)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
320
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
321 def test_move_file_to_dir(self):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
322 # Move a file inside an existing dir on the same filesystem.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
323 self._check_move_file(self.src_file, self.dst_dir, self.dst_file)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
324
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
325 def test_move_file_other_fs(self):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
326 # Move a file to an existing dir on another filesystem.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
327 if not self.dir_other_fs:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
328 # skip
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
329 return
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
330 self._check_move_file(self.src_file, self.file_other_fs,
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
331 self.file_other_fs)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
332
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
333 def test_move_file_to_dir_other_fs(self):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
334 # Move a file to another location on another filesystem.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
335 if not self.dir_other_fs:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
336 # skip
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
337 return
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
338 self._check_move_file(self.src_file, self.dir_other_fs,
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
339 self.file_other_fs)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
340
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
341 def test_move_dir(self):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
342 # Move a dir to another location on the same filesystem.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
343 dst_dir = tempfile.mktemp()
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
344 try:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
345 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
346 finally:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
347 try:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
348 shutil.rmtree(dst_dir)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
349 except:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
350 pass
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
351
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
352 def test_move_dir_other_fs(self):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
353 # Move a dir to another location on another filesystem.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
354 if not self.dir_other_fs:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
355 # skip
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
356 return
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
357 dst_dir = tempfile.mktemp(dir=self.dir_other_fs)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
358 try:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
359 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
360 finally:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
361 try:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
362 shutil.rmtree(dst_dir)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
363 except:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
364 pass
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
365
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
366 def test_move_dir_to_dir(self):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
367 # Move a dir inside an existing dir on the same filesystem.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
368 self._check_move_dir(self.src_dir, self.dst_dir,
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
369 os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
370
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
371 def test_move_dir_to_dir_other_fs(self):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
372 # Move a dir inside an existing dir on another filesystem.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
373 if not self.dir_other_fs:
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
374 # skip
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
375 return
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
376 self._check_move_dir(self.src_dir, self.dir_other_fs,
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
377 os.path.join(self.dir_other_fs, os.path.basename(self.src_dir)))
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
378
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
379 def test_existing_file_inside_dest_dir(self):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
380 # A file with the same name inside the destination dir already exists.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
381 with open(self.dst_file, "wb"):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
382 pass
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
383 self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
384
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
385 def test_dont_move_dir_in_itself(self):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
386 # Moving a dir inside itself raises an Error.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
387 dst = os.path.join(self.src_dir, "bar")
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
388 self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst)
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
389
51179
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
390 def test_destinsrc_false_negative(self):
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
391 os.mkdir(TESTFN)
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
392 try:
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
393 for src, dst in [('srcdir', 'srcdir/dest')]:
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
394 src = os.path.join(TESTFN, src)
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
395 dst = os.path.join(TESTFN, dst)
51446
0ab36796dd79 make destinsrc private
Benjamin Peterson <benjamin@python.org>
parents: 51179
diff changeset
396 self.assert_(shutil._destinsrc(src, dst),
0ab36796dd79 make destinsrc private
Benjamin Peterson <benjamin@python.org>
parents: 51179
diff changeset
397 msg='_destinsrc() wrongly concluded that '
51179
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
398 'dst (%s) is not in src (%s)' % (dst, src))
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
399 finally:
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
400 shutil.rmtree(TESTFN, ignore_errors=True)
45858
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
401
51179
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
402 def test_destinsrc_false_positive(self):
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
403 os.mkdir(TESTFN)
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
404 try:
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
405 for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]:
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
406 src = os.path.join(TESTFN, src)
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
407 dst = os.path.join(TESTFN, dst)
51446
0ab36796dd79 make destinsrc private
Benjamin Peterson <benjamin@python.org>
parents: 51179
diff changeset
408 self.failIf(shutil._destinsrc(src, dst),
0ab36796dd79 make destinsrc private
Benjamin Peterson <benjamin@python.org>
parents: 51179
diff changeset
409 msg='_destinsrc() wrongly concluded that '
51179
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
410 'dst (%s) is in src (%s)' % (dst, src))
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
411 finally:
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
412 shutil.rmtree(TESTFN, ignore_errors=True)
44823
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 43924
diff changeset
413
27360
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
414 def test_main():
45858
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
415 test_support.run_unittest(TestShutil, TestMove)
27360
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
416
273b2b49bfd8 A very minimal start to a test of the shutil module.
Barry Warsaw <barry@python.org>
parents:
diff changeset
417 if __name__ == '__main__':
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 27360
diff changeset
418 test_main()