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

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

Issue 5178: Add context manager for temporary directory
Patch Set: Created 8 years, 11 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
« no previous file with comments | « Lib/test/test_shelve.py ('k') | Misc/NEWS » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # tempfile.py unit tests. 1 # tempfile.py unit tests.
2 import tempfile 2 import tempfile
3 import os 3 import os
4 import sys 4 import sys
5 import re 5 import re
6 import errno 6 import errno
7 import warnings 7 import warnings
8 8
9 import unittest 9 import unittest
10 from test import test_support 10 from test import test_support
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 "NamedTemporaryFile" : 1, 74 "NamedTemporaryFile" : 1,
75 "TemporaryFile" : 1, 75 "TemporaryFile" : 1,
76 "mkstemp" : 1, 76 "mkstemp" : 1,
77 "mkdtemp" : 1, 77 "mkdtemp" : 1,
78 "mktemp" : 1, 78 "mktemp" : 1,
79 "TMP_MAX" : 1, 79 "TMP_MAX" : 1,
80 "gettempprefix" : 1, 80 "gettempprefix" : 1,
81 "gettempdir" : 1, 81 "gettempdir" : 1,
82 "tempdir" : 1, 82 "tempdir" : 1,
83 "template" : 1, 83 "template" : 1,
84 "SpooledTemporaryFile" : 1 84 "SpooledTemporaryFile" : 1,
85 "TemporaryDirectory" : 1,
85 } 86 }
86 87
87 unexp = [] 88 unexp = []
88 for key in dict: 89 for key in dict:
89 if key[0] != '_' and key not in expected: 90 if key[0] != '_' and key not in expected:
90 unexp.append(key) 91 unexp.append(key)
91 self.failUnless(len(unexp) == 0, 92 self.failUnless(len(unexp) == 0,
92 "unexpected keys: %s" % unexp) 93 "unexpected keys: %s" % unexp)
93 94
94 test_classes.append(test_exports) 95 test_classes.append(test_exports)
(...skipping 738 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 f.close() 834 f.close()
834 f.close() 835 f.close()
835 except: 836 except:
836 self.failOnException("close") 837 self.failOnException("close")
837 838
838 # How to test the mode and bufsize parameters? 839 # How to test the mode and bufsize parameters?
839 840
840 841
841 if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile: 842 if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile:
842 test_classes.append(test_TemporaryFile) 843 test_classes.append(test_TemporaryFile)
844
845
846 class test_TemporaryDirectory(TC):
847 """Test TemporaryDirectory()."""
848
849 def do_create(self, dir=None, pre="", suf=""):
850 if dir is None:
851 dir = tempfile.gettempdir()
852 try:
853 tmp = tempfile.TemporaryDirectory(dir=dir, prefix=pre, suffix=suf)
854 except:
855 self.failOnException("TemporaryDirectory")
856 self.nameCheck(tmp.name, dir, pre, suf)
857 return tmp
858
859 def test_creates_named(self):
860 d = tempfile.TemporaryDirectory()
861 self.failUnless(os.path.exists(d.name),
862 "TemporaryDirectory %s does not exist" % d.name)
863
864 def test_del_on_close(self):
865 # A TemporaryDirectory is deleted when closed
866 dir = tempfile.mkdtemp()
867 try:
868 d = tempfile.TemporaryDirectory(dir=dir)
869 d.cleanup()
870 self.failIf(os.path.exists(d.name),
871 "TemporaryDirectory %s exists after cleanup" % d.name)
872 finally:
873 os.rmdir(dir)
874
875 def test_multiple_close(self):
876 # Can be cleaned-up many times without error
877 d = tempfile.TemporaryDirectory()
878 d.cleanup()
879 try:
880 d.cleanup()
881 d.cleanup()
882 except:
883 self.failOnException("cleanup")
884
885 def test_context_manager(self):
886 # Can be used as a context manager
887 with tempfile.TemporaryDirectory() as name:
888 self.failUnless(os.path.exists(name))
889 self.failIf(os.path.exists(name))
890
891
892 test_classes.append(test_TemporaryDirectory)
843 893
844 def test_main(): 894 def test_main():
845 test_support.run_unittest(*test_classes) 895 test_support.run_unittest(*test_classes)
846 896
847 if __name__ == "__main__": 897 if __name__ == "__main__":
848 test_main() 898 test_main()
OLDNEW
« 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+