lint_tests.reformat.test_reformat_dctrl

tests/lint_tests/reformat/test_reformat_dctrl.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import textwrap
from typing import List, TYPE_CHECKING
from collections.abc import Sequence

import pytest

from debputy.lsp.languages.lsp_debian_control import _reformat_debian_control
from debputy.lsp.maint_prefs import MaintainerPreferenceTable
from debputy.lsp.text_edit import apply_text_edits
from debputy.packages import DctrlParser
from debputy.plugin.api.feature_set import PluginProvidedFeatureSet
from lint_tests.lint_tutil import (
    ReformatWrapper,
    apply_formatting_edits,
)

if TYPE_CHECKING:
    import lsprotocol.types as types
else:
    import debputy.lsprotocol.types as types


ALL_REQUIRED_FILES = [
    "debian/changelog",
    "debian/copyright",
]


@pytest.fixture
def reformater(
    debputy_plugin_feature_set: PluginProvidedFeatureSet,
    lint_dctrl_parser: DctrlParser,
    maintainer_preference_table: MaintainerPreferenceTable,
) -> ReformatWrapper:

    return ReformatWrapper(
        "/nowhere/debian/control",
        _reformat_debian_control,
        debputy_plugin_feature_set,
        lint_dctrl_parser,
        maintainer_preference_table,
    )


def test_dctrl_reformat(reformater: ReformatWrapper) -> None:
    lines = textwrap.dedent(
        """\
    Source: foo
    Some-Other-Field: bar
    Build-Depends: debhelper-compat (= 13)
    Homepage: https://example.com

    Package: foo
    Architecture: all
    Section: devel
    """
    ).splitlines(keepends=True)

    edits = reformater.reformat(lines)
    # By default, we do nothing
    assert not edits

    edits_black = reformater.reformat_with_named_style("black", lines)
    assert edits_black

    actual_reformatted_black = apply_formatting_edits(lines, edits_black)

    expected_reformatted_black = textwrap.dedent(
        """\
    Source: foo
    Some-Other-Field: bar
    Build-Depends:
     debhelper-compat (= 13),
    Homepage: https://example.com

    Package: foo
    Architecture: all
    Section: devel
    """
    )

    assert actual_reformatted_black == expected_reformatted_black


def test_dctrl_reformat_canonicalize_field_names(reformater: ReformatWrapper) -> None:
    lines = textwrap.dedent(
        """\
    source: foo
    # This field will not change case, since it is unknown
    some-other-field: bar
    BUILD-DEPENDS:
     debhelper-compat (= 13),
    # In memory of Steve (#1068220)
    HoMEpaGe: https://example.com

    package: foo
    architecture: all
    Section: devel
    """
    ).splitlines(keepends=True)

    edits = reformater.reformat(lines)
    # By default, we do nothing
    assert not edits

    edits_black = reformater.reformat_with_named_style("black", lines)
    assert edits_black

    actual_reformatted_black = apply_formatting_edits(lines, edits_black)

    expected_reformatted_black = textwrap.dedent(
        """\
    Source: foo
    # This field will not change case, since it is unknown
    some-other-field: bar
    Build-Depends:
     debhelper-compat (= 13),
    # In memory of Steve (#1068220)
    Homepage: https://example.com

    Package: foo
    Architecture: all
    Section: devel
    """
    )

    assert actual_reformatted_black == expected_reformatted_black


def test_dctrl_reformat_preserve_comments(reformater: ReformatWrapper) -> None:
    # Based on an example observed in  sbuild`. Thanks to zeha fore reporting.
    lines = textwrap.dedent(
        """\
    Source: foo
    # Test
    Build-Depends:
     foo,
     bar,

    Package: foo
    # Test
    architecture: i386 amd64
    Section: devel
    # Test
    Depends: foo
    # Test
    recommends:
     bar,
    """
    ).splitlines(keepends=True)

    edits = reformater.reformat(lines)
    # By default, we do nothing
    assert not edits

    edits_black = reformater.reformat_with_named_style("black", lines)
    assert edits_black

    actual_reformatted_black = apply_formatting_edits(lines, edits_black)

    expected_reformatted_black = textwrap.dedent(
        """\
    Source: foo
    # Test
    Build-Depends:
     bar,
     foo,

    Package: foo
    # Test
    Architecture: amd64 i386
    Section: devel
    # Test
    Depends:
     foo,
    # Test
    Recommends:
     bar,
    """
    )

    assert actual_reformatted_black == expected_reformatted_black