summaryrefslogtreecommitdiff
path: root/tests/functional/test_warning.py
blob: c47443ca90383712b94a67fd69df4d941e89bc07 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import sys
import textwrap
from pathlib import Path

import pytest

from tests.lib import PipTestEnvironment


@pytest.fixture
def warnings_demo(tmpdir: Path) -> Path:
    demo = tmpdir.joinpath("warnings_demo.py")
    demo.write_text(
        textwrap.dedent(
            """
        from logging import basicConfig
        from pip._internal.utils import deprecation

        deprecation.install_warning_logger()
        basicConfig()

        deprecation.deprecated(reason="deprecated!", replacement=None, gone_in=None)
    """
        )
    )
    return demo


def test_deprecation_warnings_are_correct(
    script: PipTestEnvironment, warnings_demo: Path
) -> None:
    result = script.run("python", os.fspath(warnings_demo), expect_stderr=True)
    expected = "WARNING:pip._internal.deprecations:DEPRECATION: deprecated!\n"
    assert result.stderr == expected


def test_deprecation_warnings_can_be_silenced(
    script: PipTestEnvironment, warnings_demo: Path
) -> None:
    script.environ["PYTHONWARNINGS"] = "ignore"
    result = script.run("python", os.fspath(warnings_demo))
    assert result.stderr == ""


DEPRECATION_TEXT = "drop support for Python 2.7"
CPYTHON_DEPRECATION_TEXT = "January 1st, 2020"


def test_version_warning_is_not_shown_if_python_version_is_not_2(
    script: PipTestEnvironment,
) -> None:
    result = script.pip("debug", allow_stderr_warning=True)
    assert DEPRECATION_TEXT not in result.stderr, str(result)
    assert CPYTHON_DEPRECATION_TEXT not in result.stderr, str(result)


def test_flag_does_nothing_if_python_version_is_not_2(
    script: PipTestEnvironment,
) -> None:
    script.pip("list", "--no-python-version-warning")


@pytest.mark.skipif(
    sys.version_info >= (3, 10), reason="distutils is deprecated in 3.10+"
)
def test_pip_works_with_warnings_as_errors(script: PipTestEnvironment) -> None:
    script.environ["PYTHONWARNINGS"] = "error"
    script.pip("--version")