blob: 9e5f6ff0489607998e59542d69438b386a20beaf [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
Brett Cannon1c3fa182004-06-19 21:11:35 +000019 def test_dont_move_dir_in_itself(self):
20 src_dir = tempfile.mkdtemp()
21 try:
22 dst = os.path.join(src_dir, 'foo')
23 self.assertRaises(shutil.Error, shutil.move, src_dir, dst)
24 finally:
25 try:
26 os.rmdir(src_dir)
27 except:
28 pass
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000029
Johannes Gijsbers46f14592004-08-14 13:30:02 +000030 if hasattr(os, "symlink"):
31 def test_dont_copy_file_onto_link_to_itself(self):
32 # bug 851123.
33 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +000034 src = os.path.join(TESTFN, 'cheese')
35 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +000036 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +000037 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +000038 f.write('cheddar')
39 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +000040
41 os.link(src, dst)
42 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
43 self.assertEqual(open(src,'r').read(), 'cheddar')
44 os.remove(dst)
45
46 # Using `src` here would mean we end up with a symlink pointing
47 # to TESTFN/TESTFN/cheese, while it should point at
48 # TESTFN/cheese.
49 os.symlink('cheese', dst)
50 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
51 self.assertEqual(open(src,'r').read(), 'cheddar')
52 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +000053 finally:
54 try:
55 shutil.rmtree(TESTFN)
56 except OSError:
57 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +000058
59
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000060def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +000061 test_support.run_unittest(TestShutil)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000062
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000063if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +000064 test_main()