Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes | Private Member Functions
test.test_tarfile.WriteTest Class Reference
Inheritance diagram for test.test_tarfile.WriteTest:
Inheritance graph
[legend]
Collaboration diagram for test.test_tarfile.WriteTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_100_char_name
def test_tar_size
def test_file_size
def test_directory_size
def test_link_size
def test_symlink_size
def test_add_self
def test_exclude
def test_filter
def test_extractall_symlinks
def test_pathnames
def test_abs_pathnames
def test_cwd
def test_fileobj_no_close

Static Public Attributes

string mode = "w:"

Private Member Functions

def _test_pathname

Detailed Description

Definition at line 772 of file test_tarfile.py.


Member Function Documentation

def test.test_tarfile.WriteTest._test_pathname (   self,
  path,
  cmp_path = None,
  dir = False 
) [private]

Definition at line 957 of file test_tarfile.py.

00957 
00958     def _test_pathname(self, path, cmp_path=None, dir=False):
00959         # Create a tarfile with an empty member named path
00960         # and compare the stored name with the original.
00961         foo = os.path.join(TEMPDIR, "foo")
00962         if not dir:
00963             open(foo, "w").close()
00964         else:
00965             os.mkdir(foo)
00966 
00967         tar = tarfile.open(tmpname, self.mode)
00968         try:
00969             tar.add(foo, arcname=path)
00970         finally:
00971             tar.close()
00972 
00973         tar = tarfile.open(tmpname, "r")
00974         try:
00975             t = tar.next()
00976         finally:
00977             tar.close()
00978 
00979         if not dir:
00980             os.remove(foo)
00981         else:
00982             os.rmdir(foo)
00983 
00984         self.assertEqual(t.name, cmp_path or path.replace(os.sep, "/"))
00985 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 776 of file test_tarfile.py.

00776 
00777     def test_100_char_name(self):
00778         # The name field in a tar header stores strings of at most 100 chars.
00779         # If a string is shorter than 100 chars it has to be padded with '\0',
00780         # which implies that a string of exactly 100 chars is stored without
00781         # a trailing '\0'.
00782         name = "0123456789" * 10
00783         tar = tarfile.open(tmpname, self.mode)
00784         try:
00785             t = tarfile.TarInfo(name)
00786             tar.addfile(t)
00787         finally:
00788             tar.close()
00789 
00790         tar = tarfile.open(tmpname)
00791         try:
00792             self.assertTrue(tar.getnames()[0] == name,
00793                     "failed to store 100 char filename")
00794         finally:
00795             tar.close()

Here is the call graph for this function:

Definition at line 1032 of file test_tarfile.py.

01032 
01033     def test_abs_pathnames(self):
01034         if sys.platform == "win32":
01035             self._test_pathname("C:\\foo", "foo")
01036         else:
01037             self._test_pathname("/foo", "foo")
01038             self._test_pathname("///foo", "foo")

Here is the call graph for this function:

Definition at line 873 of file test_tarfile.py.

00873 
00874     def test_add_self(self):
00875         # Test for #1257255.
00876         dstname = os.path.abspath(tmpname)
00877         tar = tarfile.open(tmpname, self.mode)
00878         try:
00879             self.assertTrue(tar.name == dstname, "archive name must be absolute")
00880             tar.add(dstname)
00881             self.assertTrue(tar.getnames() == [], "added the archive to itself")
00882 
00883             cwd = os.getcwd()
00884             os.chdir(TEMPDIR)
00885             tar.add(dstname)
00886             os.chdir(cwd)
00887             self.assertTrue(tar.getnames() == [], "added the archive to itself")
00888         finally:
00889             tar.close()

Here is the call graph for this function:

Definition at line 1039 of file test_tarfile.py.

01039 
01040     def test_cwd(self):
01041         # Test adding the current working directory.
01042         cwd = os.getcwd()
01043         os.chdir(TEMPDIR)
01044         try:
01045             tar = tarfile.open(tmpname, self.mode)
01046             try:
01047                 tar.add(".")
01048             finally:
01049                 tar.close()
01050 
01051             tar = tarfile.open(tmpname, "r")
01052             try:
01053                 for t in tar:
01054                     self.assertTrue(t.name == "." or t.name.startswith("./"))
01055             finally:
01056                 tar.close()
01057         finally:
01058             os.chdir(cwd)
01059 

Here is the call graph for this function:

Definition at line 826 of file test_tarfile.py.

00826 
00827     def test_directory_size(self):
00828         path = os.path.join(TEMPDIR, "directory")
00829         os.mkdir(path)
00830         try:
00831             tar = tarfile.open(tmpname, self.mode)
00832             try:
00833                 tarinfo = tar.gettarinfo(path)
00834                 self.assertEqual(tarinfo.size, 0)
00835             finally:
00836                 tar.close()
00837         finally:
00838             os.rmdir(path)

Here is the call graph for this function:

Definition at line 890 of file test_tarfile.py.

00890 
00891     def test_exclude(self):
00892         tempdir = os.path.join(TEMPDIR, "exclude")
00893         os.mkdir(tempdir)
00894         try:
00895             for name in ("foo", "bar", "baz"):
00896                 name = os.path.join(tempdir, name)
00897                 open(name, "wb").close()
00898 
00899             exclude = os.path.isfile
00900 
00901             tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
00902             try:
00903                 with support.check_warnings(("use the filter argument",
00904                                              DeprecationWarning)):
00905                     tar.add(tempdir, arcname="empty_dir", exclude=exclude)
00906             finally:
00907                 tar.close()
00908 
00909             tar = tarfile.open(tmpname, "r")
00910             try:
00911                 self.assertEqual(len(tar.getmembers()), 1)
00912                 self.assertEqual(tar.getnames()[0], "empty_dir")
00913             finally:
00914                 tar.close()
00915         finally:
00916             shutil.rmtree(tempdir)

Here is the call graph for this function:

Definition at line 987 of file test_tarfile.py.

00987 
00988     def test_extractall_symlinks(self):
00989         # Test if extractall works properly when tarfile contains symlinks
00990         tempdir = os.path.join(TEMPDIR, "testsymlinks")
00991         temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
00992         os.mkdir(tempdir)
00993         try:
00994             source_file = os.path.join(tempdir,'source')
00995             target_file = os.path.join(tempdir,'symlink')
00996             with open(source_file,'w') as f:
00997                 f.write('something\n')
00998             os.symlink(source_file, target_file)
00999             tar = tarfile.open(temparchive,'w')
01000             tar.add(source_file)
01001             tar.add(target_file)
01002             tar.close()
01003             # Let's extract it to the location which contains the symlink
01004             tar = tarfile.open(temparchive,'r')
01005             # this should not raise OSError: [Errno 17] File exists
01006             try:
01007                 tar.extractall(path=tempdir)
01008             except OSError:
01009                 self.fail("extractall failed with symlinked files")
01010             finally:
01011                 tar.close()
01012         finally:
01013             os.unlink(temparchive)
01014             shutil.rmtree(tempdir)

Here is the call graph for this function:

Definition at line 810 of file test_tarfile.py.

00810 
00811     def test_file_size(self):
00812         tar = tarfile.open(tmpname, self.mode)
00813         try:
00814             path = os.path.join(TEMPDIR, "file")
00815             with open(path, "wb"):
00816                 pass
00817             tarinfo = tar.gettarinfo(path)
00818             self.assertEqual(tarinfo.size, 0)
00819 
00820             with open(path, "wb") as fobj:
00821                 fobj.write(b"aaa")
00822             tarinfo = tar.gettarinfo(path)
00823             self.assertEqual(tarinfo.size, 3)
00824         finally:
00825             tar.close()

Here is the call graph for this function:

Definition at line 764 of file test_tarfile.py.

00764 
00765     def test_fileobj_no_close(self):
00766         fobj = io.BytesIO()
00767         tar = tarfile.open(fileobj=fobj, mode=self.mode)
00768         tar.addfile(tarfile.TarInfo("foo"))
00769         tar.close()
00770         self.assertTrue(fobj.closed is False, "external fileobjs must never closed")
00771 

Here is the call graph for this function:

Definition at line 917 of file test_tarfile.py.

