1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/tests/unit/test_compat.py
Tzu-ping Chung 42359a9605 Migrate tests to use pathlib.Path
The pip-specific Path implementation has been removed, and all its
usages replaced by pathlib.Path. The tmpdir and tmpdir_factory fixtures
are also removed, and all usages are replaced by tmp_path and
tmp_path_factory, which use pathlib.Path.

The pip() function now also accepts pathlib.Path so we don't need to put
str() everywhere. Path arguments are coerced with os.fspath() into str.
2022-06-08 19:58:46 +08:00

46 lines
1.3 KiB
Python

import os
from pathlib import Path
import pytest
from pip._internal.utils.compat import get_path_uid
def test_get_path_uid() -> None:
path = os.getcwd()
assert get_path_uid(path) == os.stat(path).st_uid
@pytest.mark.skipif("not hasattr(os, 'O_NOFOLLOW')")
def test_get_path_uid_without_NOFOLLOW(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delattr("os.O_NOFOLLOW")
path = os.getcwd()
assert get_path_uid(path) == os.stat(path).st_uid
# Skip unconditionally on Windows, as symlinks need admin privs there
@pytest.mark.skipif("sys.platform == 'win32'")
@pytest.mark.skipif("not hasattr(os, 'symlink')")
def test_get_path_uid_symlink(tmpdir: Path) -> None:
f = tmpdir / "symlink" / "somefile"
f.parent.mkdir()
f.write_text("content")
fs = f"{f}_link"
os.symlink(f, fs)
with pytest.raises(OSError):
get_path_uid(fs)
@pytest.mark.skipif("not hasattr(os, 'O_NOFOLLOW')")
@pytest.mark.skipif("not hasattr(os, 'symlink')")
def test_get_path_uid_symlink_without_NOFOLLOW(
tmpdir: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.delattr("os.O_NOFOLLOW")
f = tmpdir / "symlink" / "somefile"
f.parent.mkdir()
f.write_text("content")
fs = f"{f}_link"
os.symlink(f, fs)
with pytest.raises(OSError):
get_path_uid(fs)