mirror of https://github.com/python/cpython.git
make sure os.symlink presence is tested before running some shutil tests -- also refactored the tests to use unittest.skipUnless
This commit is contained in:
parent
14284c2f3b
commit
51a6f72d19
|
@ -263,35 +263,36 @@ def _filter(src, names):
|
||||||
shutil.rmtree(src_dir)
|
shutil.rmtree(src_dir)
|
||||||
shutil.rmtree(os.path.dirname(dst_dir))
|
shutil.rmtree(os.path.dirname(dst_dir))
|
||||||
|
|
||||||
if hasattr(os, "symlink"):
|
@unittest.skipUnless(hasattr(os, 'symlink'), 'requires os.symlink')
|
||||||
def test_dont_copy_file_onto_link_to_itself(self):
|
def test_dont_copy_file_onto_link_to_itself(self):
|
||||||
# bug 851123.
|
# bug 851123.
|
||||||
os.mkdir(TESTFN)
|
os.mkdir(TESTFN)
|
||||||
src = os.path.join(TESTFN, 'cheese')
|
src = os.path.join(TESTFN, 'cheese')
|
||||||
dst = os.path.join(TESTFN, 'shop')
|
dst = os.path.join(TESTFN, 'shop')
|
||||||
|
try:
|
||||||
|
f = open(src, 'w')
|
||||||
|
f.write('cheddar')
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
os.link(src, dst)
|
||||||
|
self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
|
||||||
|
self.assertEqual(open(src,'r').read(), 'cheddar')
|
||||||
|
os.remove(dst)
|
||||||
|
|
||||||
|
# Using `src` here would mean we end up with a symlink pointing
|
||||||
|
# to TESTFN/TESTFN/cheese, while it should point at
|
||||||
|
# TESTFN/cheese.
|
||||||
|
os.symlink('cheese', dst)
|
||||||
|
self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
|
||||||
|
self.assertEqual(open(src,'r').read(), 'cheddar')
|
||||||
|
os.remove(dst)
|
||||||
|
finally:
|
||||||
try:
|
try:
|
||||||
f = open(src, 'w')
|
shutil.rmtree(TESTFN)
|
||||||
f.write('cheddar')
|
except OSError:
|
||||||
f.close()
|
pass
|
||||||
|
|
||||||
os.link(src, dst)
|
|
||||||
self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
|
|
||||||
self.assertEqual(open(src,'r').read(), 'cheddar')
|
|
||||||
os.remove(dst)
|
|
||||||
|
|
||||||
# Using `src` here would mean we end up with a symlink pointing
|
|
||||||
# to TESTFN/TESTFN/cheese, while it should point at
|
|
||||||
# TESTFN/cheese.
|
|
||||||
os.symlink('cheese', 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
|
|
||||||
|
|
||||||
|
@unittest.skipUnless(hasattr(os, 'symlink'), 'requires os.symlink')
|
||||||
def test_rmtree_on_symlink(self):
|
def test_rmtree_on_symlink(self):
|
||||||
# bug 1669.
|
# bug 1669.
|
||||||
os.mkdir(TESTFN)
|
os.mkdir(TESTFN)
|
||||||
|
@ -304,37 +305,38 @@ def test_rmtree_on_symlink(self):
|
||||||
finally:
|
finally:
|
||||||
shutil.rmtree(TESTFN, ignore_errors=True)
|
shutil.rmtree(TESTFN, ignore_errors=True)
|
||||||
|
|
||||||
if hasattr(os, "mkfifo"):
|
@unittest.skipUnless(hasattr(os, 'mkfifo'), 'requires os.mkfifo')
|
||||||
# Issue #3002: copyfile and copytree block indefinitely on named pipes
|
# Issue #3002: copyfile and copytree block indefinitely on named pipes
|
||||||
def test_copyfile_named_pipe(self):
|
def test_copyfile_named_pipe(self):
|
||||||
os.mkfifo(TESTFN)
|
os.mkfifo(TESTFN)
|
||||||
try:
|
try:
|
||||||
self.assertRaises(shutil.SpecialFileError,
|
self.assertRaises(shutil.SpecialFileError,
|
||||||
shutil.copyfile, TESTFN, TESTFN2)
|
shutil.copyfile, TESTFN, TESTFN2)
|
||||||
self.assertRaises(shutil.SpecialFileError,
|
self.assertRaises(shutil.SpecialFileError,
|
||||||
shutil.copyfile, __file__, TESTFN)
|
shutil.copyfile, __file__, TESTFN)
|
||||||
finally:
|
finally:
|
||||||
os.remove(TESTFN)
|
os.remove(TESTFN)
|
||||||
|
|
||||||
def test_copytree_named_pipe(self):
|
@unittest.skipUnless(hasattr(os, 'mkfifo'), 'requires os.mkfifo')
|
||||||
os.mkdir(TESTFN)
|
def test_copytree_named_pipe(self):
|
||||||
|
os.mkdir(TESTFN)
|
||||||
|
try:
|
||||||
|
subdir = os.path.join(TESTFN, "subdir")
|
||||||
|
os.mkdir(subdir)
|
||||||
|
pipe = os.path.join(subdir, "mypipe")
|
||||||
|
os.mkfifo(pipe)
|
||||||
try:
|
try:
|
||||||
subdir = os.path.join(TESTFN, "subdir")
|
shutil.copytree(TESTFN, TESTFN2)
|
||||||
os.mkdir(subdir)
|
except shutil.Error as e:
|
||||||
pipe = os.path.join(subdir, "mypipe")
|
errors = e.args[0]
|
||||||
os.mkfifo(pipe)
|
self.assertEqual(len(errors), 1)
|
||||||
try:
|
src, dst, error_msg = errors[0]
|
||||||
shutil.copytree(TESTFN, TESTFN2)
|
self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
|
||||||
except shutil.Error as e:
|
else:
|
||||||
errors = e.args[0]
|
self.fail("shutil.Error should have been raised")
|
||||||
self.assertEqual(len(errors), 1)
|
finally:
|
||||||
src, dst, error_msg = errors[0]
|
shutil.rmtree(TESTFN, ignore_errors=True)
|
||||||
self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
|
shutil.rmtree(TESTFN2, ignore_errors=True)
|
||||||
else:
|
|
||||||
self.fail("shutil.Error should have been raised")
|
|
||||||
finally:
|
|
||||||
shutil.rmtree(TESTFN, ignore_errors=True)
|
|
||||||
shutil.rmtree(TESTFN2, ignore_errors=True)
|
|
||||||
|
|
||||||
def test_copytree_special_func(self):
|
def test_copytree_special_func(self):
|
||||||
|
|
||||||
|
@ -351,6 +353,7 @@ def _copy(src, dst):
|
||||||
shutil.copytree(src_dir, dst_dir, copy_function=_copy)
|
shutil.copytree(src_dir, dst_dir, copy_function=_copy)
|
||||||
self.assertEquals(len(copied), 2)
|
self.assertEquals(len(copied), 2)
|
||||||
|
|
||||||
|
@unittest.skipUnless(hasattr(os, 'symlink'), 'requires os.symlink')
|
||||||
def test_copytree_dangling_symlinks(self):
|
def test_copytree_dangling_symlinks(self):
|
||||||
|
|
||||||
# a dangling symlink raises an error at the end
|
# a dangling symlink raises an error at the end
|
||||||
|
|
Loading…
Reference in New Issue