]> granicus.if.org Git - python/commitdiff
Add tests for tarfile extractall feature when with symlinks
authorSenthil Kumaran <orsenthil@gmail.com>
Thu, 28 Apr 2011 07:38:12 +0000 (15:38 +0800)
committerSenthil Kumaran <orsenthil@gmail.com>
Thu, 28 Apr 2011 07:38:12 +0000 (15:38 +0800)
Lib/test/test_tarfile.py

index 124f0e97f8455ac66c7f1d78ac18f0383a9619f2..487f28f6223687343ae617641efd03ff9360e9ab 100644 (file)
@@ -678,6 +678,33 @@ class WriteTest(WriteTestBase):
         finally:
             shutil.rmtree(tempdir)
 
+    def test_extractall_symlinks(self):
+        # Test if extractall works properly when tarfile contains symlinks
+        tempdir = os.path.join(TEMPDIR, "testsymlinks")
+        temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
+        os.mkdir(tempdir)
+        try:
+            source_file = os.path.join(tempdir,'source')
+            target_file = os.path.join(tempdir,'symlink')
+            with open(source_file,'w') as f:
+                f.write('something\n')
+            os.symlink(source_file, target_file)
+            tar = tarfile.open(temparchive,'w')
+            tar.add(source_file)
+            tar.add(target_file)
+            tar.close()
+            # Let's extract it to the location which contains the symlink
+            tar = tarfile.open(temparchive,'r')
+            # this should not raise OSError: [Errno 17] File exists
+            try:
+                tar.extractall(path=tempdir)
+            except OSError:
+                self.fail("extractall failed with symlinked files")
+            finally:
+                tar.close()
+        finally:
+            os.unlink(temparchive)
+            shutil.rmtree(tempdir)
 
 class StreamWriteTest(WriteTestBase):