summaryrefslogtreecommitdiffstats
path: root/tests/test_style.py
blob: 9ce83bd9cd4b9c42b52deba89b8370894d07144b (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import pytest
from debian.deb822 import Deb822

from debputy.lsp.style_prefs import StylePreferenceTable, determine_effective_style
from debputy.packages import SourcePackage


def test_load_styles() -> None:
    styles = StylePreferenceTable.load_styles()
    assert "niels@thykier.net" in styles.maintainer_preferences
    nt_style = styles.maintainer_preferences["niels@thykier.net"]
    # Note this is data dependent; if it fails because the style changes, update the test
    assert nt_style.canonical_name == "Niels Thykier"
    assert not nt_style.is_packaging_team
    assert nt_style.formatting_deb822_normalize_field_content
    assert nt_style.formatting_deb822_short_indent
    assert nt_style.formatting_deb822_always_wrap
    assert nt_style.formatting_deb822_trailing_separator
    assert nt_style.formatting_deb822_max_line_length == 79
    assert nt_style.formatting_deb822_normalize_stanza_order

    # TODO: Not implemented yet
    assert not nt_style.formatting_deb822_normalize_field_order


def test_load_named_styles() -> None:
    styles = StylePreferenceTable.load_styles()
    assert "black" in styles.named_styles
    black_style = styles.named_styles["black"]
    # Note this is data dependent; if it fails because the style changes, update the test
    assert black_style.formatting_deb822_normalize_field_content
    assert black_style.formatting_deb822_short_indent
    assert black_style.formatting_deb822_always_wrap
    assert black_style.formatting_deb822_trailing_separator
    assert black_style.formatting_deb822_max_line_length == 79
    assert black_style.formatting_deb822_normalize_stanza_order

    # TODO: Not implemented yet
    assert not black_style.formatting_deb822_normalize_field_order


def test_compat_styles() -> None:
    styles = StylePreferenceTable.load_styles()

    # Data dependent; if it breaks, provide a stubbed style preference table
    assert "niels@thykier.net" in styles.maintainer_preferences
    assert "zeha@debian.org" in styles.maintainer_preferences
    assert "random-package@packages.debian.org" not in styles.maintainer_preferences
    assert "random@example.org" not in styles.maintainer_preferences

    nt_pref = styles.maintainer_preferences["niels@thykier.net"].as_effective_pref()
    zeha_pref = styles.maintainer_preferences["zeha@debian.org"].as_effective_pref()

    # Data dependency
    assert nt_pref == zeha_pref

    fields = Deb822(
        {
            "Package": "foo",
            "Maintainer": "Foo <random-package@packages.debian.org>",
            "Uploaders": "Niels Thykier <niels@thykier.net>",
        },
    )
    src = SourcePackage(fields)

    effective_style = determine_effective_style(styles, src)
    assert effective_style == nt_pref

    fields["Uploaders"] = (
        "Niels Thykier <niels@thykier.net>, Chris Hofstaedtler <zeha@debian.org>"
    )
    src = SourcePackage(fields)

    effective_style = determine_effective_style(styles, src)
    assert effective_style == nt_pref
    assert effective_style == zeha_pref

    fields["Uploaders"] = (
        "Niels Thykier <niels@thykier.net>, Chris Hofstaedtler <zeha@debian.org>, Random Developer <random@example.org>"
    )
    src = SourcePackage(fields)

    effective_style = determine_effective_style(styles, src)
    assert effective_style is None


@pytest.mark.xfail
def test_compat_styles_team_maint() -> None:
    styles = StylePreferenceTable.load_styles()
    fields = Deb822(
        {
            "Package": "foo",
            # Missing a stubbed definition for `team@lists.debian.org`
            "Maintainer": "Packaging Team <team@lists.debian.org>",
            "Uploaders": "Random Developer <random@example.org>",
        },
    )
    src = SourcePackage(fields)
    assert "team@lists.debian.org" in styles.maintainer_preferences
    assert "random@example.org" not in styles.maintainer_preferences
    team_style = styles.maintainer_preferences["team@lists.debian.org"]
    assert team_style.is_packaging_team
    effective_style = determine_effective_style(styles, src)
    assert effective_style == team_style.as_effective_pref()


def test_x_style() -> None:
    styles = StylePreferenceTable.load_styles()
    fields = Deb822(
        {
            "Package": "foo",
            "X-Style": "black",
            "Maintainer": "Random Developer <random@example.org>",
        },
    )
    src = SourcePackage(fields)
    assert "random@example.org" not in styles.maintainer_preferences
    assert "black" in styles.named_styles
    black_style = styles.named_styles["black"]
    effective_style = determine_effective_style(styles, src)
    assert effective_style == black_style