Commit a7b5152f authored by Serhiy Storchaka's avatar Serhiy Storchaka

Use support.change_cwd() in tests.

parent c3b46f75
...@@ -158,17 +158,11 @@ class UnicodeFileTests(unittest.TestCase): ...@@ -158,17 +158,11 @@ class UnicodeFileTests(unittest.TestCase):
def test_directory(self): def test_directory(self):
dirname = os.path.join(support.TESTFN, 'Gr\xfc\xdf-\u66e8\u66e9\u66eb') dirname = os.path.join(support.TESTFN, 'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
filename = '\xdf-\u66e8\u66e9\u66eb' filename = '\xdf-\u66e8\u66e9\u66eb'
oldwd = os.getcwd() with support.temp_cwd(dirname):
os.mkdir(dirname)
os.chdir(dirname)
try:
with open(filename, 'wb') as f: with open(filename, 'wb') as f:
f.write((filename + '\n').encode("utf-8")) f.write((filename + '\n').encode("utf-8"))
os.access(filename,os.R_OK) os.access(filename,os.R_OK)
os.remove(filename) os.remove(filename)
finally:
os.chdir(oldwd)
os.rmdir(dirname)
class UnicodeNFCFileTests(UnicodeFileTests): class UnicodeNFCFileTests(UnicodeFileTests):
......
...@@ -328,7 +328,6 @@ class PosixPathTest(unittest.TestCase): ...@@ -328,7 +328,6 @@ class PosixPathTest(unittest.TestCase):
# Bug #930024, return the path unchanged if we get into an infinite # Bug #930024, return the path unchanged if we get into an infinite
# symlink loop. # symlink loop.
try: try:
old_path = abspath('.')
os.symlink(ABSTFN, ABSTFN) os.symlink(ABSTFN, ABSTFN)
self.assertEqual(realpath(ABSTFN), ABSTFN) self.assertEqual(realpath(ABSTFN), ABSTFN)
...@@ -354,10 +353,9 @@ class PosixPathTest(unittest.TestCase): ...@@ -354,10 +353,9 @@ class PosixPathTest(unittest.TestCase):
self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c") self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c")
# Test using relative path as well. # Test using relative path as well.
os.chdir(dirname(ABSTFN)) with support.change_cwd(dirname(ABSTFN)):
self.assertEqual(realpath(basename(ABSTFN)), ABSTFN) self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
finally: finally:
os.chdir(old_path)
support.unlink(ABSTFN) support.unlink(ABSTFN)
support.unlink(ABSTFN+"1") support.unlink(ABSTFN+"1")
support.unlink(ABSTFN+"2") support.unlink(ABSTFN+"2")
...@@ -385,7 +383,6 @@ class PosixPathTest(unittest.TestCase): ...@@ -385,7 +383,6 @@ class PosixPathTest(unittest.TestCase):
@skip_if_ABSTFN_contains_backslash @skip_if_ABSTFN_contains_backslash
def test_realpath_deep_recursion(self): def test_realpath_deep_recursion(self):
depth = 10 depth = 10
old_path = abspath('.')
try: try:
os.mkdir(ABSTFN) os.mkdir(ABSTFN)
for i in range(depth): for i in range(depth):
...@@ -394,10 +391,9 @@ class PosixPathTest(unittest.TestCase): ...@@ -394,10 +391,9 @@ class PosixPathTest(unittest.TestCase):
self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN) self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN)
# Test using relative path as well. # Test using relative path as well.
os.chdir(ABSTFN) with support.change_cwd(ABSTFN):
self.assertEqual(realpath('%d' % depth), ABSTFN) self.assertEqual(realpath('%d' % depth), ABSTFN)
finally: finally:
os.chdir(old_path)
for i in range(depth + 1): for i in range(depth + 1):
support.unlink(ABSTFN + '/%d' % i) support.unlink(ABSTFN + '/%d' % i)
safe_rmdir(ABSTFN) safe_rmdir(ABSTFN)
...@@ -411,15 +407,13 @@ class PosixPathTest(unittest.TestCase): ...@@ -411,15 +407,13 @@ class PosixPathTest(unittest.TestCase):
# /usr/doc with 'doc' being a symlink to /usr/share/doc. We call # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
# realpath("a"). This should return /usr/share/doc/a/. # realpath("a"). This should return /usr/share/doc/a/.
try: try:
old_path = abspath('.')
os.mkdir(ABSTFN) os.mkdir(ABSTFN)
os.mkdir(ABSTFN + "/y") os.mkdir(ABSTFN + "/y")
os.symlink(ABSTFN + "/y", ABSTFN + "/k") os.symlink(ABSTFN + "/y", ABSTFN + "/k")
os.chdir(ABSTFN + "/k") with support.change_cwd(ABSTFN + "/k"):
self.assertEqual(realpath("a"), ABSTFN + "/y/a") self.assertEqual(realpath("a"), ABSTFN + "/y/a")
finally: finally:
os.chdir(old_path)
support.unlink(ABSTFN + "/k") support.unlink(ABSTFN + "/k")
safe_rmdir(ABSTFN + "/y") safe_rmdir(ABSTFN + "/y")
safe_rmdir(ABSTFN) safe_rmdir(ABSTFN)
...@@ -436,7 +430,6 @@ class PosixPathTest(unittest.TestCase): ...@@ -436,7 +430,6 @@ class PosixPathTest(unittest.TestCase):
# and a symbolic link 'link-y' pointing to 'y' in directory 'a', # and a symbolic link 'link-y' pointing to 'y' in directory 'a',
# then realpath("link-y/..") should return 'k', not 'a'. # then realpath("link-y/..") should return 'k', not 'a'.
try: try:
old_path = abspath('.')
os.mkdir(ABSTFN) os.mkdir(ABSTFN)
os.mkdir(ABSTFN + "/k") os.mkdir(ABSTFN + "/k")
os.mkdir(ABSTFN + "/k/y") os.mkdir(ABSTFN + "/k/y")
...@@ -445,11 +438,10 @@ class PosixPathTest(unittest.TestCase): ...@@ -445,11 +438,10 @@ class PosixPathTest(unittest.TestCase):
# Absolute path. # Absolute path.
self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k") self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k")
# Relative path. # Relative path.
os.chdir(dirname(ABSTFN)) with support.change_cwd(dirname(ABSTFN)):
self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."),
ABSTFN + "/k") ABSTFN + "/k")
finally: finally:
os.chdir(old_path)
support.unlink(ABSTFN + "/link-y") support.unlink(ABSTFN + "/link-y")
safe_rmdir(ABSTFN + "/k/y") safe_rmdir(ABSTFN + "/k/y")
safe_rmdir(ABSTFN + "/k") safe_rmdir(ABSTFN + "/k")
...@@ -463,17 +455,14 @@ class PosixPathTest(unittest.TestCase): ...@@ -463,17 +455,14 @@ class PosixPathTest(unittest.TestCase):
# must be resolved too. # must be resolved too.
try: try:
old_path = abspath('.')
os.mkdir(ABSTFN) os.mkdir(ABSTFN)
os.mkdir(ABSTFN + "/k") os.mkdir(ABSTFN + "/k")
os.symlink(ABSTFN, ABSTFN + "link") os.symlink(ABSTFN, ABSTFN + "link")
os.chdir(dirname(ABSTFN)) with support.change_cwd(dirname(ABSTFN)):
base = basename(ABSTFN)
base = basename(ABSTFN) self.assertEqual(realpath(base + "link"), ABSTFN)
self.assertEqual(realpath(base + "link"), ABSTFN) self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
finally: finally:
os.chdir(old_path)
support.unlink(ABSTFN + "link") support.unlink(ABSTFN + "link")
safe_rmdir(ABSTFN + "/k") safe_rmdir(ABSTFN + "/k")
safe_rmdir(ABSTFN) safe_rmdir(ABSTFN)
......
...@@ -63,11 +63,9 @@ class PyCompileTests(unittest.TestCase): ...@@ -63,11 +63,9 @@ class PyCompileTests(unittest.TestCase):
self.assertTrue(os.path.exists(self.cache_path)) self.assertTrue(os.path.exists(self.cache_path))
def test_cwd(self): def test_cwd(self):
cwd = os.getcwd() with support.change_cwd(self.directory):
os.chdir(self.directory) py_compile.compile(os.path.basename(self.source_path),
py_compile.compile(os.path.basename(self.source_path), os.path.basename(self.pyc_path))
os.path.basename(self.pyc_path))
os.chdir(cwd)
self.assertTrue(os.path.exists(self.pyc_path)) self.assertTrue(os.path.exists(self.pyc_path))
self.assertFalse(os.path.exists(self.cache_path)) self.assertFalse(os.path.exists(self.cache_path))
......
...@@ -12,8 +12,6 @@ import errno ...@@ -12,8 +12,6 @@ import errno
import functools import functools
import subprocess import subprocess
from contextlib import ExitStack from contextlib import ExitStack
from test import support
from test.support import TESTFN
from os.path import splitdrive from os.path import splitdrive
from distutils.spawn import find_executable, spawn from distutils.spawn import find_executable, spawn
from shutil import (_make_tarball, _make_zipfile, make_archive, from shutil import (_make_tarball, _make_zipfile, make_archive,
...@@ -968,12 +966,8 @@ class TestShutil(unittest.TestCase): ...@@ -968,12 +966,8 @@ class TestShutil(unittest.TestCase):
base_name = os.path.join(tmpdir2, 'archive') base_name = os.path.join(tmpdir2, 'archive')
# working with relative paths to avoid tar warnings # working with relative paths to avoid tar warnings
old_dir = os.getcwd() with support.change_cwd(tmpdir):
os.chdir(tmpdir)
try:
_make_tarball(splitdrive(base_name)[1], '.') _make_tarball(splitdrive(base_name)[1], '.')
finally:
os.chdir(old_dir)
# check if the compressed tarball was created # check if the compressed tarball was created
tarball = base_name + '.tar.gz' tarball = base_name + '.tar.gz'
...@@ -981,12 +975,8 @@ class TestShutil(unittest.TestCase): ...@@ -981,12 +975,8 @@ class TestShutil(unittest.TestCase):
# trying an uncompressed one # trying an uncompressed one
base_name = os.path.join(tmpdir2, 'archive') base_name = os.path.join(tmpdir2, 'archive')
old_dir = os.getcwd() with support.change_cwd(tmpdir):
os.chdir(tmpdir)
try:
_make_tarball(splitdrive(base_name)[1], '.', compress=None) _make_tarball(splitdrive(base_name)[1], '.', compress=None)
finally:
os.chdir(old_dir)
tarball = base_name + '.tar' tarball = base_name + '.tar'
self.assertTrue(os.path.exists(tarball)) self.assertTrue(os.path.exists(tarball))
...@@ -1018,12 +1008,8 @@ class TestShutil(unittest.TestCase): ...@@ -1018,12 +1008,8 @@ class TestShutil(unittest.TestCase):
'Need the tar command to run') 'Need the tar command to run')
def test_tarfile_vs_tar(self): def test_tarfile_vs_tar(self):
tmpdir, tmpdir2, base_name = self._create_files() tmpdir, tmpdir2, base_name = self._create_files()
old_dir = os.getcwd() with support.change_cwd(tmpdir):
os.chdir(tmpdir)
try:
_make_tarball(base_name, 'dist') _make_tarball(base_name, 'dist')
finally:
os.chdir(old_dir)
# check if the compressed tarball was created # check if the compressed tarball was created
tarball = base_name + '.tar.gz' tarball = base_name + '.tar.gz'
...@@ -1033,14 +1019,10 @@ class TestShutil(unittest.TestCase): ...@@ -1033,14 +1019,10 @@ class TestShutil(unittest.TestCase):
tarball2 = os.path.join(tmpdir, 'archive2.tar.gz') tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist'] tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']
gzip_cmd = ['gzip', '-f9', 'archive2.tar'] gzip_cmd = ['gzip', '-f9', 'archive2.tar']
old_dir = os.getcwd() with support.change_cwd(tmpdir):
os.chdir(tmpdir)
try:
with captured_stdout() as s: with captured_stdout() as s:
spawn(tar_cmd) spawn(tar_cmd)
spawn(gzip_cmd) spawn(gzip_cmd)
finally:
os.chdir(old_dir)
self.assertTrue(os.path.exists(tarball2)) self.assertTrue(os.path.exists(tarball2))
# let's compare both tarballs # let's compare both tarballs
...@@ -1048,23 +1030,15 @@ class TestShutil(unittest.TestCase): ...@@ -1048,23 +1030,15 @@ class TestShutil(unittest.TestCase):
# trying an uncompressed one # trying an uncompressed one
base_name = os.path.join(tmpdir2, 'archive') base_name = os.path.join(tmpdir2, 'archive')
old_dir = os.getcwd() with support.change_cwd(tmpdir):
os.chdir(tmpdir)
try:
_make_tarball(base_name, 'dist', compress=None) _make_tarball(base_name, 'dist', compress=None)
finally:
os.chdir(old_dir)
tarball = base_name + '.tar' tarball = base_name + '.tar'
self.assertTrue(os.path.exists(tarball)) self.assertTrue(os.path.exists(tarball))
# now for a dry_run # now for a dry_run
base_name = os.path.join(tmpdir2, 'archive') base_name = os.path.join(tmpdir2, 'archive')
old_dir = os.getcwd() with support.change_cwd(tmpdir):
os.chdir(tmpdir)
try:
_make_tarball(base_name, 'dist', compress=None, dry_run=True) _make_tarball(base_name, 'dist', compress=None, dry_run=True)
finally:
os.chdir(old_dir)
tarball = base_name + '.tar' tarball = base_name + '.tar'
self.assertTrue(os.path.exists(tarball)) self.assertTrue(os.path.exists(tarball))
...@@ -1124,15 +1098,11 @@ class TestShutil(unittest.TestCase): ...@@ -1124,15 +1098,11 @@ class TestShutil(unittest.TestCase):
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
def test_tarfile_root_owner(self): def test_tarfile_root_owner(self):
tmpdir, tmpdir2, base_name = self._create_files() tmpdir, tmpdir2, base_name = self._create_files()
old_dir = os.getcwd()
os.chdir(tmpdir)
group = grp.getgrgid(0)[0] group = grp.getgrgid(0)[0]
owner = pwd.getpwuid(0)[0] owner = pwd.getpwuid(0)[0]
try: with support.change_cwd(tmpdir):
archive_name = _make_tarball(base_name, 'dist', compress=None, archive_name = _make_tarball(base_name, 'dist', compress=None,
owner=owner, group=group) owner=owner, group=group)
finally:
os.chdir(old_dir)
# check if the compressed tarball was created # check if the compressed tarball was created
self.assertTrue(os.path.exists(archive_name)) self.assertTrue(os.path.exists(archive_name))
......
...@@ -317,11 +317,8 @@ class ProcessTestCase(BaseTestCase): ...@@ -317,11 +317,8 @@ class ProcessTestCase(BaseTestCase):
# Normalize an expected cwd (for Tru64 support). # Normalize an expected cwd (for Tru64 support).
# We can't use os.path.realpath since it doesn't expand Tru64 {memb} # We can't use os.path.realpath since it doesn't expand Tru64 {memb}
# strings. See bug #1063571. # strings. See bug #1063571.
original_cwd = os.getcwd() with support.change_cwd(cwd):
os.chdir(cwd) return os.getcwd()
cwd = os.getcwd()
os.chdir(original_cwd)
return cwd
# For use in the test_cwd* tests below. # For use in the test_cwd* tests below.
def _split_python_path(self): def _split_python_path(self):
......
...@@ -6,7 +6,7 @@ import shutil ...@@ -6,7 +6,7 @@ import shutil
from copy import copy from copy import copy
from test.support import (run_unittest, TESTFN, unlink, check_warnings, from test.support import (run_unittest, TESTFN, unlink, check_warnings,
captured_stdout, skip_unless_symlink) captured_stdout, skip_unless_symlink, change_cwd)
import sysconfig import sysconfig
from sysconfig import (get_paths, get_platform, get_config_vars, from sysconfig import (get_paths, get_platform, get_config_vars,
...@@ -361,12 +361,8 @@ class TestSysConfig(unittest.TestCase): ...@@ -361,12 +361,8 @@ class TestSysConfig(unittest.TestCase):
# srcdir should be independent of the current working directory # srcdir should be independent of the current working directory
# See Issues #15322, #15364. # See Issues #15322, #15364.
srcdir = sysconfig.get_config_var('srcdir') srcdir = sysconfig.get_config_var('srcdir')
cwd = os.getcwd() with change_cwd(os.pardir):
try:
os.chdir('..')
srcdir2 = sysconfig.get_config_var('srcdir') srcdir2 = sysconfig.get_config_var('srcdir')
finally:
os.chdir(cwd)
self.assertEqual(srcdir, srcdir2) self.assertEqual(srcdir, srcdir2)
@unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None,
......
...@@ -1104,10 +1104,8 @@ class WriteTest(WriteTestBase, unittest.TestCase): ...@@ -1104,10 +1104,8 @@ class WriteTest(WriteTestBase, unittest.TestCase):
self.assertEqual(tar.getnames(), [], self.assertEqual(tar.getnames(), [],
"added the archive to itself") "added the archive to itself")
cwd = os.getcwd() with support.change_cwd(TEMPDIR):
os.chdir(TEMPDIR) tar.add(dstname)
tar.add(dstname)
os.chdir(cwd)
self.assertEqual(tar.getnames(), [], self.assertEqual(tar.getnames(), [],
"added the archive to itself") "added the archive to itself")
finally: finally:
...@@ -1264,9 +1262,7 @@ class WriteTest(WriteTestBase, unittest.TestCase): ...@@ -1264,9 +1262,7 @@ class WriteTest(WriteTestBase, unittest.TestCase):
def test_cwd(self): def test_cwd(self):
# Test adding the current working directory. # Test adding the current working directory.
cwd = os.getcwd() with support.change_cwd(TEMPDIR):
os.chdir(TEMPDIR)
try:
tar = tarfile.open(tmpname, self.mode) tar = tarfile.open(tmpname, self.mode)
try: try:
tar.add(".") tar.add(".")
...@@ -1280,8 +1276,6 @@ class WriteTest(WriteTestBase, unittest.TestCase): ...@@ -1280,8 +1276,6 @@ class WriteTest(WriteTestBase, unittest.TestCase):
self.assertTrue(t.name.startswith("./"), t.name) self.assertTrue(t.name.startswith("./"), t.name)
finally: finally:
tar.close() tar.close()
finally:
os.chdir(cwd)
def test_open_nonwritable_fileobj(self): def test_open_nonwritable_fileobj(self):
for exctype in OSError, EOFError, RuntimeError: for exctype in OSError, EOFError, RuntimeError:
......
...@@ -5,7 +5,7 @@ import os, glob, time, shutil ...@@ -5,7 +5,7 @@ import os, glob, time, shutil
import unicodedata import unicodedata
import unittest 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) TESTFN_ENCODING, TESTFN_UNICODE, TESTFN_UNENCODABLE, create_empty_file)
if not os.path.supports_unicode_filenames: if not os.path.supports_unicode_filenames:
...@@ -82,13 +82,11 @@ class TestUnicodeFiles(unittest.TestCase): ...@@ -82,13 +82,11 @@ class TestUnicodeFiles(unittest.TestCase):
self.assertFalse(os.path.exists(filename2 + '.new')) self.assertFalse(os.path.exists(filename2 + '.new'))
def _do_directory(self, make_name, chdir_name): def _do_directory(self, make_name, chdir_name):
cwd = os.getcwd()
if os.path.isdir(make_name): if os.path.isdir(make_name):
rmtree(make_name) rmtree(make_name)
os.mkdir(make_name) os.mkdir(make_name)
try: try:
os.chdir(chdir_name) with change_cwd(chdir_name):
try:
cwd_result = os.getcwd() cwd_result = os.getcwd()
name_result = make_name name_result = make_name
...@@ -96,8 +94,6 @@ class TestUnicodeFiles(unittest.TestCase): ...@@ -96,8 +94,6 @@ class TestUnicodeFiles(unittest.TestCase):
name_result = unicodedata.normalize("NFD", name_result) name_result = unicodedata.normalize("NFD", name_result)
self.assertEqual(os.path.basename(cwd_result),name_result) self.assertEqual(os.path.basename(cwd_result),name_result)
finally:
os.chdir(cwd)
finally: finally:
os.rmdir(make_name) os.rmdir(make_name)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment