Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(14857)

Side by Side Diff: Lib/test/test_tarfile.py

Issue 19974: tarfile doesn't overwrite symlink by directory
Patch Set: Created 5 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« Lib/tarfile.py ('K') | « Lib/tarfile.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import sys 1 import sys
2 import os 2 import os
3 import io 3 import io
4 import shutil 4 import shutil
5 from hashlib import md5 5 from hashlib import md5
6 6
7 import unittest 7 import unittest
8 import tarfile 8 import tarfile
9 9
10 from test import support, script_helper 10 from test import support, script_helper
(...skipping 1050 matching lines...) Expand 10 before | Expand all | Expand 10 after
1061 self.assertEqual(t.name, cmp_path or path.replace(os.sep, "/")) 1061 self.assertEqual(t.name, cmp_path or path.replace(os.sep, "/"))
1062 1062
1063 1063
1064 @support.skip_unless_symlink 1064 @support.skip_unless_symlink
1065 def test_extractall_symlinks(self): 1065 def test_extractall_symlinks(self):
1066 # Test if extractall works properly when tarfile contains symlinks 1066 # Test if extractall works properly when tarfile contains symlinks
1067 tempdir = os.path.join(TEMPDIR, "testsymlinks") 1067 tempdir = os.path.join(TEMPDIR, "testsymlinks")
1068 temparchive = os.path.join(TEMPDIR, "testsymlinks.tar") 1068 temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
1069 os.mkdir(tempdir) 1069 os.mkdir(tempdir)
1070 try: 1070 try:
1071 source_file = os.path.join(tempdir,'source') 1071 source_file = os.path.join(tempdir, 'source')
1072 other_source_file = os.path.join(tempdir, 'other_source')
1072 target_file = os.path.join(tempdir,'symlink') 1073 target_file = os.path.join(tempdir,'symlink')
1073 with open(source_file,'w') as f: 1074 open(source_file, 'w').close()
storchaka 2013/12/15 21:38:11 Now source file is empty. It will be better make i
vajrasky 2014/01/16 08:21:52 Okay, I will.
1074 f.write('something\n') 1075 open(other_source_file, 'w').close()
1075 os.symlink(source_file, target_file) 1076 os.symlink(source_file, target_file)
1076 tar = tarfile.open(temparchive,'w') 1077 tar = tarfile.open(temparchive,'w')
1077 tar.add(source_file) 1078 tar.add(source_file)
1078 tar.add(target_file) 1079 tar.add(target_file)
1079 tar.close() 1080 tar.close()
1081 # Point target_file to other_source_file to exercise overwriting
1082 # behavior.
1083 os.unlink(target_file)
1084 os.symlink(other_source_file, target_file)
1080 # Let's extract it to the location which contains the symlink 1085 # Let's extract it to the location which contains the symlink
1081 tar = tarfile.open(temparchive,'r') 1086 tar = tarfile.open(temparchive,'r')
1082 # this should not raise OSError: [Errno 17] File exists 1087 # This should not raise OSError: [Errno 17] File exists
1083 try: 1088 try:
1084 tar.extractall(path=tempdir) 1089 tar.extractall(path=tempdir)
1090 # target_file that symlinked to other_source_file now symlinks
1091 # back to source_file.
1092 self.assertEqual(os.readlink(target_file), source_file)
storchaka 2013/12/15 21:38:11 Add a check that content (or md5sum) of other_sour
vajrasky 2014/01/16 08:21:52 Okay, I will.
1085 except OSError: 1093 except OSError:
1086 self.fail("extractall failed with symlinked files") 1094 self.fail("extractall failed with symlinked files")
1087 finally: 1095 finally:
1088 tar.close() 1096 tar.close()
1089 finally: 1097 finally:
1090 os.unlink(temparchive) 1098 os.unlink(temparchive)
1091 shutil.rmtree(tempdir) 1099 shutil.rmtree(tempdir)
1092 1100
1093 def test_pathnames(self): 1101 def test_pathnames(self):
1094 self._test_pathname("foo") 1102 self._test_pathname("foo")
(...skipping 932 matching lines...) Expand 10 before | Expand all | Expand 10 after
2027 testtarnames.append(c.tarname) 2035 testtarnames.append(c.tarname)
2028 with c.open(c.tarname, "wb") as tar: 2036 with c.open(c.tarname, "wb") as tar:
2029 tar.write(data) 2037 tar.write(data)
2030 2038
2031 def tearDownModule(): 2039 def tearDownModule():
2032 if os.path.exists(TEMPDIR): 2040 if os.path.exists(TEMPDIR):
2033 shutil.rmtree(TEMPDIR) 2041 shutil.rmtree(TEMPDIR)
2034 2042
2035 if __name__ == "__main__": 2043 if __name__ == "__main__":
2036 unittest.main() 2044 unittest.main()
OLDNEW
« Lib/tarfile.py ('K') | « Lib/tarfile.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+