Skip to content

Commit

Permalink
Use unix line-endings in bash activate script (#1924)
Browse files Browse the repository at this point in the history
Co-authored-by: Bernat Gabor <[email protected]>
  • Loading branch information
Siddhant Kumar and gaborbernat authored Aug 23, 2020
1 parent 7de4f3f commit 6c098d8
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 73 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
*.bat text eol=crlf
*.ps1 text eol=lf
*.fish text eol=lf
*.csh text eol=lf
*.sh text eol=lf
1 change: 1 addition & 0 deletions docs/changelog/1818.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
For activation scripts always use UNIX line endings (unless it's BATCH shell related) - by :user:`saytosid`.
120 changes: 60 additions & 60 deletions src/virtualenv/activation/powershell/activate.ps1
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
$script:THIS_PATH = $myinvocation.mycommand.path
$script:BASE_DIR = Split-Path (Resolve-Path "$THIS_PATH/..") -Parent

function global:deactivate([switch] $NonDestructive) {
if (Test-Path variable:_OLD_VIRTUAL_PATH) {
$env:PATH = $variable:_OLD_VIRTUAL_PATH
Remove-Variable "_OLD_VIRTUAL_PATH" -Scope global
}

if (Test-Path function:_old_virtual_prompt) {
$function:prompt = $function:_old_virtual_prompt
Remove-Item function:\_old_virtual_prompt
}

if ($env:VIRTUAL_ENV) {
Remove-Item env:VIRTUAL_ENV -ErrorAction SilentlyContinue
}

if (!$NonDestructive) {
# Self destruct!
Remove-Item function:deactivate
Remove-Item function:pydoc
}
}

function global:pydoc {
python -m pydoc $args
}

# unset irrelevant variables
deactivate -nondestructive

$VIRTUAL_ENV = $BASE_DIR
$env:VIRTUAL_ENV = $VIRTUAL_ENV

New-Variable -Scope global -Name _OLD_VIRTUAL_PATH -Value $env:PATH

$env:PATH = "$env:VIRTUAL_ENV/__BIN_NAME____PATH_SEP__" + $env:PATH
if (!$env:VIRTUAL_ENV_DISABLE_PROMPT) {
function global:_old_virtual_prompt {
""
}
$function:_old_virtual_prompt = $function:prompt

if ("__VIRTUAL_PROMPT__" -ne "") {
function global:prompt {
# Add the custom prefix to the existing prompt
$previous_prompt_value = & $function:_old_virtual_prompt
("__VIRTUAL_PROMPT__" + $previous_prompt_value)
}
}
else {
function global:prompt {
# Add a prefix to the current prompt, but don't discard it.
$previous_prompt_value = & $function:_old_virtual_prompt
$new_prompt_value = "($( Split-Path $env:VIRTUAL_ENV -Leaf )) "
($new_prompt_value + $previous_prompt_value)
}
}
}
$script:THIS_PATH = $myinvocation.mycommand.path
$script:BASE_DIR = Split-Path (Resolve-Path "$THIS_PATH/..") -Parent

function global:deactivate([switch] $NonDestructive) {
if (Test-Path variable:_OLD_VIRTUAL_PATH) {
$env:PATH = $variable:_OLD_VIRTUAL_PATH
Remove-Variable "_OLD_VIRTUAL_PATH" -Scope global
}

if (Test-Path function:_old_virtual_prompt) {
$function:prompt = $function:_old_virtual_prompt
Remove-Item function:\_old_virtual_prompt
}

if ($env:VIRTUAL_ENV) {
Remove-Item env:VIRTUAL_ENV -ErrorAction SilentlyContinue
}

if (!$NonDestructive) {
# Self destruct!
Remove-Item function:deactivate
Remove-Item function:pydoc
}
}

function global:pydoc {
python -m pydoc $args
}

# unset irrelevant variables
deactivate -nondestructive

$VIRTUAL_ENV = $BASE_DIR
$env:VIRTUAL_ENV = $VIRTUAL_ENV

New-Variable -Scope global -Name _OLD_VIRTUAL_PATH -Value $env:PATH

$env:PATH = "$env:VIRTUAL_ENV/__BIN_NAME____PATH_SEP__" + $env:PATH
if (!$env:VIRTUAL_ENV_DISABLE_PROMPT) {
function global:_old_virtual_prompt {
""
}
$function:_old_virtual_prompt = $function:prompt

if ("__VIRTUAL_PROMPT__" -ne "") {
function global:prompt {
# Add the custom prefix to the existing prompt
$previous_prompt_value = & $function:_old_virtual_prompt
("__VIRTUAL_PROMPT__" + $previous_prompt_value)
}
}
else {
function global:prompt {
# Add a prefix to the current prompt, but don't discard it.
$previous_prompt_value = & $function:_old_virtual_prompt
$new_prompt_value = "($( Split-Path $env:VIRTUAL_ENV -Leaf )) "
($new_prompt_value + $previous_prompt_value)
}
}
}
12 changes: 7 additions & 5 deletions src/virtualenv/activation/via_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from .activator import Activator

if sys.version_info >= (3, 7):
from importlib.resources import read_text
from importlib.resources import read_binary
else:
from importlib_resources import read_text
from importlib_resources import read_binary


@add_metaclass(ABCMeta)
Expand Down Expand Up @@ -44,16 +44,18 @@ def _generate(self, replacements, templates, to_folder, creator):
for template in templates:
text = self.instantiate_template(replacements, template, creator)
dest = to_folder / self.as_name(template)
dest.write_text(text, encoding="utf-8")
# use write_bytes to avoid platform specific line normalization (\n -> \r\n)
dest.write_bytes(text.encode("utf-8"))
generated.append(dest)
return generated

def as_name(self, template):
return template.name

def instantiate_template(self, replacements, template, creator):
# read text and do replacements
text = read_text(self.__module__, str(template), encoding="utf-8", errors="strict")
# read content as binary to avoid platform specific line normalization (\n -> \r\n)
binary = read_binary(self.__module__, str(template))
text = binary.decode("utf-8", errors="strict")
for key, value in replacements.items():
value = self._repr_unicode(creator, value)
text = text.replace(key, value)
Expand Down
5 changes: 3 additions & 2 deletions src/virtualenv/create/via_global_ref/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from virtualenv.info import fs_supports_symlink
from virtualenv.util.path import Path
from virtualenv.util.six import ensure_text

from ..creator import Creator, CreatorMeta

Expand Down Expand Up @@ -91,10 +92,10 @@ def install_patch(self):
text = self.env_patch_text()
if text:
pth = self.purelib / "_virtualenv.pth"
logging.debug("create virtualenv import hook file %s", pth)
logging.debug("create virtualenv import hook file %s", ensure_text(str(pth)))
pth.write_text("import _virtualenv")
dest_path = self.purelib / "_virtualenv.py"
logging.debug("create %s", dest_path)
logging.debug("create %s", ensure_text(str(dest_path)))
dest_path.write_text(text)

def env_patch_text(self):
Expand Down
7 changes: 5 additions & 2 deletions src/virtualenv/util/path/_pathlib/via_os_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ def read_bytes(self):
with open(self._path, "rb") as file_handler:
return file_handler.read()

def write_text(self, text, encoding="utf-8"):
def write_bytes(self, content):
with open(self._path, "wb") as file_handler:
file_handler.write(text.encode(encoding))
file_handler.write(content)

def write_text(self, text, encoding="utf-8"):
self.write_bytes(text.encode(encoding))

def iterdir(self):
for p in os.listdir(self._path):
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/activation/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self, of_class, session, cmd, activate_script, extension):
self.pydoc_call = "pydoc -w pydoc_test"
self.script_encoding = "utf-8"
self._version = None
self.unix_line_ending = True

def get_version(self, raise_on_fail):
if self._version is None:
Expand Down Expand Up @@ -63,6 +64,16 @@ def __repr__(self):

def __call__(self, monkeypatch, tmp_path):
activate_script = self._creator.bin_dir / self.activate_script

# check line endings are correct type
script_content = activate_script.read_bytes()
for line in script_content.split(b"\n")[:-1]:
cr = b"\r" if sys.version_info.major == 2 else 13
if self.unix_line_ending:
assert line == b"" or line[-1] != cr, script_content.decode("utf-8")
else:
assert line[-1] == cr, script_content.decode("utf-8")

test_script = self._generate_test_script(activate_script, tmp_path)
monkeypatch.chdir(ensure_text(str(tmp_path)))

Expand Down
5 changes: 2 additions & 3 deletions tests/unit/activation/test_bash.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from __future__ import absolute_import, unicode_literals

import sys

import pytest

from virtualenv.activation import BashActivator
from virtualenv.info import IS_WIN


@pytest.mark.skipif(sys.platform == "win32", reason="Github Actions ships with WSL bash")
@pytest.mark.skipif(IS_WIN, reason="Github Actions ships with WSL bash")
def test_bash(raise_on_non_source_class, activation_tester):
class Bash(raise_on_non_source_class):
def __init__(self, session):
Expand Down
1 change: 1 addition & 0 deletions tests/unit/activation/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(self, session):
self.deactivate = "call deactivate"
self.activate_cmd = "call"
self.pydoc_call = "call {}".format(self.pydoc_call)
self.unix_line_ending = False

def _get_test_lines(self, activate_script):
# for BATCH utf-8 support need change the character code page to 650001
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/activation/test_python_activator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from textwrap import dedent

from virtualenv.activation import PythonActivator
from virtualenv.info import WIN_CPYTHON_2
from virtualenv.info import IS_WIN, WIN_CPYTHON_2
from virtualenv.util.six import ensure_text


Expand All @@ -21,6 +21,7 @@ def __init__(self, session):
extension="py",
non_source_fail_message="You must use exec(open(this_file).read(), {'__file__': this_file}))",
)
self.unix_line_ending = not IS_WIN

def env(self, tmp_path):
env = os.environ.copy()
Expand Down

0 comments on commit 6c098d8

Please sign in to comment.