summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_support.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_support.py')
-rw-r--r--Lib/test/test_support.py130
1 files changed, 122 insertions, 8 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 340b8da..4edb1a8 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -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}")