diff options
author | Lars Gustäbel <lars@gustaebel.de> | 2007-02-06 18:38:13 (GMT) |
---|---|---|
committer | Lars Gustäbel <lars@gustaebel.de> | 2007-02-06 18:38:13 (GMT) |
commit | 3f8aca1164099c3f81522a06504e4003df9f6b6d (patch) | |
tree | 8da7db204d2ffcbef3f04e11fa2c3feeb64bd780 /Lib/test/test_tarfile.py | |
parent | 5f9c6ae545e006a748f6fd7594852e72376e0c97 (diff) | |
download | cpython-3f8aca1164099c3f81522a06504e4003df9f6b6d.zip cpython-3f8aca1164099c3f81522a06504e4003df9f6b6d.tar.gz cpython-3f8aca1164099c3f81522a06504e4003df9f6b6d.tar.bz2 |
Patch #1652681: create nonexistent files in append mode and
allow appending to empty files.
Diffstat (limited to 'Lib/test/test_tarfile.py')
-rw-r--r-- | Lib/test/test_tarfile.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 1674594..642f376 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -305,6 +305,61 @@ class WriteTest(BaseTest): self.assertEqual(self.dst.getnames(), [], "added the archive to itself") +class AppendTest(unittest.TestCase): + # Test append mode (cp. patch #1652681). + + def setUp(self): + self.tarname = tmpname() + if os.path.exists(self.tarname): + os.remove(self.tarname) + + def _add_testfile(self, fileobj=None): + tar = tarfile.open(self.tarname, "a", fileobj=fileobj) + tar.addfile(tarfile.TarInfo("bar")) + tar.close() + + def _create_testtar(self): + src = tarfile.open(tarname()) + t = src.getmember("0-REGTYPE") + t.name = "foo" + f = src.extractfile(t) + tar = tarfile.open(self.tarname, "w") + tar.addfile(t, f) + tar.close() + + def _test(self, names=["bar"], fileobj=None): + tar = tarfile.open(self.tarname, fileobj=fileobj) + self.assert_(tar.getnames() == names) + + def test_non_existing(self): + self._add_testfile() + self._test() + + def test_empty(self): + open(self.tarname, "w").close() + self._add_testfile() + self._test() + + def test_empty_fileobj(self): + fobj = StringIO.StringIO() + self._add_testfile(fobj) + fobj.seek(0) + self._test(fileobj=fobj) + + def test_fileobj(self): + self._create_testtar() + data = open(self.tarname).read() + fobj = StringIO.StringIO(data) + self._add_testfile(fobj) + fobj.seek(0) + self._test(names=["foo", "bar"], fileobj=fobj) + + def test_existing(self): + self._create_testtar() + self._add_testfile() + self._test(names=["foo", "bar"]) + + class Write100Test(BaseTest): # The name field in a tar header stores strings of at most 100 chars. # If a string is shorter than 100 chars it has to be padded with '\0', @@ -711,6 +766,7 @@ def test_main(): ReadAsteriskTest, ReadStreamAsteriskTest, WriteTest, + AppendTest, Write100Test, WriteSize0Test, WriteStreamTest, |