Skip to content

Commit 2b0bbba

Browse files
authored
Fix absolute paths for Cygwin (#1970)
* Fix absolute paths for Cygwin Absolute paths on Windows take the form "c:\somePath" -- these need to be mapped to the form "/cygdrive/c/somePath" on Cygwin. Otherwise, the virtualenv path gets garbled when activated on Cygwin, which can cause the wrong Python environment to be used. * Remove Cygwin from the list, rely on the shell script for that * Improved formatting * Update tests to handle mingw + msys explicitly * Add changelog entry * lint errors * Add msys support to the activation script * Fix script error & linting
1 parent 8df2649 commit 2b0bbba

File tree

4 files changed

+5
-58
lines changed

4 files changed

+5
-58
lines changed

docs/changelog/1969.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Handle Cygwin path conversion in the activation script - by :user:`davidcoghlan`.

src/virtualenv/activation/bash/activate.sh

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ deactivate () {
4747
deactivate nondestructive
4848

4949
VIRTUAL_ENV='__VIRTUAL_ENV__'
50+
if ([ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ]) && $(command -v cygpath &> /dev/null) ; then
51+
VIRTUAL_ENV=$(cygpath -u "$VIRTUAL_ENV")
52+
fi
5053
export VIRTUAL_ENV
5154

5255
_OLD_VIRTUAL_PATH="$PATH"

src/virtualenv/activation/via_template.py

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from __future__ import absolute_import, unicode_literals
22

33
import os
4-
import re
54
import sys
6-
import sysconfig
75
from abc import ABCMeta, abstractmethod
86

97
from six import add_metaclass
@@ -33,20 +31,9 @@ def generate(self, creator):
3331
return generated
3432

3533
def replacements(self, creator, dest_folder):
36-
current_platform = sysconfig.get_platform()
37-
platforms = ["mingw", "cygwin", "msys"]
38-
if any(platform in current_platform for platform in platforms):
39-
pattern = re.compile("^([A-Za-z]):(.*)")
40-
match = pattern.match(str(creator.dest))
41-
if match:
42-
virtual_env = "/" + match.group(1).lower() + match.group(2)
43-
else:
44-
virtual_env = str(creator.dest)
45-
else:
46-
virtual_env = str(creator.dest)
4734
return {
4835
"__VIRTUAL_PROMPT__": "" if self.flag_prompt is None else self.flag_prompt,
49-
"__VIRTUAL_ENV__": ensure_text(virtual_env),
36+
"__VIRTUAL_ENV__": ensure_text(str(creator.dest)),
5037
"__VIRTUAL_NAME__": creator.env_name,
5138
"__BIN_NAME__": ensure_text(str(creator.bin_dir.relative_to(creator.dest))),
5239
"__PATH_SEP__": ensure_text(os.pathsep),

tests/unit/activation/test_activation_support.py

-44
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
PythonActivator,
1414
)
1515
from virtualenv.discovery.py_info import PythonInfo
16-
from virtualenv.info import IS_WIN
17-
from virtualenv.util.path import Path
1816

1917

2018
@pytest.mark.parametrize(
@@ -55,45 +53,3 @@ def test_activator_no_support_posix(mocker, activator_class):
5553
interpreter = mocker.Mock(spec=PythonInfo)
5654
interpreter.os = "posix"
5755
assert not activator.supports(interpreter)
58-
59-
60-
class Creator:
61-
def __init__(self):
62-
self.dest = "C:/tools/msys64/home"
63-
self.env_name = "venv"
64-
self.bin_dir = Path("C:/tools/msys64/home/bin")
65-
66-
67-
@pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash")
68-
@pytest.mark.parametrize("activator_class", [BashActivator])
69-
def test_cygwin_msys2_path_conversion(mocker, activator_class):
70-
mocker.patch("sysconfig.get_platform", return_value="mingw")
71-
activator = activator_class(Namespace(prompt=None))
72-
creator = Creator()
73-
mocker.stub(creator.bin_dir.relative_to)
74-
resource = activator.replacements(creator, "")
75-
assert resource["__VIRTUAL_ENV__"] == "/c/tools/msys64/home"
76-
77-
78-
@pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash")
79-
@pytest.mark.parametrize("activator_class", [BashActivator])
80-
def test_win_path_no_conversion(mocker, activator_class):
81-
mocker.patch("sysconfig.get_platform", return_value="win-amd64")
82-
activator = activator_class(Namespace(prompt=None))
83-
creator = Creator()
84-
mocker.stub(creator.bin_dir.relative_to)
85-
resource = activator.replacements(creator, "")
86-
assert resource["__VIRTUAL_ENV__"] == "C:/tools/msys64/home"
87-
88-
89-
@pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash")
90-
@pytest.mark.parametrize("activator_class", [BashActivator])
91-
def test_cygwin_path_no_conversion(mocker, activator_class):
92-
mocker.patch("sysconfig.get_platform", return_value="cygwin")
93-
activator = activator_class(Namespace(prompt=None))
94-
creator = Creator()
95-
creator.dest = "/c/tools/msys64/home"
96-
creator.bin_dir = Path("/c/tools/msys64/home/bin")
97-
mocker.stub(creator.bin_dir.relative_to)
98-
resource = activator.replacements(creator, "")
99-
assert resource["__VIRTUAL_ENV__"] == "/c/tools/msys64/home"

0 commit comments

Comments
 (0)