2013-11-25 00:26:22 +01:00
|
|
|
import os
|
2022-06-07 11:52:38 +02:00
|
|
|
from pathlib import Path
|
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
|
|
|
|
2021-01-02 19:30:08 +01:00
|
|
|
from pip._internal.utils.compat import get_path_uid
|
2013-11-25 00:26:22 +01:00
|
|
|
|
2014-01-28 15:17:51 +01:00
|
|
|
|
2021-08-30 00:43:28 +02:00
|
|
|
def test_get_path_uid() -> None:
|
2013-11-25 00:26:22 +01:00
|
|
|
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')")
|
2021-08-30 00:43:28 +02:00
|
|
|
def test_get_path_uid_without_NOFOLLOW(monkeypatch: pytest.MonkeyPatch) -> None:
|
2013-11-25 00:26:22 +01:00
|
|
|
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')")
|
2021-08-30 00:43:28 +02:00
|
|
|
def test_get_path_uid_symlink(tmpdir: Path) -> None:
|
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")
|
2022-06-07 11:52:38 +02:00
|
|
|
fs = f"{f}_link"
|
2013-11-25 00:26:22 +01:00
|
|
|
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')")
|
2021-08-30 00:43:28 +02:00
|
|
|
def test_get_path_uid_symlink_without_NOFOLLOW(
|
|
|
|
tmpdir: Path, monkeypatch: pytest.MonkeyPatch
|
|
|
|
) -> None:
|
2013-11-25 00:26:22 +01:00
|
|
|
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")
|
2022-06-07 11:52:38 +02:00
|
|
|
fs = f"{f}_link"
|
2013-11-25 00:26:22 +01:00
|
|
|
os.symlink(f, fs)
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
get_path_uid(fs)
|