blob: b63031ebc60a63e8aedf0eb60fece9d13079e662 [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 Gijsbersb8b09d02004-12-06 20:50:15 +000019 # 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 Gijsbers6b220b02004-12-12 15:52:57 +000022 and not (hasattr(os, 'geteuid') and os.geteuid() == 0)):
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000023 def test_on_error(self):
24 self.errorState = 0
25 os.mkdir(TESTFN)
Tim Peters4590c002004-11-01 02:40:52 +000026 self.childpath = os.path.join(TESTFN, 'a')
27 f = open(self.childpath, 'w')
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000028 f.close()
Tim Peters4590c002004-11-01 02:40:52 +000029 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 Gijsbersef5ffc42004-10-31 12:05:31 +000034
35 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000036 # Test whether onerror has actually been called.
Johannes Gijsbersb8b09d02004-12-06 20:50:15 +000037 self.assertEqual(self.errorState, 2,
38 "Expected call to onerror function did not happen.")
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000039
Tim Peters4590c002004-11-01 02:40:52 +000040 # Make writable again.
41 os.chmod(TESTFN, old_dir_mode)
42 os.chmod(self.childpath, old_child_mode)
43
44 # Clean up.
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000045 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 Peters4590c002004-11-01 02:40:52 +000050 self.assertEqual(arg, self.childpath)
Thomas Wouters477c8d52006-05-27 19:21:47 +000051 self.failUnless(issubclass(exc[0], OSError))
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000052 self.errorState = 1
53 else:
54 self.assertEqual(func, os.rmdir)
55 self.assertEqual(arg, TESTFN)
Thomas Wouters477c8d52006-05-27 19:21:47 +000056 self.failUnless(issubclass(exc[0], OSError))
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +000057 self.errorState = 2
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000058
Johannes Gijsbersd60e92a2004-09-11 21:26:21 +000059 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 Cannon1c3fa182004-06-19 21:11:35 +000066 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 Warsaw7fc2cca2003-01-24 17:34:13 +000076
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077 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)
Christian Heimese052dd82007-11-20 03:20:04 +0000116 for path in (src_dir,
117 os.path.abspath(os.path.join(dst_dir, os.path.pardir))
118 ):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000119 if os.path.exists(path):
Christian Heimes94140152007-11-20 01:45:17 +0000120 shutil.rmtree(path)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000121
122
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000123 if hasattr(os, "symlink"):
124 def test_dont_copy_file_onto_link_to_itself(self):
125 # bug 851123.
126 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +0000127 src = os.path.join(TESTFN, 'cheese')
128 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000129 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +0000130 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000131 f.write('cheddar')
132 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +0000133
134 os.link(src, dst)
135 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
136 self.assertEqual(open(src,'r').read(), 'cheddar')
137 os.remove(dst)
138
139 # Using `src` here would mean we end up with a symlink pointing
140 # to TESTFN/TESTFN/cheese, while it should point at
141 # TESTFN/cheese.
142 os.symlink('cheese', dst)
143 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
144 self.assertEqual(open(src,'r').read(), 'cheddar')
145 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000146 finally:
147 try:
148 shutil.rmtree(TESTFN)
149 except OSError:
150 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +0000151
Christian Heimes9bd667a2008-01-20 15:14:11 +0000152 def test_rmtree_on_symlink(self):
153 # bug 1669.
154 os.mkdir(TESTFN)
155 try:
156 src = os.path.join(TESTFN, 'cheese')
157 dst = os.path.join(TESTFN, 'shop')
158 os.mkdir(src)
159 os.symlink(src, dst)
160 self.assertRaises(OSError, shutil.rmtree, dst)
161 finally:
162 shutil.rmtree(TESTFN, ignore_errors=True)
163
164
165
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000166def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000167 test_support.run_unittest(TestShutil)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000168
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000169if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000170 test_main()