summaryrefslogtreecommitdiffstats
path: root/tests/lint_tests/test_lint_dpatches_series.py
blob: ed2a8025b32aa7a112d0a9137f86c1fa20e57374 (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
import textwrap

import pytest

from debputy.lsp.lsp_debian_patches_series import _lint_debian_patches_series
from debputy.packages import DctrlParser
from debputy.plugin.api.feature_set import PluginProvidedFeatureSet
from debputy.plugin.api.test_api import build_virtual_file_system
from lint_tests.lint_tutil import (
    LintWrapper,
)

try:
    from lsprotocol.types import Diagnostic, DiagnosticSeverity
except ImportError:
    pass


@pytest.fixture
def line_linter(
    debputy_plugin_feature_set: PluginProvidedFeatureSet,
    lint_dctrl_parser: DctrlParser,
) -> LintWrapper:
    return LintWrapper(
        "/nowhere/debian/patches/series",
        _lint_debian_patches_series,
        debputy_plugin_feature_set,
        lint_dctrl_parser,
    )


def test_dpatches_series_files_lint(line_linter: LintWrapper) -> None:
    lines = textwrap.dedent(
        """\
    # Some leading comment

    ../some.patch

    .//.//./subdir/another-delta.diff # foo

    subdir/no-issues.patch # bar
    """
    ).splitlines(keepends=True)

    fs = build_virtual_file_system(
        [
            "./debian/patches/series",
            "./debian/some.patch",
            "./debian/patches/subdir/another-delta.diff",
            "./debian/patches/subdir/no-issues.patch",
        ]
    )

    line_linter.source_root = fs

    diagnostics = line_linter(lines)
    print(diagnostics)
    assert len(diagnostics) == 2

    first_issue, second_issue = diagnostics

    msg = 'Disallowed prefix "../"'
    assert first_issue.message == msg
    assert f"{first_issue.range}" == "2:0-2:3"
    assert first_issue.severity == DiagnosticSeverity.Error

    msg = 'Unnecessary prefix ".//.//./"'
    assert second_issue.message == msg
    assert f"{second_issue.range}" == "4:0-4:8"
    assert second_issue.severity == DiagnosticSeverity.Warning


def test_dpatches_series_files_file_mismatch_lint(line_linter: LintWrapper) -> None:
    lines = textwrap.dedent(
        """\
    # Some leading comment

    some/used-twice.patch

    some/missing-file.patch

    some/used-twice.patch
    """
    ).splitlines(keepends=True)

    fs = build_virtual_file_system(
        [
            "./debian/patches/series",
            "./debian/ignored.patch",
            "./debian/patches/some/unused-file.diff",
            "./debian/patches/some/used-twice.patch",
        ]
    )

    line_linter.source_root = fs

    diagnostics = line_linter(lines)
    print(diagnostics)
    assert len(diagnostics) == 3

    first_issue, second_issue, third_issue = diagnostics

    msg = 'Non-existing patch "some/missing-file.patch"'
    assert first_issue.message == msg
    assert f"{first_issue.range}" == "4:0-4:23"
    assert first_issue.severity == DiagnosticSeverity.Error

    msg = 'Duplicate patch: "some/used-twice.patch"'
    assert second_issue.message == msg
    assert f"{second_issue.range}" == "6:0-6:21"
    assert second_issue.severity == DiagnosticSeverity.Error

    msg = 'Unused patch: "some/unused-file.diff"'
    assert third_issue.message == msg
    assert f"{third_issue.range}" == "0:0-7:22"
    assert third_issue.severity == DiagnosticSeverity.Warning


def test_dpatches_series_files_ext_lint(line_linter: LintWrapper) -> None:
    lines = textwrap.dedent(
        """\
    # Some leading comment

    some/ok.diff

    some/ok.patch

    some/no-extension
    """
    ).splitlines(keepends=True)

    fs = build_virtual_file_system(
        [
            "./debian/patches/series",
            "./debian/patches/some/ok.diff",
            "./debian/patches/some/ok.patch",
            "./debian/patches/some/no-extension",
        ]
    )

    line_linter.source_root = fs

    diagnostics = line_linter(lines)
    print(diagnostics)
    assert len(diagnostics) == 1

    issue = diagnostics[0]

    msg = 'Patch not using ".patch" or ".diff" as extension: "some/no-extension"'
    assert issue.message == msg
    assert f"{issue.range}" == "6:0-6:17"
    assert issue.severity == DiagnosticSeverity.Hint