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__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)
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: support.rmtree(parent_dir)
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') # XXX backport: self.assertRaises(FileNotFoundError, call_change_cwd, non_existent_dir) with self.assertRaises(OSError) as cm: call_change_cwd(non_existent_dir) self.assertEqual(cm.exception.errno, errno.ENOENT) self.assertEqual(os.getcwd(), original_cwd)
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)
def call_temp_dir(path): with support.temp_dir(path) as temp_path: raise Exception( "should not get here" ) # pragma: no cover (intentionally unreachable)
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 call_temp_dir(path): with support.temp_dir(path) as temp_path: raise Exception("should not get here") # pragma: no cover (intentionally unreachable)