mirror of https://github.com/python/cpython
Use support.change_cwd() in tests.
This commit is contained in:
commit
5fbadb63ef
|
@ -242,9 +242,7 @@ class GlobTests(unittest.TestCase):
|
|||
('a', 'bcd', 'EF'), ('a', 'bcd', 'efg')))
|
||||
eq(self.rglob('a', '**', 'bcd'), self.joins(('a', 'bcd')))
|
||||
|
||||
predir = os.path.abspath(os.curdir)
|
||||
try:
|
||||
os.chdir(self.tempdir)
|
||||
with change_cwd(self.tempdir):
|
||||
join = os.path.join
|
||||
eq(glob.glob('**', recursive=True), [join(*i) for i in full])
|
||||
eq(glob.glob(join('**', ''), recursive=True),
|
||||
|
@ -256,8 +254,6 @@ class GlobTests(unittest.TestCase):
|
|||
if can_symlink():
|
||||
expect += [join('sym3', 'EF')]
|
||||
eq(glob.glob(join('**', 'EF'), recursive=True), expect)
|
||||
finally:
|
||||
os.chdir(predir)
|
||||
|
||||
|
||||
@skip_unless_symlink
|
||||
|
|
|
@ -158,17 +158,11 @@ class UnicodeFileTests(unittest.TestCase):
|
|||
def test_directory(self):
|
||||
dirname = os.path.join(support.TESTFN, 'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
|
||||
filename = '\xdf-\u66e8\u66e9\u66eb'
|
||||
oldwd = os.getcwd()
|
||||
os.mkdir(dirname)
|
||||
os.chdir(dirname)
|
||||
try:
|
||||
with support.temp_cwd(dirname):
|
||||
with open(filename, 'wb') as f:
|
||||
f.write((filename + '\n').encode("utf-8"))
|
||||
os.access(filename,os.R_OK)
|
||||
os.remove(filename)
|
||||
finally:
|
||||
os.chdir(oldwd)
|
||||
os.rmdir(dirname)
|
||||
|
||||
|
||||
class UnicodeNFCFileTests(UnicodeFileTests):
|
||||
|
|
|
@ -316,7 +316,6 @@ class PosixPathTest(unittest.TestCase):
|
|||
# Bug #930024, return the path unchanged if we get into an infinite
|
||||
# symlink loop.
|
||||
try:
|
||||
old_path = abspath('.')
|
||||
os.symlink(ABSTFN, ABSTFN)
|
||||
self.assertEqual(realpath(ABSTFN), ABSTFN)
|
||||
|
||||
|
@ -342,10 +341,9 @@ class PosixPathTest(unittest.TestCase):
|
|||
self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c")
|
||||
|
||||
# Test using relative path as well.
|
||||
os.chdir(dirname(ABSTFN))
|
||||
self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
|
||||
with support.change_cwd(dirname(ABSTFN)):
|
||||
self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
|
||||
finally:
|
||||
os.chdir(old_path)
|
||||
support.unlink(ABSTFN)
|
||||
support.unlink(ABSTFN+"1")
|
||||
support.unlink(ABSTFN+"2")
|
||||
|
@ -373,7 +371,6 @@ class PosixPathTest(unittest.TestCase):
|
|||
@skip_if_ABSTFN_contains_backslash
|
||||
def test_realpath_deep_recursion(self):
|
||||
depth = 10
|
||||
old_path = abspath('.')
|
||||
try:
|
||||
os.mkdir(ABSTFN)
|
||||
for i in range(depth):
|
||||
|
@ -382,10 +379,9 @@ class PosixPathTest(unittest.TestCase):
|
|||
self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN)
|
||||
|
||||
# Test using relative path as well.
|
||||
os.chdir(ABSTFN)
|
||||
self.assertEqual(realpath('%d' % depth), ABSTFN)
|
||||
with support.change_cwd(ABSTFN):
|
||||
self.assertEqual(realpath('%d' % depth), ABSTFN)
|
||||
finally:
|
||||
os.chdir(old_path)
|
||||
for i in range(depth + 1):
|
||||
support.unlink(ABSTFN + '/%d' % i)
|
||||
safe_rmdir(ABSTFN)
|
||||
|
@ -399,15 +395,13 @@ class PosixPathTest(unittest.TestCase):
|
|||
# /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
|
||||
# realpath("a"). This should return /usr/share/doc/a/.
|
||||
try:
|
||||
old_path = abspath('.')
|
||||
os.mkdir(ABSTFN)
|
||||
os.mkdir(ABSTFN + "/y")
|
||||
os.symlink(ABSTFN + "/y", ABSTFN + "/k")
|
||||
|
||||
os.chdir(ABSTFN + "/k")
|
||||
self.assertEqual(realpath("a"), ABSTFN + "/y/a")
|
||||
with support.change_cwd(ABSTFN + "/k"):
|
||||
self.assertEqual(realpath("a"), ABSTFN + "/y/a")
|
||||
finally:
|
||||
os.chdir(old_path)
|
||||
support.unlink(ABSTFN + "/k")
|
||||
safe_rmdir(ABSTFN + "/y")
|
||||
safe_rmdir(ABSTFN)
|
||||
|
@ -424,7 +418,6 @@ class PosixPathTest(unittest.TestCase):
|
|||
# and a symbolic link 'link-y' pointing to 'y' in directory 'a',
|
||||
# then realpath("link-y/..") should return 'k', not 'a'.
|
||||
try:
|
||||
old_path = abspath('.')
|
||||
os.mkdir(ABSTFN)
|
||||
os.mkdir(ABSTFN + "/k")
|
||||
os.mkdir(ABSTFN + "/k/y")
|
||||
|
@ -433,11 +426,10 @@ class PosixPathTest(unittest.TestCase):
|
|||
# Absolute path.
|
||||
self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k")
|
||||
# Relative path.
|
||||
os.chdir(dirname(ABSTFN))
|
||||
self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."),
|
||||
ABSTFN + "/k")
|
||||
with support.change_cwd(dirname(ABSTFN)):
|
||||
self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."),
|
||||
ABSTFN + "/k")
|
||||
finally:
|
||||
os.chdir(old_path)
|
||||
support.unlink(ABSTFN + "/link-y")
|
||||
safe_rmdir(ABSTFN + "/k/y")
|
||||
safe_rmdir(ABSTFN + "/k")
|
||||
|
@ -451,17 +443,14 @@ class PosixPathTest(unittest.TestCase):
|
|||
# must be resolved too.
|
||||
|
||||
try:
|
||||
old_path = abspath('.')
|
||||
os.mkdir(ABSTFN)
|
||||
os.mkdir(ABSTFN + "/k")
|
||||
os.symlink(ABSTFN, ABSTFN + "link")
|
||||
os.chdir(dirname(ABSTFN))
|
||||
|
||||
base = basename(ABSTFN)
|
||||
self.assertEqual(realpath(base + "link"), ABSTFN)
|
||||
self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
|
||||
with support.change_cwd(dirname(ABSTFN)):
|
||||
base = basename(ABSTFN)
|
||||
self.assertEqual(realpath(base + "link"), ABSTFN)
|
||||
self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
|
||||
finally:
|
||||
os.chdir(old_path)
|
||||
support.unlink(ABSTFN + "link")
|
||||
safe_rmdir(ABSTFN + "/k")
|
||||
safe_rmdir(ABSTFN)
|
||||
|
|
|
@ -63,11 +63,9 @@ class PyCompileTests(unittest.TestCase):
|
|||
self.assertTrue(os.path.exists(self.cache_path))
|
||||
|
||||
def test_cwd(self):
|
||||
cwd = os.getcwd()
|
||||
os.chdir(self.directory)
|
||||
py_compile.compile(os.path.basename(self.source_path),
|
||||
os.path.basename(self.pyc_path))
|
||||
os.chdir(cwd)
|
||||
with support.change_cwd(self.directory):
|
||||
py_compile.compile(os.path.basename(self.source_path),
|
||||
os.path.basename(self.pyc_path))
|
||||
self.assertTrue(os.path.exists(self.pyc_path))
|
||||
self.assertFalse(os.path.exists(self.cache_path))
|
||||
|
||||
|
|
|
@ -12,8 +12,6 @@ import errno
|
|||
import functools
|
||||
import subprocess
|
||||
from contextlib import ExitStack
|
||||
from test import support
|
||||
from test.support import TESTFN
|
||||
from os.path import splitdrive
|
||||
from distutils.spawn import find_executable, spawn
|
||||
from shutil import (_make_tarball, _make_zipfile, make_archive,
|
||||
|
@ -974,12 +972,8 @@ class TestShutil(unittest.TestCase):
|
|||
base_name = os.path.join(tmpdir2, 'archive')
|
||||
|
||||
# working with relative paths to avoid tar warnings
|
||||
old_dir = os.getcwd()
|
||||
os.chdir(tmpdir)
|
||||
try:
|
||||
with support.change_cwd(tmpdir):
|
||||
_make_tarball(splitdrive(base_name)[1], '.')
|
||||
finally:
|
||||
os.chdir(old_dir)
|
||||
|
||||
# check if the compressed tarball was created
|
||||
tarball = base_name + '.tar.gz'
|
||||
|
@ -987,12 +981,8 @@ class TestShutil(unittest.TestCase):
|
|||
|
||||
# trying an uncompressed one
|
||||
base_name = os.path.join(tmpdir2, 'archive')
|
||||
old_dir = os.getcwd()
|
||||
os.chdir(tmpdir)
|
||||
try:
|
||||
with support.change_cwd(tmpdir):
|
||||
_make_tarball(splitdrive(base_name)[1], '.', compress=None)
|
||||
finally:
|
||||
os.chdir(old_dir)
|
||||
tarball = base_name + '.tar'
|
||||
self.assertTrue(os.path.exists(tarball))
|
||||
|
||||
|
@ -1024,12 +1014,8 @@ class TestShutil(unittest.TestCase):
|
|||
'Need the tar command to run')
|
||||
def test_tarfile_vs_tar(self):
|
||||
tmpdir, tmpdir2, base_name = self._create_files()
|
||||
old_dir = os.getcwd()
|
||||
os.chdir(tmpdir)
|
||||
try:
|
||||
with support.change_cwd(tmpdir):
|
||||
_make_tarball(base_name, 'dist')
|
||||
finally:
|
||||
os.chdir(old_dir)
|
||||
|
||||
# check if the compressed tarball was created
|
||||
tarball = base_name + '.tar.gz'
|
||||
|
@ -1039,14 +1025,10 @@ class TestShutil(unittest.TestCase):
|
|||
tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
|
||||
tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']
|
||||
gzip_cmd = ['gzip', '-f9', 'archive2.tar']
|
||||
old_dir = os.getcwd()
|
||||
os.chdir(tmpdir)
|
||||
try:
|
||||
with support.change_cwd(tmpdir):
|
||||
with captured_stdout() as s:
|
||||
spawn(tar_cmd)
|
||||
spawn(gzip_cmd)
|
||||
finally:
|
||||
os.chdir(old_dir)
|
||||
|
||||
self.assertTrue(os.path.exists(tarball2))
|
||||
# let's compare both tarballs
|
||||
|
@ -1054,23 +1036,15 @@ class TestShutil(unittest.TestCase):
|
|||
|
||||
# trying an uncompressed one
|
||||
base_name = os.path.join(tmpdir2, 'archive')
|
||||
old_dir = os.getcwd()
|
||||
os.chdir(tmpdir)
|
||||
try:
|
||||
with support.change_cwd(tmpdir):
|
||||
_make_tarball(base_name, 'dist', compress=None)
|
||||
finally:
|
||||
os.chdir(old_dir)
|
||||
tarball = base_name + '.tar'
|
||||
self.assertTrue(os.path.exists(tarball))
|
||||
|
||||
# now for a dry_run
|
||||
base_name = os.path.join(tmpdir2, 'archive')
|
||||
old_dir = os.getcwd()
|
||||
os.chdir(tmpdir)
|
||||
try:
|
||||
with support.change_cwd(tmpdir):
|
||||
_make_tarball(base_name, 'dist', compress=None, dry_run=True)
|
||||
finally:
|
||||
os.chdir(old_dir)
|
||||
tarball = base_name + '.tar'
|
||||
self.assertTrue(os.path.exists(tarball))
|
||||
|
||||
|
@ -1130,15 +1104,11 @@ class TestShutil(unittest.TestCase):
|
|||
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
|
||||
def test_tarfile_root_owner(self):
|
||||
tmpdir, tmpdir2, base_name = self._create_files()
|
||||
old_dir = os.getcwd()
|
||||
os.chdir(tmpdir)
|
||||
group = grp.getgrgid(0)[0]
|
||||
owner = pwd.getpwuid(0)[0]
|
||||
try:
|
||||
with support.change_cwd(tmpdir):
|
||||
archive_name = _make_tarball(base_name, 'dist', compress=None,
|
||||
owner=owner, group=group)
|
||||
finally:
|
||||
os.chdir(old_dir)
|
||||
|
||||
# check if the compressed tarball was created
|
||||
self.assertTrue(os.path.exists(archive_name))
|
||||
|
|
|
@ -317,11 +317,8 @@ class ProcessTestCase(BaseTestCase):
|
|||
# Normalize an expected cwd (for Tru64 support).
|
||||
# We can't use os.path.realpath since it doesn't expand Tru64 {memb}
|
||||
# strings. See bug #1063571.
|
||||
original_cwd = os.getcwd()
|
||||
os.chdir(cwd)
|
||||
cwd = os.getcwd()
|
||||
os.chdir(original_cwd)
|
||||
return cwd
|
||||
with support.change_cwd(cwd):
|
||||
return os.getcwd()
|
||||
|
||||
# For use in the test_cwd* tests below.
|
||||
def _split_python_path(self):
|
||||
|
|
|
@ -6,7 +6,7 @@ import shutil
|
|||
from copy import copy
|
||||
|
||||
from test.support import (run_unittest, TESTFN, unlink, check_warnings,
|
||||
captured_stdout, skip_unless_symlink)
|
||||
captured_stdout, skip_unless_symlink, change_cwd)
|
||||
|
||||
import sysconfig
|
||||
from sysconfig import (get_paths, get_platform, get_config_vars,
|
||||
|
@ -361,12 +361,8 @@ class TestSysConfig(unittest.TestCase):
|
|||
# srcdir should be independent of the current working directory
|
||||
# See Issues #15322, #15364.
|
||||
srcdir = sysconfig.get_config_var('srcdir')
|
||||
cwd = os.getcwd()
|
||||
try:
|
||||
os.chdir('..')
|
||||
with change_cwd(os.pardir):
|
||||
srcdir2 = sysconfig.get_config_var('srcdir')
|
||||
finally:
|
||||
os.chdir(cwd)
|
||||
self.assertEqual(srcdir, srcdir2)
|
||||
|
||||
@unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None,
|
||||
|
|
|
@ -1132,10 +1132,8 @@ class WriteTest(WriteTestBase, unittest.TestCase):
|
|||
self.assertEqual(tar.getnames(), [],
|
||||
"added the archive to itself")
|
||||
|
||||
cwd = os.getcwd()
|
||||
os.chdir(TEMPDIR)
|
||||
tar.add(dstname)
|
||||
os.chdir(cwd)
|
||||
with support.change_cwd(TEMPDIR):
|
||||
tar.add(dstname)
|
||||
self.assertEqual(tar.getnames(), [],
|
||||
"added the archive to itself")
|
||||
finally:
|
||||
|
@ -1292,9 +1290,7 @@ class WriteTest(WriteTestBase, unittest.TestCase):
|
|||
|
||||
def test_cwd(self):
|
||||
# Test adding the current working directory.
|
||||
cwd = os.getcwd()
|
||||
os.chdir(TEMPDIR)
|
||||
try:
|
||||
with support.change_cwd(TEMPDIR):
|
||||
tar = tarfile.open(tmpname, self.mode)
|
||||
try:
|
||||
tar.add(".")
|
||||
|
@ -1308,8 +1304,6 @@ class WriteTest(WriteTestBase, unittest.TestCase):
|
|||
self.assertTrue(t.name.startswith("./"), t.name)
|
||||
finally:
|
||||
tar.close()
|
||||
finally:
|
||||
os.chdir(cwd)
|
||||
|
||||
def test_open_nonwritable_fileobj(self):
|
||||
for exctype in OSError, EOFError, RuntimeError:
|
||||
|
|
|
@ -5,7 +5,7 @@ import os, glob, time, shutil
|
|||
import unicodedata
|
||||
|
||||
import unittest
|
||||
from test.support import (run_unittest, rmtree,
|
||||
from test.support import (run_unittest, rmtree, change_cwd,
|
||||
TESTFN_ENCODING, TESTFN_UNICODE, TESTFN_UNENCODABLE, create_empty_file)
|
||||
|
||||
if not os.path.supports_unicode_filenames:
|
||||
|
@ -82,13 +82,11 @@ class TestUnicodeFiles(unittest.TestCase):
|
|||
self.assertFalse(os.path.exists(filename2 + '.new'))
|
||||
|
||||
def _do_directory(self, make_name, chdir_name):
|
||||
cwd = os.getcwd()
|
||||
if os.path.isdir(make_name):
|
||||
rmtree(make_name)
|
||||
os.mkdir(make_name)
|
||||
try:
|
||||
os.chdir(chdir_name)
|
||||
try:
|
||||
with change_cwd(chdir_name):
|
||||
cwd_result = os.getcwd()
|
||||
name_result = make_name
|
||||
|
||||
|
@ -96,8 +94,6 @@ class TestUnicodeFiles(unittest.TestCase):
|
|||
name_result = unicodedata.normalize("NFD", name_result)
|
||||
|
||||
self.assertEqual(os.path.basename(cwd_result),name_result)
|
||||
finally:
|
||||
os.chdir(cwd)
|
||||
finally:
|
||||
os.rmdir(make_name)
|
||||
|
||||
|
|
Loading…
Reference in New Issue