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

Unified Diff: Lib/test/test_tempfile.py

Issue 5178: Add context manager for temporary directory
Patch Set: Created 8 years, 11 months ago
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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/test/test_shelve.py ('k') | Misc/NEWS » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Lib/test/test_tempfile.py
===================================================================
--- Lib/test/test_tempfile.py (revision 69412)
+++ Lib/test/test_tempfile.py (working copy)
@@ -81,7 +81,8 @@
"gettempdir" : 1,
"tempdir" : 1,
"template" : 1,
- "SpooledTemporaryFile" : 1
+ "SpooledTemporaryFile" : 1,
+ "TemporaryDirectory" : 1,
}
unexp = []
@@ -841,6 +842,55 @@
if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile:
test_classes.append(test_TemporaryFile)
+
+class test_TemporaryDirectory(TC):
+ """Test TemporaryDirectory()."""
+
+ def do_create(self, dir=None, pre="", suf=""):
+ if dir is None:
+ dir = tempfile.gettempdir()
+ try:
+ tmp = tempfile.TemporaryDirectory(dir=dir, prefix=pre, suffix=suf)
+ except:
+ self.failOnException("TemporaryDirectory")
+ self.nameCheck(tmp.name, dir, pre, suf)
+ return tmp
+
+ def test_creates_named(self):
+ d = tempfile.TemporaryDirectory()
+ self.failUnless(os.path.exists(d.name),
+ "TemporaryDirectory %s does not exist" % d.name)
+
+ def test_del_on_close(self):
+ # A TemporaryDirectory is deleted when closed
+ dir = tempfile.mkdtemp()
+ try:
+ d = tempfile.TemporaryDirectory(dir=dir)
+ d.cleanup()
+ self.failIf(os.path.exists(d.name),
+ "TemporaryDirectory %s exists after cleanup" % d.name)
+ finally:
+ os.rmdir(dir)
+
+ def test_multiple_close(self):
+ # Can be cleaned-up many times without error
+ d = tempfile.TemporaryDirectory()
+ d.cleanup()
+ try:
+ d.cleanup()
+ d.cleanup()
+ except:
+ self.failOnException("cleanup")
+
+ def test_context_manager(self):
+ # Can be used as a context manager
+ with tempfile.TemporaryDirectory() as name:
+ self.failUnless(os.path.exists(name))
+ self.failIf(os.path.exists(name))
+
+
+test_classes.append(test_TemporaryDirectory)
+
def test_main():
test_support.run_unittest(*test_classes)
« no previous file with comments | « Lib/test/test_shelve.py ('k') | Misc/NEWS » ('j') | no next file with comments »

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