Skip to content

Commit 91a4e17

Browse files
committed
Merge test_os and test_posix into test_misc
1 parent 8134e19 commit 91a4e17

File tree

2 files changed

+162
-175
lines changed

2 files changed

+162
-175
lines changed

Lib/test/test_os/test_posix.py renamed to Lib/test/test_os/test_misc.py

Lines changed: 162 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
"Test posix functions"
2-
3-
from test import support
4-
from test.support import os_helper
5-
from test.support import warnings_helper
6-
from test.support.script_helper import assert_python_ok
7-
81
import errno
92
import os
103
import signal
114
import stat
125
import sys
136
import unittest
147
import warnings
8+
9+
from test import support
10+
from test.support import os_helper
11+
from test.support import warnings_helper
12+
from test.support.os_helper import FakePath
13+
from test.support.script_helper import assert_python_ok
14+
1515
from .utils import create_file
1616

1717
try:
@@ -25,6 +25,161 @@
2525
pwd = None
2626

2727

28+
@unittest.skipIf(support.is_wasi, "WASI has no /dev/null")
29+
class DevNullTests(unittest.TestCase):
30+
def test_devnull(self):
31+
with open(os.devnull, 'wb', 0) as f:
32+
f.write(b'hello')
33+
f.close()
34+
with open(os.devnull, 'rb') as f:
35+
self.assertEqual(f.read(), b'')
36+
37+
38+
class OSErrorTests(unittest.TestCase):
39+
def setUp(self):
40+
class Str(str):
41+
pass
42+
43+
self.bytes_filenames = []
44+
self.unicode_filenames = []
45+
if os_helper.TESTFN_UNENCODABLE is not None:
46+
decoded = os_helper.TESTFN_UNENCODABLE
47+
else:
48+
decoded = os_helper.TESTFN
49+
self.unicode_filenames.append(decoded)
50+
self.unicode_filenames.append(Str(decoded))
51+
if os_helper.TESTFN_UNDECODABLE is not None:
52+
encoded = os_helper.TESTFN_UNDECODABLE
53+
else:
54+
encoded = os.fsencode(os_helper.TESTFN)
55+
self.bytes_filenames.append(encoded)
56+
57+
self.filenames = self.bytes_filenames + self.unicode_filenames
58+
59+
def test_oserror_filename(self):
60+
funcs = [
61+
(self.filenames, os.chdir,),
62+
(self.filenames, os.lstat,),
63+
(self.filenames, os.open, os.O_RDONLY),
64+
(self.filenames, os.rmdir,),
65+
(self.filenames, os.stat,),
66+
(self.filenames, os.unlink,),
67+
(self.filenames, os.listdir,),
68+
(self.filenames, os.rename, "dst"),
69+
(self.filenames, os.replace, "dst"),
70+
]
71+
if os_helper.can_chmod():
72+
funcs.append((self.filenames, os.chmod, 0o777))
73+
if hasattr(os, "chown"):
74+
funcs.append((self.filenames, os.chown, 0, 0))
75+
if hasattr(os, "lchown"):
76+
funcs.append((self.filenames, os.lchown, 0, 0))
77+
if hasattr(os, "truncate"):
78+
funcs.append((self.filenames, os.truncate, 0))
79+
if hasattr(os, "chflags"):
80+
funcs.append((self.filenames, os.chflags, 0))
81+
if hasattr(os, "lchflags"):
82+
funcs.append((self.filenames, os.lchflags, 0))
83+
if hasattr(os, "chroot"):
84+
funcs.append((self.filenames, os.chroot,))
85+
if hasattr(os, "link"):
86+
funcs.append((self.filenames, os.link, "dst"))
87+
if hasattr(os, "listxattr"):
88+
funcs.extend((
89+
(self.filenames, os.listxattr,),
90+
(self.filenames, os.getxattr, "user.test"),
91+
(self.filenames, os.setxattr, "user.test", b'user'),
92+
(self.filenames, os.removexattr, "user.test"),
93+
))
94+
if hasattr(os, "lchmod"):
95+
funcs.append((self.filenames, os.lchmod, 0o777))
96+
if hasattr(os, "readlink"):
97+
funcs.append((self.filenames, os.readlink,))
98+
99+
for filenames, func, *func_args in funcs:
100+
for name in filenames:
101+
try:
102+
func(name, *func_args)
103+
except OSError as err:
104+
self.assertIs(err.filename, name, str(func))
105+
except UnicodeDecodeError:
106+
pass
107+
else:
108+
self.fail(f"No exception thrown by {func}")
109+
110+
111+
class PathTConverterTests(unittest.TestCase):
112+
# tuples of (function name, allows fd arguments, additional arguments to
113+
# function, cleanup function)
114+
functions = [
115+
('stat', True, (), None),
116+
('lstat', False, (), None),
117+
('access', False, (os.F_OK,), None),
118+
('chflags', False, (0,), None),
119+
('lchflags', False, (0,), None),
120+
('open', False, (os.O_RDONLY,), getattr(os, 'close', None)),
121+
]
122+
123+
def test_path_t_converter(self):
124+
str_filename = os_helper.TESTFN
125+
if os.name == 'nt':
126+
bytes_fspath = bytes_filename = None
127+
else:
128+
bytes_filename = os.fsencode(os_helper.TESTFN)
129+
bytes_fspath = FakePath(bytes_filename)
130+
fd = os.open(FakePath(str_filename), os.O_WRONLY|os.O_CREAT)
131+
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
132+
self.addCleanup(os.close, fd)
133+
134+
int_fspath = FakePath(fd)
135+
str_fspath = FakePath(str_filename)
136+
137+
for name, allow_fd, extra_args, cleanup_fn in self.functions:
138+
with self.subTest(name=name):
139+
try:
140+
fn = getattr(os, name)
141+
except AttributeError:
142+
continue
143+
144+
for path in (str_filename, bytes_filename, str_fspath,
145+
bytes_fspath):
146+
if path is None:
147+
continue
148+
with self.subTest(name=name, path=path):
149+
result = fn(path, *extra_args)
150+
if cleanup_fn is not None:
151+
cleanup_fn(result)
152+
153+
with self.assertRaisesRegex(
154+
TypeError, 'to return str or bytes'):
155+
fn(int_fspath, *extra_args)
156+
157+
if allow_fd:
158+
result = fn(fd, *extra_args) # should not fail
159+
if cleanup_fn is not None:
160+
cleanup_fn(result)
161+
else:
162+
with self.assertRaisesRegex(
163+
TypeError,
164+
'os.PathLike'):
165+
fn(fd, *extra_args)
166+
167+
def test_path_t_converter_and_custom_class(self):
168+
msg = r'__fspath__\(\) to return str or bytes, not %s'
169+
with self.assertRaisesRegex(TypeError, msg % r'int'):
170+
os.stat(FakePath(2))
171+
with self.assertRaisesRegex(TypeError, msg % r'float'):
172+
os.stat(FakePath(2.34))
173+
with self.assertRaisesRegex(TypeError, msg % r'object'):
174+
os.stat(FakePath(object()))
175+
176+
177+
class ExportsTests(unittest.TestCase):
178+
def test_os_all(self):
179+
self.assertIn('open', os.__all__)
180+
self.assertIn('walk', os.__all__)
181+
182+
28183
class PosixTester(unittest.TestCase):
29184

30185
def setUp(self):

Lib/test/test_os/test_os.py

Lines changed: 0 additions & 168 deletions
This file was deleted.

0 commit comments

Comments
 (0)