diff options
author | Hynek Schlawack <hs@ox.cx> | 2012-06-28 12:07:29 +0200 |
---|---|---|
committer | Hynek Schlawack <hs@ox.cx> | 2012-06-28 12:07:29 +0200 |
commit | a75cd1ce73f627f12329b782922dfc85387be3cc (patch) | |
tree | 461078a872fb285108925cf83c01cd8a6b5cf26a /Lib/test | |
parent | 591c1cca325af53db16b302fac212107d5500ec9 (diff) | |
download | cpython-git-a75cd1ce73f627f12329b782922dfc85387be3cc.tar.gz |
#4489: Don't follow ever symlinks in rmtree
Also added several regression tests.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_shutil.py | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 49be391ec5..cb712b3e61 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -117,6 +117,38 @@ class TestShutil(unittest.TestCase): self.assertIsInstance(victim, bytes) shutil.rmtree(victim) + @support.skip_unless_symlink + def test_rmtree_fails_on_symlink(self): + tmp = self.mkdtemp() + dir_ = os.path.join(tmp, 'dir') + os.mkdir(dir_) + link = os.path.join(tmp, 'link') + os.symlink(dir_, link) + self.assertRaises(OSError, shutil.rmtree, link) + self.assertTrue(os.path.exists(dir_)) + + @support.skip_unless_symlink + def test_rmtree_works_on_symlinks(self): + tmp = self.mkdtemp() + dir1 = os.path.join(tmp, 'dir1') + dir2 = os.path.join(dir1, 'dir2') + dir3 = os.path.join(tmp, 'dir3') + for d in dir1, dir2, dir3: + os.mkdir(d) + file1 = os.path.join(tmp, 'file1') + write_file(file1, 'foo') + link1 = os.path.join(dir1, 'link1') + os.symlink(dir2, link1) + link2 = os.path.join(dir1, 'link2') + os.symlink(dir3, link2) + link3 = os.path.join(dir1, 'link3') + os.symlink(file1, link3) + # make sure symlinks are removed but not followed + shutil.rmtree(dir1) + self.assertFalse(os.path.exists(dir1)) + self.assertTrue(os.path.exists(dir3)) + self.assertTrue(os.path.exists(file1)) + def test_rmtree_errors(self): # filename is guaranteed not to exist filename = tempfile.mktemp() @@ -184,7 +216,7 @@ class TestShutil(unittest.TestCase): def test_rmtree_does_not_choke_on_failing_lstat(self): try: orig_lstat = os.lstat - def raiser(fn): + def raiser(fn, *args, **kwargs): if fn != TESTFN: raise OSError() else: |