Debian Codegraph @

tests/test_pathio.py

raw symbols SCIP
1 import contextlib
2
3 import pytest
4
5 import aioftp
6
7
8 @contextlib.contextmanager
9 def universal_exception_reason(*exc):
10 try:
11 yield
12 except aioftp.PathIOError as e:
13 type, instance, traceback = e.reason
14 m = f"Expect one of {exc}, got {instance}"
15 assert isinstance(instance, exc), m
16 else:
17 raise Exception(f"No excepton. Expect one of {exc}")
18
19
20 def test_has_state(path_io):
21 assert hasattr(path_io, "state")
22 path_io.state
23
24
25 @pytest.mark.asyncio
26 async def test_exists(path_io, temp_dir):
27 assert await path_io.exists(temp_dir)
28 assert not await path_io.exists(temp_dir / "foo")
29
30
31 @pytest.mark.asyncio
32 async def test_is_dir(path_io, temp_dir):
33 assert await path_io.is_dir(temp_dir)
34 p = temp_dir / "foo"
35 async with path_io.open(p, mode="wb"):
36 pass
37 assert not await path_io.is_dir(p)
38
39
40 @pytest.mark.asyncio
41 async def test_is_file(path_io, temp_dir):
42 p = temp_dir / "foo"
43 assert not await path_io.is_file(temp_dir)
44 async with path_io.open(p, mode="wb"):
45 pass
46 assert await path_io.is_file(p)
47
48
49 @pytest.mark.asyncio
50 async def test_mkdir(path_io, temp_dir):
51 p = temp_dir / "foo"
52 assert not await path_io.exists(p)
53 await path_io.mkdir(p)
54 assert await path_io.exists(p)
55
56
57 @pytest.mark.asyncio
58 async def test_rmdir(path_io, temp_dir):
59 p = temp_dir / "foo"
60 assert not await path_io.exists(p)
61 await path_io.mkdir(p)
62 assert await path_io.exists(p)
63 await path_io.rmdir(p)
64 assert not await path_io.exists(p)
65
66
67 @pytest.mark.asyncio
68 async def test_ulink(path_io, temp_dir):
69 p = temp_dir / "foo"
70 assert not await path_io.exists(p)
71 async with path_io.open(p, mode="wb"):
72 pass
73 assert await path_io.exists(p)
74 await path_io.unlink(p)
75 assert not await path_io.exists(p)
76
77
78 @pytest.mark.asyncio
79 async def test_list(path_io, temp_dir):
80 d, f = temp_dir / "dir", temp_dir / "file"
81 await path_io.mkdir(d)
82 async with path_io.open(f, mode="wb"):
83 pass
84 paths = await path_io.list(temp_dir)
85 assert len(paths) == 2
86 assert set(paths) == {d, f}
87 paths = set()
88 async for p in path_io.list(temp_dir):
89 paths.add(p)
90 assert set(paths) == {d, f}
91
92
93 @pytest.mark.asyncio
94 async def test_stat(path_io, temp_dir):
95 stat = await path_io.stat(temp_dir)
96 for a in ["st_size", "st_mtime", "st_ctime", "st_nlink", "st_mode"]:
97 assert hasattr(stat, a)
98
99
100 @pytest.mark.asyncio
101 async def test_open_context(path_io, temp_dir):
102 p = temp_dir / "context"
103 async with path_io.open(p, mode="wb") as f:
104 await f.write(b"foo")
105 async with path_io.open(p, mode="rb") as f:
106 assert await f.read() == b"foo"
107 async with path_io.open(p, mode="ab") as f:
108 await f.write(b"bar")
109 async with path_io.open(p, mode="rb") as f:
110 assert await f.read() == b"foobar"
111 async with path_io.open(p, mode="wb") as f:
112 await f.write(b"foo")
113 async with path_io.open(p, mode="rb") as f:
114 assert await f.read() == b"foo"
115 async with path_io.open(p, mode="r+b") as f:
116 assert await f.read(1) == b"f"
117 await f.write(b"un")
118 async with path_io.open(p, mode="rb") as f:
119 assert await f.read() == b"fun"
120
121
122 @pytest.mark.asyncio
123 async def test_open_plain(path_io, temp_dir):
124 p = temp_dir / "plain"
125 f = await path_io.open(p, mode="wb")
126 await f.write(b"foo")
127 await f.close()
128 f = await path_io.open(p, mode="rb")
129 assert await f.read() == b"foo"
130 await f.close()
131 f = await path_io.open(p, mode="ab")
132 await f.write(b"bar")
133 await f.close()
134 f = await path_io.open(p, mode="rb")
135 assert await f.read() == b"foobar"
136 await f.close()
137 f = await path_io.open(p, mode="wb")
138 await f.write(b"foo")
139 await f.close()
140 f = await path_io.open(p, mode="rb")
141 assert await f.read() == b"foo"
142 await f.close()
143 f = await path_io.open(p, mode="r+b")
144 assert await f.read(1) == b"f"
145 await f.write(b"un")
146 await f.close()
147 f = await path_io.open(p, mode="rb")
148 assert await f.read() == b"fun"
149 await f.close()
150
151
152 @pytest.mark.asyncio
153 async def test_file_methods(path_io, temp_dir):
154 p = temp_dir / "foo"
155 async with path_io.open(p, mode="wb") as f:
156 await f.write(b"foo")
157 with universal_exception_reason(ValueError):
158 await path_io.seek(f, 0)
159 await f.seek(0)
160 await f.write(b"bar")
161 async with path_io.open(p, mode="rb") as f:
162 assert await f.read() == b"bar"
163
164
165 @pytest.mark.asyncio
166 async def test_rename(path_io, temp_dir):
167 old = temp_dir / "foo"
168 new = temp_dir / "bar"
169 await path_io.mkdir(old)
170 assert await path_io.exists(old)
171 assert not await path_io.exists(new)
172 await path_io.rename(old, new)
173 assert not await path_io.exists(old)
174 assert await path_io.exists(new)
175
176
177 def test_repr_works(path_io, temp_dir):
178 repr(path_io)
179
180
181 @pytest.mark.asyncio
182 async def test_path_over_file(path_io, temp_dir):
183 f = temp_dir / "file"
184 async with path_io.open(f, mode="wb"):
185 pass
186 assert not await path_io.exists(f / "dir")
187
188
189 @pytest.mark.asyncio
190 async def test_mkdir_over_file(path_io, temp_dir):
191 f = temp_dir / "file"
192 async with path_io.open(f, mode="wb"):
193 pass
194 with universal_exception_reason(FileExistsError):
195 await path_io.mkdir(f)
196
197
198 @pytest.mark.asyncio
199 async def test_mkdir_no_parents(path_io, temp_dir):
200 p = temp_dir / "foo" / "bar"
201 with universal_exception_reason(FileNotFoundError):
202 await path_io.mkdir(p)
203
204
205 @pytest.mark.asyncio
206 async def test_mkdir_parent_is_file(path_io, temp_dir):
207 f = temp_dir / "foo"
208 async with path_io.open(f, mode="wb"):
209 pass
210 with universal_exception_reason(NotADirectoryError):
211 await path_io.mkdir(f / "bar")
212
213
214 @pytest.mark.asyncio
215 async def test_mkdir_parent_is_file_with_parents(path_io, temp_dir):
216 f = temp_dir / "foo"
217 async with path_io.open(f, mode="wb"):
218 pass
219 with universal_exception_reason(NotADirectoryError):
220 await path_io.mkdir(f / "bar", parents=True)
221
222
223 @pytest.mark.asyncio
224 async def test_rmdir_not_exist(path_io, temp_dir):
225 with universal_exception_reason(FileNotFoundError):
226 await path_io.rmdir(temp_dir / "foo")
227
228
229 @pytest.mark.asyncio
230 async def test_rmdir_on_file(path_io, temp_dir):
231 f = temp_dir / "foo"
232 async with path_io.open(f, mode="wb"):
233 pass
234 with universal_exception_reason(NotADirectoryError):
235 await path_io.rmdir(f)
236
237
238 @pytest.mark.asyncio
239 async def test_rmdir_not_empty(path_io, temp_dir):
240 f = temp_dir / "foo"
241 async with path_io.open(f, mode="wb"):
242 pass
243 with universal_exception_reason(OSError):
244 await path_io.rmdir(temp_dir)
245
246
247 @pytest.mark.asyncio
248 async def test_unlink_not_exist(path_io, temp_dir):
249 with universal_exception_reason(FileNotFoundError):
250 await path_io.unlink(temp_dir / "foo")
251
252
253 @pytest.mark.asyncio
254 async def test_unlink_on_dir(path_io, temp_dir):
255 with universal_exception_reason(IsADirectoryError, PermissionError):
256 await path_io.unlink(temp_dir)
257
258
259 @pytest.mark.asyncio
260 async def test_list_not_exist(path_io, temp_dir):
261 assert await path_io.list(temp_dir / "foo") == []
262
263
264 @pytest.mark.asyncio
265 async def test_stat_not_exist(path_io, temp_dir):
266 with universal_exception_reason(FileNotFoundError):
267 await path_io.stat(temp_dir / "foo")
268
269
270 @pytest.mark.asyncio
271 async def test_open_read_not_exist(path_io, temp_dir):
272 with universal_exception_reason(FileNotFoundError):
273 await path_io.open(temp_dir / "foo", mode="rb")
274
275
276 @pytest.mark.asyncio
277 async def test_open_write_unreachable(path_io, temp_dir):
278 with universal_exception_reason(FileNotFoundError):
279 await path_io.open(temp_dir / "foo" / "bar", mode="wb")
280
281
282 @pytest.mark.asyncio
283 async def test_open_write_directory(path_io, temp_dir):
284 with universal_exception_reason(IsADirectoryError):
285 await path_io.open(temp_dir, mode="wb")
286
287
288 @pytest.mark.asyncio
289 async def test_open_bad_mode(path_io, temp_dir):
290 with universal_exception_reason(ValueError):
291 await path_io.open(temp_dir, mode="bad")
292
293
294 @pytest.mark.asyncio
295 async def test_rename_source_or_dest_parent_not_exist(path_io, temp_dir):
296 with universal_exception_reason(FileNotFoundError):
297 await path_io.rename(temp_dir / "foo", temp_dir / "bar")
298 with universal_exception_reason(FileNotFoundError):
299 await path_io.rename(temp_dir, temp_dir / "foo" / "bar")
300
301
302 @pytest.mark.asyncio
303 async def test_rename_over_exists(path_io, temp_dir):
304 source = temp_dir / "source"
305 destination = temp_dir / "destination"
306 await path_io.mkdir(source)
307 await path_io.mkdir(destination)
308 await path_io.rename(source, destination)