Debian Codegraph @

pypy/module/posix/test/test_scandir.py

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