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 |
Barry Warsaw | 7fc2cca | 2003-01-24 17:34:13 +0000 | [diff] [blame] | 10 | from test import test_support |
Johannes Gijsbers | 46f1459 | 2004-08-14 13:30:02 +0000 | [diff] [blame] | 11 | from test.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 | |
Brett Cannon | 1c3fa18 | 2004-06-19 21:11:35 +0000 | [diff] [blame] | 66 | def test_dont_move_dir_in_itself(self): |
| 67 | src_dir = tempfile.mkdtemp() |
| 68 | try: |
| 69 | dst = os.path.join(src_dir, 'foo') |
| 70 | self.assertRaises(shutil.Error, shutil.move, src_dir, dst) |
| 71 | finally: |
| 72 | try: |
| 73 | os.rmdir(src_dir) |
| 74 | except: |
| 75 | pass |
Barry Warsaw | 7fc2cca | 2003-01-24 17:34:13 +0000 | [diff] [blame] | 76 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | def test_copytree_simple(self): |
| 78 | def write_data(path, data): |
| 79 | f = open(path, "w") |
| 80 | f.write(data) |
| 81 | f.close() |
| 82 | |
| 83 | def read_data(path): |
| 84 | f = open(path) |
| 85 | data = f.read() |
| 86 | f.close() |
| 87 | return data |
| 88 | |
| 89 | src_dir = tempfile.mkdtemp() |
| 90 | dst_dir = os.path.join(tempfile.mkdtemp(), 'destination') |
| 91 | |
| 92 | write_data(os.path.join(src_dir, 'test.txt'), '123') |
| 93 | |
| 94 | os.mkdir(os.path.join(src_dir, 'test_dir')) |
| 95 | write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456') |
| 96 | |
| 97 | try: |
| 98 | shutil.copytree(src_dir, dst_dir) |
| 99 | self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt'))) |
| 100 | self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir'))) |
| 101 | self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir', |
| 102 | 'test.txt'))) |
| 103 | actual = read_data(os.path.join(dst_dir, 'test.txt')) |
| 104 | self.assertEqual(actual, '123') |
| 105 | actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt')) |
| 106 | self.assertEqual(actual, '456') |
| 107 | finally: |
| 108 | for path in ( |
| 109 | os.path.join(src_dir, 'test.txt'), |
| 110 | os.path.join(dst_dir, 'test.txt'), |
| 111 | os.path.join(src_dir, 'test_dir', 'test.txt'), |
| 112 | os.path.join(dst_dir, 'test_dir', 'test.txt'), |
| 113 | ): |
| 114 | if os.path.exists(path): |
| 115 | os.remove(path) |
| 116 | for path in ( |
| 117 | os.path.join(src_dir, 'test_dir'), |
| 118 | os.path.join(dst_dir, 'test_dir'), |
| 119 | ): |
| 120 | if os.path.exists(path): |
| 121 | os.removedirs(path) |
| 122 | |
| 123 | |
Johannes Gijsbers | 46f1459 | 2004-08-14 13:30:02 +0000 | [diff] [blame] | 124 | if hasattr(os, "symlink"): |
| 125 | def test_dont_copy_file_onto_link_to_itself(self): |
| 126 | # bug 851123. |
| 127 | os.mkdir(TESTFN) |
Johannes Gijsbers | 6812871 | 2004-08-14 13:57:08 +0000 | [diff] [blame] | 128 | src = os.path.join(TESTFN, 'cheese') |
| 129 | dst = os.path.join(TESTFN, 'shop') |
Johannes Gijsbers | 46f1459 | 2004-08-14 13:30:02 +0000 | [diff] [blame] | 130 | try: |
Johannes Gijsbers | 6812871 | 2004-08-14 13:57:08 +0000 | [diff] [blame] | 131 | f = open(src, 'w') |
Johannes Gijsbers | 46f1459 | 2004-08-14 13:30:02 +0000 | [diff] [blame] | 132 | f.write('cheddar') |
| 133 | f.close() |
Johannes Gijsbers | 6812871 | 2004-08-14 13:57:08 +0000 | [diff] [blame] | 134 | |
| 135 | os.link(src, dst) |
| 136 | self.assertRaises(shutil.Error, shutil.copyfile, src, dst) |
| 137 | self.assertEqual(open(src,'r').read(), 'cheddar') |
| 138 | os.remove(dst) |
| 139 | |
| 140 | # Using `src` here would mean we end up with a symlink pointing |
| 141 | # to TESTFN/TESTFN/cheese, while it should point at |
| 142 | # TESTFN/cheese. |
| 143 | os.symlink('cheese', dst) |
| 144 | self.assertRaises(shutil.Error, shutil.copyfile, src, dst) |
| 145 | self.assertEqual(open(src,'r').read(), 'cheddar') |
| 146 | os.remove(dst) |
Johannes Gijsbers | 46f1459 | 2004-08-14 13:30:02 +0000 | [diff] [blame] | 147 | finally: |
| 148 | try: |
| 149 | shutil.rmtree(TESTFN) |
| 150 | except OSError: |
| 151 | pass |
Brett Cannon | 1c3fa18 | 2004-06-19 21:11:35 +0000 | [diff] [blame] | 152 | |
Barry Warsaw | 7fc2cca | 2003-01-24 17:34:13 +0000 | [diff] [blame] | 153 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 154 | test_support.run_unittest(TestShutil) |
Barry Warsaw | 7fc2cca | 2003-01-24 17:34:13 +0000 | [diff] [blame] | 155 | |
Barry Warsaw | 7fc2cca | 2003-01-24 17:34:13 +0000 | [diff] [blame] | 156 | if __name__ == '__main__': |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 157 | test_main() |