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

Added test for test_util to ensure file absence when open for reading #1794

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 24 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pathlib

import os
import aiofiles.os
import pytest

Expand Down Expand Up @@ -31,3 +31,26 @@ 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_absent_on_directory_without_write_permissions(
tmp_path: pathlib.Path,
):
# Create a directory
path = tmp_path / "test_dir"
await aiofiles.os.mkdir(path)

# Change permissions to remove write access (read and execute only)
os.chmod(path, 0o500)

# 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)