From b46d762865a11b5ff3a5e144537fd391e8dfa9a3 Mon Sep 17 00:00:00 2001 From: Ilyas Shaikh Date: Wed, 2 Oct 2024 18:07:50 +0530 Subject: [PATCH 1/3] Added test for test_util to ensure file absence when open for reading --- tests/test_util.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_util.py b/tests/test_util.py index 58e7ed708..a4e9949b0 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -31,3 +31,17 @@ async def test_ensure_directory_absent(tmp_path: pathlib.Path): await ensure_absent(path) assert not await aiofiles.os.path.exists(path) + +@pytest.mark.asyncio +async def test_ensure_file_absent_when_open_for_reading(tmp_path: pathlib.Path): + path = tmp_path / "test_file" + async with aiofiles.open(path, "w") as test_file: + await test_file.write("foobar") + + # Open the file for reading + async with aiofiles.open(path, "r"): + pass # Simply open and close the file + + # Ensure the file is removed + await ensure_absent(path) + assert not await aiofiles.os.path.exists(path) \ No newline at end of file From 5d141f0a35bf2d292b92e99e4e171fe3f8ccc7a8 Mon Sep 17 00:00:00 2001 From: Ilyas Shaikh Date: Wed, 2 Oct 2024 19:02:34 +0530 Subject: [PATCH 2/3] Added ruff formatting --- tests/test_util.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index a4e9949b0..4e7bb7b00 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -32,16 +32,17 @@ async def test_ensure_directory_absent(tmp_path: pathlib.Path): await ensure_absent(path) assert not await aiofiles.os.path.exists(path) + @pytest.mark.asyncio async def test_ensure_file_absent_when_open_for_reading(tmp_path: pathlib.Path): path = tmp_path / "test_file" async with aiofiles.open(path, "w") as test_file: await test_file.write("foobar") - + # Open the file for reading async with aiofiles.open(path, "r"): pass # Simply open and close the file # Ensure the file is removed await ensure_absent(path) - assert not await aiofiles.os.path.exists(path) \ No newline at end of file + assert not await aiofiles.os.path.exists(path) From 1c8fdd6b9175a229aba4152281601130a4126764 Mon Sep 17 00:00:00 2001 From: Ilyas Shaikh Date: Wed, 2 Oct 2024 22:14:38 +0530 Subject: [PATCH 3/3] Updated test --- tests/test_util.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index 4e7bb7b00..087520e7e 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1,5 +1,5 @@ import pathlib - +import os import aiofiles.os import pytest @@ -34,15 +34,23 @@ async def test_ensure_directory_absent(tmp_path: pathlib.Path): @pytest.mark.asyncio -async def test_ensure_file_absent_when_open_for_reading(tmp_path: pathlib.Path): - path = tmp_path / "test_file" - async with aiofiles.open(path, "w") as test_file: - await test_file.write("foobar") +async def test_ensure_absent_on_directory_without_write_permissions( + tmp_path: pathlib.Path, +): + # Create a directory + path = tmp_path / "test_dir" + await aiofiles.os.mkdir(path) - # Open the file for reading - async with aiofiles.open(path, "r"): - pass # Simply open and close the file + # Change permissions to remove write access (read and execute only) + os.chmod(path, 0o500) - # Ensure the file is removed + # Try to remove the directory - should raise a PermissionError or OSError + with pytest.raises((PermissionError, OSError)): + await ensure_absent(path) + + # Clean up by restoring write permissions to allow removal after test + os.chmod(path, 0o700) await ensure_absent(path) + + # Ensure the directory was successfully cleaned up assert not await aiofiles.os.path.exists(path)