diff --git a/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py b/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py index ccc1432..a2e7d4d 100644 --- a/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py +++ b/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py @@ -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) diff --git a/pytest-playwright/pytest_playwright/pytest_playwright.py b/pytest-playwright/pytest_playwright/pytest_playwright.py index 4a6a5a2..62c061c 100644 --- a/pytest-playwright/pytest_playwright/pytest_playwright.py +++ b/pytest-playwright/pytest_playwright/pytest_playwright.py @@ -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) diff --git a/tests/test_asyncio.py b/tests/test_asyncio.py index 26e83a0..c9567f9 100644 --- a/tests/test_asyncio.py +++ b/tests/test_asyncio.py @@ -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: diff --git a/tests/test_sync.py b/tests/test_sync.py index 866967d..88a982f 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -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: