From 5db1e380e307d0f96177d18db778f82ea0ef87ce Mon Sep 17 00:00:00 2001 From: harry Date: Sat, 6 Apr 2019 15:15:23 +0800 Subject: [PATCH] Add several async os functions Calls of rename, mkdir, rmdir are added together with tests --- aiofiles/os.py | 3 +++ tests/test_os.py | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/aiofiles/os.py b/aiofiles/os.py index 52b9af2..f42c5e9 100644 --- a/aiofiles/os.py +++ b/aiofiles/os.py @@ -17,7 +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) diff --git a/tests/test_os.py b/tests/test_os.py index e9dc1cd..27a5269 100644 --- a/tests/test_os.py +++ b/tests/test_os.py @@ -1,7 +1,7 @@ """Tests for asyncio's os module.""" import aiofiles.os import asyncio -from os.path import join, dirname, exists +from os.path import join, dirname, exists, isdir import pytest import platform @@ -29,6 +29,30 @@ def test_remove(): 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")