blob: d6160c6b409e28e5a3df54a2cc0b0c31e513fc45 [file] [log] [blame]
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +00001
Barry Warsaw7fc2cca2003-01-24 17:34:13 +00002# Copyright (C) 2003 Python Software Foundation
3
4import unittest
5import shutil
6import tempfile
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
19 if hasattr(os, 'chmod'):
20 def test_on_error(self):
21 self.errorState = 0
22 os.mkdir(TESTFN)
23 f = open(os.path.join(TESTFN, 'a'), 'w')
24 f.close()
25 # Make TESTFN unwritable.
26 os.chmod(TESTFN, stat.S_IRUSR)
27
28 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
29
30 # Make TESTFN writable again.
31 os.chmod(TESTFN, stat.S_IRWXU)
32 shutil.rmtree(TESTFN)
33
34 def check_args_to_onerror(self, func, arg, exc):
35 if self.errorState == 0:
36 self.assertEqual(func, os.remove)
37 self.assertEqual(arg, os.path.join(TESTFN, 'a'))
38 self.assertEqual(exc[0], OSError)
39 self.errorState = 1
40 else:
41 self.assertEqual(func, os.rmdir)
42 self.assertEqual(arg, TESTFN)
43 self.assertEqual(exc[0], OSError)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000044
Johannes Gijsbersd60e92a2004-09-11 21:26:21 +000045 def test_rmtree_dont_delete_file(self):
46 # When called on a file instead of a directory, don't delete it.
47 handle, path = tempfile.mkstemp()
48 os.fdopen(handle).close()
49 self.assertRaises(OSError, shutil.rmtree, path)
50 os.remove(path)
51
Brett Cannon1c3fa182004-06-19 21:11:35 +000052 def test_dont_move_dir_in_itself(self):
53 src_dir = tempfile.mkdtemp()
54 try:
55 dst = os.path.join(src_dir, 'foo')
56 self.assertRaises(shutil.Error, shutil.move, src_dir, dst)
57 finally:
58 try:
59 os.rmdir(src_dir)
60 except:
61 pass
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000062
Johannes Gijsbers46f14592004-08-14 13:30:02 +000063 if hasattr(os, "symlink"):
64 def test_dont_copy_file_onto_link_to_itself(self):
65 # bug 851123.
66 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +000067 src = os.path.join(TESTFN, 'cheese')
68 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +000069 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +000070 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +000071 f.write('cheddar')
72 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +000073
74 os.link(src, dst)
75 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
76 self.assertEqual(open(src,'r').read(), 'cheddar')
77 os.remove(dst)
78
79 # Using `src` here would mean we end up with a symlink pointing
80 # to TESTFN/TESTFN/cheese, while it should point at
81 # TESTFN/cheese.
82 os.symlink('cheese', dst)
83 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
84 self.assertEqual(open(src,'r').read(), 'cheddar')
85 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +000086 finally:
87 try:
88 shutil.rmtree(TESTFN)
89 except OSError:
90 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +000091
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000092def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +000093 test_support.run_unittest(TestShutil)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000094
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000095if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +000096 test_main()