Skip to content
Snippets Groups Projects
conftest.py 2.02 KiB
Newer Older
Simon Rose's avatar
Simon Rose committed
from pathlib import Path
Simon Rose's avatar
Simon Rose committed
from random import choice
from string import ascii_lowercase
Simon Rose's avatar
Simon Rose committed

import pytest
from git import Repo


Simon Rose's avatar
Simon Rose committed
class Wrapper:
    def __init__(self, root_path: Path, name=None, include_dbd=True, **kwargs):
        if name is None:
            name = "test_mod_" + "".join(choice(ascii_lowercase) for _ in range(16))
        self.path = Path(root_path / f"e3-{name}")
        self.name = name
Simon Rose's avatar
Simon Rose committed

Simon Rose's avatar
Simon Rose committed
        module_path = (
            name if "E3_MODULE_SRC_PATH" not in kwargs else kwargs["E3_MODULE_SRC_PATH"]
        )
        self.module_dir = self.path / module_path
        self.module_dir.mkdir(parents=True)
Simon Rose's avatar
Simon Rose committed
        self.makefile = self.path / f"{name}.Makefile"

        makefile = f"""
Simon Rose's avatar
Simon Rose committed
TOP:=$(CURDIR)

Simon Rose's avatar
Simon Rose committed
E3_MODULE_NAME:={name}
E3_MODULE_SRC_PATH:={module_path}
E3_MODULE_MAKEFILE:={name}.Makefile

Simon Rose's avatar
Simon Rose committed
include $(REQUIRE_CONFIG)/CONFIG
include $(REQUIRE_CONFIG)/RULES_SITEMODS
"""
Simon Rose's avatar
Simon Rose committed
        with open(self.path / "Makefile", "w") as f:
            f.write(makefile)
Simon Rose's avatar
Simon Rose committed

Simon Rose's avatar
Simon Rose committed
        module_makefile = """
Simon Rose's avatar
Simon Rose committed
where_am_I := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
include $(E3_REQUIRE_TOOLS)/driver.makefile
"""
Simon Rose's avatar
Simon Rose committed
        with open(self.path / f"{name}.Makefile", "w") as f:
            f.write(module_makefile)
Simon Rose's avatar
Simon Rose committed

Simon Rose's avatar
Simon Rose committed
        if include_dbd:
            self.add_file("test.dbd")
Simon Rose's avatar
Simon Rose committed
        Repo.init(self.path)
Simon Rose's avatar
Simon Rose committed

Simon Rose's avatar
Simon Rose committed
    def add_file(self, name, makefile_var=None, add_file=True):
        if add_file:
            (self.module_dir / name).touch()
        if makefile_var:
            with open(self.makefile, "a") as f:
                f.write(f"{makefile_var} += {name}")
Simon Rose's avatar
Simon Rose committed

@pytest.fixture
def wrappers(tmpdir, request):
    class WrapperFactory:
        def get(self):
            """
            Sets up a wrapper with the minimal necessary configuration in order to build a module

            Note that a number of necessary variables are expected to be present in the environment
            """
            temp_wrapper = Wrapper(tmpdir)
            return temp_wrapper.path

    yield WrapperFactory()


@pytest.fixture
def wrapper(wrappers):
    yield wrappers.get()