Skip to content

Commit

Permalink
Use unix line-endings in bash activate script
Browse files Browse the repository at this point in the history
Fixes: pypa#1818

Signed-off-by: Siddhant Kumar <skumar619@bloomberg.net>
  • Loading branch information
Siddhant Kumar committed Aug 15, 2020
1 parent 10f232b commit 2ceb225
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
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`.
21 changes: 21 additions & 0 deletions src/virtualenv/activation/bash/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
from __future__ import absolute_import, unicode_literals

from virtualenv.info import IS_WIN
from virtualenv.util.path import Path

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(file_path, "rb") as open_file:
content = open_file.read()

content = content.replace(WINDOWS_LINE_ENDING, UNIX_LINE_ENDING)

with open(file_path, "wb") as open_file:
open_file.write(content)
24 changes: 22 additions & 2 deletions tests/unit/activation/test_bash.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from __future__ import absolute_import, unicode_literals

import sys
from unittest.mock import MagicMock

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


@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 +19,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):
class Mock_ViaTemplateActivator(ViaTemplateActivator):
def generate(self, creator):
file_path = 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(MagicMock()).generate(MagicMock())
assert b"\r\n" not in file_path.read_bytes()

0 comments on commit 2ceb225

Please sign in to comment.