Skip to content

Commit

Permalink
Added os scantdir with is't tests and improved the tests for listdir
Browse files Browse the repository at this point in the history
  • Loading branch information
santigandolfo authored and Tinche committed Sep 1, 2022
1 parent 2aefa1e commit 9bc662a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ several useful ``os`` functions that deal with files:
* ``symlink``
* ``readlink``
* ``listdir``
* ``scandir``
* ``path.exists``
* ``path.isfile``
* ``path.isdir``
Expand Down
1 change: 1 addition & 0 deletions src/aiofiles/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async def run(*args, loop=None, executor=None, **kwargs):
symlink = wrap(os.symlink)
readlink = wrap(os.readlink)
listdir = wrap(os.listdir)
scandir = wrap(os.scandir)

if hasattr(os, "sendfile"):
sendfile = wrap(os.sendfile)
87 changes: 75 additions & 12 deletions tests/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,28 +312,41 @@ async def test_listdir_empty_dir():
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")
some_file = join(some_dir, "some_file.txt")
await aiofiles.os.mkdir(some_dir)
with open(some_file, 'w') as f:
with open(some_file, "w") as f:
f.write("Test file")
dir_list = await aiofiles.os.listdir(some_dir)
assert dir_list == ["some_file"]
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 cas 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")
other_file = join(some_dir, "other_file")
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:
with open(some_file, "w") as f:
f.write("Test file")
with open(other_file, 'w') as f:
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"]
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)
Expand All @@ -343,13 +356,14 @@ 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")
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:
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"]
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)
Expand All @@ -361,3 +375,52 @@ async def test_listdir_non_existing_dir():
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)

0 comments on commit 9bc662a

Please sign in to comment.