Skip to content

Commit

Permalink
universal newline
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbernat committed Dec 21, 2018
1 parent 823a224 commit 7d6faa2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -125,7 +125,8 @@ def get_version():
"pytest >= 4.0.0, <5",
"coverage >= 4.5.0, <5",
'pytest-timeout >= 1.3.0, <2; platform_python_implementation!="Jython"',
'xonsh;python_version>="3.4"',
'xonsh; python_version>="3.4"',
"six >= 1.10.0, < 2",
],
"docs": ["sphinx >= 1.8.0, < 2"],
},
Expand Down
44 changes: 28 additions & 16 deletions tests/test_activation.py
Expand Up @@ -4,9 +4,10 @@
import pipes
import subprocess
import sys
from os.path import join, normcase
from os.path import join, normcase, dirname

import pytest
import six

import virtualenv

Expand Down Expand Up @@ -37,17 +38,18 @@ def wrapper(fn):
return wrapper


def long_path(short_path_name):
# python 2 may return Windows short paths, normalize
if virtualenv.is_win and sys.version_info < (3,):
def norm_long_path(short_path_name):
# python may return Windows short paths, normalize
if virtualenv.is_win:
from ctypes import create_unicode_buffer, windll

buffer_cont = create_unicode_buffer(256)
get_long_path_name = windll.kernel32.GetLongPathNameW
# noinspection PyUnresolvedReferences
get_long_path_name(unicode(short_path_name), buffer_cont, 256) # noqa: F821
return buffer_cont.value
return short_path_name
get_long_path_name(six.text_type(short_path_name), buffer_cont, 256) # noqa: F821
result = buffer_cont.value
else:
result = short_path_name
return normcase(result)


@pytest.fixture(scope="session")
Expand All @@ -65,7 +67,7 @@ def activation_env(tmp_path_factory):

class Activation(object):
cmd = ""
extension = ""
extension = "test"
invoke_script = []
command_separator = os.linesep
activate_cmd = "source"
Expand Down Expand Up @@ -95,7 +97,7 @@ def print_os_env_var(self, var):
return self.python_cmd("import os; print(os.environ.get({}, None))".format(val))

def __call__(self, monkeypatch):
absolute_activate_script = normcase(long_path(join(self.bin_dir, self.activate_script)))
absolute_activate_script = norm_long_path(join(self.bin_dir, self.activate_script))

site_packages = subprocess.check_output(
[
Expand All @@ -122,24 +124,30 @@ def __call__(self, monkeypatch):
"", # just finish with an empty new line
]
script = self.command_separator.join(commands)
test_script = self.path / ("test{}".format(".{}".format(self.extension) if self.extension else ""))
test_script = self.path / "script.{}".format(self.extension)
test_script.write_text(script)
assert test_script.exists()

monkeypatch.chdir(str(self.path))
invoke_shell = self.invoke_script + [normcase(long_path(str(test_script)))]
invoke_shell = self.invoke_script + [str(test_script)]

monkeypatch.delenv(str("VIRTUAL_ENV"), raising=False)
raw = subprocess.check_output(invoke_shell, universal_newlines=True, stderr=subprocess.STDOUT)
out = raw.strip().split(os.linesep)

# in case the tool is provided by the dev environment (e.g. xonosh)
env = os.environ.copy()
env[str("PATH")] = os.linesep.join([dirname(sys.executable)] + env.get(str("PATH"), str("")).split(os.linesep))

raw = subprocess.check_output(invoke_shell, universal_newlines=True, stderr=subprocess.STDOUT, env=env)
out = raw.strip().split("\n")

# pre-activation
assert out[0], raw
assert out[1] == "None", raw

# post-activation
exe = "{}.exe".format(virtualenv.expected_exe) if virtualenv.is_win else virtualenv.expected_exe
assert normcase(long_path(out[2])) == normcase(long_path(join(self.bin_dir, exe))), raw
assert out[3] == self.home_dir, raw
assert norm_long_path(out[2]) == norm_long_path(join(self.bin_dir, exe)), raw
assert norm_long_path(out[3]) == norm_long_path(str(self.home_dir)).replace("\\\\", "\\"), raw

assert out[4] == "wrote pydoc_test.html"
content = self.path / "pydoc_test.html"
Expand All @@ -153,6 +161,7 @@ def __call__(self, monkeypatch):
class BashActivation(Activation):
cmd = "bash.exe" if virtualenv.is_win else "bash"
invoke_script = [cmd]
extension = "sh"
activate_script = "activate"
check = [cmd, "--version"]

Expand All @@ -166,6 +175,7 @@ def test_bash(activation_env, monkeypatch, tmp_path):
class CshActivation(Activation):
cmd = "csh.exe" if virtualenv.is_win else "csh"
invoke_script = [cmd]
extension = "csh"
activate_script = "activate.csh"
check = [cmd, "--version"]

Expand All @@ -179,6 +189,7 @@ def test_csh(activation_env, monkeypatch, tmp_path):
class FishActivation(Activation):
cmd = "fish.exe" if virtualenv.is_win else "fish"
invoke_script = [cmd]
extension = "fish"
activate_script = "activate.fish"
check = [cmd, "--version"]

Expand Down Expand Up @@ -210,6 +221,7 @@ def test_powershell(activation_env, monkeypatch, tmp_path):

class XonoshActivation(Activation):
cmd = "xonsh.exe" if virtualenv.is_win else "xonsh"
extension = "xsh"
invoke_script = [cmd]
activate_script = "activate.xsh"
check = [cmd, "--version"]
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -117,7 +117,7 @@ force_grid_wrap = 0
line_length = 120
known_standard_library = ConfigParser
known_first_party = virtualenv
known_third_party = pytest
known_third_party = pytest,six

[flake8]
max-complexity = 22
Expand Down

0 comments on commit 7d6faa2

Please sign in to comment.