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
|
import os
import typing
from collections.abc import Mapping
import pytest
from debian.deb822 import Deb822
from debian.debian_support import DpkgArchTable
from debputy import DEBPUTY_PLUGIN_ROOT_DIR
from debputy._deb_options_profiles import DebBuildOptionsAndProfiles
from debputy.architecture_support import (
DpkgArchitectureBuildProcessValuesTable,
faked_arch_table,
)
from debputy.exceptions import PureVirtualPathError
from debputy.filesystem_scan import OSFSROOverlay
from debputy.highlevel_manifest_parser import YAMLManifestParser
from debputy.manifest_parser.util import AttributePath
from debputy.packages import BinaryPackage, SourcePackage, DctrlParser
from debputy.plugin.api import VirtualPath
from debputy.plugin.api.feature_set import PluginProvidedFeatureSet
from debputy.plugin.api.impl import (
DebputyPluginInitializerProvider,
_resolve_bundled_plugin_docs_path,
find_json_plugin,
)
from debputy.plugin.api.impl_types import (
DebputyPluginMetadata,
)
from debputy.plugins.debputy.debputy_plugin import initialize_debputy_features
from debputy.substitution import (
NULL_SUBSTITUTION,
Substitution,
SubstitutionImpl,
VariableContext,
)
# Disable dpkg's translation layer. It is very slow and disabling it makes it easier to debug
# test-failure reports from systems with translations active.
os.environ["DPKG_NLS"] = "0"
@pytest.fixture(scope="session")
def amd64_dpkg_architecture_variables() -> DpkgArchitectureBuildProcessValuesTable:
return faked_arch_table("amd64")
@pytest.fixture(scope="session")
def dpkg_arch_query() -> DpkgArchTable:
return DpkgArchTable.load_arch_table()
@pytest.fixture()
def source_package() -> SourcePackage:
return SourcePackage(
{
"Source": "foo",
}
)
@pytest.fixture()
def package_single_foo_arch_all_cxt_amd64(
amd64_dpkg_architecture_variables,
dpkg_arch_query,
) -> Mapping[str, BinaryPackage]:
return {
p.name: p
for p in [
BinaryPackage(
Deb822(
{
"Package": "foo",
"Architecture": "all",
}
),
amd64_dpkg_architecture_variables,
dpkg_arch_query,
is_main_package=True,
)
]
}
@pytest.fixture()
def package_foo_w_udeb_arch_any_cxt_amd64(
amd64_dpkg_architecture_variables,
dpkg_arch_query,
) -> Mapping[str, BinaryPackage]:
return {
p.name: p
for p in [
BinaryPackage(
Deb822(
{
"Package": "foo",
"Architecture": "any",
}
),
amd64_dpkg_architecture_variables,
dpkg_arch_query,
is_main_package=True,
),
BinaryPackage(
Deb822(
{
"Package": "foo-udeb",
"Architecture": "any",
"Package-Type": "udeb",
}
),
amd64_dpkg_architecture_variables,
dpkg_arch_query,
),
]
}
@pytest.fixture(scope="session")
def null_substitution() -> Substitution:
return NULL_SUBSTITUTION
@pytest.fixture(scope="function")
def _empty_debputy_plugin_feature_set() -> PluginProvidedFeatureSet:
return PluginProvidedFeatureSet()
@pytest.fixture(scope="function")
def amd64_substitution(
amd64_dpkg_architecture_variables,
_empty_debputy_plugin_feature_set,
) -> Substitution:
debian_dir = OSFSROOverlay.create_root_dir("debian", "debian")
variable_context = VariableContext(
debian_dir,
)
return SubstitutionImpl(
plugin_feature_set=_empty_debputy_plugin_feature_set,
dpkg_arch_table=amd64_dpkg_architecture_variables,
static_variables=None,
environment={},
unresolvable_substitutions=frozenset(["SOURCE_DATE_EPOCH", "PACKAGE"]),
variable_context=variable_context,
)
@pytest.fixture(scope="session")
def no_profiles_or_build_options() -> DebBuildOptionsAndProfiles:
return DebBuildOptionsAndProfiles(environ={})
@pytest.fixture(scope="function")
def debputy_plugin_feature_set(
request,
_empty_debputy_plugin_feature_set,
amd64_substitution,
) -> PluginProvidedFeatureSet:
plugin_name = "debputy"
debputy_plugin_metadata = DebputyPluginMetadata(
plugin_name=plugin_name,
api_compat_version=1,
plugin_initializer=initialize_debputy_features,
plugin_doc_path_resolver=lambda: _resolve_bundled_plugin_docs_path(
plugin_name,
initialize_debputy_features,
),
plugin_loader=None,
plugin_path="<loaded-via-test>",
)
feature_set = _empty_debputy_plugin_feature_set
api = DebputyPluginInitializerProvider(
debputy_plugin_metadata,
feature_set,
amd64_substitution,
)
api.load_plugin()
plugin_names = getattr(request.function, "_required_plugins", [])
if plugin_names:
for plugin_name in plugin_names:
plugin_metadata = find_json_plugin([DEBPUTY_PLUGIN_ROOT_DIR], plugin_name)
print(
f"debputy_plugin_feature_set - Loading extra plugin: {plugin_name} [{plugin_metadata.plugin_path}]"
)
plugin_under_test_provider = DebputyPluginInitializerProvider(
plugin_metadata,
feature_set,
amd64_substitution,
)
plugin_under_test_provider.load_plugin()
return feature_set
@pytest.fixture
def attribute_path(request) -> AttributePath:
return AttributePath.builtin_path()[request.node.nodeid]
class ManifestParserFactory(typing.Protocol):
def __call__(
self,
debian_dir: VirtualPath,
*,
dctrl_parser: DctrlParser | None = None,
) -> YAMLManifestParser: ...
@pytest.fixture()
def manifest_parser_amd64_factory(
amd64_dpkg_architecture_variables,
dpkg_arch_query,
amd64_substitution,
no_profiles_or_build_options,
debputy_plugin_feature_set,
) -> ManifestParserFactory:
def _factory(
debian_dir: VirtualPath,
*,
dctrl_parser: DctrlParser | None = None,
) -> YAMLManifestParser:
if dctrl_parser is None:
dctrl_parser = DctrlParser(
frozenset(),
frozenset(),
False,
False,
amd64_dpkg_architecture_variables,
dpkg_arch_query,
no_profiles_or_build_options,
)
dctrl = debian_dir.get("control")
try:
if dctrl is None:
raise PureVirtualPathError()
with dctrl.open() as fd:
_, source_package, binary_packages = (
dctrl_parser.parse_source_debian_control(fd.readlines())
)
except PureVirtualPathError:
raise ValueError(
"The manifest_parser_amd64_factory requires `control` to be present and readable in the `debian_dir`"
) from None
return YAMLManifestParser(
"debian/test-debputy.manifest",
source_package,
binary_packages,
amd64_substitution,
# To align with the substitution, we do not allow this field to
# be overwritten by the DctrlParser
amd64_dpkg_architecture_variables,
# Use the same instances
dctrl_parser.dpkg_arch_query_table,
dctrl_parser.deb_options_and_profiles,
debputy_plugin_feature_set,
"full",
debian_dir=debian_dir,
)
return _factory
|