From 601dc7896c812d39b75f2358b17a9460561d3866 Mon Sep 17 00:00:00 2001 From: Adam Gregory Date: Fri, 5 Aug 2022 17:40:40 +0100 Subject: [PATCH] Resolve cwd using os.path.realpath() when generating environment names (#6110) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Resolve cwd using os.path.realpath() when generating environment names * Add test_generate_env_name_uses_real_path() unit test * ensure normcase * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Randy Döring <30527984+radoering@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- src/poetry/utils/env.py | 2 +- tests/utils/test_env.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/poetry/utils/env.py b/src/poetry/utils/env.py index 024713e9272..6c7e60deb34 100644 --- a/src/poetry/utils/env.py +++ b/src/poetry/utils/env.py @@ -1145,7 +1145,7 @@ def get_base_prefix(cls) -> Path: def generate_env_name(cls, name: str, cwd: str) -> str: name = name.lower() sanitized_name = re.sub(r'[ $`!*@"\\\r\n\t]', "_", name)[:42] - normalized_cwd = os.path.normcase(cwd) + normalized_cwd = os.path.normcase(os.path.realpath(cwd)) h_bytes = hashlib.sha256(encode(normalized_cwd)).digest() h_str = base64.urlsafe_b64encode(h_bytes).decode()[:8] diff --git a/tests/utils/test_env.py b/tests/utils/test_env.py index a3a4474b399..03c1e4c29ff 100644 --- a/tests/utils/test_env.py +++ b/tests/utils/test_env.py @@ -1362,6 +1362,13 @@ def test_generate_env_name_ignores_case_for_case_insensitive_fs(tmp_dir: str): assert venv_name1 != venv_name2 +def test_generate_env_name_uses_real_path(tmp_dir: str, mocker: MockerFixture): + mocker.patch("os.path.realpath", return_value="the_real_dir") + venv_name1 = EnvManager.generate_env_name("simple-project", "the_real_dir") + venv_name2 = EnvManager.generate_env_name("simple-project", "linked_dir") + assert venv_name1 == venv_name2 + + @pytest.fixture() def extended_without_setup_poetry() -> Poetry: poetry = Factory().create_poetry(