Use support.change_cwd() in tests.

This commit is contained in:
Serhiy Storchaka 2015-09-06 14:14:49 +03:00
commit 5fbadb63ef
9 changed files with 34 additions and 104 deletions

View file

@ -242,9 +242,7 @@ def test_recursive_glob(self):
('a', 'bcd', 'EF'), ('a', 'bcd', 'efg'))) ('a', 'bcd', 'EF'), ('a', 'bcd', 'efg')))
eq(self.rglob('a', '**', 'bcd'), self.joins(('a', 'bcd'))) eq(self.rglob('a', '**', 'bcd'), self.joins(('a', 'bcd')))
predir = os.path.abspath(os.curdir) with change_cwd(self.tempdir):
try:
os.chdir(self.tempdir)
join = os.path.join join = os.path.join
eq(glob.glob('**', recursive=True), [join(*i) for i in full]) eq(glob.glob('**', recursive=True), [join(*i) for i in full])
eq(glob.glob(join('**', ''), recursive=True), eq(glob.glob(join('**', ''), recursive=True),
@ -256,8 +254,6 @@ def test_recursive_glob(self):
if can_symlink(): if can_symlink():
expect += [join('sym3', 'EF')] expect += [join('sym3', 'EF')]
eq(glob.glob(join('**', 'EF'), recursive=True), expect) eq(glob.glob(join('**', 'EF'), recursive=True), expect)
finally:
os.chdir(predir)
@skip_unless_symlink @skip_unless_symlink

View file

@ -158,17 +158,11 @@ def test_rename(self):
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):

View file

@ -316,7 +316,6 @@ def test_realpath_symlink_loops(self):
# 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)
@ -342,10 +341,9 @@ def test_realpath_symlink_loops(self):
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")
@ -373,7 +371,6 @@ def test_realpath_repeated_indirect_symlinks(self):
@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):
@ -382,10 +379,9 @@ def test_realpath_deep_recursion(self):
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)
@ -399,15 +395,13 @@ def test_realpath_resolve_parents(self):
# /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)
@ -424,7 +418,6 @@ def test_realpath_resolve_before_normalizing(self):
# 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")
@ -433,11 +426,10 @@ def test_realpath_resolve_before_normalizing(self):
# 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")
@ -451,17 +443,14 @@ def test_realpath_resolve_first(self):
# 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)

View file

@ -63,11 +63,9 @@ def test_cache_path(self):
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))

View file

@ -12,8 +12,6 @@
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,
@ -974,12 +972,8 @@ def test_make_tarball(self):
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'
@ -987,12 +981,8 @@ def test_make_tarball(self):
# 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))
@ -1024,12 +1014,8 @@ def _create_files(self):
'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'
@ -1039,14 +1025,10 @@ def test_tarfile_vs_tar(self):
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
@ -1054,23 +1036,15 @@ def test_tarfile_vs_tar(self):
# 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))
@ -1130,15 +1104,11 @@ def test_make_archive_owner_group(self):
@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))

View file

@ -317,11 +317,8 @@ def _normalize_cwd(self, cwd):
# 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):

View file

@ -6,7 +6,7 @@
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 @@ def test_srcdir_independent_of_cwd(self):
# 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,

View file

@ -1132,10 +1132,8 @@ def test_add_self(self):
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:
@ -1292,9 +1290,7 @@ def test_abs_pathnames(self):
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(".")
@ -1308,8 +1304,6 @@ def test_cwd(self):
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:

View file

@ -5,7 +5,7 @@
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 @@ def _do_copyish(self, filename1, filename2):
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 @@ def _do_directory(self, make_name, chdir_name):
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)