blob: 32197b3093bb17d765dfa20770b898089a4b0369 [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 Gijsbersef5ffc42004-10-31 12:05:31 +00006import stat
Brett Cannon1c3fa182004-06-19 21:11:35 +00007import os
8import os.path
Barry Warsaw7fc2cca2003-01-24 17:34:13 +00009from test import test_support
Johannes Gijsbers46f14592004-08-14 13:30:02 +000010from test.test_support import TESTFN
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000011
12class TestShutil(unittest.TestCase):
13 def test_rmtree_errors(self):
14 # filename is guaranteed not to exist
15 filename = tempfile.mktemp()
16 self.assertRaises(OSError, shutil.rmtree, filename)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000017
18 if hasattr(os, 'chmod'):
19 def test_on_error(self):
20 self.errorState = 0
21 os.mkdir(TESTFN)
Tim Peters4590c002004-11-01 02:40:52 +000022 self.childpath = os.path.join(TESTFN, 'a')
23 f = open(self.childpath, 'w')
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000024 f.close()
Tim Peters4590c002004-11-01 02:40:52 +000025 old_dir_mode = os.stat(TESTFN).st_mode
26 old_child_mode = os.stat(self.childpath).st_mode
27 # Make unwritable.
28 os.chmod(self.childpath, stat.S_IREAD)
29 os.chmod(TESTFN, stat.S_IREAD)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000030
31 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
32
Tim Peters4590c002004-11-01 02:40:52 +000033 # Make writable again.
34 os.chmod(TESTFN, old_dir_mode)
35 os.chmod(self.childpath, old_child_mode)
36
37 # Clean up.
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000038 shutil.rmtree(TESTFN)
39
40 def check_args_to_onerror(self, func, arg, exc):
41 if self.errorState == 0:
42 self.assertEqual(func, os.remove)
Tim Peters4590c002004-11-01 02:40:52 +000043 self.assertEqual(arg, self.childpath)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000044 self.assertEqual(exc[0], OSError)
45 self.errorState = 1
46 else:
47 self.assertEqual(func, os.rmdir)
48 self.assertEqual(arg, TESTFN)
49 self.assertEqual(exc[0], OSError)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000050
Johannes Gijsbersd60e92a2004-09-11 21:26:21 +000051 def test_rmtree_dont_delete_file(self):
52 # When called on a file instead of a directory, don't delete it.
53 handle, path = tempfile.mkstemp()
54 os.fdopen(handle).close()
55 self.assertRaises(OSError, shutil.rmtree, path)
56 os.remove(path)
57
Brett Cannon1c3fa182004-06-19 21:11:35 +000058 def test_dont_move_dir_in_itself(self):
59 src_dir = tempfile.mkdtemp()
60 try:
61 dst = os.path.join(src_dir, 'foo')
62 self.assertRaises(shutil.Error, shutil.move, src_dir, dst)
63 finally:
64 try:
65 os.rmdir(src_dir)
66 except:
67 pass
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000068
Johannes Gijsbers46f14592004-08-14 13:30:02 +000069 if hasattr(os, "symlink"):
70 def test_dont_copy_file_onto_link_to_itself(self):
71 # bug 851123.
72 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +000073 src = os.path.join(TESTFN, 'cheese')
74 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +000075 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +000076 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +000077 f.write('cheddar')
78 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +000079
80 os.link(src, dst)
81 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
82 self.assertEqual(open(src,'r').read(), 'cheddar')
83 os.remove(dst)
84
85 # Using `src` here would mean we end up with a symlink pointing
86 # to TESTFN/TESTFN/cheese, while it should point at
87 # TESTFN/cheese.
88 os.symlink('cheese', dst)
89 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
90 self.assertEqual(open(src,'r').read(), 'cheddar')
91 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +000092 finally:
93 try:
94 shutil.rmtree(TESTFN)
95 except OSError:
96 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +000097
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000098def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +000099 test_support.run_unittest(TestShutil)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000100
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000101if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000102 test_main()