summaryrefslogtreecommitdiff
path: root/tests/unit/scripts/test_run_vulture.py
blob: 630a543abaef15951c8bcd62e4daa6466f0e30cb (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
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
#!/usr/bin/env python3
# Copyright 2015-2021 Florian Bruhin (The Compiler) <mail@qutebrowser.org>

# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser.  If not, see <https://www.gnu.org/licenses/>.

import sys
import textwrap

import pytest

from tests.helpers import testutils

try:
    from scripts.dev import run_vulture
except ImportError:
    if hasattr(sys, 'frozen'):
        # Tests aren't going to run anyways because of the mark
        pass
    else:
        raise


pytestmark = [pytest.mark.not_frozen]


class VultureDir:

    """Fixture similar to pytest's testdir fixture for vulture.

    Attributes:
        _tmp_path: The pytest tmp_path fixture.
    """

    def __init__(self, tmp_path):
        self._tmp_path = tmp_path

    def run(self):
        """Run vulture over all generated files and return the output."""
        names = [p.name for p in self._tmp_path.glob('*')]
        assert names
        with testutils.change_cwd(self._tmp_path):
            return run_vulture.run(names)

    def makepyfile(self, **kwargs):
        """Create a python file, similar to TestDir.makepyfile."""
        for filename, data in kwargs.items():
            text = textwrap.dedent(data)
            (self._tmp_path / (filename + '.py')).write_text(text, 'utf-8')


@pytest.fixture
def vultdir(tmp_path):
    return VultureDir(tmp_path)


def test_used(vultdir):
    vultdir.makepyfile(foo="""
        def foo():
            pass

        foo()
    """)
    assert not vultdir.run()


def test_unused_func(vultdir):
    vultdir.makepyfile(foo="""
        def foo():
            pass
    """)
    msg = "*foo.py:2: unused function 'foo' (60% confidence)"
    msgs = vultdir.run()
    assert len(msgs) == 1
    assert testutils.pattern_match(pattern=msg, value=msgs[0])


def test_unused_method_camelcase(vultdir):
    """Should be ignored because those are Qt methods."""
    vultdir.makepyfile(foo="""
        class Foo():

            def fooBar(self):
                pass

        Foo()
    """)
    assert not vultdir.run()