00917 
00918     def test_filter(self):
00919         tempdir = os.path.join(TEMPDIR, "filter")
00920         os.mkdir(tempdir)
00921         try:
00922             for name in ("foo", "bar", "baz"):
00923                 name = os.path.join(tempdir, name)
00924                 open(name, "wb").close()
00925 
00926             def filter(tarinfo):
00927                 if os.path.basename(tarinfo.name) == "bar":
00928                     return
00929                 tarinfo.uid = 123
00930                 tarinfo.uname = "foo"
00931                 return tarinfo
00932 
00933             tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
00934             try:
00935                 tar.add(tempdir, arcname="empty_dir", filter=filter)
00936             finally:
00937                 tar.close()
00938 
00939             # Verify that filter is a keyword-only argument
00940             with self.assertRaises(TypeError):
00941                 tar.add(tempdir, "empty_dir", True, None, filter)
00942 
00943             tar = tarfile.open(tmpname, "r")
00944             try:
00945                 for tarinfo in tar:
00946                     self.assertEqual(tarinfo.uid, 123)
00947                     self.assertEqual(tarinfo.uname, "foo")
00948                 self.assertEqual(len(tar.getmembers()), 3)
00949             finally:
00950                 tar.close()
00951         finally:
00952             shutil.rmtree(tempdir)

Here is the call graph for this function:

Definition at line 839 of file test_tarfile.py.

00839 
00840     def test_link_size(self):
00841         if hasattr(os, "link"):
00842             link = os.path.join(TEMPDIR, "link")
00843             target = os.path.join(TEMPDIR, "link_target")
00844             with open(target, "wb") as fobj:
00845                 fobj.write(b"aaa")
00846             os.link(target, link)
00847             try:
00848                 tar = tarfile.open(tmpname, self.mode)
00849                 try:
00850                     # Record the link target in the inodes list.
00851                     tar.gettarinfo(target)
00852                     tarinfo = tar.gettarinfo(link)
00853                     self.assertEqual(tarinfo.size, 0)
00854                 finally:
00855                     tar.close()
00856             finally:
00857                 os.remove(target)
00858                 os.remove(link)

Here is the call graph for this function:

Definition at line 1015 of file test_tarfile.py.

01015 
01016     def test_pathnames(self):
01017         self._test_pathname("foo")
01018         self._test_pathname(os.path.join("foo", ".", "bar"))
01019         self._test_pathname(os.path.join("foo", "..", "bar"))
01020         self._test_pathname(os.path.join(".", "foo"))
01021         self._test_pathname(os.path.join(".", "foo", "."))
01022         self._test_pathname(os.path.join(".", "foo", ".", "bar"))
01023         self._test_pathname(os.path.join(".", "foo", "..", "bar"))
01024         self._test_pathname(os.path.join(".", "foo", "..", "bar"))
01025         self._test_pathname(os.path.join("..", "foo"))
01026         self._test_pathname(os.path.join("..", "foo", ".."))
01027         self._test_pathname(os.path.join("..", "foo", ".", "bar"))
01028         self._test_pathname(os.path.join("..", "foo", "..", "bar"))
01029 
01030         self._test_pathname("foo" + os.sep + os.sep + "bar")
01031         self._test_pathname("foo" + os.sep + os.sep, "foo", dir=True)

Here is the call graph for this function:

Definition at line 860 of file test_tarfile.py.

00860 
00861     def test_symlink_size(self):
00862         path = os.path.join(TEMPDIR, "symlink")
00863         os.symlink("link_target", path)
00864         try:
00865             tar = tarfile.open(tmpname, self.mode)
00866             try:
00867                 tarinfo = tar.gettarinfo(path)
00868                 self.assertEqual(tarinfo.size, 0)
00869             finally:
00870                 tar.close()
00871         finally:
00872             os.remove(path)

Here is the call graph for this function:

Definition at line 796 of file test_tarfile.py.

00796 
00797     def test_tar_size(self):
00798         # Test for bug #1013882.
00799         tar = tarfile.open(tmpname, self.mode)
00800         try:
00801             path = os.path.join(TEMPDIR, "file")
00802             with open(path, "wb") as fobj:
00803                 fobj.write(b"aaa")
00804             tar.add(path)
00805         finally:
00806             tar.close()
00807         self.assertTrue(os.path.getsize(tmpname) > 0,
00808                 "tarfile is empty")

Here is the call graph for this function:


Member Data Documentation

Definition at line 774 of file test_tarfile.py.


The documentation for this class was generated from the following file: