Debian Codegraph @

extra_tests/test_scandir.py

raw symbols SCIP
1 # coding=utf-8
2 import pytest
3 import os
4 import sys
5 try:
6 import nt as posix
7 except ImportError:
8 import posix
9
10 has_os_symlink = hasattr(os, 'symlink')
11
12
13 def _make_dir(tmpdir, dirname, content):
14 d = os.path.join(str(tmpdir), dirname)
15 os.mkdir(d)
16 for key, value in content.items():
17 filename = os.path.join(d, key)
18 if value == 'dir':
19 os.mkdir(filename)
20 elif value == 'file':
21 with open(filename, 'w'):
22 pass
23 elif value == 'symlink-file':
24 some_file = os.path.join(d, 'some_file')
25 with open(some_file, 'w'):
26 pass
27 os.symlink(some_file, filename)
28 elif value == 'symlink-dir':
29 os.symlink(str(tmpdir), filename)
30 elif value == 'symlink-broken':
31 os.symlink(filename + '-broken', filename)
32 elif value == 'symlink-error':
33 os.symlink(filename, filename)
34 else:
35 raise NotImplementedError(repr(value))
36 return d
37
38
39 def _make_dir_empty(tmpdir):
40 return _make_dir(tmpdir, 'empty', {})
41
42
43 def _make_dir0(tmpdir):
44 return _make_dir(tmpdir, 'dir0', {'f1': 'file',
45 'f2': 'file',
46 'f3': 'file'})
47
48
49 def _make_dir1(tmpdir):
50 return _make_dir(tmpdir, 'dir1', {'file1': 'file'})
51
52
53 def _make_dir2(tmpdir):
54 return _make_dir(tmpdir, 'dir2', {'subdir2': 'dir'})
55
56
57 def _make_dir3(tmpdir):
58 return _make_dir(tmpdir, 'dir3', {'sfile3': 'symlink-file'})
59
60
61 def _make_dir4(tmpdir):
62 return _make_dir(tmpdir, 'dir4', {'ünicode': 'symlink-dir'})
63
64
65 def _make_dir5(tmpdir):
66 return _make_dir(tmpdir, 'dir5', {'sbrok5': 'symlink-broken'})
67
68
69 def _make_dir6(tmpdir):
70 return _make_dir(tmpdir, 'dir6', {'serr6': 'symlink-error'})
71
72
73 def test_scandir_empty(tmpdir):
74 dir_empty = _make_dir_empty(tmpdir)
75 sd = os.scandir(dir_empty)
76 assert list(sd) == []
77
78
79 def test_scandir_files(tmpdir):
80 dir0 = _make_dir0(tmpdir)
81 sd = os.scandir(dir0)
82 names = [d.name for d in sd]
83 assert sorted(names) == ['f1', 'f2', 'f3']
84
85
86 def test_unicode_versus_bytes():
87 d = next(os.scandir())
88 assert type(d.name) is str
89 assert type(d.path) is str
90 assert d.path == '.' + os.sep + d.name
91 d = next(os.scandir(None))
92 assert type(d.name) is str
93 assert type(d.path) is str
94 assert d.path == '.' + os.sep + d.name
95 d = next(os.scandir(u'.'))
96 assert type(d.name) is str
97 assert type(d.path) is str
98 assert d.path == '.' + os.sep + d.name
99 d = next(os.scandir(os.sep))
100 assert type(d.name) is str
101 assert type(d.path) is str
102 assert d.path == os.sep + d.name
103 d = next(os.scandir(b'.'))
104 assert type(d.name) is bytes
105 assert type(d.path) is bytes
106 assert d.path == b'.' + os.sep.encode('ascii') + d.name
107 d = next(os.scandir(b'/'))
108 assert type(d.name) is bytes
109 assert type(d.path) is bytes
110 assert d.path == b'/' + d.name
111
112
113 def test_stat1(tmpdir):
114 dir1 = _make_dir1(tmpdir)
115 d = next(os.scandir(dir1))
116 assert d.name == 'file1'
117 assert d.stat().st_mode & 0o170000 == 0o100000 # S_IFREG
118 assert d.stat().st_size == 0
119
120
121 @pytest.mark.skipif(not has_os_symlink, reason="no symlink support")
122 def test_stat4(tmpdir):
123 dir4 = _make_dir4(tmpdir)
124 d = next(os.scandir(dir4))
125 assert d.name == 'ünicode'
126 assert d.stat().st_mode & 0o170000 == 0o040000 # S_IFDIR
127 assert d.stat(follow_symlinks=True).st_mode &0o170000 == 0o040000
128 assert d.stat(follow_symlinks=False).st_mode&0o170000 == 0o120000 #IFLNK
129
130
131 def test_dir1(tmpdir):
132 dir1 = _make_dir1(tmpdir)
133 d = next(os.scandir(dir1))
134 assert d.name == 'file1'
135 assert d.is_file()
136 assert not d.is_dir()
137 assert not d.is_symlink()
138 with pytest.raises(TypeError):
139 assert d.is_file(True)
140 assert d.is_file(follow_symlinks=False)
141 assert not d.is_dir(follow_symlinks=False)
142
143
144 def test_dir2(tmpdir):
145 dir2 = _make_dir2(tmpdir)
146 d = next(os.scandir(dir2))
147 assert d.name == 'subdir2'
148 assert not d.is_file()
149 assert d.is_dir()
150 assert not d.is_symlink()
151 assert not d.is_file(follow_symlinks=False)
152 assert d.is_dir(follow_symlinks=False)
153
154
155 @pytest.mark.skipif(not has_os_symlink, reason="no symlink support")
156 def test_dir3(tmpdir):
157 dir3 = _make_dir3(tmpdir)
158 sd = os.scandir(dir3)
159 d = next(sd)
160 if d.name == 'some_file':
161 d = next(sd)
162 assert d.name == 'sfile3'
163 assert d.is_file()
164 assert not d.is_dir()
165 assert d.is_symlink()
166 assert d.is_file(follow_symlinks=True)
167 assert not d.is_file(follow_symlinks=False)
168
169
170 @pytest.mark.skipif(not has_os_symlink, reason="no symlink support")
171 def test_dir4(tmpdir):
172 dir4 = _make_dir4(tmpdir)
173 sd = os.scandir(dir4)
174 d = next(sd)
175 assert d.name == 'ünicode'
176 assert not d.is_file()
177 assert d.is_dir()
178 assert d.is_symlink()
179 assert d.is_dir(follow_symlinks=True)
180 assert not d.is_dir(follow_symlinks=False)
181
182
183 @pytest.mark.skipif(not has_os_symlink, reason="no symlink support")
184 def test_dir5(tmpdir):
185 dir5 = _make_dir5(tmpdir)
186 d = next(os.scandir(dir5))
187 assert d.name == 'sbrok5'
188 assert not d.is_file()
189 assert not d.is_dir()
190 assert d.is_symlink()
191 with pytest.raises(OSError):
192 d.stat()
193
194
195 @pytest.mark.skipif(not has_os_symlink, reason="no symlink support")
196 def test_dir6(tmpdir):
197 dir6 = _make_dir6(tmpdir)
198 d = next(os.scandir(dir6))
199 assert d.name == 'serr6'
200 with pytest.raises(OSError):
201 d.is_file()
202 with pytest.raises(OSError):
203 d.is_dir()
204 assert d.is_symlink()
205
206
207 def test_fdopendir(tmpdir):
208 import stat
209 dir0 = _make_dir0(tmpdir)
210 dir2 = _make_dir2(tmpdir)
211 if 'HAVE_FDOPENDIR' in posix._have_functions:
212 with pytest.raises(OSError):
213 os.scandir(1234)
214 # do like shutil._rmtree_safe_fd
215 topfd = os.open(dir2, os.O_RDONLY)
216 try:
217 with os.scandir(topfd) as scandir_it:
218 entries = list(scandir_it)
219 assert len(entries) > 0
220 entry = entries[0]
221 stat_val = entry.stat(follow_symlinks=False)
222 assert stat.S_ISDIR(stat_val.st_mode)
223 finally:
224 os.close(topfd)
225 fd = os.open(dir0 + os.sep + 'f1', os.O_RDONLY)
226 try:
227 with pytest.raises(NotADirectoryError):
228 os.scandir(fd)
229 finally:
230 os.close(fd)
231 else:
232 with pytest.raises(TypeError):
233 os.scandir(1234)
234
235
236 @pytest.mark.skipif(sys.platform == "win32", reason="no inode support")
237 def test_inode(tmpdir):
238 dir1 = _make_dir1(tmpdir)
239 d = next(os.scandir(dir1))
240 assert d.name == 'file1'
241 ino = d.inode()
242 assert ino == d.stat().st_ino
243
244
245 def test_repr(tmpdir):
246 dir1 = _make_dir1(tmpdir)
247 d = next(os.scandir(dir1))
248 assert isinstance(d, os.DirEntry)
249 assert repr(d) == "<DirEntry 'file1'>"
250
251
252 def test_direntry_unpicklable(tmpdir):
253 import pickle
254 dir1 = _make_dir1(tmpdir)
255 d = next(os.scandir(dir1))
256 with pytest.raises(TypeError):
257 pickle.dumps(d)
258
259
260 def test_fspath(tmpdir):
261 dir1 = _make_dir1(tmpdir)
262 d = next(os.scandir(dir1))
263 assert os.fspath(d).endswith('dir1' + os.sep + 'file1')
264
265
266 def test_resource_warning(tmpdir):
267 import warnings, gc
268 dir1 = _make_dir1(tmpdir)
269 iterator = os.scandir(dir1)
270 next(iterator)
271 with warnings.catch_warnings(record=True) as l:
272 warnings.simplefilter("always")
273 del iterator
274 gc.collect()
275 assert isinstance(l[0].message, ResourceWarning)
276 #
277 iterator = os.scandir(dir1)
278 next(iterator)
279 with warnings.catch_warnings(record=True) as w:
280 warnings.simplefilter("always")
281 iterator.close()
282 del iterator
283 gc.collect()
284 if len(w) > 0:
285 [print(x) for x in w]
286 assert len(w) == 0
287
288
289 def test_context_manager(tmpdir):
290 import warnings, gc
291 dir1 = _make_dir1(tmpdir)
292 with warnings.catch_warnings(record=True) as l:
293 warnings.simplefilter("always")
294 with os.scandir(dir1) as iterator:
295 next(iterator)
296 del iterator
297 gc.collect()
298 assert not l
299
300
301 def test_lstat(tmpdir):
302 dir1 = _make_dir1(tmpdir)
303 d = next(os.scandir(dir1))
304 with open(d) as fp:
305 length = len(fp.read())
306 assert os.lstat(d).st_size == length