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

normalize path if it is a case insensitive file system #2421

Closed
Closed
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
3 changes: 2 additions & 1 deletion poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,8 @@ def get_base_prefix(self): # type: () -> Path
def generate_env_name(cls, name, cwd): # type: (str, str) -> str
name = name.lower()
sanitized_name = re.sub(r'[ $`!*@"\\\r\n\t]', "_", name)[:42]
h = hashlib.sha256(encode(cwd)).digest()
normalized_cwd = os.path.normcase(cwd)
h = hashlib.sha256(encode(normalized_cwd)).digest()
h = base64.urlsafe_b64encode(h).decode()[:8]

return "{}-{}".format(sanitized_name, h)
Expand Down
21 changes: 21 additions & 0 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import shutil
import sys
import tempfile

import pytest
import tomlkit
Expand Down Expand Up @@ -831,3 +832,23 @@ def test_env_site_packages_should_raise_an_error_if_no_site_packages(tmp_dir):

with pytest.raises(RuntimeError):
env.site_packages


def test_case_should_be_ignored_if_fs_is_case_sensitive(tmp_dir):
with tempfile.NamedTemporaryFile(prefix="TmP") as tmp_file:
if not os.path.exists(tmp_file.name.lower()):
path_with_lowercase = Path(tmp_dir) / "lowerpath"
path_with_uppercase = Path(tmp_dir) / "UPPERPATH"
path_with_bothcase = Path(tmp_dir) / "BoThCaSe"

venv_with_lowercase = EnvManager.generate_env_name(
"simple-project", str(path_with_lowercase)
)
venv_with_uppercase = EnvManager.generate_env_name(
"simple-project", str(path_with_uppercase)
)
venv_with_bothcase = EnvManager.generate_env_name(
"simple-project", str(path_with_bothcase)
)

assert venv_with_lowercase != venv_with_uppercase != venv_with_bothcase
Comment on lines +837 to +854
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, in its current state, the test does not do what it is supposed to do (even if the approach is fine). The test only checks if different pathes result in different venv names. (The pathes are different even if case is ignored!) The test should at least check two pathes that are equal for case insensitive file systems but different for case sensitive file systems.

Further, there are two minor quirks:

  1. The assert will be true even if venv_with_lowercase and venv_with_bothcase are equal, e.g.

    >>> 'a' != 'b' != 'a'
    True
    

    Probably, there should be three asserts each comparing a pair of venv names.

  2. The name of the test seems a little bit ambiguous to me (the meaning is "test: case should ...", but reads "'test case' should ...") and probably the wrong way around (case should be ignored for case insensitive file systems and not for case sensitive file systems).

All in all, my proposal for improvement:

Suggested change
def test_case_should_be_ignored_if_fs_is_case_sensitive(tmp_dir):
with tempfile.NamedTemporaryFile(prefix="TmP") as tmp_file:
if not os.path.exists(tmp_file.name.lower()):
path_with_lowercase = Path(tmp_dir) / "lowerpath"
path_with_uppercase = Path(tmp_dir) / "UPPERPATH"
path_with_bothcase = Path(tmp_dir) / "BoThCaSe"
venv_with_lowercase = EnvManager.generate_env_name(
"simple-project", str(path_with_lowercase)
)
venv_with_uppercase = EnvManager.generate_env_name(
"simple-project", str(path_with_uppercase)
)
venv_with_bothcase = EnvManager.generate_env_name(
"simple-project", str(path_with_bothcase)
)
assert venv_with_lowercase != venv_with_uppercase != venv_with_bothcase
def test_generate_env_name_ignores_case_for_case_insensitive_fs(tmp_dir):
path_with_lowercase = Path(tmp_dir) / "cwd"
path_with_uppercase = Path(tmp_dir) / "CWD"
path_with_bothcase = Path(tmp_dir) / "CwD"
venv_with_lowercase = EnvManager.generate_env_name(
"simple-project", str(path_with_lowercase)
)
venv_with_uppercase = EnvManager.generate_env_name(
"simple-project", str(path_with_uppercase)
)
venv_with_bothcase = EnvManager.generate_env_name(
"simple-project", str(path_with_bothcase)
)
with tempfile.NamedTemporaryFile(prefix="TmP") as tmp_file:
is_case_insensitive_fs = os.path.exists(tmp_file.name.lower())
if is_case_insensitive_fs:
assert venv_with_lowercase == venv_with_uppercase
assert venv_with_lowercase == venv_with_bothcase
assert venv_with_uppercase == venv_with_bothcase
else:
assert venv_with_lowercase != venv_with_uppercase
assert venv_with_lowercase != venv_with_bothcase
assert venv_with_uppercase != venv_with_bothcase