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 activator path in cygwin and msys2 #1952

Merged
merged 1 commit into from
Sep 30, 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
1 change: 1 addition & 0 deletions docs/changelog/1940.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Provide correct path for bash activator in cygwin or msys2 - by :user:`danyeaw`.
12 changes: 11 additions & 1 deletion src/virtualenv/activation/via_template.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import absolute_import, unicode_literals

import os
import re
import sys
import sysconfig
from abc import ABCMeta, abstractmethod

from six import add_metaclass
Expand Down Expand Up @@ -31,9 +33,17 @@ def generate(self, creator):
return generated

def replacements(self, creator, dest_folder):
current_platform = sysconfig.get_platform()
platforms = ["mingw", "cygwin", "msys"]
if any(platform in current_platform for platform in platforms):
pattern = re.compile("^([A-Za-z]):(.*)")
match = pattern.match(str(creator.dest))
virtual_env = "/" + match.group(1).lower() + match.group(2)
else:
virtual_env = str(creator.dest)
return {
"__VIRTUAL_PROMPT__": "" if self.flag_prompt is None else self.flag_prompt,
"__VIRTUAL_ENV__": ensure_text(str(creator.dest)),
"__VIRTUAL_ENV__": ensure_text(virtual_env),
"__VIRTUAL_NAME__": creator.env_name,
"__BIN_NAME__": ensure_text(str(creator.bin_dir.relative_to(creator.dest))),
"__PATH_SEP__": ensure_text(os.pathsep),
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/activation/test_activation_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
PythonActivator,
)
from virtualenv.discovery.py_info import PythonInfo
from virtualenv.info import IS_WIN
from virtualenv.util.path import Path


@pytest.mark.parametrize(
Expand Down Expand Up @@ -53,3 +55,32 @@ def test_activator_no_support_posix(mocker, activator_class):
interpreter = mocker.Mock(spec=PythonInfo)
interpreter.os = "posix"
assert not activator.supports(interpreter)


class Creator:
def __init__(self):
self.dest = "C:/tools/msys64/home"
self.env_name = "venv"
self.bin_dir = Path("C:/tools/msys64/home/bin")


@pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash")
@pytest.mark.parametrize("activator_class", [BashActivator])
def test_cygwin_msys2_path_conversion(mocker, activator_class):
mocker.patch("sysconfig.get_platform", return_value="mingw")
activator = activator_class(Namespace(prompt=None))
creator = Creator()
mocker.stub(creator.bin_dir.relative_to)
resource = activator.replacements(creator, "")
assert resource["__VIRTUAL_ENV__"] == "/c/tools/msys64/home"


@pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash")
@pytest.mark.parametrize("activator_class", [BashActivator])
def test_win_path_no_conversion(mocker, activator_class):
mocker.patch("sysconfig.get_platform", return_value="win-amd64")
activator = activator_class(Namespace(prompt=None))
creator = Creator()
mocker.stub(creator.bin_dir.relative_to)
resource = activator.replacements(creator, "")
assert resource["__VIRTUAL_ENV__"] == "C:/tools/msys64/home"