Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,12 @@ def delete_output_dir(pytestconfig: Any) -> None:
# https://github.com/microsoft/playwright/issues/12106
# https://github.com/microsoft/playwright-python/issues/1781
# Do a best-effort to remove all files inside of it instead.
entries = os.listdir(output_dir)
for entry in entries:
shutil.rmtree(entry)
for entry in os.listdir(output_dir):
path = os.path.join(output_dir, entry)
if os.path.isdir(path) and not os.path.islink(path):
shutil.rmtree(path)
else:
os.remove(path)


@pytest.hookimpl(wrapper=True, tryfirst=True)
Expand Down
9 changes: 6 additions & 3 deletions pytest-playwright/pytest_playwright/pytest_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ def delete_output_dir(pytestconfig: Any) -> None:
# https://github.com/microsoft/playwright/issues/12106
# https://github.com/microsoft/playwright-python/issues/1781
# Do a best-effort to remove all files inside of it instead.
entries = os.listdir(output_dir)
for entry in entries:
shutil.rmtree(entry)
for entry in os.listdir(output_dir):
path = os.path.join(output_dir, entry)
if os.path.isdir(path) and not os.path.islink(path):
shutil.rmtree(path)
else:
os.remove(path)


@pytest.hookimpl(wrapper=True, tryfirst=True)
Expand Down
38 changes: 38 additions & 0 deletions tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,44 @@ async def test_artifact_collection(new_context):
result.assert_outcomes(passed=1)


def test_output_folder_is_emptied_when_it_cannot_be_removed(
testdir: pytest.Testdir,
) -> None:
# e.g. a mounted folder: https://github.com/microsoft/playwright/issues/12106
testdir.makeconftest(
"""
import errno
import os
import shutil

_rmtree = shutil.rmtree

def rmtree(path, *args, **kwargs):
if os.path.abspath(path) == os.path.abspath("test-results"):
raise OSError(errno.EBUSY, "Device or resource busy", path)
return _rmtree(path, *args, **kwargs)

shutil.rmtree = rmtree
"""
)
testdir.makepyfile(
"""
import pytest

@pytest.mark.asyncio
async def test_passing(page):
pass
"""
)
os.makedirs(os.path.join(testdir.tmpdir, "test-results", "old-test"))
testdir.tmpdir.join("test-results", "old-file.txt").write("")
os.makedirs(os.path.join(testdir.tmpdir, "old-test"))
result = testdir.runpytest()
result.assert_outcomes(passed=1)
assert os.listdir(os.path.join(testdir.tmpdir, "test-results")) == []
assert os.path.isdir(os.path.join(testdir.tmpdir, "old-test"))


def test_output_path_via_pytest_runtest_makereport_hook(
testdir: pytest.Testdir,
) -> None:
Expand Down
35 changes: 35 additions & 0 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,41 @@ def test_artifact_collection(new_context):
result.assert_outcomes(passed=1)


def test_output_folder_is_emptied_when_it_cannot_be_removed(
testdir: pytest.Testdir,
) -> None:
# e.g. a mounted folder: https://github.com/microsoft/playwright/issues/12106
testdir.makeconftest(
"""
import errno
import os
import shutil

_rmtree = shutil.rmtree

def rmtree(path, *args, **kwargs):
if os.path.abspath(path) == os.path.abspath("test-results"):
raise OSError(errno.EBUSY, "Device or resource busy", path)
return _rmtree(path, *args, **kwargs)

shutil.rmtree = rmtree
"""
)
testdir.makepyfile(
"""
def test_passing(page):
pass
"""
)
os.makedirs(os.path.join(testdir.tmpdir, "test-results", "old-test"))
testdir.tmpdir.join("test-results", "old-file.txt").write("")
os.makedirs(os.path.join(testdir.tmpdir, "old-test"))
result = testdir.runpytest()
result.assert_outcomes(passed=1)
assert os.listdir(os.path.join(testdir.tmpdir, "test-results")) == []
assert os.path.isdir(os.path.join(testdir.tmpdir, "old-test"))


def test_output_path_via_pytest_runtest_makereport_hook(
testdir: pytest.Testdir,
) -> None:
Expand Down
Loading