1import platform
2import textwrap
3
4import pytest
5
6from tests.lib import skip_if_not_python2, skip_if_python2
7
8
9@pytest.fixture
10def warnings_demo(tmpdir):
11    demo = tmpdir.joinpath('warnings_demo.py')
12    demo.write_text(textwrap.dedent('''
13        from logging import basicConfig
14        from pip._internal.utils import deprecation
15
16        deprecation.install_warning_logger()
17        basicConfig()
18
19        deprecation.deprecated("deprecated!", replacement=None, gone_in=None)
20    '''))
21    return demo
22
23
24def test_deprecation_warnings_are_correct(script, warnings_demo):
25    result = script.run('python', warnings_demo, expect_stderr=True)
26    expected = 'WARNING:pip._internal.deprecations:DEPRECATION: deprecated!\n'
27    assert result.stderr == expected
28
29
30def test_deprecation_warnings_can_be_silenced(script, warnings_demo):
31    script.environ['PYTHONWARNINGS'] = 'ignore'
32    result = script.run('python', warnings_demo)
33    assert result.stderr == ''
34
35
36DEPRECATION_TEXT = "drop support for Python 2.7"
37CPYTHON_DEPRECATION_TEXT = "January 1st, 2020"
38
39
40@skip_if_python2
41def test_version_warning_is_not_shown_if_python_version_is_not_2(script):
42    result = script.pip("debug", allow_stderr_warning=True)
43    assert DEPRECATION_TEXT not in result.stderr, str(result)
44    assert CPYTHON_DEPRECATION_TEXT not in result.stderr, str(result)
45
46
47@skip_if_python2
48def test_flag_does_nothing_if_python_version_is_not_2(script):
49    script.pip("list", "--no-python-version-warning")
50
51
52@skip_if_not_python2
53def test_version_warning_is_shown_if_python_version_is_2(script):
54    result = script.pip("debug", allow_stderr_warning=True)
55    assert DEPRECATION_TEXT in result.stderr, str(result)
56    if platform.python_implementation() == 'CPython':
57        assert CPYTHON_DEPRECATION_TEXT in result.stderr, str(result)
58    else:
59        assert CPYTHON_DEPRECATION_TEXT not in result.stderr, str(result)
60
61
62@skip_if_not_python2
63def test_version_warning_is_not_shown_when_flag_is_passed(script):
64    result = script.pip(
65        "debug", "--no-python-version-warning", allow_stderr_warning=True
66    )
67    assert DEPRECATION_TEXT not in result.stderr, str(result)
68    assert CPYTHON_DEPRECATION_TEXT not in result.stderr, str(result)
69    assert "--no-python-version-warning" not in result.stderr
70