blob: 1db3fc527e4ed2bb07117765e304a57939c559e9 [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 Gijsbers8e6f2de2004-11-23 09:27:27 +00006import sys
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +00007import stat
Brett Cannon1c3fa182004-06-19 21:11:35 +00008import os
9import os.path
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000010from test import test_support
Johannes Gijsbers46f14592004-08-14 13:30:02 +000011from test.test_support import TESTFN
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000012
13class 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 Gijsbersef5ffc42004-10-31 12:05:31 +000018
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000019 if hasattr(os, 'chmod') and sys.platform[:6] != 'cygwin':
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000020 def test_on_error(self):
21 self.errorState = 0
22 os.mkdir(TESTFN)
Tim Peters4590c002004-11-01 02:40:52 +000023 self.childpath = os.path.join(TESTFN, 'a')
24 f = open(self.childpath, 'w')
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000025 f.close()
Tim Peters4590c002004-11-01 02:40:52 +000026 old_dir_mode = os.stat(TESTFN).st_mode
27 old_child_mode = os.stat(self.childpath).st_mode
28 # Make unwritable.
29 os.chmod(self.childpath, stat.S_IREAD)
30 os.chmod(TESTFN, stat.S_IREAD)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000031
32 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000033 # Test whether onerror has actually been called.
34 self.assertEqual(self.errorState, 2)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000035
Tim Peters4590c002004-11-01 02:40:52 +000036 # Make writable again.
37 os.chmod(TESTFN, old_dir_mode)
38 os.chmod(self.childpath, old_child_mode)
39
40 # Clean up.
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000041 shutil.rmtree(TESTFN)
42
43 def check_args_to_onerror(self, func, arg, exc):
44 if self.errorState == 0:
45 self.assertEqual(func, os.remove)
Tim Peters4590c002004-11-01 02:40:52 +000046 self.assertEqual(arg, self.childpath)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000047 self.assertEqual(exc[0], OSError)
48 self.errorState = 1
49 else:
50 self.assertEqual(func, os.rmdir)
51 self.assertEqual(arg, TESTFN)
52 self.assertEqual(exc[0], OSError)
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000053 self.errorState = 2
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000054
Johannes Gijsbersd60e92a2004-09-11 21:26:21 +000055 def test_rmtree_dont_delete_file(self):
56 # When called on a file instead of a directory, don't delete it.
57 handle, path = tempfile.mkstemp()
58 os.fdopen(handle).close()
59 self.assertRaises(OSError, shutil.rmtree, path)
60 os.remove(path)
61
Brett Cannon1c3fa182004-06-19 21:11:35 +000062 def test_dont_move_dir_in_itself(self):
63 src_dir = tempfile.mkdtemp()
64 try:
65 dst = os.path.join(src_dir, 'foo')
66 self.assertRaises(shutil.Error, shutil.move, src_dir, dst)
67 finally:
68 try:
69 os.rmdir(src_dir)
70 except:
71 pass
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000072
Johannes Gijsbers46f14592004-08-14 13:30:02 +000073 if hasattr(os, "symlink"):
74 def test_dont_copy_file_onto_link_to_itself(self):
75 # bug 851123.
76 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +000077 src = os.path.join(TESTFN, 'cheese')
78 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +000079 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +000080 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +000081 f.write('cheddar')
82 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +000083
84 os.link(src, dst)
85 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
86 self.assertEqual(open(src,'r').read(), 'cheddar')
87 os.remove(dst)
88
89 # Using `src` here would mean we end up with a symlink pointing
90 # to TESTFN/TESTFN/cheese, while it should point at
91 # TESTFN/cheese.
92 os.symlink('cheese', dst)
93 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
94 self.assertEqual(open(src,'r').read(), 'cheddar')
95 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +000096 finally:
97 try:
98 shutil.rmtree(TESTFN)
99 except OSError:
100 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +0000101
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000102def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000103 test_support.run_unittest(TestShutil)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000104
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000105if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000106 test_main()