tests: add tests for Arch and Chroot types (MR 2252)

Add some exhaustive unit testing to validate that these types behave as
expected.

And fix a few bugs uncovered by the tests.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-06-13 04:35:10 +02:00 committed by Oliver Smith
parent 6087a9df8f
commit ca722b499e
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
8 changed files with 334 additions and 57 deletions

View file

@ -1,50 +1,55 @@
import os
from pathlib import Path
import pytest
from contextlib import contextmanager
import shutil
@contextmanager
def _fixture_context(val):
yield val
import pmb.core
from pmb.types import PmbArgs
from pmb.helpers.args import init as init_args
@pytest.fixture(scope="session")
def config_file_session(tmp_path_factory):
_testdir = Path(__file__).parent / "data/tests"
@pytest.fixture
def config_file(tmp_path_factory):
"""Fixture to create a temporary pmbootstrap.cfg file."""
tmp_path = tmp_path_factory.mktemp("pmbootstrap")
file = tmp_path / "pmbootstrap.cfg"
out_file = tmp_path / "pmbootstrap.cfg"
workdir = tmp_path / "work"
workdir.mkdir()
contents = """[pmbootstrap]
build_default_device_arch = True
ccache_size = 5G
device = qemu-amd64
extra_packages = neofetch,neovim,reboot-mode
hostname = qemu-amd64
is_default_channel = False
jobs = 8
kernel = edge
locale = C.UTF-8
ssh_keys = True
sudo_timer = True
systemd = always
timezone = Europe/Berlin
ui = gnome
work = {0}
[providers]
file = _testdir / "pmbootstrap.cfg"
contents = open(file).read().format(workdir)
[mirrors]
""".format(workdir)
open(file, "w").write(contents)
return file
open(out_file, "w").write(contents)
return out_file
@pytest.fixture
def config_file(config_file_session):
"""Fixture to create a temporary pmbootstrap.cfg file."""
with _fixture_context(config_file_session) as val:
yield val
def device_package(config_file):
"""Fixture to create a temporary deviceinfo file."""
MOCK_DEVICE = "qemu-amd64"
pkgdir = config_file.parent / f"device-{MOCK_DEVICE}"
pkgdir.mkdir()
for file in ["APKBUILD", "deviceinfo"]:
shutil.copy(_testdir / f"{file}.{MOCK_DEVICE}",
pkgdir / file)
return pkgdir
@pytest.fixture
def mock_devices_find_path(device_package, monkeypatch):
"""Fixture to mock pmb.helpers.devices.find_path()"""
def mock_find_path(device, file=''):
print(f"mock_find_path({device}, {file})")
out = device_package / file
if not out.exists():
return None
return out
monkeypatch.setattr("pmb.helpers.devices.find_path", mock_find_path)
@pytest.fixture(autouse=True)
@ -67,30 +72,44 @@ def setup_mock_ask(monkeypatch):
monkeypatch.setattr(pmb.helpers.cli, "ask", mock_ask)
# FIXME: get/set_context() is a bad hack :(
@pytest.fixture
def mock_context(monkeypatch):
"""Mock set_context() to bypass sanity checks. Ideally we would
mock get_context() as well, but since every submodule of pmb imports
it like "from pmb.core.context import get_context()", we can't
actually override it with monkeypatch.setattr(). So this is the
best we can do... set_context() is only called from one place and is
done so with the full namespace, so this works."""
def mock_set_context(ctx):
print(f"mock_set_context({ctx})")
setattr(pmb.core.context, "__context", ctx)
monkeypatch.setattr("pmb.core.context.set_context", mock_set_context)
# FIXME: get_context() at runtime somehow doesn't return the
# custom context we set up here.
# @pytest.fixture(scope="session")
# def pmb_args(config_file_session):
# """This is (still) a hack, since a bunch of the codebase still
# expects some global state to be initialised. We do that here."""
@pytest.fixture
def pmb_args(config_file, mock_context):
"""This is (still) a hack, since a bunch of the codebase still
expects some global state to be initialised. We do that here."""
# from pmb.types import PmbArgs
# from pmb.helpers.args import init as init_args
args = PmbArgs()
args.config = config_file
args.aports = None
args.timeout = 900
args.details_to_stdout = False
args.quiet = False
args.verbose = False
args.offline = False
args.action = "init"
args.cross = False
args.log = Path()
# args = PmbArgs()
# args.config = config_file_session
# args.aports = None
# args.timeout = 900
# args.details_to_stdout = False
# args.quiet = False
# args.verbose = False
# args.offline = False
# args.action = "init"
# args.cross = False
# args.log = Path()
# print("init_args")
# return init_args(args)
print("init_args")
init_args(args)
@pytest.fixture
def foreign_arch():