| 1 | # Copyright (C) 2003 Python Software Foundation |
| 2 | |
| 3 | import unittest |
| 4 | import shutil |
| 5 | import tempfile |
| 6 | import sys |
| 7 | import stat |
| 8 | import os |
| 9 | import os.path |
| 10 | from test import test_support |
| 11 | from test.test_support import TESTFN |
| 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) |
| 18 | |
| 19 | if (hasattr(os, 'chmod') and sys.platform[:6] != 'cygwin' |
| 20 | and not (hasattr(os, 'geteuid') and os.geteuid() == 0)): |
| 21 | def test_on_error(self): |
| 22 | self.errorState = 0 |
| 23 | os.mkdir(TESTFN) |
| 24 | self.childpath = os.path.join(TESTFN, 'a') |
| 25 | f = open(self.childpath, 'w') |
| 26 | f.close() |
| 27 | old_dir_mode = os.stat(TESTFN).st_mode |
| 28 | old_child_mode = os.stat(self.childpath).st_mode |
| 29 | # Make unwritable. |
| 30 | os.chmod(self.childpath, stat.S_IREAD) |
| 31 | os.chmod(TESTFN, stat.S_IREAD) |
| 32 | |
| 33 | shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror) |
| 34 | # Test whether onerror has actually been called. |
| 35 | self.assertEqual(self.errorState, 2) |
| 36 | |
| 37 | # Make writable again. |
| 38 | os.chmod(TESTFN, old_dir_mode) |
| 39 | os.chmod(self.childpath, old_child_mode) |
| 40 | |
| 41 | # Clean up. |
| 42 | shutil.rmtree(TESTFN) |
| 43 | |
| 44 | def check_args_to_onerror(self, func, arg, exc): |
| 45 | if self.errorState == 0: |
| 46 | self.assertEqual(func, os.remove) |
| 47 | self.assertEqual(arg, self.childpath) |
| 48 | self.assertEqual(exc[0], OSError) |
| 49 | self.errorState = 1 |
| 50 | else: |
| 51 | self.assertEqual(func, os.rmdir) |
| 52 | self.assertEqual(arg, TESTFN) |
| 53 | self.assertEqual(exc[0], OSError) |
| 54 | self.errorState = 2 |
| 55 | |
| 56 | def test_rmtree_dont_delete_file(self): |
| 57 | # When called on a file instead of a directory, don't delete it. |
| 58 | handle, path = tempfile.mkstemp() |
| 59 | os.fdopen(handle).close() |
| 60 | self.assertRaises(OSError, shutil.rmtree, path) |
| 61 | os.remove(path) |
| 62 | |
| 63 | def test_dont_move_dir_in_itself(self): |
| 64 | src_dir = tempfile.mkdtemp() |
| 65 | try: |
| 66 | dst = os.path.join(src_dir, 'foo') |
| 67 | self.assertRaises(shutil.Error, shutil.move, src_dir, dst) |
| 68 | finally: |
| 69 | try: |
| 70 | os.rmdir(src_dir) |
| 71 | except: |
| 72 | pass |
| 73 | |
| 74 | if hasattr(os, "symlink"): |
| 75 | def test_dont_copy_file_onto_link_to_itself(self): |
| 76 | # bug 851123. |
| 77 | os.mkdir(TESTFN) |
| 78 | src = os.path.join(TESTFN, 'cheese') |
| 79 | dst = os.path.join(TESTFN, 'shop') |
| 80 | try: |
| 81 | f = open(src, 'w') |
| 82 | f.write('cheddar') |
| 83 | f.close() |
| 84 | |
| 85 | os.link(src, dst) |
| 86 | self.assertRaises(shutil.Error, shutil.copyfile, src, dst) |
| 87 | self.assertEqual(open(src,'r').read(), 'cheddar') |
| 88 | os.remove(dst) |
| 89 | |
| 90 | # Using `src` here would mean we end up with a symlink pointing |
| 91 | # to TESTFN/TESTFN/cheese, while it should point at |
| 92 | # TESTFN/cheese. |
| 93 | os.symlink('cheese', dst) |
| 94 | self.assertRaises(shutil.Error, shutil.copyfile, src, dst) |
| 95 | self.assertEqual(open(src,'r').read(), 'cheddar') |
| 96 | os.remove(dst) |
| 97 | finally: |
| 98 | try: |
| 99 | shutil.rmtree(TESTFN) |
| 100 | except OSError: |
| 101 | pass |
| 102 | |
| 103 | def test_main(): |
| 104 | test_support.run_unittest(TestShutil) |
| 105 | |
| 106 | if __name__ == '__main__': |
| 107 | test_main() |