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

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

Issue 16510: Using appropriate checks in tests
Patch Set: Created 5 years, 9 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_telnetlib.py ('k') | Lib/test/test_time.py » ('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 errno 3 import errno
4 import io 4 import io
5 import os 5 import os
6 import signal 6 import signal
7 import sys 7 import sys
8 import re 8 import re
9 import warnings 9 import warnings
10 import contextlib 10 import contextlib
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 "tempdir" : 1, 85 "tempdir" : 1,
86 "template" : 1, 86 "template" : 1,
87 "SpooledTemporaryFile" : 1, 87 "SpooledTemporaryFile" : 1,
88 "TemporaryDirectory" : 1, 88 "TemporaryDirectory" : 1,
89 } 89 }
90 90
91 unexp = [] 91 unexp = []
92 for key in dict: 92 for key in dict:
93 if key[0] != '_' and key not in expected: 93 if key[0] != '_' and key not in expected:
94 unexp.append(key) 94 unexp.append(key)
95 self.assertTrue(len(unexp) == 0, 95 self.assertEqual(len(unexp), 0,
96 "unexpected keys: %s" % unexp) 96 "unexpected keys: %s" % unexp)
97 97
98 98
99 class TestRandomNameSequence(BaseTestCase): 99 class TestRandomNameSequence(BaseTestCase):
100 """Test the internal iterator object _RandomNameSequence.""" 100 """Test the internal iterator object _RandomNameSequence."""
101 101
102 def setUp(self): 102 def setUp(self):
103 self.r = tempfile._RandomNameSequence() 103 self.r = tempfile._RandomNameSequence()
104 super().setUp() 104 super().setUp()
105 105
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 162
163 163
164 class TestCandidateTempdirList(BaseTestCase): 164 class TestCandidateTempdirList(BaseTestCase):
165 """Test the internal function _candidate_tempdir_list.""" 165 """Test the internal function _candidate_tempdir_list."""
166 166
167 def test_nonempty_list(self): 167 def test_nonempty_list(self):
168 # _candidate_tempdir_list returns a nonempty list of strings 168 # _candidate_tempdir_list returns a nonempty list of strings
169 169
170 cand = tempfile._candidate_tempdir_list() 170 cand = tempfile._candidate_tempdir_list()
171 171
172 self.assertFalse(len(cand) == 0) 172 self.assertNotEqual(len(cand), 0)
173 for c in cand: 173 for c in cand:
174 self.assertIsInstance(c, str) 174 self.assertIsInstance(c, str)
175 175
176 def test_wanted_dirs(self): 176 def test_wanted_dirs(self):
177 # _candidate_tempdir_list contains the expected directories 177 # _candidate_tempdir_list contains the expected directories
178 178
179 # Make sure the interesting environment variables are all set. 179 # Make sure the interesting environment variables are all set.
180 with support.EnvironmentVarGuard() as env: 180 with support.EnvironmentVarGuard() as env:
181 for envname in 'TMPDIR', 'TEMP', 'TMP': 181 for envname in 'TMPDIR', 'TEMP', 'TMP':
182 dirname = os.getenv(envname) 182 dirname = os.getenv(envname)
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 def test_retval(self): 247 def test_retval(self):
248 # _get_candidate_names returns a _RandomNameSequence object 248 # _get_candidate_names returns a _RandomNameSequence object
249 obj = tempfile._get_candidate_names() 249 obj = tempfile._get_candidate_names()
250 self.assertIsInstance(obj, tempfile._RandomNameSequence) 250 self.assertIsInstance(obj, tempfile._RandomNameSequence)
251 251
252 def test_same_thing(self): 252 def test_same_thing(self):
253 # _get_candidate_names always returns the same object 253 # _get_candidate_names always returns the same object
254 a = tempfile._get_candidate_names() 254 a = tempfile._get_candidate_names()
255 b = tempfile._get_candidate_names() 255 b = tempfile._get_candidate_names()
256 256
257 self.assertTrue(a is b) 257 self.assertIs(a, b)
258 258
259 259
260 @contextlib.contextmanager 260 @contextlib.contextmanager
261 def _inside_empty_temp_dir(): 261 def _inside_empty_temp_dir():
262 dir = tempfile.mkdtemp() 262 dir = tempfile.mkdtemp()
263 try: 263 try:
264 with support.swap_attr(tempfile, 'tempdir', dir): 264 with support.swap_attr(tempfile, 'tempdir', dir):
265 yield 265 yield
266 finally: 266 finally:
267 support.rmtree(dir) 267 support.rmtree(dir)
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 421
422 422
423 class TestGetTempPrefix(BaseTestCase): 423 class TestGetTempPrefix(BaseTestCase):
424 """Test gettempprefix().""" 424 """Test gettempprefix()."""
425 425
426 def test_sane_template(self): 426 def test_sane_template(self):
427 # gettempprefix returns a nonempty prefix string 427 # gettempprefix returns a nonempty prefix string
428 p = tempfile.gettempprefix() 428 p = tempfile.gettempprefix()
429 429
430 self.assertIsInstance(p, str) 430 self.assertIsInstance(p, str)
431 self.assertTrue(len(p) > 0) 431 self.assertGreater(len(p), 0)
432 432
433 def test_usable_template(self): 433 def test_usable_template(self):
434 # gettempprefix returns a usable prefix string 434 # gettempprefix returns a usable prefix string
435 435
436 # Create a temp directory, avoiding use of the prefix. 436 # Create a temp directory, avoiding use of the prefix.
437 # Then attempt to create a file whose name is 437 # Then attempt to create a file whose name is
438 # prefix + 'xxxxxx.xxx' in that directory. 438 # prefix + 'xxxxxx.xxx' in that directory.
439 p = tempfile.gettempprefix() + "xxxxxx.xxx" 439 p = tempfile.gettempprefix() + "xxxxxx.xxx"
440 d = tempfile.mkdtemp(prefix="") 440 d = tempfile.mkdtemp(prefix="")
441 try: 441 try:
442 p = os.path.join(d, p) 442 p = os.path.join(d, p)
443 fd = os.open(p, os.O_RDWR | os.O_CREAT) 443 fd = os.open(p, os.O_RDWR | os.O_CREAT)
444 os.close(fd) 444 os.close(fd)
445 os.unlink(p) 445 os.unlink(p)
446 finally: 446 finally:
447 os.rmdir(d) 447 os.rmdir(d)
448 448
449 449
450 class TestGetTempDir(BaseTestCase): 450 class TestGetTempDir(BaseTestCase):
451 """Test gettempdir().""" 451 """Test gettempdir()."""
452 452
453 def test_directory_exists(self): 453 def test_directory_exists(self):
454 # gettempdir returns a directory which exists 454 # gettempdir returns a directory which exists
455 455
456 dir = tempfile.gettempdir() 456 dir = tempfile.gettempdir()
457 self.assertTrue(os.path.isabs(dir) or dir == os.curdir, 457 if dir != os.curdir:
458 "%s is not an absolute path" % dir) 458 self.assertTrue(os.path.isabs(dir),
459 "%s is not an absolute path" % dir)
459 self.assertTrue(os.path.isdir(dir), 460 self.assertTrue(os.path.isdir(dir),
460 "%s is not a directory" % dir) 461 "%s is not a directory" % dir)
461 462
462 def test_directory_writable(self): 463 def test_directory_writable(self):
463 # gettempdir returns a directory writable by the user 464 # gettempdir returns a directory writable by the user
464 465
465 # sneaky: just instantiate a NamedTemporaryFile, which 466 # sneaky: just instantiate a NamedTemporaryFile, which
466 # defaults to writing into the directory returned by 467 # defaults to writing into the directory returned by
467 # gettempdir. 468 # gettempdir.
468 file = tempfile.NamedTemporaryFile() 469 file = tempfile.NamedTemporaryFile()
469 file.write(b"blat") 470 file.write(b"blat")
470 file.close() 471 file.close()
471 472
472 def test_same_thing(self): 473 def test_same_thing(self):
473 # gettempdir always returns the same object 474 # gettempdir always returns the same object
474 a = tempfile.gettempdir() 475 a = tempfile.gettempdir()
475 b = tempfile.gettempdir() 476 b = tempfile.gettempdir()
476 477
477 self.assertTrue(a is b) 478 self.assertIs(a, b)
478 479
479 def test_case_sensitive(self): 480 def test_case_sensitive(self):
480 # gettempdir should not flatten its case 481 # gettempdir should not flatten its case
481 # even on a case-insensitive file system 482 # even on a case-insensitive file system
482 case_sensitive_tempdir = tempfile.mkdtemp("-Temp") 483 case_sensitive_tempdir = tempfile.mkdtemp("-Temp")
483 _tempdir, tempfile.tempdir = tempfile.tempdir, None 484 _tempdir, tempfile.tempdir = tempfile.tempdir, None
484 try: 485 try:
485 with support.EnvironmentVarGuard() as env: 486 with support.EnvironmentVarGuard() as env:
486 # Fake the first env var which is checked as a candidate 487 # Fake the first env var which is checked as a candidate
487 env["TMPDIR"] = case_sensitive_tempdir 488 env["TMPDIR"] = case_sensitive_tempdir
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
839 self.assertFalse(f._rolled) 840 self.assertFalse(f._rolled)
840 f.seek(100, 0) 841 f.seek(100, 0)
841 self.assertFalse(f._rolled) 842 self.assertFalse(f._rolled)
842 f.write(b'x') 843 f.write(b'x')
843 self.assertTrue(f._rolled) 844 self.assertTrue(f._rolled)
844 845
845 def test_fileno(self): 846 def test_fileno(self):
846 # A SpooledTemporaryFile should roll over to a real file on fileno() 847 # A SpooledTemporaryFile should roll over to a real file on fileno()
847 f = self.do_create(max_size=30) 848 f = self.do_create(max_size=30)
848 self.assertFalse(f._rolled) 849 self.assertFalse(f._rolled)
849 self.assertTrue(f.fileno() > 0) 850 self.assertGreater(f.fileno(), 0)
850 self.assertTrue(f._rolled) 851 self.assertTrue(f._rolled)
851 852
852 def test_multiple_close_before_rollover(self): 853 def test_multiple_close_before_rollover(self):
853 # A SpooledTemporaryFile can be closed many times without error 854 # A SpooledTemporaryFile can be closed many times without error
854 f = tempfile.SpooledTemporaryFile() 855 f = tempfile.SpooledTemporaryFile()
855 f.write(b'abc\n') 856 f.write(b'abc\n')
856 self.assertFalse(f._rolled) 857 self.assertFalse(f._rolled)
857 f.close() 858 f.close()
858 f.close() 859 f.close()
859 f.close() 860 f.close()
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
1211 self.assertTrue(os.path.exists(name)) 1212 self.assertTrue(os.path.exists(name))
1212 self.assertEqual(name, d.name) 1213 self.assertEqual(name, d.name)
1213 self.assertFalse(os.path.exists(name)) 1214 self.assertFalse(os.path.exists(name))
1214 1215
1215 1216
1216 def test_main(): 1217 def test_main():
1217 support.run_unittest(__name__) 1218 support.run_unittest(__name__)
1218 1219
1219 if __name__ == "__main__": 1220 if __name__ == "__main__":
1220 test_main() 1221 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_telnetlib.py ('k') | Lib/test/test_time.py » ('j') | no next file with comments »

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