File: test_source.py

package info (click to toggle)
quodlibet 4.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,228 kB
  • sloc: python: 89,728; sh: 381; xml: 110; makefile: 91
file content (133 lines) | stat: -rw-r--r-- 4,641 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright 2014, 2015 Christoph Reiter
#              2020-21 Nick Boultbee
#
# This program 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 2 of the License, or
# (at your option) any later version.

import os
import re
from pathlib import Path
from re import Pattern
from collections.abc import Iterable

import pytest
from gi.repository import Gtk
from pytest import fixture

from quodlibet.util import get_module_dir
from tests.test_po import QL_BASE_PATH


def iter_py_paths() -> Iterable[Path]:
    """Iterates over all Python source files that are part of Quod Libet"""

    import quodlibet

    root = Path(get_module_dir(quodlibet)).parent

    skip = [
        root / d
        for d in ("build", "dist", "docs", "dev-utils", Path("quodlibet") / "packages")
    ]
    # Path.glob() not efficient on big trees :(
    for dirpath, dirnames, filenames in os.walk(root):
        root = Path(dirpath)
        parents = root.parents
        if root.name.startswith(".") or any(s in parents for s in skip):
            # Don't test *any* subdirs of hidden / ignored parents
            dirnames.clear()
            continue
        for filename in filenames:
            if filename.endswith(".py"):
                yield root / filename


def prettify_path(p: Path) -> str:
    return os.path.splitext(p.relative_to(QL_BASE_PATH))[0]


@pytest.fixture(params=list(iter_py_paths()), ids=prettify_path)
def py_path(request) -> Path:
    return request.param


@pytest.mark.quality
class TestLicense:
    ALLOWED_RAW = [
        """
This program 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 2 of the License, or
(at your option) any later version.
""",
        """
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
""",
    ]
    ALLOWED = ["".join(license.split()) for license in ALLOWED_RAW]

    def test_license_is_compliant(self, py_path: Path):
        header = b""
        with open(py_path, "rb") as h:
            for line in h:
                line = line.strip()
                if not line.startswith(b"#"):
                    break
                header += line.lstrip(b"# ") + b"\n"

        norm = b"".join(header.split())
        norm = norm.decode("utf-8")
        assert any(l in norm for l in self.ALLOWED)


# Don't mark this as quality - useful to execute _everywhere_
class TestStockIcons:
    @fixture
    def res(self) -> Iterable[Pattern]:
        return [
            re.compile(r) for r in ("(Gtk\\.STOCK_[_A-Z]*)", "[\"'](gtk-[\\-a-z]*)")
        ]

    @fixture
    def white(self) -> list[str]:
        # gtk setting keys start like stock icons, so white list them
        white = [
            x.replace("_", "-")
            for x in dir(Gtk.Settings.get_default().props)
            if x.startswith("gtk_")
        ]
        # older gtk doesn't have those, but we still have them in the source
        white += ["gtk-dialogs-use-header", "gtk-primary-button-warps-slider"]
        # some more..
        white += ["gtk-tooltip", "gtk-", "gtk-update-icon-cache-"]
        return white

    def test_icons_used(self, py_path: Path, res, white):
        if py_path.name in ("icons.py", "test_source.py"):
            return
        with open(py_path, "rb") as h:
            data = h.read().decode("utf-8")
            for r in res:
                match = r.search(data)
                if match:
                    group = match.group(1)
                    assert group in white