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

Simplify temporary_directory and use safe_rmtree #119

Merged
merged 5 commits into from
Dec 31, 2020
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
14 changes: 3 additions & 11 deletions poetry/core/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,9 @@ def normalize_version(version): # type: (str) -> str

@contextmanager
def temporary_directory(*args, **kwargs):
try:
from tempfile import TemporaryDirectory

with TemporaryDirectory(*args, **kwargs) as name:
yield name
except ImportError:
name = tempfile.mkdtemp(*args, **kwargs)
try:
yield name
finally:
shutil.rmtree(name)
name = tempfile.mkdtemp(*args, **kwargs)
yield name
safe_rmtree(name)


def parse_requires(requires): # type: (str) -> List[str]
Expand Down
16 changes: 16 additions & 0 deletions tests/utils/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import os

from stat import S_IREAD

import pytest

from poetry.core.utils.helpers import canonicalize_name
from poetry.core.utils.helpers import parse_requires
from poetry.core.utils.helpers import temporary_directory


def test_parse_requires():
Expand Down Expand Up @@ -62,3 +67,14 @@ def test_parse_requires():
)
def test_utils_helpers_canonical_names(raw):
assert canonicalize_name(raw) == "a-b-c"


def test_utils_helpers_temporary_directory_readonly_file():
thejcannon marked this conversation as resolved.
Show resolved Hide resolved
with temporary_directory() as temp_dir:
readonly_filename = os.path.join(temp_dir, "file.txt")
with open(readonly_filename, "w+") as readonly_file:
readonly_file.write("Poetry rocks!")
os.chmod(str(readonly_filename), S_IREAD)

assert not os.path.exists(temp_dir)
assert not os.path.exists(readonly_filename)