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
|
"debputy: test generation of the (static-)built-using field."
import typing
import pytest
from conftest import ManifestParserFactory
from debputy.filesystem_scan import FSRootDir
from debputy.plugin.api import virtual_path_def
from debputy.plugin.api.impl_types import (
PackageProcessingContextProvider,
PackageDataTable,
)
from debputy.plugin.api.test_api import build_virtual_file_system
from debputy.plugin.api.test_api.test_impl import initialize_plugin_under_test_preloaded
from debputy.plugins.debputy.debputy_plugin import initialize_debputy_features
from debputy.util import PKGNAME_REGEX
from tutil import is_pkg_installed
class TDefinition(typing.NamedTuple):
name: str
manifest: str
expected: set[str]
build_depends: str
architecture: str = "any"
field: str = "Built-Using"
required_pkgs: tuple[str, ...] = ()
# More or less in sync with dh-builtusing unit-tests.
tests = (
TDefinition(
name="01basic",
build_depends="dpkg",
manifest="sources-for: dpkg",
expected={"dpkg"},
),
TDefinition(
name="01static",
build_depends="dpkg",
manifest="sources-for: dpkg",
expected={"dpkg"},
field="Static-Built-Using",
),
TDefinition(
name="20build-depends-arch-restriction",
build_depends="dpkg [i386]",
manifest="sources-for: dpkg",
expected={},
),
TDefinition(
name="20build-depends-profile-restriction",
build_depends="dpkg <dummy>",
manifest="sources-for: dpkg",
expected={},
),
TDefinition(
name="20build-depends-arch",
required_pkgs=("libc6", "libdpkg-perl", "libbinutils"),
build_depends="""libc6
Build-Depends-Arch: libdpkg-perl
Build-Depends-Indep: libbinutils""",
manifest="sources-for: 'lib*'",
expected={"glibc", "dpkg"},
),
TDefinition(
name="20build-depends-indep",
required_pkgs=("libc6", "libdpkg-perl", "libbinutils"),
architecture="all",
build_depends="""libc6
Build-Depends-Arch: libdpkg-perl
Build-Depends-Indep: libbinutils""",
manifest="sources-for: 'lib*'",
expected={"glibc", "binutils"},
),
TDefinition(
name="30or-dependency-first",
build_depends="dpkg | dummy",
manifest="sources-for: dpkg",
expected={"dpkg"},
),
TDefinition(
name="30or-dependency-not-first",
build_depends="dummy | dpkg",
manifest="sources-for: dpkg",
expected={"dpkg"},
),
TDefinition(
name="40pattern-star-initial",
build_depends="dpkg",
manifest="sources-for: '*kg'",
expected={"dpkg"},
),
TDefinition(
name="40pattern-star-final",
build_depends="dpkg",
manifest="sources-for: 'dp*'",
expected={"dpkg"},
),
TDefinition(
name="40pattern-star-empty",
build_depends="dpkg",
manifest="sources-for: 'dp*kg'",
expected={"dpkg"},
),
TDefinition(
name="40pattern-star-one-char",
build_depends="dpkg",
manifest="sources-for: 'dpk*g'",
expected={"dpkg"},
),
TDefinition(
name="40pattern-re-char-plus",
required_pkgs=("g++",),
build_depends="g++",
manifest="sources-for: g*+",
expected={"gcc-defaults"},
),
TDefinition(
name="40pattern-ambiguous-all",
required_pkgs=("libbinutils", "libc6"),
build_depends="""
Build-Depends-Arch: libbinutils
Build-Depends-Indep: libc6""",
architecture="all",
manifest="sources-for: 'lib*'",
expected={"glibc"},
),
TDefinition(
name="40pattern-ambiguous-any",
required_pkgs=("libbinutils", "libc6"),
build_depends="""
Build-Depends-Arch: libbinutils
Build-Depends-Indep: libc6""",
manifest="sources-for: 'lib*'",
expected={"binutils"},
),
TDefinition(
name="45multiple-matches",
required_pkgs=("dpkg-dev", "autotools-dev"),
build_depends="dpkg-dev, autotools-dev",
manifest="sources-for: '*-dev'",
expected={"autotools-dev", "dpkg"},
),
TDefinition(
name="50same-source",
required_pkgs=("dpkg-dev",),
build_depends="dpkg, dpkg-dev",
manifest="sources-for: dpkg, sources-for: dpkg-dev",
expected={"dpkg"},
),
TDefinition(
name="70arch-profile-restrictions-arch-yes",
build_depends="dpkg",
manifest="{sources-for: dpkg, when: {arch-matches: amd64}}",
expected={"dpkg"},
),
TDefinition(
name="70arch-profile-restrictions-arch-no",
build_depends="dpkg",
manifest="{sources-for: dpkg, when: {arch-matches: '!amd64'}}",
expected={},
),
TDefinition(
name="70arch-profile-restrictions-profile-no",
build_depends="dpkg",
manifest="{sources-for: dpkg, when: {build-profiles-matches: '<dummy>'}}",
expected={},
),
TDefinition(
name="70arch-suffix-all",
required_pkgs=("debhelper",),
build_depends="debhelper:all",
manifest="sources-for: debhelper",
expected={"debhelper"},
),
TDefinition(
name="70arch-suffix-native",
required_pkgs=("gcc",),
build_depends="gcc:amd64",
manifest="sources-for: gcc",
expected={"gcc-defaults"},
),
TDefinition(
name="70arch-suffix-same",
required_pkgs=("libc6",),
build_depends="libc6:amd64",
manifest="sources-for: libc6",
expected={"glibc"},
),
TDefinition(
name="70multiarch-no",
required_pkgs=("gcc",),
build_depends="gcc",
manifest="sources-for: gcc",
expected={"gcc-defaults"},
),
# 70multiarch-foreign would duplicate 01basic
TDefinition(
name="70multiarch-same",
required_pkgs=("libc6",),
build_depends="libc6",
manifest="sources-for: libc6",
expected={"glibc"},
),
TDefinition(
name="70multiarch-allowed",
required_pkgs=("make",),
build_depends="make",
manifest="sources-for: make",
expected={"make-dfsg"},
),
)
def _pkg_names_from_substvar(resolve_substvar: str) -> set[str]:
return set(
PKGNAME_REGEX.match(p.strip()).group(0) for p in resolve_substvar.split(",")
)
@pytest.mark.parametrize("test_definition", tests, ids=[t.name for t in tests])
def test_built_using(
manifest_parser_amd64_factory: ManifestParserFactory,
test_definition: TDefinition,
) -> None:
for p in test_definition.required_pkgs:
if not is_pkg_installed(p):
pytest.skip(f"The test requires the {p} package installed")
return
dctrl_contents = f"""\
Source: foo
Build-Depends: {test_definition.build_depends}
Package: foo
Architecture: {test_definition.architecture}
"""
manifest_contents = f"""\
manifest-version: '0.1'
packages:
foo:
{test_definition.field.lower()}: [{test_definition.manifest}]
"""
# A bit of debugging output in case the test fails
print(" --- d/control ---")
print(dctrl_contents)
print(" --- d/debputy.manifest ---")
print(manifest_contents)
manifest = manifest_parser_amd64_factory(
build_virtual_file_system(
(virtual_path_def("./control", content=dctrl_contents),),
)
).parse_manifest(fd=manifest_contents)
binary_package = manifest.package_state_for("foo").binary_package
assert binary_package.should_be_acted_on
substvars = (
initialize_plugin_under_test_preloaded(
1,
initialize_debputy_features,
plugin_name="debputy",
load_debputy_plugin=False,
)
.run_metadata_detector(
metadata_detector_id="built-using-relations",
fs_root=FSRootDir(),
context=PackageProcessingContextProvider(
manifest=manifest,
binary_package=binary_package,
related_udeb_package=None,
package_data_table=PackageDataTable({}),
),
)
.substvars
)
key = f"debputy:{test_definition.field}"
if test_definition.expected:
actual = _pkg_names_from_substvar(substvars[key])
assert actual == test_definition.expected
else:
assert key not in substvars
|