File: test_stdin.py

package info (click to toggle)
flake8-polyfill 1.0.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 176 kB
  • sloc: python: 275; makefile: 3
file content (86 lines) | stat: -rw-r--r-- 2,618 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Tests for polyfill's stdin monkey patching."""
import flake8
import pep8
import pycodestyle
import pytest

from flake8_polyfill import stdin
from flake8_polyfill import version

if (4, 0, 0) <= flake8.__version_info__:
    pytestmark = pytest.mark.skip


def test_modules_dict():
    """Verify that it works the way we expect."""
    assert len(stdin.modules['pep8']) == 1
    assert stdin.modules['pep8'] == [pep8]
    assert len(stdin.modules['pycodestyle']) == 1
    assert stdin.modules['pycodestyle'] == [pycodestyle]
    assert len(stdin.modules['all']) == 2
    assert stdin.modules['all'] == [pep8, pycodestyle]


@pytest.fixture
def pep8_mod(request):
    """Fixture to replace pep8's default stdin_get_value."""
    original_stdin_get_value = pep8.stdin_get_value

    def replace():
        pep8.stdin_get_value = original_stdin_get_value

    request.addfinalizer(replace)
    return pep8


@pytest.fixture
def pycodestyle_mod(request):
    """Fixture to replace pycodestyle's default stdin_get_value."""
    original_stdin_get_value = pycodestyle.stdin_get_value

    def replace():
        pycodestyle.stdin_get_value = original_stdin_get_value

    request.addfinalizer(replace)
    return pycodestyle


# NOTE(sigmavirus24): These are weak tests but *shrug* I'm not sure of a
# better way to test these.
def test_monkey_patch_all(pep8_mod, pycodestyle_mod):
    """Verify we monkey patch everything."""
    stdin.monkey_patch('all')
    assert pep8_mod.stdin_get_value is pycodestyle_mod.stdin_get_value


@pytest.mark.skipif(
    (2, 5) < version.version_info < (2, 7),
    reason='They are the same on everything less than 2.6.'
)
def test_monkey_patch_pep8(pep8_mod):
    """Verify we monkey patch pep8 only."""
    stdin.monkey_patch('pep8')
    assert pep8_mod.stdin_get_value is not pycodestyle.stdin_get_value


@pytest.mark.skipif(
    version.version_info < (2, 6),
    reason='They are the same on everything less than 2.6.'
)
def test_monkey_patch_pycodestyle(pycodestyle_mod):
    """Verify we monkey patch pycodestyle only."""
    stdin.monkey_patch('pycodestyle')
    assert pep8.stdin_get_value is not pycodestyle_mod.stdin_get_value


@pytest.mark.skipif(
    version.version_info < (3, 0) or version.version_info > (4, 0),
    reason='Requires Flake8 3.x'
)
def test_uses_flake8_util_stdin(pep8_mod, pycodestyle_mod):
    """Verify we monkey-patch using internal flake8 functions."""
    import flake8.utils

    stdin.monkey_patch('all')
    assert pep8_mod.stdin_get_value is flake8.utils.stdin_get_value
    assert pycodestyle_mod.stdin_get_value is flake8.utils.stdin_get_value