blob: 2e680a90d354200d98c43261a9ca9a0e296dec41 [file] [log] [blame]
Barry Warsaw7fc2cca2003-01-24 17:34:13 +00001# Copyright (C) 2003 Python Software Foundation
2
3import unittest
4import shutil
5import tempfile
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +00006import sys
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +00007import stat
Brett Cannon1c3fa182004-06-19 21:11:35 +00008import os
9import os.path
Benjamin Petersonee8712c2008-05-20 21:35:26 +000010from test import support
11from test.support import TESTFN
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000012
13class TestShutil(unittest.TestCase):
14 def test_rmtree_errors(self):
15 # filename is guaranteed not to exist
16 filename = tempfile.mktemp()
17 self.assertRaises(OSError, shutil.rmtree, filename)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000018
Johannes Gijsbersb8b09d02004-12-06 20:50:15 +000019 # See bug #1071513 for why we don't run this on cygwin
20 # and bug #1076467 for why we don't run this as root.
21 if (hasattr(os, 'chmod') and sys.platform[:6] != 'cygwin'
Johannes Gijsbers6b220b02004-12-12 15:52:57 +000022 and not (hasattr(os, 'geteuid') and os.geteuid() == 0)):
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000023 def test_on_error(self):
24 self.errorState = 0
25 os.mkdir(TESTFN)
Tim Peters4590c002004-11-01 02:40:52 +000026 self.childpath = os.path.join(TESTFN, 'a')
27 f = open(self.childpath, 'w')
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000028 f.close()
Tim Peters4590c002004-11-01 02:40:52 +000029 old_dir_mode = os.stat(TESTFN).st_mode
30 old_child_mode = os.stat(self.childpath).st_mode
31 # Make unwritable.
32 os.chmod(self.childpath, stat.S_IREAD)
33 os.chmod(TESTFN, stat.S_IREAD)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000034
35 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000036 # Test whether onerror has actually been called.
Johannes Gijsbersb8b09d02004-12-06 20:50:15 +000037 self.assertEqual(self.errorState, 2,
38 "Expected call to onerror function did not happen.")
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000039
Tim Peters4590c002004-11-01 02:40:52 +000040 # Make writable again.
41 os.chmod(TESTFN, old_dir_mode)
42 os.chmod(self.childpath, old_child_mode)
43
44 # Clean up.
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000045 shutil.rmtree(TESTFN)
46
47 def check_args_to_onerror(self, func, arg, exc):
48 if self.errorState == 0:
49 self.assertEqual(func, os.remove)
Tim Peters4590c002004-11-01 02:40:52 +000050 self.assertEqual(arg, self.childpath)
Thomas Wouters477c8d52006-05-27 19:21:47 +000051 self.failUnless(issubclass(exc[0], OSError))
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000052 self.errorState = 1
53 else:
54 self.assertEqual(func, os.rmdir)
55 self.assertEqual(arg, TESTFN)
Thomas Wouters477c8d52006-05-27 19:21:47 +000056 self.failUnless(issubclass(exc[0], OSError))
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000057 self.errorState = 2
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000058
Johannes Gijsbersd60e92a2004-09-11 21:26:21 +000059 def test_rmtree_dont_delete_file(self):
60 # When called on a file instead of a directory, don't delete it.
61 handle, path = tempfile.mkstemp()
62 os.fdopen(handle).close()
63 self.assertRaises(OSError, shutil.rmtree, path)
64 os.remove(path)
65
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066 def test_copytree_simple(self):
67 def write_data(path, data):
68 f = open(path, "w")
69 f.write(data)
70 f.close()
71
72 def read_data(path):
73 f = open(path)
74 data = f.read()
75 f.close()
76 return data
77
78 src_dir = tempfile.mkdtemp()
79 dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
80
81 write_data(os.path.join(src_dir, 'test.txt'), '123')
82
83 os.mkdir(os.path.join(src_dir, 'test_dir'))
84 write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
85
86 try:
87 shutil.copytree(src_dir, dst_dir)
88 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
89 self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir')))
90 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir',
91 'test.txt')))
92 actual = read_data(os.path.join(dst_dir, 'test.txt'))
93 self.assertEqual(actual, '123')
94 actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt'))
95 self.assertEqual(actual, '456')
96 finally:
97 for path in (
98 os.path.join(src_dir, 'test.txt'),
99 os.path.join(dst_dir, 'test.txt'),
100 os.path.join(src_dir, 'test_dir', 'test.txt'),
101 os.path.join(dst_dir, 'test_dir', 'test.txt'),
102 ):
103 if os.path.exists(path):
104 os.remove(path)
Christian Heimese052dd82007-11-20 03:20:04 +0000105 for path in (src_dir,
106 os.path.abspath(os.path.join(dst_dir, os.path.pardir))
107 ):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000108 if os.path.exists(path):
Christian Heimes94140152007-11-20 01:45:17 +0000109 shutil.rmtree(path)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000110
111
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000112 if hasattr(os, "symlink"):
113 def test_dont_copy_file_onto_link_to_itself(self):
114 # bug 851123.
115 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +0000116 src = os.path.join(TESTFN, 'cheese')
117 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000118 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +0000119 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000120 f.write('cheddar')
121 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +0000122
123 os.link(src, dst)
124 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
125 self.assertEqual(open(src,'r').read(), 'cheddar')
126 os.remove(dst)
127
128 # Using `src` here would mean we end up with a symlink pointing
129 # to TESTFN/TESTFN/cheese, while it should point at
130 # TESTFN/cheese.
131 os.symlink('cheese', dst)
132 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
133 self.assertEqual(open(src,'r').read(), 'cheddar')
134 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000135 finally:
136 try:
137 shutil.rmtree(TESTFN)
138 except OSError:
139 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +0000140
Christian Heimes9bd667a2008-01-20 15:14:11 +0000141 def test_rmtree_on_symlink(self):
142 # bug 1669.
143 os.mkdir(TESTFN)
144 try:
145 src = os.path.join(TESTFN, 'cheese')
146 dst = os.path.join(TESTFN, 'shop')
147 os.mkdir(src)
148 os.symlink(src, dst)
149 self.assertRaises(OSError, shutil.rmtree, dst)
150 finally:
151 shutil.rmtree(TESTFN, ignore_errors=True)
152
153
Christian Heimesada8c3b2008-03-18 18:26:33 +0000154class TestMove(unittest.TestCase):
155
156 def setUp(self):
157 filename = "foo"
158 self.src_dir = tempfile.mkdtemp()
159 self.dst_dir = tempfile.mkdtemp()
160 self.src_file = os.path.join(self.src_dir, filename)
161 self.dst_file = os.path.join(self.dst_dir, filename)
162 # Try to create a dir in the current directory, hoping that it is
163 # not located on the same filesystem as the system tmp dir.
164 try:
165 self.dir_other_fs = tempfile.mkdtemp(
166 dir=os.path.dirname(__file__))
167 self.file_other_fs = os.path.join(self.dir_other_fs,
168 filename)
169 except OSError:
170 self.dir_other_fs = None
171 with open(self.src_file, "wb") as f:
172 f.write(b"spam")
173
174 def tearDown(self):
175 for d in (self.src_dir, self.dst_dir, self.dir_other_fs):
176 try:
177 if d:
178 shutil.rmtree(d)
179 except:
180 pass
181
182 def _check_move_file(self, src, dst, real_dst):
183 contents = open(src, "rb").read()
184 shutil.move(src, dst)
185 self.assertEqual(contents, open(real_dst, "rb").read())
186 self.assertFalse(os.path.exists(src))
187
188 def _check_move_dir(self, src, dst, real_dst):
189 contents = sorted(os.listdir(src))
190 shutil.move(src, dst)
191 self.assertEqual(contents, sorted(os.listdir(real_dst)))
192 self.assertFalse(os.path.exists(src))
193
194 def test_move_file(self):
195 # Move a file to another location on the same filesystem.
196 self._check_move_file(self.src_file, self.dst_file, self.dst_file)
197
198 def test_move_file_to_dir(self):
199 # Move a file inside an existing dir on the same filesystem.
200 self._check_move_file(self.src_file, self.dst_dir, self.dst_file)
201
202 def test_move_file_other_fs(self):
203 # Move a file to an existing dir on another filesystem.
204 if not self.dir_other_fs:
205 # skip
206 return
207 self._check_move_file(self.src_file, self.file_other_fs,
208 self.file_other_fs)
209
210 def test_move_file_to_dir_other_fs(self):
211 # Move a file to another location on another filesystem.
212 if not self.dir_other_fs:
213 # skip
214 return
215 self._check_move_file(self.src_file, self.dir_other_fs,
216 self.file_other_fs)
217
218 def test_move_dir(self):
219 # Move a dir to another location on the same filesystem.
220 dst_dir = tempfile.mktemp()
221 try:
222 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
223 finally:
224 try:
225 shutil.rmtree(dst_dir)
226 except:
227 pass
228
229 def test_move_dir_other_fs(self):
230 # Move a dir to another location on another filesystem.
231 if not self.dir_other_fs:
232 # skip
233 return
234 dst_dir = tempfile.mktemp(dir=self.dir_other_fs)
235 try:
236 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
237 finally:
238 try:
239 shutil.rmtree(dst_dir)
240 except:
241 pass
242
243 def test_move_dir_to_dir(self):
244 # Move a dir inside an existing dir on the same filesystem.
245 self._check_move_dir(self.src_dir, self.dst_dir,
246 os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
247
248 def test_move_dir_to_dir_other_fs(self):
249 # Move a dir inside an existing dir on another filesystem.
250 if not self.dir_other_fs:
251 # skip
252 return
253 self._check_move_dir(self.src_dir, self.dir_other_fs,
254 os.path.join(self.dir_other_fs, os.path.basename(self.src_dir)))
255
256 def test_existing_file_inside_dest_dir(self):
257 # A file with the same name inside the destination dir already exists.
258 with open(self.dst_file, "wb"):
259 pass
260 self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir)
261
262 def test_dont_move_dir_in_itself(self):
263 # Moving a dir inside itself raises an Error.
264 dst = os.path.join(self.src_dir, "bar")
265 self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst)
266
267
Christian Heimes9bd667a2008-01-20 15:14:11 +0000268
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000269def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000270 support.run_unittest(TestShutil, TestMove)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000271
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000272if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000273 test_main()