File: checks.py

package info (click to toggle)
python-invocations 4.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 592 kB
  • sloc: python: 2,262; makefile: 3
file content (107 lines) | stat: -rw-r--r-- 3,873 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from unittest.mock import call

import pytest

from invocations.checks import blacken, lint, all_ as all_task


class checks:
    class blacken_:
        @pytest.mark.parametrize(
            "kwargs,command",
            [
                (dict(), "find . -name '*.py' | xargs black -l 79"),
                (
                    dict(line_length=80),
                    "find . -name '*.py' | xargs black -l 80",
                ),
                (
                    dict(folders=["foo", "bar"]),
                    "find foo bar -name '*.py' | xargs black -l 79",
                ),
                (
                    # Explicit invocation that matches a default CLI
                    # invocation, since 'folders' is an iterable and thus shows
                    # up as an empty list in real life. Ehhh.
                    dict(folders=[]),
                    "find . -name '*.py' | xargs black -l 79",
                ),
                (
                    dict(check=True),
                    "find . -name '*.py' | xargs black -l 79 --check",
                ),
                (
                    dict(diff=True),
                    "find . -name '*.py' | xargs black -l 79 --diff",
                ),
                (
                    dict(
                        diff=True,
                        check=True,
                        line_length=80,
                        folders=["foo", "bar"],
                    ),
                    "find foo bar -name '*.py' | xargs black -l 80 --check --diff",  # noqa
                ),
                (
                    dict(find_opts="-and -not -name foo"),
                    "find . -name '*.py' -and -not -name foo | xargs black -l 79",  # noqa
                ),
            ],
            ids=[
                "base case is all files and 79 characters",
                "line length controllable",
                "folders controllable",
                "folders real default value",
                "check flag passed through",
                "diff flag passed through",
                "most args combined",
                "find opts controllable",
            ],
        )
        def runs_black(self, ctx, kwargs, command):
            blacken(ctx, **kwargs)
            ctx.run.assert_called_once_with(command, pty=True)

        def folders_configurable(self, ctx):
            # Just config -> works fine
            ctx.blacken = dict(folders=["elsewhere"])
            blacken(ctx)
            assert "elsewhere" in ctx.run_command

        def folders_config_loses_to_runtime(self, ctx):
            # Config + CLI opt -> CLI opt wins
            ctx.blacken = dict(folders=["nowhere"])
            blacken(ctx, folders=["nowhere"])
            assert "nowhere" in ctx.run_command

        def find_opts_configurable(self, ctx):
            ctx.blacken = dict(find_opts="-and -not -name foo.py")
            blacken(ctx)
            assert (
                "find . -name '*.py' -and -not -name foo.py" in ctx.run_command
            )

        def find_opts_config_loses_to_runtime(self, ctx):
            ctx.blacken = dict(find_opts="-and -not -name foo.py")
            blacken(ctx, find_opts="-or -name '*.js'")
            assert "find . -name '*.py' -or -name '*.js'" in ctx.run_command

        def aliased_to_format(self):
            assert blacken.aliases == ["format"]

    class lint_:
        def runs_flake8_by_default(self, ctx):
            lint(ctx)
            assert ctx.run_command == "flake8"

    class all_:
        def runs_blacken_and_lint(self, ctx):
            all_task(ctx)
            assert ctx.run.call_args_list == [
                call("find . -name '*.py' | xargs black -l 79", pty=True),
                call("flake8", pty=True, warn=True),
            ]

        def is_default_task(self):
            assert all_task.is_default