Barry Warsaw | 7fc2cca | 2003-01-24 17:34:13 +0000 | [diff] [blame] | 1 | # Copyright (C) 2003 Python Software Foundation |
| 2 | |
| 3 | import unittest |
| 4 | import shutil |
| 5 | import tempfile |
Johannes Gijsbers | 8e6f2de | 2004-11-23 09:27:27 +0000 | [diff] [blame] | 6 | import sys |
Johannes Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 7 | import stat |
Brett Cannon | 1c3fa18 | 2004-06-19 21:11:35 +0000 | [diff] [blame] | 8 | import os |
| 9 | import os.path |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 10 | from test import support |
| 11 | from test.support import TESTFN |
Barry Warsaw | 7fc2cca | 2003-01-24 17:34:13 +0000 | [diff] [blame] | 12 | |
| 13 | class 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 Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 18 | |
Johannes Gijsbers | b8b09d0 | 2004-12-06 20:50:15 +0000 | [diff] [blame] | 19 | # 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 Gijsbers | 6b220b0 | 2004-12-12 15:52:57 +0000 | [diff] [blame] | 22 | and not (hasattr(os, 'geteuid') and os.geteuid() == 0)): |
Johannes Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 23 | def test_on_error(self): |
| 24 | self.errorState = 0 |
| 25 | os.mkdir(TESTFN) |
Tim Peters | 4590c00 | 2004-11-01 02:40:52 +0000 | [diff] [blame] | 26 | self.childpath = os.path.join(TESTFN, 'a') |
| 27 | f = open(self.childpath, 'w') |
Johannes Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 28 | f.close() |
Tim Peters | 4590c00 | 2004-11-01 02:40:52 +0000 | [diff] [blame] | 29 | 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 Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 34 | |
| 35 | shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror) |
Johannes Gijsbers | 8e6f2de | 2004-11-23 09:27:27 +0000 | [diff] [blame] | 36 | # Test whether onerror has actually been called. |
Johannes Gijsbers | b8b09d0 | 2004-12-06 20:50:15 +0000 | [diff] [blame] | 37 | self.assertEqual(self.errorState, 2, |
| 38 | "Expected call to onerror function did not happen.") |
Johannes Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 39 | |
Tim Peters | 4590c00 | 2004-11-01 02:40:52 +0000 | [diff] [blame] | 40 | # Make writable again. |
| 41 | os.chmod(TESTFN, old_dir_mode) |
| 42 | os.chmod(self.childpath, old_child_mode) |
| 43 | |
| 44 | # Clean up. |
Johannes Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 45 | 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 Peters | 4590c00 | 2004-11-01 02:40:52 +0000 | [diff] [blame] | 50 | self.assertEqual(arg, self.childpath) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 51 | self.failUnless(issubclass(exc[0], OSError)) |
Johannes Gijsbers | ef5ffc4 | 2004-10-31 12:05:31 +0000 | [diff] [blame] | 52 | self.errorState = 1 |
| 53 | else: |
| 54 | self.assertEqual(func, os.rmdir) |
| 55 | self.assertEqual(arg, TESTFN) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 56 | self.failUnless(issubclass(exc[0], OSError)) |
Johannes Gijsbers | 8e6f2de | 2004-11-23 09:27:27 +0000 | [diff] [blame] | 57 | self.errorState = 2 |
Barry Warsaw | 7fc2cca | 2003-01-24 17:34:13 +0000 | [diff] [blame] | 58 | |
Johannes Gijsbers | d60e92a | 2004-09-11 21:26:21 +0000 | [diff] [blame] | 59 | 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 66 | 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 Heimes | e052dd8 | 2007-11-20 03:20:04 +0000 | [diff] [blame] | 105 | for path in (src_dir, |
| 106 | os.path.abspath(os.path.join(dst_dir, os.path.pardir)) |
| 107 | ): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 108 | if os.path.exists(path): |
Christian Heimes | 9414015 | 2007-11-20 01:45:17 +0000 | [diff] [blame] | 109 | shutil.rmtree(path) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 110 | |
| 111 | |
Johannes Gijsbers | 46f1459 | 2004-08-14 13:30:02 +0000 | [diff] [blame] | 112 | if hasattr(os, "symlink"): |
| 113 | def test_dont_copy_file_onto_link_to_itself(self): |
| 114 | # bug 851123. |
| 115 | os.mkdir(TESTFN) |
Johannes Gijsbers | 6812871 | 2004-08-14 13:57:08 +0000 | [diff] [blame] | 116 | src = os.path.join(TESTFN, 'cheese') |
| 117 | dst = os.path.join(TESTFN, 'shop') |
Johannes Gijsbers | 46f1459 | 2004-08-14 13:30:02 +0000 | [diff] [blame] | 118 | try: |
Johannes Gijsbers | 6812871 | 2004-08-14 13:57:08 +0000 | [diff] [blame] | 119 | f = open(src, 'w') |
Johannes Gijsbers | 46f1459 | 2004-08-14 13:30:02 +0000 | [diff] [blame] | 120 | f.write('cheddar') |
| 121 | f.close() |
Johannes Gijsbers | 6812871 | 2004-08-14 13:57:08 +0000 | [diff] [blame] | 122 | |
| 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 Gijsbers | 46f1459 | 2004-08-14 13:30:02 +0000 | [diff] [blame] | 135 | finally: |
| 136 | try: |
| 137 | shutil.rmtree(TESTFN) |
| 138 | except OSError: |
| 139 | pass |
Brett Cannon | 1c3fa18 | 2004-06-19 21:11:35 +0000 | [diff] [blame] | 140 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 141 | 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 Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 154 | class 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 Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 268 | |
Barry Warsaw | 7fc2cca | 2003-01-24 17:34:13 +0000 | [diff] [blame] | 269 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 270 | support.run_unittest(TestShutil, TestMove) |
Barry Warsaw | 7fc2cca | 2003-01-24 17:34:13 +0000 | [diff] [blame] | 271 | |
Barry Warsaw | 7fc2cca | 2003-01-24 17:34:13 +0000 | [diff] [blame] | 272 | if __name__ == '__main__': |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 273 | test_main() |