File: test_detect_env.py

package info (click to toggle)
python-pipdeptree 2.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: python: 3,286; sh: 28; makefile: 5
file content (54 lines) | stat: -rw-r--r-- 1,878 bytes parent folder | download
1
2
3
4
5
6
7
8
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
50
51
52
53
54
from __future__ import annotations

from pathlib import Path
from subprocess import CompletedProcess  # noqa: S404
from typing import TYPE_CHECKING

import pytest

from pipdeptree._detect_env import detect_active_interpreter

if TYPE_CHECKING:
    from pytest_mock import MockFixture


@pytest.mark.parametrize(("env_var"), ["VIRTUAL_ENV", "CONDA_PREFIX"])
def test_detect_active_interpreter_using_env_vars(tmp_path: Path, mocker: MockFixture, env_var: str) -> None:
    mocker.patch("pipdeptree._detect_env.os.environ", {env_var: str(tmp_path)})
    mocker.patch("pipdeptree._detect_env.Path.exists", return_value=True)

    actual_path = detect_active_interpreter()

    assert actual_path.startswith(str(tmp_path))


def test_detect_active_interpreter_poetry(tmp_path: Path, mocker: MockFixture) -> None:
    faked_result = CompletedProcess("", 0, stdout=str(tmp_path))
    mocker.patch("pipdeptree._detect_env.subprocess.run", return_value=faked_result)
    mocker.patch("pipdeptree._detect_env.os.environ", {})

    actual_path = detect_active_interpreter()

    assert str(tmp_path) == actual_path


def test_detect_active_interpreter_non_supported_python_implementation(
    tmp_path: Path,
    mocker: MockFixture,
) -> None:
    mocker.patch("pipdeptree._detect_env.os.environ", {"VIRTUAL_ENV": str(tmp_path)})
    mocker.patch("pipdeptree._detect_env.Path.exists", return_value=True)
    mocker.patch("pipdeptree._detect_env.platform.python_implementation", return_value="NotSupportedPythonImpl")

    with pytest.raises(SystemExit):
        detect_active_interpreter()


def test_detect_active_interpreter_non_existent_path(
    mocker: MockFixture,
) -> None:
    fake_path = str(Path(*("i", "dont", "exist")))
    mocker.patch("pipdeptree._detect_env.os.environ", {"VIRTUAL_ENV": fake_path})

    with pytest.raises(SystemExit):
        detect_active_interpreter()