Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add several async os calls #62

Merged
merged 3 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ several useful ``os`` functions that deal with files:

* ``stat``
* ``sendfile``
* ``rename``
* ``remove``
* ``mkdir``
* ``rmdir``

Writing tests for aiofiles
~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 4 additions & 0 deletions aiofiles/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def run(*args, loop=None, executor=None, **kwargs):


stat = wrap(os.stat)
rename = wrap(os.rename)
remove = wrap(os.remove)
mkdir = wrap(os.mkdir)
rmdir = wrap(os.rmdir)

if hasattr(os, "sendfile"):
sendfile = wrap(os.sendfile)
38 changes: 37 additions & 1 deletion tests/test_os.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Tests for asyncio's os module."""
import aiofiles.os
import asyncio
from os.path import join, dirname
from os.path import join, dirname, exists, isdir
import pytest
import platform

Expand All @@ -17,6 +17,42 @@ def test_stat():
assert stat_res.st_size == 10


@asyncio.coroutine
@pytest.mark.asyncio
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)
yield from aiofiles.os.remove(filename)
assert exists(filename) is False


@asyncio.coroutine
@pytest.mark.asyncio
def test_mkdir_and_rmdir():
"""Test the mkdir and rmdir call."""
directory = join(dirname(__file__), 'resources', 'test_dir')
yield from aiofiles.os.mkdir(directory)
assert isdir(directory)
yield from aiofiles.os.rmdir(directory)
assert exists(directory) is False


@asyncio.coroutine
@pytest.mark.asyncio
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')
yield from aiofiles.os.rename(old_filename, new_filename)
assert exists(old_filename) is False and exists(new_filename)
yield from aiofiles.os.rename(new_filename, old_filename)
assert exists(old_filename) and exists(new_filename) is False


@asyncio.coroutine
@pytest.mark.skipif('2.4' < platform.release() < '2.6.33',
reason = "sendfile() syscall doesn't allow file->file")
Expand Down