From 2aefa1e4df8356604b5692fda45877b8a401e0df Mon Sep 17 00:00:00 2001 From: santigandolfo Date: Thu, 1 Sep 2022 10:26:42 -0300 Subject: [PATCH] Added os listdir function with it's tests --- README.rst | 1 + src/aiofiles/os.py | 1 + tests/test_os.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/README.rst b/README.rst index 289b891..463a42f 100644 --- a/README.rst +++ b/README.rst @@ -113,6 +113,7 @@ several useful ``os`` functions that deal with files: * ``link`` * ``symlink`` * ``readlink`` +* ``listdir`` * ``path.exists`` * ``path.isfile`` * ``path.isdir`` diff --git a/src/aiofiles/os.py b/src/aiofiles/os.py index 19e5699..7227bb4 100644 --- a/src/aiofiles/os.py +++ b/src/aiofiles/os.py @@ -31,6 +31,7 @@ async def run(*args, loop=None, executor=None, **kwargs): link = wrap(os.link) symlink = wrap(os.symlink) readlink = wrap(os.readlink) +listdir = wrap(os.listdir) if hasattr(os, "sendfile"): sendfile = wrap(os.sendfile) diff --git a/tests/test_os.py b/tests/test_os.py index 28da638..372d1ad 100644 --- a/tests/test_os.py +++ b/tests/test_os.py @@ -296,3 +296,68 @@ async def test_readlink(): 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") + 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 dir_list == ["some_file"] + await aiofiles.os.remove(some_file) + await aiofiles.os.rmdir(some_dir) + +@pytest.mark.asyncio +async def test_listdir_dir_with_multiple_files(): + """Test the listdir call when the dir cas multiple files.""" + some_dir = join(dirname(__file__), "resources", "some_dir") + some_file = join(some_dir, "some_file") + other_file = join(some_dir, "other_file") + 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 dir_list == ["some_file", "other_file"] + 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") + 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 dir_list == ["some_file", "other_dir"] + 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)