Skip to content

Commit

Permalink
fixup! use importlib instead of a symlink (thanks to Simon!)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Nov 6, 2023
1 parent 95956e5 commit 449fddd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
Empty file added osbuild/testutil/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions osbuild/testutil/imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/python3
"""
Test related utilities
"""
import importlib
from types import ModuleType


def import_module_from_path(fullname, path: str) -> ModuleType:
"""import_module_from_path imports the given path as a python module
This helper is useful when importing things that are not in the
import path or have invalid python import filenames, e.g. all
filenames in the stages/ dir of osbuild.
Keyword arguments:
fullname -- The absolute name of the module (can be arbitrary, used on in ModuleSpec.name)
path -- The full path to the python file
"""
loader = importlib.machinery.SourceFileLoader(fullname, path)
spec = importlib.util.spec_from_loader(loader.name, loader)
if spec is None:
# mypy warns that spec might be None so handle it
raise ImportError(f"cannot import {fullname} from {path}, got None as the spec")
mod = importlib.util.module_from_spec(spec)
loader.exec_module(mod)
return mod
1 change: 0 additions & 1 deletion stages/test/org_osbuild_kickstart.py

This file was deleted.

8 changes: 6 additions & 2 deletions stages/test/test_kickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from dataclasses import dataclass

import pytest
from org_osbuild_kickstart import main as kickstart_main

from osbuild.testutil.imports import import_module_from_path


@dataclass
Expand Down Expand Up @@ -40,12 +41,15 @@ def ks_test_cases_fixtures():


def test_kickstart(ks_test_cases):
ks_stage_path = os.path.join(os.path.dirname(__file__), "../org.osbuild.kickstart")
ks_stage = import_module_from_path("ks_stage", ks_stage_path)

ks_path = "kickstart/kfs.cfg"
with tempfile.TemporaryDirectory("kickstart-test-") as tmpdir:
for tc in ks_test_cases:
options = {"path": ks_path}
options.update(tc.options)
kickstart_main(tmpdir, options)
ks_stage.main(tmpdir, options)

with open(os.path.join(tmpdir, ks_path), encoding="utf-8") as fp:
ks_content = fp.read()
Expand Down
17 changes: 17 additions & 0 deletions test/mod/test_testutil_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Tests for the 'osbuild.util.testutil' module.
#
import os.path
import tempfile

import pytest

from osbuild.testutil.imports import import_module_from_path


canary = "import-went-okay"


def test_import_module_from_path_happy():
mod = import_module_from_path("myself", __file__)
assert mod.canary == "import-went-okay"

0 comments on commit 449fddd

Please sign in to comment.