blob: da71fa83bcd886cc0a1e1df4116568f10219cf1a [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)
116 for path in (
117 os.path.join(src_dir, 'test_dir'),
118 os.path.join(dst_dir, 'test_dir'),
119 ):
120 if os.path.exists(path):
121 os.removedirs(path)
122
123
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000124 if hasattr(os, "symlink"):
125 def test_dont_copy_file_onto_link_to_itself(self):
126 # bug 851123.
127 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +0000128 src = os.path.join(TESTFN, 'cheese')
129 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000130 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +0000131 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000132 f.write('cheddar')
133 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +0000134
135 os.link(src, dst)
136 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
137 self.assertEqual(open(src,'r').read(), 'cheddar')
138 os.remove(dst)
139
140 # Using `src` here would mean we end up with a symlink pointing
141 # to TESTFN/TESTFN/cheese, while it should point at
142 # TESTFN/cheese.
143 os.symlink('cheese', dst)
144 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
145 self.assertEqual(open(src,'r').read(), 'cheddar')
146 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000147 finally:
148 try:
149 shutil.rmtree(TESTFN)
150 except OSError:
151 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +0000152
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000153def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000154 test_support.run_unittest(TestShutil)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000155
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000156if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000157 test_main()