File: test_pylama.py

package info (click to toggle)
python-noseofyeti 2.4.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 360 kB
  • sloc: python: 2,581; sh: 31; makefile: 12
file content (85 lines) | stat: -rw-r--r-- 2,797 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
# coding: spec

import shutil
import subprocess
import sys
from pathlib import Path
from textwrap import dedent

import pytest

try:
    __import__("pylama")
except ImportError:
    pytestmark = pytest.mark.skip("pylama is not current installed")

here = Path(__file__).parent
test_dir = here / "for_formatting_and_pylama"


class TestPylama:
    @pytest.fixture(params=(False, True), ids=["Without black compat", "With black compat"])
    def directory(self, request, tmp_path_factory, monkeypatch):
        directory = tmp_path_factory.mktemp("files")

        monkeypatch.setenv("PYTHONPATH", str(directory))
        monkeypatch.setenv("NOSE_OF_YETI_BLACK_COMPAT", str(request.param).lower())

        with open(directory / "pylama.ini", "w") as fle:
            fle.write(
                dedent(
                    """
            [pylama]
            linters = pylama_noy,mccabe,pycodestyle,pyflakes
            ignore = E701,E704,E225,E202,E211,E231,E226,W292,W291,E251,E122,E501,E701,E227,E305,E128,E228,E203,E261
            """
                )
            )

        if request.param:
            path = Path(__import__("noseOfYeti").__file__).parent / "black" / "noy_black.pth"
            shutil.copy(path, directory)

            with open(directory / "sitecustomize.py ", "w") as fle:
                fle.write(
                    dedent(
                        """
            import site
            import os
            site.addsitedir(os.getcwd())
            """
                    )
                )

        yield directory

    def test_it_lints_when_there_is_black_compat(self, directory):
        shutil.copy(test_dir / "formatted_spec.py", directory / "formatted_spec.py")
        shutil.copy(test_dir / "formatted_normal.py", directory / "formatted_normal.py")

        subprocess.check_call([sys.executable, "-m", "pylama", str(directory)], cwd=directory)

    def test_it_finds_problems(self, directory):
        shutil.copy(test_dir / "failing_pylama_spec.py", directory / "formatted_spec.py")

        p = subprocess.run(
            [sys.executable, "-m", "pylama", str(directory)],
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            cwd=directory,
            check=False,
        )

        assert p.returncode == 1

        assert (
            p.stdout.decode().strip().replace("\r\n", "\n")
            == dedent(
                """
            formatted_spec.py:3:1 W0611 'sys' imported but unused [pyflakes]
            formatted_spec.py:5:1 E302 expected 2 blank lines, found 1 [pycodestyle]
            formatted_spec.py:7:9 W0612 local variable 'a' is assigned to but never used [pyflakes]
            formatted_spec.py:8:9 E0602 undefined name 'stuff' [pyflakes]
            """
            ).strip()
        )