2019-07-02 09:49:06 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-05-27 23:02:08 +02:00
|
|
|
import locale
|
2013-11-25 00:26:22 +01:00
|
|
|
import os
|
2017-06-13 14:17:00 +02:00
|
|
|
|
2013-11-25 00:26:22 +01:00
|
|
|
import pytest
|
2017-06-13 14:17:00 +02:00
|
|
|
|
2018-07-29 13:11:37 +02:00
|
|
|
import pip._internal.utils.compat as pip_compat
|
|
|
|
from pip._internal.utils.compat import (
|
2019-07-22 06:45:27 +02:00
|
|
|
console_to_str,
|
|
|
|
expanduser,
|
|
|
|
get_path_uid,
|
|
|
|
str_to_display,
|
2017-08-31 17:48:18 +02:00
|
|
|
)
|
2013-11-25 00:26:22 +01:00
|
|
|
|
2014-01-28 15:17:51 +01:00
|
|
|
|
2013-11-25 00:26:22 +01:00
|
|
|
def test_get_path_uid():
|
|
|
|
path = os.getcwd()
|
|
|
|
assert get_path_uid(path) == os.stat(path).st_uid
|
|
|
|
|
2014-01-28 15:17:51 +01:00
|
|
|
|
2013-11-25 00:26:22 +01:00
|
|
|
@pytest.mark.skipif("not hasattr(os, 'O_NOFOLLOW')")
|
|
|
|
def test_get_path_uid_without_NOFOLLOW(monkeypatch):
|
|
|
|
monkeypatch.delattr("os.O_NOFOLLOW")
|
|
|
|
path = os.getcwd()
|
|
|
|
assert get_path_uid(path) == os.stat(path).st_uid
|
|
|
|
|
2014-01-28 15:17:51 +01:00
|
|
|
|
2016-09-01 15:10:07 +02:00
|
|
|
# Skip unconditionally on Windows, as symlinks need admin privs there
|
|
|
|
@pytest.mark.skipif("sys.platform == 'win32'")
|
2013-11-25 00:50:11 +01:00
|
|
|
@pytest.mark.skipif("not hasattr(os, 'symlink')")
|
2013-11-25 00:26:22 +01:00
|
|
|
def test_get_path_uid_symlink(tmpdir):
|
2019-08-17 03:34:17 +02:00
|
|
|
f = tmpdir / "symlink" / "somefile"
|
|
|
|
f.parent.mkdir()
|
2019-07-02 07:00:32 +02:00
|
|
|
f.write_text("content")
|
2013-11-25 00:26:22 +01:00
|
|
|
fs = f + '_link'
|
|
|
|
os.symlink(f, fs)
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
get_path_uid(fs)
|
|
|
|
|
2014-01-28 15:17:51 +01:00
|
|
|
|
2013-11-25 00:26:22 +01:00
|
|
|
@pytest.mark.skipif("not hasattr(os, 'O_NOFOLLOW')")
|
2013-11-25 00:50:11 +01:00
|
|
|
@pytest.mark.skipif("not hasattr(os, 'symlink')")
|
2013-11-25 00:26:22 +01:00
|
|
|
def test_get_path_uid_symlink_without_NOFOLLOW(tmpdir, monkeypatch):
|
|
|
|
monkeypatch.delattr("os.O_NOFOLLOW")
|
2019-08-17 03:34:17 +02:00
|
|
|
f = tmpdir / "symlink" / "somefile"
|
|
|
|
f.parent.mkdir()
|
2019-07-02 07:00:32 +02:00
|
|
|
f.write_text("content")
|
2013-11-25 00:26:22 +01:00
|
|
|
fs = f + '_link'
|
|
|
|
os.symlink(f, fs)
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
get_path_uid(fs)
|
2014-08-18 06:43:46 +02:00
|
|
|
|
|
|
|
|
2019-07-02 09:49:06 +02:00
|
|
|
@pytest.mark.parametrize('data, expected', [
|
|
|
|
('abc', u'abc'),
|
|
|
|
# Test text (unicode in Python 2) input.
|
|
|
|
(u'abc', u'abc'),
|
|
|
|
# Test text input with non-ascii characters.
|
|
|
|
(u'déf', u'déf'),
|
|
|
|
])
|
|
|
|
def test_str_to_display(data, expected):
|
|
|
|
actual = str_to_display(data)
|
|
|
|
assert actual == expected, (
|
|
|
|
# Show the encoding for easier troubleshooting.
|
|
|
|
'encoding: {!r}'.format(locale.getpreferredencoding())
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('data, encoding, expected', [
|
|
|
|
# Test str input with non-ascii characters.
|
|
|
|
('déf', 'utf-8', u'déf'),
|
|
|
|
# Test bytes input with non-ascii characters:
|
|
|
|
(u'déf'.encode('utf-8'), 'utf-8', u'déf'),
|
|
|
|
# Test a Windows encoding.
|
|
|
|
(u'déf'.encode('cp1252'), 'cp1252', u'déf'),
|
|
|
|
# Test a Windows encoding with incompatibly encoded text.
|
|
|
|
(u'déf'.encode('utf-8'), 'cp1252', u'déf'),
|
|
|
|
])
|
|
|
|
def test_str_to_display__encoding(monkeypatch, data, encoding, expected):
|
|
|
|
monkeypatch.setattr(locale, 'getpreferredencoding', lambda: encoding)
|
|
|
|
actual = str_to_display(data)
|
|
|
|
assert actual == expected, (
|
|
|
|
# Show the encoding for easier troubleshooting.
|
|
|
|
'encoding: {!r}'.format(locale.getpreferredencoding())
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_str_to_display__decode_error(monkeypatch, caplog):
|
|
|
|
monkeypatch.setattr(locale, 'getpreferredencoding', lambda: 'utf-8')
|
|
|
|
# Encode with an incompatible encoding.
|
|
|
|
data = u'ab'.encode('utf-16')
|
|
|
|
actual = str_to_display(data)
|
|
|
|
|
|
|
|
assert actual == u'\\xff\\xfea\x00b\x00', (
|
|
|
|
# Show the encoding for easier troubleshooting.
|
|
|
|
'encoding: {!r}'.format(locale.getpreferredencoding())
|
|
|
|
)
|
|
|
|
assert len(caplog.records) == 1
|
|
|
|
record = caplog.records[0]
|
|
|
|
assert record.levelname == 'WARNING'
|
|
|
|
assert record.message == (
|
|
|
|
'Bytes object does not appear to be encoded as utf-8'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-05-27 23:02:08 +02:00
|
|
|
def test_console_to_str(monkeypatch):
|
|
|
|
some_bytes = b"a\xE9\xC3\xE9b"
|
|
|
|
encodings = ('ascii', 'utf-8', 'iso-8859-1', 'iso-8859-5',
|
|
|
|
'koi8_r', 'cp850')
|
|
|
|
for e in encodings:
|
|
|
|
monkeypatch.setattr(locale, 'getpreferredencoding', lambda: e)
|
|
|
|
result = console_to_str(some_bytes)
|
|
|
|
assert result.startswith("a")
|
|
|
|
assert result.endswith("b")
|
|
|
|
|
|
|
|
|
|
|
|
def test_console_to_str_warning(monkeypatch):
|
|
|
|
some_bytes = b"a\xE9b"
|
|
|
|
|
2017-12-25 10:53:27 +01:00
|
|
|
def check_warning(msg, *args, **kwargs):
|
2017-05-27 23:02:08 +02:00
|
|
|
assert msg.startswith(
|
|
|
|
"Subprocess output does not appear to be encoded as")
|
|
|
|
|
|
|
|
monkeypatch.setattr(locale, 'getpreferredencoding', lambda: 'utf-8')
|
2018-07-29 13:11:37 +02:00
|
|
|
monkeypatch.setattr(pip_compat.logger, 'warning', check_warning)
|
2017-05-27 23:02:08 +02:00
|
|
|
console_to_str(some_bytes)
|
|
|
|
|
|
|
|
|
2015-09-29 21:31:27 +02:00
|
|
|
@pytest.mark.parametrize("home,path,expanded", [
|
|
|
|
("/Users/test", "~", "/Users/test"),
|
|
|
|
("/Users/test", "~/.cache", "/Users/test/.cache"),
|
2018-06-10 17:35:00 +02:00
|
|
|
# Verify that we are not affected by https://bugs.python.org/issue14768
|
2015-09-29 21:31:27 +02:00
|
|
|
("/", "~", "/"),
|
|
|
|
("/", "~/.cache", "/.cache"),
|
|
|
|
])
|
|
|
|
def test_expanduser(home, path, expanded, monkeypatch):
|
|
|
|
monkeypatch.setenv("HOME", home)
|
2020-01-13 01:44:43 +01:00
|
|
|
monkeypatch.setenv("USERPROFILE", home)
|
2015-09-29 21:31:27 +02:00
|
|
|
assert expanduser(path) == expanded
|