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

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

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

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