summaryrefslogtreecommitdiffstats
path: root/tests/lsp_tests/test_lsp_dctrl.py
blob: ab55bb80d92567c9a6e399f45f17c35f39cc0c93 (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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import textwrap
from typing import Optional

import pytest

from debputy.lsp.debputy_ls import DebputyLanguageServer
from debputy.lsprotocol.types import (
    CompletionParams,
    TextDocumentIdentifier,
    HoverParams,
    MarkupContent,
    SemanticTokensParams,
)

try:
    from debputy.lsp.lsp_debian_control import (
        _debian_control_completions,
        _debian_control_hover,
        _debian_control_semantic_tokens_full,
    )

    from pygls.server import LanguageServer
except ImportError:
    pass
from lsp_tests.lsp_tutil import (
    put_doc_with_cursor,
    put_doc_no_cursor,
    resolve_semantic_tokens,
    resolved_semantic_token,
)


def test_dctrl_complete_field(ls: "DebputyLanguageServer") -> None:
    dctrl_uri = "file:///nowhere/debian/control"

    cursor_pos = put_doc_with_cursor(
        ls,
        dctrl_uri,
        "debian/control",
        textwrap.dedent(
            """\
        Source: foo

        Package: foo
        <CURSOR>
"""
        ),
    )
    matches = _debian_control_completions(
        ls,
        CompletionParams(TextDocumentIdentifier(dctrl_uri), cursor_pos),
    )
    assert isinstance(matches, list)
    keywords = {m.label for m in matches}
    assert "Multi-Arch" in keywords
    assert "Architecture" in keywords
    # Already present or wrong section
    assert "Package" not in keywords
    assert "Source" not in keywords

    cursor_pos = put_doc_with_cursor(
        ls,
        dctrl_uri,
        "debian/control",
        textwrap.dedent(
            """\
        Source: foo

        Package: foo
        <CURSOR>
        Architecture: any
"""
        ),
    )

    matches = _debian_control_completions(
        ls,
        CompletionParams(TextDocumentIdentifier(dctrl_uri), cursor_pos),
    )
    assert isinstance(matches, list)
    keywords = {m.label for m in matches}
    assert "Multi-Arch" in keywords
    # Should be considered present even though it is parsed as two stanzas with a space
    assert "Architecture" not in keywords
    # Already present or wrong section
    assert "Package" not in keywords
    assert "Source" not in keywords

    cursor_pos = put_doc_with_cursor(
        ls,
        dctrl_uri,
        "debian/control",
        textwrap.dedent(
            """\
        Source: foo

        Package: foo
        Sec<CURSOR>
        Architecture: any
"""
        ),
    )

    matches = _debian_control_completions(
        ls,
        CompletionParams(TextDocumentIdentifier(dctrl_uri), cursor_pos),
    )
    assert isinstance(matches, list)
    keywords = {m.label for m in matches}
    # Included since we rely on client filtering (some clients let "RRR" match "R(ules-)R(equires-)R(oot), etc).
    assert "Multi-Arch" in keywords
    # Should be considered present even though it is parsed as two stanzas with an error
    assert "Architecture" not in keywords
    # Already present or wrong section
    assert "Package" not in keywords
    assert "Source" not in keywords


@pytest.mark.parametrize(
    "case,is_arch_all",
    [
        ("Architecture: any\n<CURSOR>", False),
        ("Architecture: any\nM-A<CURSOR>", False),
        ("<CURSOR>\nArchitecture: any", False),
        ("M-A<CURSOR>\nArchitecture: any", False),
        ("Architecture: all\n<CURSOR>", True),
        ("Architecture: all\nM-A<CURSOR>", True),
        ("<CURSOR>\nArchitecture: all", True),
        ("M-A<CURSOR>\nArchitecture: all", True),
        # Does not have architecture
        ("M-A<CURSOR>", None),
    ],
)
def test_dctrl_complete_field_context(
    ls: "DebputyLanguageServer",
    case: str,
    is_arch_all: Optional[bool],
) -> None:
    dctrl_uri = "file:///nowhere/debian/control"

    content = textwrap.dedent(
        """\
    Source: foo

    Package: foo
    {CASE}
"""
    ).format(CASE=case)
    cursor_pos = put_doc_with_cursor(
        ls,
        dctrl_uri,
        "debian/control",
        content,
    )

    matches = _debian_control_completions(
        ls,
        CompletionParams(TextDocumentIdentifier(dctrl_uri), cursor_pos),
    )
    assert isinstance(matches, list)
    keywords = {m.label for m in matches}
    # Missing Architecture counts as "arch:all" by the completion logic
    if is_arch_all is False:
        assert "X-DH-Build-For-Type" in keywords
    else:
        assert "X-DH-Build-For-Type" not in keywords


def test_dctrl_complete_field_value_context(ls: "DebputyLanguageServer") -> None:
    dctrl_uri = "file:///nowhere/debian/control"

    content = textwrap.dedent(
        """\
    Source: foo

    Package: foo
    Architecture: any
    Multi-Arch: <CURSOR>
"""
    )
    cursor_pos = put_doc_with_cursor(
        ls,
        dctrl_uri,
        "debian/control",
        content,
    )

    matches = _debian_control_completions(
        ls,
        CompletionParams(TextDocumentIdentifier(dctrl_uri), cursor_pos),
    )
    assert isinstance(matches, list)
    keywords = {m.label for m in matches}
    assert keywords == {"no", "same", "foreign", "allowed"}

    content = textwrap.dedent(
        """\
    Source: foo

    Package: foo
    Architecture: all
    Multi-Arch: <CURSOR>
"""
    )
    cursor_pos = put_doc_with_cursor(
        ls,
        dctrl_uri,
        "debian/control",
        content,
    )

    matches = _debian_control_completions(
        ls,
        CompletionParams(TextDocumentIdentifier(dctrl_uri), cursor_pos),
    )
    assert isinstance(matches, list)
    keywords = {m.label for m in matches}
    assert keywords == {"no", "foreign", "allowed"}


def test_dctrl_hover_doc_field(ls: "DebputyLanguageServer") -> None:
    dctrl_uri = "file:///nowhere/debian/control"
    cursor_pos = put_doc_with_cursor(
        ls,
        dctrl_uri,
        "debian/control",
        textwrap.dedent(
            """\
        Source: foo

        Package: foo
        Arch<CURSOR>itecture: any
"""
        ),
    )

    hover_doc = _debian_control_hover(
        ls,
        HoverParams(TextDocumentIdentifier(dctrl_uri), cursor_pos),
    )
    assert hover_doc is not None and isinstance(hover_doc.contents, MarkupContent)
    assert "Determines which architecture" in hover_doc.contents.value


def test_dctrl_hover_doc_synopsis(ls: "DebputyLanguageServer") -> None:
    dctrl_uri = "file:///nowhere/debian/control"
    cursor_pos = put_doc_with_cursor(
        ls,
        dctrl_uri,
        "debian/control",
        textwrap.dedent(
            """\
        Source: foo

        Package: foo
        Architecture: any
        Description: super charged<CURSOR> tool with batteries included
"""
        ),
    )

    hover_doc = _debian_control_hover(
        ls,
        HoverParams(TextDocumentIdentifier(dctrl_uri), cursor_pos),
    )
    assert hover_doc is not None and isinstance(hover_doc.contents, MarkupContent)
    assert hover_doc.contents.value.startswith("# Package synopsis")
    assert "super charged tool with batteries included" in hover_doc.contents.value


def test_dctrl_hover_doc_substvars(ls: "DebputyLanguageServer") -> None:
    dctrl_uri = "file:///nowhere/debian/control"
    matching_cases = [
        "bar (= <CURSOR>${binary:Version})",
        "bar (= $<CURSOR>{binary:Version})",
        "bar (= ${binary:Version<CURSOR>})",
    ]
    for variant in matching_cases:
        cursor_pos = put_doc_with_cursor(
            ls,
            dctrl_uri,
            "debian/control",
            textwrap.dedent(
                f"""\
            Source: foo

            Package: foo
            Architecture: any
            Depends: bar (= {variant})
            Description: super charged tool with batteries included
    """
            ),
        )

        hover_doc = _debian_control_hover(
            ls,
            HoverParams(TextDocumentIdentifier(dctrl_uri), cursor_pos),
        )
        assert hover_doc is not None and isinstance(hover_doc.contents, MarkupContent)
        assert hover_doc.contents.value.startswith("# Substvar `${binary:Version}`")

    non_matching_cases = [
        "bar (=<CURSOR> ${binary:Version})",
        "bar (= ${binary:Version}<CURSOR>)",
    ]
    for variant in non_matching_cases:
        cursor_pos = put_doc_with_cursor(
            ls,
            dctrl_uri,
            "debian/control",
            textwrap.dedent(
                f"""\
            Source: foo

            Package: foo
            Architecture: any
            Depends: bar (= {variant})
            Description: super charged tool with batteries included
    """
            ),
        )

        hover_doc = _debian_control_hover(
            ls,
            HoverParams(TextDocumentIdentifier(dctrl_uri), cursor_pos),
        )
        provided_doc = ""
        if hover_doc is not None and isinstance(hover_doc.contents, MarkupContent):
            provided_doc = hover_doc.contents.value
        assert not provided_doc.startswith("# Substvar `${binary:Version}`")


def test_dctrl_semantic_tokens(ls: "DebputyLanguageServer") -> None:
    dctrl_uri = "file:///nowhere/debian/control"
    put_doc_no_cursor(
        ls,
        dctrl_uri,
        "debian/control",
        textwrap.dedent(
            """\
        # Some leading comment

        Source: foo

        # Comment between stanzas

        Package: foo
        # Comment before Architecture
        Architecture: any
        Depends:
        # Comment about bar
             bar (>= 1.0),
             baz [linux-any] <!pkg.foo.bootstrap>
        Description: super charged tool with batteries included
        Unknown-Field: Some value
        # Comment in that field
          that we do not know about.
"""
        ),
    )

    semantic_tokens = _debian_control_semantic_tokens_full(
        ls,
        SemanticTokensParams(TextDocumentIdentifier(dctrl_uri)),
    )
    resolved_semantic_tokens = resolve_semantic_tokens(semantic_tokens)
    assert resolved_semantic_tokens is not None
    assert resolved_semantic_tokens == [
        resolved_semantic_token(0, 0, len("# Some leading comment"), "comment"),
        resolved_semantic_token(2, 0, len("Source"), "keyword"),
        resolved_semantic_token(4, 0, len("# Comment between stanzas"), "comment"),
        resolved_semantic_token(6, 0, len("Package"), "keyword"),
        resolved_semantic_token(7, 0, len("# Comment before Architecture"), "comment"),
        resolved_semantic_token(8, 0, len("Architecture"), "keyword"),
        resolved_semantic_token(8, len("Architecture: "), len("any"), "enumMember"),
        resolved_semantic_token(9, 0, len("Depends"), "keyword"),
        resolved_semantic_token(10, 0, len("# Comment about bar"), "comment"),
        resolved_semantic_token(13, 0, len("Description"), "keyword"),
        resolved_semantic_token(14, 0, len("Unknown-Field"), "keyword"),
        # TODO: resolved_semantic_token(15, 0, len("# Comment in that field"), "comment"),
    ]