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

fix: temp dir cleanup #775

Merged
merged 1 commit into from
Oct 13, 2024
Merged
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
6 changes: 4 additions & 2 deletions src/poetry/core/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ def temporary_directory(*args: Any, **kwargs: Any) -> Iterator[Path]:
yield Path(name)
else:
name = tempfile.mkdtemp(*args, **kwargs)
yield Path(name)
robust_rmtree(name)
try:
yield Path(name)
finally:
robust_rmtree(name)


def parse_requires(requires: str) -> list[str]:
Expand Down
52 changes: 50 additions & 2 deletions tests/utils/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ def test_utils_helpers_readme_content_type(
assert readme_content_type(readme) == content_type


@pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires Python 3.10 or higher")
def test_temporary_directory_python_3_10_or_newer(mocker: MockerFixture) -> None:
mocked_rmtree = mocker.patch("shutil.rmtree")
mocked_temp_dir = mocker.patch("tempfile.TemporaryDirectory")
mocked_mkdtemp = mocker.patch("tempfile.mkdtemp")

mocked_temp_dir.return_value.__enter__.return_value = "hello from test"

mocker.patch.object(sys, "version_info", (3, 10))
with temporary_directory() as tmp:
assert tmp == Path("hello from test")

Expand All @@ -143,14 +143,38 @@ def test_temporary_directory_python_3_10_or_newer(mocker: MockerFixture) -> None
mocked_temp_dir.assert_called_with(ignore_cleanup_errors=True)


@pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires Python 3.10 or higher")
def test_temporary_directory_python_3_10_or_newer_ensure_cleanup_on_error(
mocker: MockerFixture,
) -> None:
mocked_rmtree = mocker.patch("shutil.rmtree")
mocked_temp_dir = mocker.patch("tempfile.TemporaryDirectory")
mocked_mkdtemp = mocker.patch("tempfile.mkdtemp")

mocked_temp_dir.return_value.__enter__.return_value = "hello from test"

with pytest.raises(
Exception, match="Something went wrong"
), temporary_directory() as tmp:
assert tmp == Path("hello from test")

raise Exception("Something went wrong")

assert not mocked_rmtree.called
assert not mocked_mkdtemp.called
mocked_temp_dir.assert_called_with(ignore_cleanup_errors=True)


@pytest.mark.skipif(
sys.version_info >= (3, 10), reason="Not supported on Python 3.10 or higher"
)
def test_temporary_directory_python_3_9_or_older(mocker: MockerFixture) -> None:
mocked_rmtree = mocker.patch("shutil.rmtree")
mocked_temp_dir = mocker.patch("tempfile.TemporaryDirectory")
mocked_mkdtemp = mocker.patch("tempfile.mkdtemp")

mocked_mkdtemp.return_value = "hello from test"

mocker.patch.object(sys, "version_info", (3, 9))
with temporary_directory() as tmp:
assert tmp == Path("hello from test")

Expand All @@ -159,6 +183,30 @@ def test_temporary_directory_python_3_9_or_older(mocker: MockerFixture) -> None:
assert not mocked_temp_dir.called


@pytest.mark.skipif(
sys.version_info >= (3, 10), reason="Not supported on Python 3.10 or higher"
)
def test_temporary_directory_python_3_9_or_older_ensure_cleanup_on_error(
mocker: MockerFixture,
) -> None:
mocked_rmtree = mocker.patch("shutil.rmtree")
mocked_temp_dir = mocker.patch("tempfile.TemporaryDirectory")
mocked_mkdtemp = mocker.patch("tempfile.mkdtemp")

mocked_mkdtemp.return_value = "hello from test"

with pytest.raises(
Exception, match="Something went wrong"
), temporary_directory() as tmp:
assert tmp == Path("hello from test")

raise Exception("Something went wrong")

assert mocked_rmtree.called
assert mocked_mkdtemp.called
assert not mocked_temp_dir.called


def test_robust_rmtree(mocker: MockerFixture) -> None:
mocked_rmtree = mocker.patch("shutil.rmtree")

Expand Down
Loading