Skip to content

Commit 4746cf3

Browse files
committed
Split test_os files into smaller files
Fix test_fs_holes(): use "w+b" mode, and delete the file once done.
1 parent 2bd6bea commit 4746cf3

24 files changed

+8034
-7696
lines changed

Lib/test/test_os/test_dirfd.py

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
"""
2+
Tests for the posix *at functions.
3+
"""
4+
5+
import posix
6+
import os
7+
import unittest
8+
import time
9+
import stat
10+
import errno
11+
from contextlib import contextmanager
12+
from test import support
13+
from test.support import os_helper
14+
15+
16+
class TestPosixDirFd(unittest.TestCase):
17+
count = 0
18+
19+
@contextmanager
20+
def prepare(self):
21+
TestPosixDirFd.count += 1
22+
name = f'{os_helper.TESTFN}_{self.count}'
23+
base_dir = f'{os_helper.TESTFN}_{self.count}base'
24+
posix.mkdir(base_dir)
25+
self.addCleanup(posix.rmdir, base_dir)
26+
fullname = os.path.join(base_dir, name)
27+
assert not os.path.exists(fullname)
28+
with os_helper.open_dir_fd(base_dir) as dir_fd:
29+
yield (dir_fd, name, fullname)
30+
31+
@contextmanager
32+
def prepare_file(self):
33+
with self.prepare() as (dir_fd, name, fullname):
34+
os_helper.create_empty_file(fullname)
35+
self.addCleanup(posix.unlink, fullname)
36+
yield (dir_fd, name, fullname)
37+
38+
@unittest.skipUnless(os.access in os.supports_dir_fd, "test needs dir_fd support for os.access()")
39+
def test_access_dir_fd(self):
40+
with self.prepare_file() as (dir_fd, name, fullname):
41+
self.assertTrue(posix.access(name, os.R_OK, dir_fd=dir_fd))
42+
43+
@unittest.skipUnless(os.chmod in os.supports_dir_fd, "test needs dir_fd support in os.chmod()")
44+
def test_chmod_dir_fd(self):
45+
with self.prepare_file() as (dir_fd, name, fullname):
46+
posix.chmod(fullname, stat.S_IRUSR)
47+
posix.chmod(name, stat.S_IRUSR | stat.S_IWUSR, dir_fd=dir_fd)
48+
s = posix.stat(fullname)
49+
self.assertEqual(s.st_mode & stat.S_IRWXU,
50+
stat.S_IRUSR | stat.S_IWUSR)
51+
52+
@unittest.skipUnless(hasattr(os, 'chown') and (os.chown in os.supports_dir_fd),
53+
"test needs dir_fd support in os.chown()")
54+
@unittest.skipIf(support.is_emscripten, "getgid() is a stub")
55+
def test_chown_dir_fd(self):
56+
with self.prepare_file() as (dir_fd, name, fullname):
57+
posix.chown(name, os.getuid(), os.getgid(), dir_fd=dir_fd)
58+
59+
@unittest.skipUnless(os.stat in os.supports_dir_fd, "test needs dir_fd support in os.stat()")
60+
def test_stat_dir_fd(self):
61+
with self.prepare() as (dir_fd, name, fullname):
62+
with open(fullname, 'w') as outfile:
63+
outfile.write("testline\n")
64+
self.addCleanup(posix.unlink, fullname)
65+
66+
s1 = posix.stat(fullname)
67+
s2 = posix.stat(name, dir_fd=dir_fd)
68+
self.assertEqual(s1, s2)
69+
s2 = posix.stat(fullname, dir_fd=None)
70+
self.assertEqual(s1, s2)
71+
72+
self.assertRaisesRegex(TypeError, 'should be integer or None, not',
73+
posix.stat, name, dir_fd=posix.getcwd())
74+
self.assertRaisesRegex(TypeError, 'should be integer or None, not',
75+
posix.stat, name, dir_fd=float(dir_fd))
76+
self.assertRaises(OverflowError,
77+
posix.stat, name, dir_fd=10**20)
78+
79+
for fd in False, True:
80+
with self.assertWarnsRegex(RuntimeWarning,
81+
'bool is used as a file descriptor') as cm:
82+
with self.assertRaises(OSError):
83+
posix.stat('nonexisting', dir_fd=fd)
84+
self.assertEqual(cm.filename, __file__)
85+
86+
@unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd support in os.utime()")
87+
def test_utime_dir_fd(self):
88+
with self.prepare_file() as (dir_fd, name, fullname):
89+
now = time.time()
90+
posix.utime(name, None, dir_fd=dir_fd)
91+
posix.utime(name, dir_fd=dir_fd)
92+
self.assertRaises(TypeError, posix.utime, name,
93+
now, dir_fd=dir_fd)
94+
self.assertRaises(TypeError, posix.utime, name,
95+
(None, None), dir_fd=dir_fd)
96+
self.assertRaises(TypeError, posix.utime, name,
97+
(now, None), dir_fd=dir_fd)
98+
self.assertRaises(TypeError, posix.utime, name,
99+
(None, now), dir_fd=dir_fd)
100+
self.assertRaises(TypeError, posix.utime, name,
101+
(now, "x"), dir_fd=dir_fd)
102+
posix.utime(name, (int(now), int(now)), dir_fd=dir_fd)
103+
posix.utime(name, (now, now), dir_fd=dir_fd)
104+
posix.utime(name,
105+
(int(now), int((now - int(now)) * 1e9)), dir_fd=dir_fd)
106+
posix.utime(name, dir_fd=dir_fd,
107+
times=(int(now), int((now - int(now)) * 1e9)))
108+
109+
# try dir_fd and follow_symlinks together
110+
if os.utime in os.supports_follow_symlinks:
111+
try:
112+
posix.utime(name, follow_symlinks=False, dir_fd=dir_fd)
113+
except ValueError:
114+
# whoops! using both together not supported on this platform.
115+
pass
116+
117+
@unittest.skipIf(
118+
support.is_wasi,
119+
"WASI: symlink following on path_link is not supported"
120+
)
121+
@unittest.skipUnless(
122+
hasattr(os, "link") and os.link in os.supports_dir_fd,
123+
"test needs dir_fd support in os.link()"
124+
)
125+
def test_link_dir_fd(self):
126+
with self.prepare_file() as (dir_fd, name, fullname), \
127+
self.prepare() as (dir_fd2, linkname, fulllinkname):
128+
try:
129+
posix.link(name, linkname, src_dir_fd=dir_fd, dst_dir_fd=dir_fd2)
130+
except PermissionError as e:
131+
self.skipTest('posix.link(): %s' % e)
132+
self.addCleanup(posix.unlink, fulllinkname)
133+
# should have same inodes
134+
self.assertEqual(posix.stat(fullname)[1],
135+
posix.stat(fulllinkname)[1])
136+
137+
@unittest.skipUnless(os.mkdir in os.supports_dir_fd, "test needs dir_fd support in os.mkdir()")
138+
def test_mkdir_dir_fd(self):
139+
with self.prepare() as (dir_fd, name, fullname):
140+
posix.mkdir(name, dir_fd=dir_fd)
141+
self.addCleanup(posix.rmdir, fullname)
142+
posix.stat(fullname) # should not raise exception
143+
144+
@unittest.skipUnless(hasattr(os, 'mknod')
145+
and (os.mknod in os.supports_dir_fd)
146+
and hasattr(stat, 'S_IFIFO'),
147+
"test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
148+
def test_mknod_dir_fd(self):
149+
# Test using mknodat() to create a FIFO (the only use specified
150+
# by POSIX).
151+
with self.prepare() as (dir_fd, name, fullname):
152+
mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
153+
try:
154+
posix.mknod(name, mode, 0, dir_fd=dir_fd)
155+
except OSError as e:
156+
# Some old systems don't allow unprivileged users to use
157+
# mknod(), or only support creating device nodes.
158+
self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES))
159+
else:
160+
self.addCleanup(posix.unlink, fullname)
161+
self.assertTrue(stat.S_ISFIFO(posix.stat(fullname).st_mode))
162+
163+
@unittest.skipUnless(os.open in os.supports_dir_fd, "test needs dir_fd support in os.open()")
164+
def test_open_dir_fd(self):
165+
with self.prepare() as (dir_fd, name, fullname):
166+
with open(fullname, 'wb') as outfile:
167+
outfile.write(b"testline\n")
168+
self.addCleanup(posix.unlink, fullname)
169+
fd = posix.open(name, posix.O_RDONLY, dir_fd=dir_fd)
170+
try:
171+
res = posix.read(fd, 9)
172+
self.assertEqual(b"testline\n", res)
173+
finally:
174+
posix.close(fd)
175+
176+
@unittest.skipUnless(hasattr(os, 'readlink') and (os.readlink in os.supports_dir_fd),
177+
"test needs dir_fd support in os.readlink()")
178+
def test_readlink_dir_fd(self):
179+
with self.prepare() as (dir_fd, name, fullname):
180+
os.symlink('symlink', fullname)
181+
self.addCleanup(posix.unlink, fullname)
182+
self.assertEqual(posix.readlink(name, dir_fd=dir_fd), 'symlink')
183+
184+
@unittest.skipUnless(os.rename in os.supports_dir_fd, "test needs dir_fd support in os.rename()")
185+
def test_rename_dir_fd(self):
186+
with self.prepare_file() as (dir_fd, name, fullname), \
187+
self.prepare() as (dir_fd2, name2, fullname2):
188+
posix.rename(name, name2,
189+
src_dir_fd=dir_fd, dst_dir_fd=dir_fd2)
190+
posix.stat(fullname2) # should not raise exception
191+
posix.rename(fullname2, fullname)
192+
193+
@unittest.skipUnless(os.symlink in os.supports_dir_fd, "test needs dir_fd support in os.symlink()")
194+
def test_symlink_dir_fd(self):
195+
with self.prepare() as (dir_fd, name, fullname):
196+
posix.symlink('symlink', name, dir_fd=dir_fd)
197+
self.addCleanup(posix.unlink, fullname)
198+
self.assertEqual(posix.readlink(fullname), 'symlink')
199+
200+
@unittest.skipUnless(os.unlink in os.supports_dir_fd, "test needs dir_fd support in os.unlink()")
201+
def test_unlink_dir_fd(self):
202+
with self.prepare() as (dir_fd, name, fullname):
203+
os_helper.create_empty_file(fullname)
204+
posix.stat(fullname) # should not raise exception
205+
try:
206+
posix.unlink(name, dir_fd=dir_fd)
207+
self.assertRaises(OSError, posix.stat, fullname)
208+
except:
209+
self.addCleanup(posix.unlink, fullname)
210+
raise
211+
212+
@unittest.skipUnless(hasattr(os, 'mkfifo') and os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
213+
def test_mkfifo_dir_fd(self):
214+
with self.prepare() as (dir_fd, name, fullname):
215+
try:
216+
posix.mkfifo(name, stat.S_IRUSR | stat.S_IWUSR, dir_fd=dir_fd)
217+
except PermissionError as e:
218+
self.skipTest('posix.mkfifo(): %s' % e)
219+
self.addCleanup(posix.unlink, fullname)
220+
self.assertTrue(stat.S_ISFIFO(posix.stat(fullname).st_mode))
221+
222+
223+
if __name__ == '__main__':
224+
unittest.main()

0 commit comments

Comments
 (0)