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

Use unix line-endings in bash activate script #1924

Merged
merged 7 commits into from
Aug 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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/1818.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use unix style line endings in bash activation script - by :user:`saytosid`.
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
25 changes: 22 additions & 3 deletions tests/unit/activation/test_bash.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from __future__ import absolute_import, unicode_literals

import sys

import pytest

from virtualenv.activation import BashActivator
from virtualenv.activation.via_template import ViaTemplateActivator
from virtualenv.info import IS_WIN
from virtualenv.util.path import Path
from virtualenv.util.six import ensure_text


@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 All @@ -16,3 +18,20 @@ def __init__(self, session):
)

activation_tester(Bash)


@pytest.mark.skipif(not IS_WIN, reason="Only makes sense on Windows")
def test_bash_activate_script_has_unix_line_endings(tmpdir, mocker):
class Mock_ViaTemplateActivator(ViaTemplateActivator):
def generate(self, creator):
file_path = ensure_text(str(tmpdir / "activate"))
with open(file_path, "wb") as windows_line_endings_file:
windows_line_endings_file.writelines([b"Test_file\r\n"] * 10)

return [Path(file_path)]

class TestBashActivator(BashActivator, Mock_ViaTemplateActivator):
pass

(file_path,) = TestBashActivator(mocker.MagicMock()).generate(mocker.MagicMock())
assert b"Test_file\n" * 10 == file_path.read_bytes()