0001 # Copyright (C) 2003 Python Software Foundation 0002 0003 import unittest 0004 import shutil 0005 import tempfile 0006 import stat 0007 import os 0008 import os.path 0009 from test import test_support 0010 from test.test_support import TESTFN 0011 0012 class TestShutil(unittest.TestCase): 0013 def test_rmtree_errors(self): 0014 # filename is guaranteed not to exist 0015 filename = tempfile.mktemp() 0016 self.assertRaises(OSError, shutil.rmtree, filename) 0017 0018 if hasattr(os, 'chmod'): 0019 def test_on_error(self): 0020 self.errorState = 0 0021 os.mkdir(TESTFN) 0022 self.childpath = os.path.join(TESTFN, 'a') 0023 f = open(self.childpath, 'w') 0024 f.close() 0025 old_dir_mode = os.stat(TESTFN).st_mode 0026 old_child_mode = os.stat(self.childpath).st_mode 0027 # Make unwritable. 0028 os.chmod(self.childpath, stat.S_IREAD) 0029 os.chmod(TESTFN, stat.S_IREAD) 0030 0031 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror) 0032 0033 # Make writable again. 0034 os.chmod(TESTFN, old_dir_mode) 0035 os.chmod(self.childpath, old_child_mode) 0036 0037 # Clean up. 0038 shutil.rmtree(TESTFN) 0039 0040 def check_args_to_onerror(self, func, arg, exc): 0041 if self.errorState == 0: 0042 self.assertEqual(func, os.remove) 0043 self.assertEqual(arg, self.childpath) 0044 self.assertEqual(exc[0], OSError) 0045 self.errorState = 1 0046 else: 0047 self.assertEqual(func, os.rmdir) 0048 self.assertEqual(arg, TESTFN) 0049 self.assertEqual(exc[0], OSError) 0050 0051 def test_rmtree_dont_delete_file(self): 0052 # When called on a file instead of a directory, don't delete it. 0053 handle, path = tempfile.mkstemp() 0054 os.fdopen(handle).close() 0055 self.assertRaises(OSError, shutil.rmtree, path) 0056 os.remove(path) 0057 0058 def test_dont_move_dir_in_itself(self): 0059 src_dir = tempfile.mkdtemp() 0060 try: 0061 dst = os.path.join(src_dir, 'foo') 0062 self.assertRaises(shutil.Error, shutil.move, src_dir, dst) 0063 finally: 0064 try: 0065 os.rmdir(src_dir) 0066 except: 0067 pass 0068 0069 if hasattr(os, "symlink"): 0070 def test_dont_copy_file_onto_link_to_itself(self): 0071 # bug 851123. 0072 os.mkdir(TESTFN) 0073 src = os.path.join(TESTFN, 'cheese') 0074 dst = os.path.join(TESTFN, 'shop') 0075 try: 0076 f = open(src, 'w') 0077 f.write('cheddar') 0078 f.close() 0079 0080 os.link(src, dst) 0081 self.assertRaises(shutil.Error, shutil.copyfile, src, dst) 0082 self.assertEqual(open(src,'r').read(), 'cheddar') 0083 os.remove(dst) 0084 0085 # Using `src` here would mean we end up with a symlink pointing 0086 # to TESTFN/TESTFN/cheese, while it should point at 0087 # TESTFN/cheese. 0088 os.symlink('cheese', dst) 0089 self.assertRaises(shutil.Error, shutil.copyfile, src, dst) 0090 self.assertEqual(open(src,'r').read(), 'cheddar') 0091 os.remove(dst) 0092 finally: 0093 try: 0094 shutil.rmtree(TESTFN) 0095 except OSError: 0096 pass 0097 0098 def test_main(): 0099 test_support.run_unittest(TestShutil) 0100 0101 if __name__ == '__main__': 0102 test_main() 0103
Generated by PyXR 0.9.4