mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
Close #15415: Factor out temp dir helpers to test.support
Patch by Chris Jerdonek
This commit is contained in:
parent
69e3bda310
commit
5517596c04
7 changed files with 223 additions and 54 deletions
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import importlib
|
||||
import shutil
|
||||
import sys
|
||||
import os
|
||||
import unittest
|
||||
|
@ -88,6 +89,118 @@ class TestSupport(unittest.TestCase):
|
|||
s.listen(1)
|
||||
s.close()
|
||||
|
||||
# Tests for temp_dir()
|
||||
|
||||
def test_temp_dir(self):
|
||||
"""Test that temp_dir() creates and destroys its directory."""
|
||||
parent_dir = tempfile.mkdtemp()
|
||||
parent_dir = os.path.realpath(parent_dir)
|
||||
|
||||
try:
|
||||
path = os.path.join(parent_dir, 'temp')
|
||||
self.assertFalse(os.path.isdir(path))
|
||||
with support.temp_dir(path) as temp_path:
|
||||
self.assertEqual(temp_path, path)
|
||||
self.assertTrue(os.path.isdir(path))
|
||||
self.assertFalse(os.path.isdir(path))
|
||||
finally:
|
||||
shutil.rmtree(parent_dir)
|
||||
|
||||
def test_temp_dir__path_none(self):
|
||||
"""Test passing no path."""
|
||||
with support.temp_dir() as temp_path:
|
||||
self.assertTrue(os.path.isdir(temp_path))
|
||||
self.assertFalse(os.path.isdir(temp_path))
|
||||
|
||||
def test_temp_dir__existing_dir__quiet_default(self):
|
||||
"""Test passing a directory that already exists."""
|
||||
def call_temp_dir(path):
|
||||
with support.temp_dir(path) as temp_path:
|
||||
raise Exception("should not get here")
|
||||
|
||||
path = tempfile.mkdtemp()
|
||||
path = os.path.realpath(path)
|
||||
try:
|
||||
self.assertTrue(os.path.isdir(path))
|
||||
self.assertRaises(FileExistsError, call_temp_dir, path)
|
||||
# Make sure temp_dir did not delete the original directory.
|
||||
self.assertTrue(os.path.isdir(path))
|
||||
finally:
|
||||
shutil.rmtree(path)
|
||||
|
||||
def test_temp_dir__existing_dir__quiet_true(self):
|
||||
"""Test passing a directory that already exists with quiet=True."""
|
||||
path = tempfile.mkdtemp()
|
||||
path = os.path.realpath(path)
|
||||
|
||||
try:
|
||||
with support.check_warnings() as recorder:
|
||||
with support.temp_dir(path, quiet=True) as temp_path:
|
||||
self.assertEqual(path, temp_path)
|
||||
warnings = [str(w.message) for w in recorder.warnings]
|
||||
# Make sure temp_dir did not delete the original directory.
|
||||
self.assertTrue(os.path.isdir(path))
|
||||
finally:
|
||||
shutil.rmtree(path)
|
||||
|
||||
expected = ['tests may fail, unable to create temp dir: ' + path]
|
||||
self.assertEqual(warnings, expected)
|
||||
|
||||
# Tests for change_cwd()
|
||||
|
||||
def test_change_cwd(self):
|
||||
original_cwd = os.getcwd()
|
||||
|
||||
with support.temp_dir() as temp_path:
|
||||
with support.change_cwd(temp_path) as new_cwd:
|
||||
self.assertEqual(new_cwd, temp_path)
|
||||
self.assertEqual(os.getcwd(), new_cwd)
|
||||
|
||||
self.assertEqual(os.getcwd(), original_cwd)
|
||||
|
||||
def test_change_cwd__non_existent_dir(self):
|
||||
"""Test passing a non-existent directory."""
|
||||
original_cwd = os.getcwd()
|
||||
|
||||
def call_change_cwd(path):
|
||||
with support.change_cwd(path) as new_cwd:
|
||||
raise Exception("should not get here")
|
||||
|
||||
with support.temp_dir() as parent_dir:
|
||||
non_existent_dir = os.path.join(parent_dir, 'does_not_exist')
|
||||
self.assertRaises(FileNotFoundError, call_change_cwd,
|
||||
non_existent_dir)
|
||||
|
||||
self.assertEqual(os.getcwd(), original_cwd)
|
||||
|
||||
def test_change_cwd__non_existent_dir__quiet_true(self):
|
||||
"""Test passing a non-existent directory with quiet=True."""
|
||||
original_cwd = os.getcwd()
|
||||
|
||||
with support.temp_dir() as parent_dir:
|
||||
bad_dir = os.path.join(parent_dir, 'does_not_exist')
|
||||
with support.check_warnings() as recorder:
|
||||
with support.change_cwd(bad_dir, quiet=True) as new_cwd:
|
||||
self.assertEqual(new_cwd, original_cwd)
|
||||
self.assertEqual(os.getcwd(), new_cwd)
|
||||
warnings = [str(w.message) for w in recorder.warnings]
|
||||
|
||||
expected = ['tests may fail, unable to change CWD to: ' + bad_dir]
|
||||
self.assertEqual(warnings, expected)
|
||||
|
||||
# Tests for change_cwd()
|
||||
|
||||
def test_change_cwd__chdir_warning(self):
|
||||
"""Check the warning message when os.chdir() fails."""
|
||||
path = TESTFN + '_does_not_exist'
|
||||
with support.check_warnings() as recorder:
|
||||
with support.change_cwd(path=path, quiet=True):
|
||||
pass
|
||||
messages = [str(w.message) for w in recorder.warnings]
|
||||
self.assertEqual(messages, ['tests may fail, unable to change CWD to: ' + path])
|
||||
|
||||
# Tests for temp_cwd()
|
||||
|
||||
def test_temp_cwd(self):
|
||||
here = os.getcwd()
|
||||
with support.temp_cwd(name=TESTFN):
|
||||
|
@ -95,14 +208,15 @@ class TestSupport(unittest.TestCase):
|
|||
self.assertFalse(os.path.exists(TESTFN))
|
||||
self.assertTrue(os.path.basename(os.getcwd()), here)
|
||||
|
||||
def test_temp_cwd__chdir_warning(self):
|
||||
"""Check the warning message when os.chdir() fails."""
|
||||
path = TESTFN + '_does_not_exist'
|
||||
with support.check_warnings() as recorder:
|
||||
with support.temp_cwd(path=path, quiet=True):
|
||||
pass
|
||||
messages = [str(w.message) for w in recorder.warnings]
|
||||
self.assertEqual(messages, ['tests may fail, unable to change the CWD to ' + path])
|
||||
|
||||
def test_temp_cwd__name_none(self):
|
||||
"""Test passing None to temp_cwd()."""
|
||||
original_cwd = os.getcwd()
|
||||
with support.temp_cwd(name=None) as new_cwd:
|
||||
self.assertNotEqual(new_cwd, original_cwd)
|
||||
self.assertTrue(os.path.isdir(new_cwd))
|
||||
self.assertEqual(os.getcwd(), new_cwd)
|
||||
self.assertEqual(os.getcwd(), original_cwd)
|
||||
|
||||
def test_sortdict(self):
|
||||
self.assertEqual(support.sortdict({3:3, 2:2, 1:1}), "{1: 1, 2: 2, 3: 3}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue