blob: f8367a60c20badfb60d9995e2d4831a0952d494f [file] [log] [blame]
Barry Warsaw7fc2cca2003-01-24 17:34:13 +00001# Copyright (C) 2003 Python Software Foundation
2
3import unittest
4import shutil
5import tempfile
Brett Cannon1c3fa182004-06-19 21:11:35 +00006import os
7import os.path
Barry Warsaw7fc2cca2003-01-24 17:34:13 +00008from test import test_support
Johannes Gijsbers46f14592004-08-14 13:30:02 +00009from test.test_support import TESTFN
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000010
11class TestShutil(unittest.TestCase):
12 def test_rmtree_errors(self):
13 # filename is guaranteed not to exist
14 filename = tempfile.mktemp()
15 self.assertRaises(OSError, shutil.rmtree, filename)
16 self.assertEqual(shutil.rmtree(filename, True), None)
Guido van Rossum8cec3ab2004-07-14 00:48:58 +000017 shutil.rmtree(filename, False, lambda func, arg, exc: None)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000018
Johannes Gijsbersd60e92a2004-09-11 21:26:21 +000019 def test_rmtree_dont_delete_file(self):
20 # When called on a file instead of a directory, don't delete it.
21 handle, path = tempfile.mkstemp()
22 os.fdopen(handle).close()
23 self.assertRaises(OSError, shutil.rmtree, path)
24 os.remove(path)
25
Brett Cannon1c3fa182004-06-19 21:11:35 +000026 def test_dont_move_dir_in_itself(self):
27 src_dir = tempfile.mkdtemp()
28 try:
29 dst = os.path.join(src_dir, 'foo')
30 self.assertRaises(shutil.Error, shutil.move, src_dir, dst)
31 finally:
32 try:
33 os.rmdir(src_dir)
34 except:
35 pass
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000036
Johannes Gijsbers46f14592004-08-14 13:30:02 +000037 if hasattr(os, "symlink"):
38 def test_dont_copy_file_onto_link_to_itself(self):
39 # bug 851123.
40 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +000041 src = os.path.join(TESTFN, 'cheese')
42 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +000043 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +000044 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +000045 f.write('cheddar')
46 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +000047
48 os.link(src, dst)
49 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
50 self.assertEqual(open(src,'r').read(), 'cheddar')
51 os.remove(dst)
52
53 # Using `src` here would mean we end up with a symlink pointing
54 # to TESTFN/TESTFN/cheese, while it should point at
55 # TESTFN/cheese.
56 os.symlink('cheese', dst)
57 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
58 self.assertEqual(open(src,'r').read(), 'cheddar')
59 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +000060 finally:
61 try:
62 shutil.rmtree(TESTFN)
63 except OSError:
64 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +000065
66
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000067def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +000068 test_support.run_unittest(TestShutil)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000069
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000070if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +000071 test_main()