2003-01-24 17:34:13 +00:00
|
|
|
# Copyright (C) 2003 Python Software Foundation
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
2004-06-19 21:11:35 +00:00
|
|
|
import os
|
|
|
|
import os.path
|
2003-01-24 17:34:13 +00:00
|
|
|
from test import test_support
|
2004-08-14 13:30:02 +00:00
|
|
|
from test.test_support import TESTFN
|
2003-01-24 17:34:13 +00:00
|
|
|
|
|
|
|
class TestShutil(unittest.TestCase):
|
|
|
|
def test_rmtree_errors(self):
|
|
|
|
# filename is guaranteed not to exist
|
|
|
|
filename = tempfile.mktemp()
|
|
|
|
self.assertRaises(OSError, shutil.rmtree, filename)
|
|
|
|
self.assertEqual(shutil.rmtree(filename, True), None)
|
2004-07-14 00:48:58 +00:00
|
|
|
shutil.rmtree(filename, False, lambda func, arg, exc: None)
|
2003-01-24 17:34:13 +00:00
|
|
|
|
2004-06-19 21:11:35 +00:00
|
|
|
def test_dont_move_dir_in_itself(self):
|
|
|
|
src_dir = tempfile.mkdtemp()
|
|
|
|
try:
|
|
|
|
dst = os.path.join(src_dir, 'foo')
|
|
|
|
self.assertRaises(shutil.Error, shutil.move, src_dir, dst)
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
os.rmdir(src_dir)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2004-08-14 13:30:02 +00:00
|
|
|
if hasattr(os, "symlink"):
|
|
|
|
def test_dont_copy_file_onto_link_to_itself(self):
|
|
|
|
# bug 851123.
|
|
|
|
os.mkdir(TESTFN)
|
|
|
|
src = os.path.join(TESTFN,'cheese')
|
|
|
|
dst = os.path.join(TESTFN,'shop')
|
|
|
|
try:
|
|
|
|
f = open(src,'w')
|
|
|
|
f.write('cheddar')
|
|
|
|
f.close()
|
|
|
|
for funcname in 'link','symlink':
|
|
|
|
getattr(os, funcname)(src, dst)
|
|
|
|
self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
|
|
|
|
self.assertEqual(open(src,'r').read(), 'cheddar')
|
|
|
|
os.remove(dst)
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
shutil.rmtree(TESTFN)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2004-06-19 21:11:35 +00:00
|
|
|
|
2003-01-24 17:34:13 +00:00
|
|
|
|
|
|
|
def test_main():
|
2003-05-01 17:45:56 +00:00
|
|
|
test_support.run_unittest(TestShutil)
|
2003-01-24 17:34:13 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2003-05-01 17:45:56 +00:00
|
|
|
test_main()
|