From 424629122bbcb4b2551b03fe04ffd6c92f3365cb Mon Sep 17 00:00:00 2001 From: Siddhant Kumar Date: Sun, 16 Aug 2020 00:10:21 +0100 Subject: [PATCH] Use unix line-endings in bash activate script Signed-off-by: Siddhant Kumar --- docs/changelog/1818.bugfix.rst | 1 + src/virtualenv/activation/bash/__init__.py | 22 +++++++++++++++++++ tests/unit/activation/test_bash.py | 25 +++++++++++++++++++--- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 docs/changelog/1818.bugfix.rst diff --git a/docs/changelog/1818.bugfix.rst b/docs/changelog/1818.bugfix.rst new file mode 100644 index 000000000..f309adbd7 --- /dev/null +++ b/docs/changelog/1818.bugfix.rst @@ -0,0 +1 @@ +Use unix style line endings in bash activation script - by :user:`saytosid`. diff --git a/src/virtualenv/activation/bash/__init__.py b/src/virtualenv/activation/bash/__init__.py index 22c90c382..705a6a08b 100644 --- a/src/virtualenv/activation/bash/__init__.py +++ b/src/virtualenv/activation/bash/__init__.py @@ -1,13 +1,35 @@ from __future__ import absolute_import, unicode_literals +from virtualenv.info import IS_WIN from virtualenv.util.path import Path +from virtualenv.util.six import ensure_text from ..via_template import ViaTemplateActivator class BashActivator(ViaTemplateActivator): + def generate(self, creator): + generated = super(BashActivator, self).generate(creator) + if IS_WIN: + _convert_to_unix_line_endings(generated) + return generated + def templates(self): yield Path("activate.sh") def as_name(self, template): return template.stem + + +def _convert_to_unix_line_endings(generated): + WINDOWS_LINE_ENDING = b"\r\n" + UNIX_LINE_ENDING = b"\n" + + for file_path in generated: + with open(ensure_text(str(file_path)), "rb") as open_file: + content = open_file.read() + + content = content.replace(WINDOWS_LINE_ENDING, UNIX_LINE_ENDING) + + with open(ensure_text(str(file_path)), "wb") as open_file: + open_file.write(content) diff --git a/tests/unit/activation/test_bash.py b/tests/unit/activation/test_bash.py index 5246be09a..2a86caa7e 100644 --- a/tests/unit/activation/test_bash.py +++ b/tests/unit/activation/test_bash.py @@ -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): @@ -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 = Path(tmpdir / "activate") + with open(ensure_text(str(file_path)), "wb") as windows_line_endings_file: + windows_line_endings_file.writelines([b"Test_file\r\n"] * 10) + + return [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()