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
|
from typing import Any, Optional, Union
from collections.abc import Mapping
import pytest
from debian.deb822 import Deb822
from debputy.plugin.api.test_api.test_impl import FAKE_DPKG_QUERY_TABLE
from debputy.yaml.compat import CommentedMap
from debputy.lsp.maint_prefs import (
MaintainerPreferenceTable,
determine_effective_preference,
EffectiveFormattingPreference,
_WAS_DEFAULTS,
)
from debputy.packages import SourcePackage
def test_load_styles() -> None:
styles = MaintainerPreferenceTable.load_preferences()
assert "niels@thykier.net" in styles.maintainer_preferences
nt_maint_pref = styles.maintainer_preferences["niels@thykier.net"]
# Note this is data dependent; if it fails because the style changes, update the test
assert nt_maint_pref.canonical_name == "Niels Thykier"
assert not nt_maint_pref.is_packaging_team
black_style = styles.named_styles["black"]
nt_style = nt_maint_pref.formatting
assert nt_style is not None
assert black_style == black_style
def test_load_no_styles() -> None:
styles = MaintainerPreferenceTable.load_preferences()
assert "packages@qa.debian.org" in styles.maintainer_preferences
qa_maint_pref = styles.maintainer_preferences["packages@qa.debian.org"]
assert qa_maint_pref.canonical_name == "Debian QA Group"
assert qa_maint_pref.is_packaging_team
# Orphaned packages do not have a predefined style, since Debian (nor Debian QA) have
# one well-defined style.
assert qa_maint_pref.formatting is None
def test_load_named_styles() -> None:
styles = MaintainerPreferenceTable.load_preferences()
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.deb822_normalize_field_content
assert black_style.deb822_short_indent
assert black_style.deb822_always_wrap
assert black_style.deb822_trailing_separator
assert black_style.deb822_max_line_length == 79
assert not black_style.deb822_normalize_stanza_order
# TODO: Not implemented yet
assert not black_style.deb822_normalize_field_order
assert black_style.deb822_auto_canonical_size_field_names
def test_compat_styles() -> None:
styles = MaintainerPreferenceTable.load_preferences()
# 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_style = styles.maintainer_preferences["niels@thykier.net"].formatting
zeha_style = styles.maintainer_preferences["zeha@debian.org"].formatting
# Data dependency
assert nt_style == zeha_style
fields = Deb822(
{
"Package": "foo",
"Maintainer": "Foo <random-package@packages.debian.org>",
"Uploaders": "Niels Thykier <niels@thykier.net>",
},
)
src = SourcePackage(fields)
effective_style, tool, _ = determine_effective_preference(styles, src, None)
assert effective_style == nt_style
assert tool == "debputy reformat"
fields["Uploaders"] = (
"Niels Thykier <niels@thykier.net>, Chris Hofstaedtler <zeha@debian.org>"
)
src = SourcePackage(fields)
effective_style, tool, _ = determine_effective_preference(styles, src, None)
assert effective_style == nt_style
assert effective_style == zeha_style
assert tool == "debputy reformat"
fields["Uploaders"] = (
"Niels Thykier <niels@thykier.net>, Chris Hofstaedtler <zeha@debian.org>, Random Developer <random@example.org>"
)
src = SourcePackage(fields)
effective_style, tool, _ = determine_effective_preference(styles, src, None)
assert effective_style is None
assert tool is None
@pytest.mark.xfail
def test_compat_styles_team_maint() -> None:
styles = MaintainerPreferenceTable.load_preferences()
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, tool, _ = determine_effective_preference(styles, src, None)
assert effective_style == team_style.formatting
assert tool is None
def test_x_style() -> None:
styles = MaintainerPreferenceTable.load_preferences()
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, tool, _ = determine_effective_preference(styles, src, None)
assert effective_style == black_style
assert tool == "debputy reformat"
@pytest.mark.parametrize(
"variables",
[
{"SALSA_CI_DISABLE_WRAP_AND_SORT": "no"},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": 0},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": False},
{"SALSA_CI_ENABLE_WRAP_AND_SORT": "yes"},
{"SALSA_CI_ENABLE_WRAP_AND_SORT": 1},
{"SALSA_CI_ENABLE_WRAP_AND_SORT": True},
],
)
def test_was_from_salsa_ci_style_enabled(variables: Mapping[str, Any]) -> None:
styles = MaintainerPreferenceTable.load_preferences()
fields = Deb822(
{
"Package": "foo",
"Maintainer": "Random Developer <random@example.org>",
},
)
src = SourcePackage(fields)
assert "random@example.org" not in styles.maintainer_preferences
effective_style, tool, _ = determine_effective_preference(styles, src, None)
assert effective_style is None
assert tool is None
salsa_ci = CommentedMap({"variables": CommentedMap(variables)})
effective_style, tool, _ = determine_effective_preference(styles, src, salsa_ci)
was_style = EffectiveFormattingPreference(**_WAS_DEFAULTS)
assert effective_style == was_style
assert tool == "wrap-and-sort"
@pytest.mark.parametrize(
"variables",
[
{},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": "yes"},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": 1},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": True},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": {}},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": []},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": None},
{"SALSA_CI_ENABLE_WRAP_AND_SORT": "no"},
{"SALSA_CI_ENABLE_WRAP_AND_SORT": 0},
{"SALSA_CI_ENABLE_WRAP_AND_SORT": False},
{"SALSA_CI_ENABLE_WRAP_AND_SORT": {}},
{"SALSA_CI_ENABLE_WRAP_AND_SORT": []},
{"SALSA_CI_ENABLE_WRAP_AND_SORT": None},
{
"SALSA_CI_DISABLE_WRAP_AND_SORT": "yes",
"SALSA_CI_ENABLE_WRAP_AND_SORT": "yes",
},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": 1, "SALSA_CI_ENABLE_WRAP_AND_SORT": 1},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": True, "SALSA_CI_ENABLE_WRAP_AND_SORT": True},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": {}, "SALSA_CI_ENABLE_WRAP_AND_SORT": True},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": [], "SALSA_CI_ENABLE_WRAP_AND_SORT": True},
{"SALSA_CI_DISABLE_WRAP_AND_SORT": None, "SALSA_CI_ENABLE_WRAP_AND_SORT": True},
],
)
def test_was_from_salsa_ci_style_disabled(variables: Mapping[str, Any]) -> None:
styles = MaintainerPreferenceTable.load_preferences()
fields = Deb822(
{
"Package": "foo",
"Maintainer": "Random Developer <random@example.org>",
},
)
src = SourcePackage(fields)
assert "random@example.org" not in styles.maintainer_preferences
effective_style, tool, _ = determine_effective_preference(styles, src, None)
assert effective_style is None
assert tool is None
salsa_ci = CommentedMap({"variables": CommentedMap(variables)})
effective_style, tool, _ = determine_effective_preference(styles, src, salsa_ci)
assert effective_style is None
assert tool is None
@pytest.mark.parametrize(
"was_args,style_delta",
[
(
"-a",
{
"deb822_always_wrap": True,
},
),
(
"-sa",
{
"deb822_always_wrap": True,
"deb822_short_indent": True,
},
),
(
"-sa --keep-first",
{
"deb822_always_wrap": True,
"deb822_short_indent": True,
},
),
(
"-sab --keep-first",
{
"deb822_always_wrap": True,
"deb822_short_indent": True,
"deb822_normalize_stanza_order": True,
},
),
(
"-sab --no-keep-first",
{
"deb822_always_wrap": True,
"deb822_short_indent": True,
"deb822_normalize_stanza_order": False,
},
),
],
)
def test_was_from_salsa_ci_style_args(
was_args: str,
style_delta: Mapping[str, Any] | None,
) -> None:
styles = MaintainerPreferenceTable.load_preferences()
fields = Deb822(
{
"Package": "foo",
"Maintainer": "Random Developer <random@example.org>",
},
)
src = SourcePackage(fields)
assert "random@example.org" not in styles.maintainer_preferences
salsa_ci = CommentedMap(
{
"variables": CommentedMap(
{
"SALSA_CI_DISABLE_WRAP_AND_SORT": "no",
"SALSA_CI_WRAP_AND_SORT_ARGS": was_args,
}
)
}
)
effective_style, tool, _ = determine_effective_preference(styles, src, salsa_ci)
if style_delta is None:
assert effective_style is None
assert tool is None
else:
was_style = EffectiveFormattingPreference(**_WAS_DEFAULTS).replace(
**style_delta,
)
assert effective_style == was_style
assert tool == f"wrap-and-sort {was_args}".strip()
|