test_utils.py 1.7 KB
Newer Older
1 2 3
import pathlib
import sys

F
Frost Ming 已提交
4 5 6
import pytest

from pdm import utils
7 8
from pdm.cli import utils as cli_utils
from pdm.exceptions import PdmException
F
Frost Ming 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49


@pytest.mark.parametrize(
    "given,expected",
    [
        ("test", "test"),
        ("", ""),
        ("${FOO}", "hello"),
        ("$FOO", "$FOO"),
        ("${BAR}", "${BAR}"),
        ("%FOO%", "%FOO%"),
        ("${FOO}_${FOO}", "hello_hello"),
    ],
)
def test_expand_env_vars(given, expected, monkeypatch):
    monkeypatch.setenv("FOO", "hello")
    assert utils.expand_env_vars(given) == expected


@pytest.mark.parametrize(
    "given,expected",
    [
        ("https://example.org/path?arg=1", "https://example.org/path?arg=1"),
        (
            "https://${FOO}@example.org/path?arg=1",
            "https://hello@example.org/path?arg=1",
        ),
        (
            "https://${FOO}:${BAR}@example.org/path?arg=1",
            "https://hello:wo%3Arld@example.org/path?arg=1",
        ),
        (
            "https://${FOOBAR}@example.org/path?arg=1",
            "https://%24%7BFOOBAR%7D@example.org/path?arg=1",
        ),
    ],
)
def test_expend_env_vars_in_auth(given, expected, monkeypatch):
    monkeypatch.setenv("FOO", "hello")
    monkeypatch.setenv("BAR", "wo:rld")
    assert utils.expand_env_vars_in_auth(given) == expected
50 51 52 53 54 55 56 57


def test_find_python_in_path(tmp_path):

    assert (
        cli_utils.find_python_in_path(sys.executable)
        == pathlib.Path(sys.executable).as_posix()
    )
F
Frost Ming 已提交
58 59 60 61
    assert (
        cli_utils.find_python_in_path(sys.prefix)
        .lower()
        .startswith(pathlib.Path(sys.executable).as_posix().lower())
62 63 64
    )
    with pytest.raises(PdmException):
        cli_utils.find_python_in_path(tmp_path)