summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_tarfile.py
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2015-02-13 19:02:12 (GMT)
committerBerker Peksag <berker.peksag@gmail.com>2015-02-13 19:02:12 (GMT)
commit0fe6325acf013c945dc003eae2ab5addc2186645 (patch)
tree6ce2f63ce24b65195cb7a6684dcce5352571b5f2 /Lib/test/test_tarfile.py
parent67677575893289abf0374243f1e603e0683a787d (diff)
downloadcpython-0fe6325acf013c945dc003eae2ab5addc2186645.zip
cpython-0fe6325acf013c945dc003eae2ab5addc2186645.tar.gz
cpython-0fe6325acf013c945dc003eae2ab5addc2186645.tar.bz2
Issue #21717: tarfile.open() now supports 'x' (exclusive creation) mode.
Diffstat (limited to 'Lib/test/test_tarfile.py')
-rw-r--r--Lib/test/test_tarfile.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index ef71f5e..01d1a92 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -1428,6 +1428,88 @@ class GNUWriteTest(unittest.TestCase):
("longlnk/" * 127) + "longlink_")
+class CreateTest(TarTest, unittest.TestCase):
+
+ prefix = "x:"
+
+ file_path = os.path.join(TEMPDIR, "spameggs42")
+
+ def setUp(self):
+ support.unlink(tmpname)
+
+ @classmethod
+ def setUpClass(cls):
+ with open(cls.file_path, "wb") as fobj:
+ fobj.write(b"aaa")
+
+ @classmethod
+ def tearDownClass(cls):
+ support.unlink(cls.file_path)
+
+ def test_create(self):
+ with tarfile.open(tmpname, self.mode) as tobj:
+ tobj.add(self.file_path)
+
+ with self.taropen(tmpname) as tobj:
+ names = tobj.getnames()
+ self.assertEqual(len(names), 1)
+ self.assertIn('spameggs42', names[0])
+
+ def test_create_existing(self):
+ with tarfile.open(tmpname, self.mode) as tobj:
+ tobj.add(self.file_path)
+
+ with self.assertRaises(FileExistsError):
+ tobj = tarfile.open(tmpname, self.mode)
+
+ with self.taropen(tmpname) as tobj:
+ names = tobj.getnames()
+ self.assertEqual(len(names), 1)
+ self.assertIn('spameggs42', names[0])
+
+ def test_create_taropen(self):
+ with self.taropen(tmpname, "x") as tobj:
+ tobj.add(self.file_path)
+
+ with self.taropen(tmpname) as tobj:
+ names = tobj.getnames()
+ self.assertEqual(len(names), 1)
+ self.assertIn('spameggs42', names[0])
+
+ def test_create_existing_taropen(self):
+ with self.taropen(tmpname, "x") as tobj:
+ tobj.add(self.file_path)
+
+ with self.assertRaises(FileExistsError):
+ with self.taropen(tmpname, "x"):
+ pass
+
+ with self.taropen(tmpname) as tobj:
+ names = tobj.getnames()
+ self.assertEqual(len(names), 1)
+ self.assertIn("spameggs42", names[0])
+
+
+class GzipCreateTest(GzipTest, CreateTest):
+ pass
+
+
+class Bz2CreateTest(Bz2Test, CreateTest):
+ pass
+
+
+class LzmaCreateTest(LzmaTest, CreateTest):
+ pass
+
+
+class CreateWithXModeTest(CreateTest):
+
+ prefix = "x"
+
+ test_create_taropen = None
+ test_create_existing_taropen = None
+
+
@unittest.skipUnless(hasattr(os, "link"), "Missing hardlink implementation")
class HardlinkTest(unittest.TestCase):
# Test the creation of LNKTYPE (hardlink) members in an archive.