1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
|
"""Tests for asyncio's os module."""
import aiofiles.os
import asyncio
import os
from os import stat
from os.path import join, dirname, exists, isdir
from pathlib import Path
import pytest
import platform
@pytest.mark.asyncio
async def test_stat():
"""Test the stat call."""
filename = join(dirname(__file__), "resources", "test_file1.txt")
stat_res = await aiofiles.os.stat(filename)
assert stat_res.st_size == 10
@pytest.mark.asyncio
async def test_remove():
"""Test the remove call."""
filename = join(dirname(__file__), "resources", "test_file2.txt")
with open(filename, "w") as f:
f.write("Test file for remove call")
assert exists(filename)
await aiofiles.os.remove(filename)
assert exists(filename) is False
@pytest.mark.asyncio
async def test_unlink():
"""Test the unlink call."""
filename = join(dirname(__file__), "resources", "test_file2.txt")
with open(filename, "w") as f:
f.write("Test file for unlink call")
assert exists(filename)
await aiofiles.os.unlink(filename)
assert exists(filename) is False
@pytest.mark.asyncio
async def test_mkdir_and_rmdir():
"""Test the mkdir and rmdir call."""
directory = join(dirname(__file__), "resources", "test_dir")
await aiofiles.os.mkdir(directory)
assert isdir(directory)
await aiofiles.os.rmdir(directory)
assert exists(directory) is False
@pytest.mark.asyncio
async def test_rename():
"""Test the rename call."""
old_filename = join(dirname(__file__), "resources", "test_file1.txt")
new_filename = join(dirname(__file__), "resources", "test_file2.txt")
await aiofiles.os.rename(old_filename, new_filename)
assert exists(old_filename) is False and exists(new_filename)
await aiofiles.os.rename(new_filename, old_filename)
assert exists(old_filename) and exists(new_filename) is False
@pytest.mark.asyncio
async def test_renames():
"""Test the renames call."""
old_filename = join(dirname(__file__), "resources", "test_file1.txt")
new_filename = join(
dirname(__file__), "resources", "subdirectory", "test_file2.txt"
)
await aiofiles.os.renames(old_filename, new_filename)
assert exists(old_filename) is False and exists(new_filename)
await aiofiles.os.renames(new_filename, old_filename)
assert (
exists(old_filename)
and exists(new_filename) is False
and exists(dirname(new_filename)) is False
)
@pytest.mark.asyncio
async def test_replace():
"""Test the replace call."""
old_filename = join(dirname(__file__), "resources", "test_file1.txt")
new_filename = join(dirname(__file__), "resources", "test_file2.txt")
await aiofiles.os.replace(old_filename, new_filename)
assert exists(old_filename) is False and exists(new_filename)
await aiofiles.os.replace(new_filename, old_filename)
assert exists(old_filename) and exists(new_filename) is False
with open(new_filename, "w") as f:
f.write("Test file")
assert exists(old_filename) and exists(new_filename)
await aiofiles.os.replace(old_filename, new_filename)
assert exists(old_filename) is False and exists(new_filename)
await aiofiles.os.replace(new_filename, old_filename)
assert exists(old_filename) and exists(new_filename) is False
@pytest.mark.skipif(
"2.4" < platform.release() < "2.6.33",
reason="sendfile() syscall doesn't allow file->file",
)
@pytest.mark.skipif(
platform.system() == "Darwin",
reason="sendfile() doesn't work on mac",
)
@pytest.mark.asyncio
async def test_sendfile_file(tmpdir):
"""Test the sendfile functionality, file-to-file."""
filename = join(dirname(__file__), "resources", "test_file1.txt")
tmp_filename = tmpdir.join("tmp.bin")
with open(filename) as f:
contents = f.read()
input_file = await aiofiles.open(filename)
output_file = await aiofiles.open(str(tmp_filename), mode="w+")
size = (await aiofiles.os.stat(filename)).st_size
input_fd = input_file.fileno()
output_fd = output_file.fileno()
await aiofiles.os.sendfile(output_fd, input_fd, 0, size)
await output_file.seek(0)
actual_contents = await output_file.read()
actual_size = (await aiofiles.os.stat(str(tmp_filename))).st_size
assert contents == actual_contents
assert size == actual_size
@pytest.mark.asyncio
async def test_sendfile_socket(unused_tcp_port):
"""Test the sendfile functionality, file-to-socket."""
filename = join(dirname(__file__), "resources", "test_file1.txt")
with open(filename, mode="rb") as f:
contents = f.read()
async def serve_file(_, writer):
out_fd = writer.transport.get_extra_info("socket").fileno()
size = (await aiofiles.os.stat(filename)).st_size
in_file = await aiofiles.open(filename)
try:
in_fd = in_file.fileno()
await aiofiles.os.sendfile(out_fd, in_fd, 0, size)
finally:
await in_file.close()
await writer.drain()
writer.close()
server = await asyncio.start_server(serve_file, port=unused_tcp_port)
reader, writer = await asyncio.open_connection("127.0.0.1", unused_tcp_port)
actual_contents = await reader.read()
writer.close()
assert contents == actual_contents
server.close()
await server.wait_closed()
@pytest.mark.asyncio
async def test_exists():
"""Test path.exists call."""
filename = join(dirname(__file__), "resources", "test_file1.txt")
result = await aiofiles.os.path.exists(filename)
assert result
@pytest.mark.asyncio
async def test_isfile():
"""Test path.isfile call."""
filename = join(dirname(__file__), "resources", "test_file1.txt")
result = await aiofiles.os.path.isfile(filename)
assert result
@pytest.mark.asyncio
async def test_isdir():
"""Test path.isdir call."""
filename = join(dirname(__file__), "resources")
result = await aiofiles.os.path.isdir(filename)
assert result
@pytest.mark.asyncio
async def test_islink():
"""Test the path.islink call."""
src_filename = join(dirname(__file__), "resources", "test_file1.txt")
dst_filename = join(dirname(__file__), "resources", "test_file2.txt")
await aiofiles.os.symlink(src_filename, dst_filename)
assert await aiofiles.os.path.islink(dst_filename)
await aiofiles.os.remove(dst_filename)
@pytest.mark.asyncio
async def test_getsize():
"""Test path.getsize call."""
filename = join(dirname(__file__), "resources", "test_file1.txt")
result = await aiofiles.os.path.getsize(filename)
assert result == 10
@pytest.mark.asyncio
async def test_samefile():
"""Test path.samefile call."""
filename = join(dirname(__file__), "resources", "test_file1.txt")
result = await aiofiles.os.path.samefile(filename, filename)
assert result
@pytest.mark.asyncio
async def test_sameopenfile():
"""Test path.samefile call."""
filename = join(dirname(__file__), "resources", "test_file1.txt")
result = await aiofiles.os.path.samefile(filename, filename)
assert result
@pytest.mark.asyncio
async def test_getmtime():
"""Test path.getmtime call."""
filename = join(dirname(__file__), "resources", "test_file1.txt")
result = await aiofiles.os.path.getmtime(filename)
assert result
@pytest.mark.asyncio
async def test_getatime():
"""Test path.getatime call."""
filename = join(dirname(__file__), "resources", "test_file1.txt")
result = await aiofiles.os.path.getatime(filename)
assert result
@pytest.mark.asyncio
async def test_getctime():
"""Test path. call."""
filename = join(dirname(__file__), "resources", "test_file1.txt")
result = await aiofiles.os.path.getctime(filename)
assert result
@pytest.mark.asyncio
async def test_link():
"""Test the link call."""
src_filename = join(dirname(__file__), "resources", "test_file1.txt")
dst_filename = join(dirname(__file__), "resources", "test_file2.txt")
initial_src_nlink = stat(src_filename).st_nlink
await aiofiles.os.link(src_filename, dst_filename)
assert (
exists(src_filename)
and exists(dst_filename)
and (stat(src_filename).st_ino == stat(dst_filename).st_ino)
and (stat(src_filename).st_nlink == initial_src_nlink + 1)
and (stat(dst_filename).st_nlink == 2)
)
await aiofiles.os.remove(dst_filename)
assert (
exists(src_filename)
and exists(dst_filename) is False
and (stat(src_filename).st_nlink == initial_src_nlink)
)
@pytest.mark.asyncio
async def test_symlink():
"""Test the symlink call."""
src_filename = join(dirname(__file__), "resources", "test_file1.txt")
dst_filename = join(dirname(__file__), "resources", "test_file2.txt")
await aiofiles.os.symlink(src_filename, dst_filename)
assert (
exists(src_filename)
and exists(dst_filename)
and stat(src_filename).st_ino == stat(dst_filename).st_ino
)
await aiofiles.os.remove(dst_filename)
assert exists(src_filename) and exists(dst_filename) is False
@pytest.mark.asyncio
async def test_readlink():
"""Test the readlink call."""
src_filename = join(dirname(__file__), "resources", "test_file1.txt")
dst_filename = join(dirname(__file__), "resources", "test_file2.txt")
await aiofiles.os.symlink(src_filename, dst_filename)
symlinked_path = await aiofiles.os.readlink(dst_filename)
assert src_filename == symlinked_path
await aiofiles.os.remove(dst_filename)
@pytest.mark.asyncio
async def test_listdir_empty_dir():
"""Test the listdir call when the dir is empty."""
directory = join(dirname(__file__), "resources", "empty_dir")
await aiofiles.os.mkdir(directory)
dir_list = await aiofiles.os.listdir(directory)
assert dir_list == []
await aiofiles.os.rmdir(directory)
@pytest.mark.asyncio
async def test_listdir_dir_with_only_one_file():
"""Test the listdir call when the dir has one file."""
some_dir = join(dirname(__file__), "resources", "some_dir")
some_file = join(some_dir, "some_file.txt")
await aiofiles.os.mkdir(some_dir)
with open(some_file, "w") as f:
f.write("Test file")
dir_list = await aiofiles.os.listdir(some_dir)
assert "some_file.txt" in dir_list
await aiofiles.os.remove(some_file)
await aiofiles.os.rmdir(some_dir)
@pytest.mark.asyncio
async def test_listdir_dir_with_only_one_dir():
"""Test the listdir call when the dir has one dir."""
some_dir = join(dirname(__file__), "resources", "some_dir")
other_dir = join(some_dir, "other_dir")
await aiofiles.os.mkdir(some_dir)
await aiofiles.os.mkdir(other_dir)
dir_list = await aiofiles.os.listdir(some_dir)
assert "other_dir" in dir_list
await aiofiles.os.rmdir(other_dir)
await aiofiles.os.rmdir(some_dir)
@pytest.mark.asyncio
async def test_listdir_dir_with_multiple_files():
"""Test the listdir call when the dir has multiple files."""
some_dir = join(dirname(__file__), "resources", "some_dir")
some_file = join(some_dir, "some_file.txt")
other_file = join(some_dir, "other_file.txt")
await aiofiles.os.mkdir(some_dir)
with open(some_file, "w") as f:
f.write("Test file")
with open(other_file, "w") as f:
f.write("Test file")
dir_list = await aiofiles.os.listdir(some_dir)
assert "some_file.txt" in dir_list
assert "other_file.txt" in dir_list
await aiofiles.os.remove(some_file)
await aiofiles.os.remove(other_file)
await aiofiles.os.rmdir(some_dir)
@pytest.mark.asyncio
async def test_listdir_dir_with_a_file_and_a_dir():
"""Test the listdir call when the dir has files and other dirs."""
some_dir = join(dirname(__file__), "resources", "some_dir")
other_dir = join(some_dir, "other_dir")
some_file = join(some_dir, "some_file.txt")
await aiofiles.os.mkdir(some_dir)
await aiofiles.os.mkdir(other_dir)
with open(some_file, "w") as f:
f.write("Test file")
dir_list = await aiofiles.os.listdir(some_dir)
assert "some_file.txt" in dir_list
assert "other_dir" in dir_list
await aiofiles.os.remove(some_file)
await aiofiles.os.rmdir(other_dir)
await aiofiles.os.rmdir(some_dir)
@pytest.mark.asyncio
async def test_listdir_non_existing_dir():
"""Test the listdir call when the dir doesn't exist."""
some_dir = join(dirname(__file__), "resources", "some_dir")
with pytest.raises(FileNotFoundError) as excinfo:
await aiofiles.os.listdir(some_dir)
@pytest.mark.asyncio
async def test_scantdir_empty_dir():
"""Test the scandir call when the dir is empty."""
empty_dir = join(dirname(__file__), "resources", "empty_dir")
await aiofiles.os.mkdir(empty_dir)
dir_iterator = await aiofiles.os.scandir(empty_dir)
dir_list = []
for dir_entity in dir_iterator:
dir_list.append(dir_entity)
assert dir_list == []
await aiofiles.os.rmdir(empty_dir)
@pytest.mark.asyncio
async def test_scandir_dir_with_only_one_file():
"""Test the scandir call when the dir has one file."""
some_dir = join(dirname(__file__), "resources", "some_dir")
some_file = join(some_dir, "some_file.txt")
await aiofiles.os.mkdir(some_dir)
with open(some_file, "w") as f:
f.write("Test file")
dir_iterator = await aiofiles.os.scandir(some_dir)
some_file_entity = next(dir_iterator)
assert some_file_entity.name == "some_file.txt"
await aiofiles.os.remove(some_file)
await aiofiles.os.rmdir(some_dir)
@pytest.mark.asyncio
async def test_scandir_dir_with_only_one_dir():
"""Test the scandir call when the dir has one dir."""
some_dir = join(dirname(__file__), "resources", "some_dir")
other_dir = join(some_dir, "other_dir")
await aiofiles.os.mkdir(some_dir)
await aiofiles.os.mkdir(other_dir)
dir_iterator = await aiofiles.os.scandir(some_dir)
other_dir_entity = next(dir_iterator)
assert other_dir_entity.name == "other_dir"
await aiofiles.os.rmdir(other_dir)
await aiofiles.os.rmdir(some_dir)
@pytest.mark.asyncio
async def test_scandir_non_existing_dir():
"""Test the scandir call when the dir doesn't exist."""
some_dir = join(dirname(__file__), "resources", "some_dir")
with pytest.raises(FileNotFoundError) as excinfo:
await aiofiles.os.scandir(some_dir)
@pytest.mark.asyncio
async def test_access():
temp_file = Path(__file__).parent.joinpath("resources", "os_access_temp.txt")
temp_dir = Path(__file__).parent.joinpath("resources", "os_access_temp")
# prepare
if temp_file.exists():
os.remove(temp_file)
assert not temp_file.exists()
temp_file.touch()
if temp_dir.exists():
os.rmdir(temp_dir)
assert not temp_dir.exists()
os.mkdir(temp_dir)
data = [
# full access
[0o777, os.F_OK, True],
[0o777, os.R_OK, True],
[0o777, os.W_OK, True],
[0o777, os.X_OK, True],
# chmod -x
[0o666, os.F_OK, True],
[0o666, os.R_OK, True],
[0o666, os.W_OK, True],
[0o666, os.X_OK, False],
# chmod -w
[0o444, os.F_OK, True],
[0o444, os.R_OK, True],
[0o444, os.W_OK, False],
[0o444, os.X_OK, False],
# chmod -r
[0o000, os.F_OK, True],
[0o000, os.R_OK, False],
[0o000, os.W_OK, False],
[0o000, os.X_OK, False],
]
for ch, mode, access in data:
print("mode:{}, access:{}".format(mode, access))
temp_file.chmod(ch)
temp_dir.chmod(ch)
assert await aiofiles.os.access(temp_file, mode) == access
assert await aiofiles.os.access(temp_dir, mode) == access
# not exists
os.remove(temp_file)
os.rmdir(temp_dir)
for mode in [os.F_OK, os.R_OK, os.W_OK, os.X_OK]:
print("mode:{}".format(mode))
assert not await aiofiles.os.access(temp_file, mode)
assert not await aiofiles.os.access(temp_dir, mode)
|