diff options
Diffstat (limited to 'Lib/test/test_tempfile.py')
-rw-r--r-- | Lib/test/test_tempfile.py | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index c8d4b49..437b02b 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1,6 +1,7 @@ # tempfile.py unit tests. import tempfile import errno +import io import os import signal import sys @@ -198,7 +199,44 @@ class TestCandidateTempdirList(BaseTestCase): # paths in this list. -# We test _get_default_tempdir by testing gettempdir. +# We test _get_default_tempdir some more by testing gettempdir. + +class TestGetDefaultTempdir(BaseTestCase): + """Test _get_default_tempdir().""" + + def test_no_files_left_behind(self): + # use a private empty directory + with tempfile.TemporaryDirectory() as our_temp_directory: + # force _get_default_tempdir() to consider our empty directory + def our_candidate_list(): + return [our_temp_directory] + + with support.swap_attr(tempfile, "_candidate_tempdir_list", + our_candidate_list): + # verify our directory is empty after _get_default_tempdir() + tempfile._get_default_tempdir() + self.assertEqual(os.listdir(our_temp_directory), []) + + def raise_OSError(*args, **kwargs): + raise OSError() + + with support.swap_attr(io, "open", raise_OSError): + # test again with failing io.open() + with self.assertRaises(FileNotFoundError): + tempfile._get_default_tempdir() + self.assertEqual(os.listdir(our_temp_directory), []) + + open = io.open + def bad_writer(*args, **kwargs): + fp = open(*args, **kwargs) + fp.write = raise_OSError + return fp + + with support.swap_attr(io, "open", bad_writer): + # test again with failing write() + with self.assertRaises(FileNotFoundError): + tempfile._get_default_tempdir() + self.assertEqual(os.listdir(our_temp_directory), []) class TestGetCandidateNames(BaseTestCase): |