Skip to content
Snippets Groups Projects
conftest.py 1.51 KiB
Newer Older
Simon Rose's avatar
Simon Rose committed
from pathlib import Path

import pytest
from git import Repo

from .utils import TEST_MODULE_NAME

Simon Rose's avatar
Simon Rose committed

@pytest.fixture
def wrapper(tmpdir, request):
    """
    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
    """
    try:
        params = request.param
    except AttributeError:
        params = {}

Simon Rose's avatar
Simon Rose committed
    wrapper_dir = Path(tmpdir / "wrapper")
    TEST_MODULE_PATH = (
        TEST_MODULE_NAME
        if "E3_MODULE_SRC_PATH" not in params
        else params["E3_MODULE_SRC_PATH"]
    )
    test_dir = wrapper_dir / TEST_MODULE_PATH
Simon Rose's avatar
Simon Rose committed
    test_dir.mkdir(parents=True)

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

include $(REQUIRE_CONFIG)/CONFIG
include $(REQUIRE_CONFIG)/RULES_SITEMODS
"""

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

TEMPLATES += {templates}
SOURCES += {sources}
DBDS += {dbds}
HEADERS += {headers}
"""

    # TODO: This should not be necessary, but it is for patching.
    Repo.init(wrapper_dir)
    with open(wrapper_dir / "Makefile", "w") as f:
Simon Rose's avatar
Simon Rose committed
        f.write(config_file)
Simon Rose's avatar
Simon Rose committed

    make_vars = {"templates": "", "sources": "", "dbds": "", "headers": ""}
    make_vars.update(**params)
Simon Rose's avatar
Simon Rose committed
    with open(wrapper_dir / f"{TEST_MODULE_NAME}.Makefile", "w") as f:
Simon Rose's avatar
Simon Rose committed
        f.write(module_makefile.format(**make_vars))

    with open(wrapper_dir / TEST_MODULE_PATH / "test.dbd", "w") as f:
Simon Rose's avatar
Simon Rose committed
        pass
Simon Rose's avatar
Simon Rose committed
    yield wrapper_dir