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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
|
import dataclasses
import os
import random
from typing import cast, TYPE_CHECKING, Optional
from collections.abc import Sequence, Mapping
import pytest
from debputy.packager_provided_files import detect_all_packager_provided_files
from debputy.plugin.api import DebputyPluginInitializer
from debputy.plugin.api.feature_set import PluginProvidedFeatureSet
from debputy.plugin.api.impl import plugin_metadata_for_debputys_own_plugin
from debputy.plugin.api.impl_types import (
PackagerProvidedFileClassSpec,
DebputyPluginMetadata,
)
from debputy.plugin.api.test_api import (
InitializedPluginUnderTest,
build_virtual_file_system,
)
from debputy.plugin.api.test_api.test_impl import (
initialize_plugin_under_test_preloaded,
)
from lint_tests.lint_tutil import requires_levenshtein
from tutil import faked_binary_package, binary_package_table
if TYPE_CHECKING:
from debputy.plugin.api.test_api.test_impl import InitializedPluginUnderTestImpl
# Irrelevant, but makes the import not "unused" for things that does not parse `cast("...", ...)` expressions
assert InitializedPluginUnderTestImpl is not None
def ppf_test_plugin(api: DebputyPluginInitializer) -> None:
api.packager_provided_file(
"arch-specific-dash",
"/some/test-directory/{name}.conf",
allow_architecture_segment=True,
)
api.packager_provided_file(
"arch.specific.dot",
"/some/test.directory/{name}",
allow_architecture_segment=True,
)
api.packager_provided_file(
"arch.specific.with.priority",
"/some/test.priority.directory/{priority:02}-{name}",
allow_architecture_segment=True,
default_priority=60,
)
api.packager_provided_file(
"packageless-fallback",
"/some/test-plfb/{name}",
packageless_is_fallback_for_all_packages=True,
)
api.packager_provided_file(
"packageless.fallback",
"/some/test.plfb/{name}",
packageless_is_fallback_for_all_packages=True,
)
@pytest.mark.parametrize(
"package_name,basename,install_target,is_main_binary",
[
("foo", "foo.arch-specific-dash", "./some/test-directory/foo.conf", True),
# main package match
("foo", "arch-specific-dash", "./some/test-directory/foo.conf", True),
# arch match
("foo", "foo.arch-specific-dash.amd64", "./some/test-directory/foo.conf", True),
# Matches with periods in both package name and in the file type
("foo.bar", "foo.bar.arch.specific.dot", "./some/test.directory/foo.bar", True),
("foo.bar", "arch.specific.dot", "./some/test.directory/foo.bar", True),
(
"foo.bar",
"foo.bar.arch.specific.dot.amd64",
"./some/test.directory/foo.bar",
True,
),
# Priority
(
"foo.bar",
"foo.bar.arch.specific.with.priority",
"./some/test.priority.directory/60-foo.bar",
True,
),
(
"foo.bar",
"arch.specific.with.priority",
"./some/test.priority.directory/60-foo.bar",
True,
),
(
"foo.bar",
"foo.bar.arch.specific.with.priority.amd64",
"./some/test.priority.directory/60-foo.bar",
True,
),
# Name
(
"foo.bar",
"foo.bar.special.name.arch.specific.with.priority",
"./some/test.priority.directory/60-special.name",
True,
),
(
"foo.bar",
"foo.bar.special.name.arch.specific.with.priority.amd64",
"./some/test.priority.directory/60-special.name",
True,
),
(
"foo.bar",
"packageless-fallback",
"./some/test-plfb/foo.bar",
False,
),
(
"foo.bar",
"packageless.fallback",
"./some/test.plfb/foo.bar",
False,
),
],
)
def test_packager_provided_files(
package_name: str, basename: str, install_target: str, is_main_binary: bool
) -> None:
# Inject our custom files
plugin = initialize_plugin_under_test_preloaded(
1,
ppf_test_plugin,
plugin_name="pff-test-plugin",
)
debputy_plugin_feature_set = _fetch_debputy_plugin_feature_set(plugin)
debian_dir = build_virtual_file_system([basename])
binary_under_test = faked_binary_package(
package_name, is_main_package=is_main_binary
)
main_package = (
binary_under_test if is_main_binary else faked_binary_package("main-pkg")
)
binaries = [main_package]
if not is_main_binary:
binaries.append(binary_under_test)
binary_packages = binary_package_table(*binaries)
ppf = detect_all_packager_provided_files(
debputy_plugin_feature_set,
debian_dir,
binary_packages,
)
assert package_name in ppf
all_matched = ppf[package_name].auto_installable
assert len(all_matched) == 1
matched = all_matched[0]
assert basename == matched.path.name
actual_install_target = "/".join(matched.compute_dest())
assert actual_install_target == install_target
@pytest.mark.parametrize(
"package_name,expected_basename,non_matched",
[
("foo", "foo.arch-specific-dash", ["arch-specific-dash"]),
(
"foo",
"foo.arch-specific-dash.amd64",
[
"foo.arch-specific-dash",
"arch-specific-dash",
"foo.arch-specific-dash.i386",
],
),
(
"foo",
"foo.arch-specific-dash",
["arch-specific-dash", "foo.arch-specific-dash.i386"],
),
],
)
def test_packager_provided_files_priority(
package_name, expected_basename, non_matched
) -> None:
assert len(non_matched) > 0
# Inject our custom files
plugin = initialize_plugin_under_test_preloaded(
1,
ppf_test_plugin,
plugin_name="pff-test-plugin",
)
debputy_plugin_feature_set = _fetch_debputy_plugin_feature_set(plugin)
binary_packages = binary_package_table(faked_binary_package(package_name))
all_entries_base = [x for x in non_matched]
for order in (0, len(all_entries_base), None):
all_entries = all_entries_base.copy()
print(f"Order: {order}")
if order is not None:
all_entries.insert(order, expected_basename)
else:
all_entries.append(expected_basename)
# Ensure there are no order dependencies in the test by randomizing it.
random.shuffle(all_entries)
debian_dir = build_virtual_file_system(all_entries)
ppf = detect_all_packager_provided_files(
debputy_plugin_feature_set,
debian_dir,
binary_packages,
)
assert package_name in ppf
all_matched = ppf[package_name].auto_installable
assert len(all_matched) == 1
matched = all_matched[0]
assert expected_basename == matched.path.name
@pytest.mark.parametrize(
"main_package,secondary_package",
[
("foo", "foo-doc"),
("foo.bar", "foo.bar-doc"),
],
)
def test_detect_ppf_simple(
main_package: str,
secondary_package: str,
debputy_plugin_feature_set: PluginProvidedFeatureSet,
) -> None:
plugin_metadata = plugin_metadata_for_debputys_own_plugin()
binary_packages = binary_package_table(
faked_binary_package(main_package, is_main_package=True),
faked_binary_package(secondary_package),
)
debian_dir = build_virtual_file_system(
[
"install",
f"{secondary_package}.install",
f"{main_package}.docs",
f"{secondary_package}.docs",
"copyright",
]
)
ppf_defs = _replace_ppf_def(
debputy_plugin_feature_set,
_fake_PPFClassSpec(
plugin_metadata,
"install",
),
_fake_PPFClassSpec(
plugin_metadata,
"docs",
),
_fake_PPFClassSpec(
plugin_metadata,
"copyright",
packageless_is_fallback_for_all_packages=True,
),
)
results = detect_all_packager_provided_files(
ppf_defs,
debian_dir,
binary_packages,
)
assert main_package in results
main_matches = results[main_package].auto_installable
assert {m.path.name for m in main_matches} == {
"install",
f"{main_package}.docs",
"copyright",
}
assert secondary_package in results
sec_matches = results[secondary_package].auto_installable
expected = {
f"{secondary_package}.install",
f"{secondary_package}.docs",
"copyright",
}
assert {m.path.name for m in sec_matches} == expected
def test_detect_ppf_name_segment(
debputy_plugin_feature_set: PluginProvidedFeatureSet,
) -> None:
plugin_metadata = plugin_metadata_for_debputys_own_plugin()
binary_packages = binary_package_table(
faked_binary_package("foo", is_main_package=True),
faked_binary_package("bar"),
)
debian_dir = build_virtual_file_system(
[
"fuu.install",
]
)
ppf_defs = _replace_ppf_def(
debputy_plugin_feature_set,
_fake_PPFClassSpec(
plugin_metadata,
"install",
),
)
results = detect_all_packager_provided_files(
ppf_defs,
debian_dir,
binary_packages,
)
assert "foo" in results
foo_matches = results["foo"].auto_installable
assert {m.path.name for m in foo_matches} == {
"fuu.install",
}
foo_install = foo_matches[0]
assert foo_install.name_segment == "fuu"
assert foo_install.package_name == "foo"
assert foo_install.architecture_restriction is None
assert not foo_install.fuzzy_match
assert "bar" in results
assert not results["bar"].auto_installable
ppf_defs = _replace_ppf_def(
debputy_plugin_feature_set,
_fake_PPFClassSpec(
plugin_metadata,
"install",
allow_name_segment=False,
),
)
results = detect_all_packager_provided_files(
ppf_defs,
debian_dir,
binary_packages,
allow_fuzzy_matches=True,
)
assert "foo" in results
foo_matches = results["foo"].auto_installable
assert {m.path.name for m in foo_matches} == {"fuu.install"}
foo_install = foo_matches[0]
assert foo_install.name_segment == "fuu"
assert foo_install.package_name == "foo"
assert foo_install.architecture_restriction is None
assert "bar" in results
assert not results["bar"].auto_installable
def test_detect_ppf_fuzzy_match_bug_950723(
debputy_plugin_feature_set: PluginProvidedFeatureSet,
) -> None:
plugin_metadata = plugin_metadata_for_debputys_own_plugin()
binary_packages = binary_package_table(
faked_binary_package("foo", is_main_package=True),
faked_binary_package("bar"),
)
debian_dir = build_virtual_file_system(
[
"bar.service",
"foo@.service",
]
)
ppf_defs = _replace_ppf_def(
debputy_plugin_feature_set,
_fake_PPFClassSpec(
plugin_metadata,
"service",
bug_950723=True,
),
)
results = detect_all_packager_provided_files(
ppf_defs,
debian_dir,
binary_packages,
)
assert "foo" in results
foo_matches = results["foo"].auto_installable
assert {m.path.name for m in foo_matches} == {"foo@.service"}
foo_at_service = foo_matches[0]
# Without bug#950723 AND fuzzy_match, it counts a name segment for the main package.
assert foo_at_service.name_segment == "foo@"
assert foo_at_service.package_name == "foo"
assert foo_at_service.architecture_restriction is None
assert not foo_at_service.fuzzy_match
assert "bar" in results
bar_matches = results["bar"].auto_installable
assert {m.path.name for m in bar_matches} == {"bar.service"}
bar_service = bar_matches[0]
assert bar_service.name_segment is None
assert bar_service.package_name == "bar"
assert bar_service.architecture_restriction is None
assert not bar_service.fuzzy_match
results = detect_all_packager_provided_files(
ppf_defs,
debian_dir,
binary_packages,
allow_fuzzy_matches=True,
)
assert "foo" in results
foo_matches = results["foo"].auto_installable
assert {m.path.name for m in foo_matches} == {"foo@.service"}
foo_at_service = foo_matches[0]
assert foo_at_service.name_segment is None
assert foo_at_service.package_name == "foo"
assert foo_at_service.architecture_restriction is None
assert "bar" in results
bar_matches = results["bar"].auto_installable
assert {m.path.name for m in bar_matches} == {"bar.service"}
bar_service = bar_matches[0]
assert bar_service.name_segment is None
assert bar_service.package_name == "bar"
assert bar_service.architecture_restriction is None
assert not bar_service.fuzzy_match
@requires_levenshtein
def test_detect_ppf_typo(debputy_plugin_feature_set: PluginProvidedFeatureSet) -> None:
plugin_metadata = plugin_metadata_for_debputys_own_plugin()
binary_packages = binary_package_table(
faked_binary_package("foo", is_main_package=True),
faked_binary_package("bar"),
)
debian_dir = build_virtual_file_system(
[
"fuu.install",
"bar.intsall",
]
)
ppf_defs = _replace_ppf_def(
debputy_plugin_feature_set,
_fake_PPFClassSpec(
plugin_metadata,
"install",
),
_fake_PPFClassSpec(
plugin_metadata,
"logcheck.violations.d",
),
)
results = detect_all_packager_provided_files(
ppf_defs,
debian_dir,
binary_packages,
detect_typos=True,
)
assert "foo" in results
foo_matches = results["foo"].auto_installable
assert {m.path.name for m in foo_matches} == {"fuu.install"}
foo_logcheck = foo_matches[0]
# Not a typo due to how name segments works with debhelper compat <= 13
# (but should probably have been one.
assert foo_logcheck.name_segment == "fuu"
assert foo_logcheck.package_name == "foo"
assert foo_logcheck.architecture_restriction is None
assert "bar" in results
bar_matches = results["bar"].auto_installable
assert {m.path.name for m in bar_matches} == {"bar.intsall"}
bar_logcheck = bar_matches[0]
assert os.path.basename(bar_logcheck.expected_path) == "bar.install"
assert bar_logcheck.name_segment is None
assert bar_logcheck.package_name == "bar"
assert bar_logcheck.architecture_restriction is None
debian_dir = build_virtual_file_system(
[
# Typo'ed by intention
"logchcek.violations.d",
"bar.logchcek.violations.d",
]
)
results = detect_all_packager_provided_files(
ppf_defs,
debian_dir,
binary_packages,
detect_typos=True,
)
assert "foo" in results
foo_matches = results["foo"].auto_installable
assert {m.path.name for m in foo_matches} == {"logchcek.violations.d"}
foo_logcheck = foo_matches[0]
assert foo_logcheck.name_segment is None
assert foo_logcheck.package_name == "foo"
assert foo_logcheck.architecture_restriction is None
assert os.path.basename(foo_logcheck.expected_path) == "logcheck.violations.d"
assert "bar" in results
bar_matches = results["bar"].auto_installable
assert {m.path.name for m in bar_matches} == {
"bar.logchcek.violations.d",
}
bar_logcheck = bar_matches[0]
assert bar_logcheck.name_segment is None
assert bar_logcheck.package_name == "bar"
assert bar_logcheck.architecture_restriction is None
assert os.path.basename(bar_logcheck.expected_path) == "bar.logcheck.violations.d"
def test_debhelper_overlapping_stems(
debputy_plugin_feature_set: PluginProvidedFeatureSet,
) -> None:
plugin_metadata = plugin_metadata_for_debputys_own_plugin()
binary_packages = binary_package_table(
faked_binary_package("foo", is_main_package=True),
)
debian_dir = build_virtual_file_system(
[
"foo.user.service",
]
)
ppf_defs = _replace_ppf_def(
debputy_plugin_feature_set,
_fake_PPFClassSpec(
plugin_metadata,
"service",
),
_fake_PPFClassSpec(
plugin_metadata,
"user.service",
),
)
results = detect_all_packager_provided_files(
ppf_defs,
debian_dir,
binary_packages,
)
assert "foo" in results
foo_matches = results["foo"].auto_installable
assert {m.path.name for m in foo_matches} == {
"foo.user.service",
}
matched_file = foo_matches[0]
assert matched_file.definition.stem == "user.service"
assert matched_file.name_segment is None
assert matched_file.package_name == "foo"
assert matched_file.architecture_restriction is None
debian_dir = build_virtual_file_system(
[
"foo.named.service",
]
)
results = detect_all_packager_provided_files(
ppf_defs,
debian_dir,
binary_packages,
)
assert "foo" in results
foo_matches = results["foo"].auto_installable
assert {m.path.name for m in foo_matches} == {"foo.named.service"}
matched_file = foo_matches[0]
assert matched_file.definition.stem == "service"
assert matched_file.name_segment == "named"
assert matched_file.package_name == "foo"
assert matched_file.architecture_restriction is None
@requires_levenshtein
def test_debhelper_overlapping_stems_typo_check(
debputy_plugin_feature_set: PluginProvidedFeatureSet,
) -> None:
plugin_metadata = plugin_metadata_for_debputys_own_plugin()
binary_packages = binary_package_table(
faked_binary_package("foo", is_main_package=True),
)
debian_dir = build_virtual_file_system(
[
"foo.uxxr.service",
]
)
ppf_defs = _replace_ppf_def(
debputy_plugin_feature_set,
_fake_PPFClassSpec(
plugin_metadata,
"service",
),
_fake_PPFClassSpec(
plugin_metadata,
"user.service",
),
)
results = detect_all_packager_provided_files(
ppf_defs,
debian_dir,
binary_packages,
detect_typos=True,
)
assert "foo" in results
foo_matches = results["foo"].auto_installable
assert {m.path.name for m in foo_matches} == {"foo.uxxr.service"}
foo_install = foo_matches[0]
# While it could just as well be a named segment, we assume it is a typo.
assert foo_install.definition.stem == "user.service"
assert foo_install.name_segment is None
assert foo_install.package_name == "foo"
assert foo_install.architecture_restriction is None
assert os.path.basename(foo_install.expected_path) == "foo.user.service"
def _fetch_debputy_plugin_feature_set(
plugin: InitializedPluginUnderTest,
) -> PluginProvidedFeatureSet:
# Very much not public API, but we need it to avoid testing on production data (also, it is hard to find
# relevant debputy files for all the cases we want to test).
return cast("InitializedPluginUnderTestImpl", plugin)._feature_set
def _fake_PPFClassSpec(
debputy_plugin_metadata: DebputyPluginMetadata,
stem: str,
*,
default_priority: int | None = None,
packageless_is_fallback_for_all_packages: bool = False,
bug_950723: bool = False,
has_active_command: bool = False,
allow_name_segment: bool = True,
) -> PackagerProvidedFileClassSpec:
return PackagerProvidedFileClassSpec(
debputy_plugin_metadata,
stem,
"not-a-real-ppf",
allow_architecture_segment=True,
allow_name_segment=allow_name_segment,
default_priority=default_priority,
default_mode=0o644,
post_formatting_rewrite=None,
packageless_is_fallback_for_all_packages=packageless_is_fallback_for_all_packages,
reservation_only=False,
formatting_callback=None,
bug_950723=bug_950723,
has_active_command=has_active_command,
)
def _replace_ppf_def(
debputy_plugin_feature_set: PluginProvidedFeatureSet,
*ppfs: PackagerProvidedFileClassSpec,
) -> PluginProvidedFeatureSet:
ppf_def = {ppf.stem: ppf for ppf in ppfs}
return dataclasses.replace(
debputy_plugin_feature_set, packager_provided_files=ppf_def
)
|