bpo-46426: Improve tests for the dir_fd argument (GH-30668) (GH-30739)

Ensure that directory file descriptors refer to directories different
from the current directory, and that src_dir_fd and dst_dir_fd refer
to different directories.

Add context manager open_dir_fd() in test.support.os_helper.
(cherry picked from commit 54610bb448)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2022-01-21 09:31:25 -08:00 committed by GitHub
parent 68a31dba97
commit a1015c6478
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 208 additions and 230 deletions

View file

@ -848,12 +848,9 @@ class UtimeTests(unittest.TestCase):
def test_utime_dir_fd(self):
def set_time(filename, ns):
dirname, name = os.path.split(filename)
dirfd = os.open(dirname, os.O_RDONLY)
try:
with os_helper.open_dir_fd(dirname) as dirfd:
# pass dir_fd to test utimensat(timespec) or futimesat(timeval)
os.utime(name, dir_fd=dirfd, ns=ns)
finally:
os.close(dirfd)
self._test_utime(set_time)
def test_utime_directory(self):
@ -4339,8 +4336,7 @@ class TestScandir(unittest.TestCase):
os.symlink('file.txt', os.path.join(self.path, 'link'))
expected_names.append('link')
fd = os.open(self.path, os.O_RDONLY)
try:
with os_helper.open_dir_fd(self.path) as fd:
with os.scandir(fd) as it:
entries = list(it)
names = [entry.name for entry in entries]
@ -4355,8 +4351,6 @@ class TestScandir(unittest.TestCase):
self.assertEqual(entry.stat(), st)
st = os.stat(entry.name, dir_fd=fd, follow_symlinks=False)
self.assertEqual(entry.stat(follow_symlinks=False), st)
finally:
os.close(fd)
def test_empty_path(self):
self.assertRaises(FileNotFoundError, os.scandir, '')