Skip to content
Snippets Groups Projects
Commit dabc8c1b authored by Simon Rose's avatar Simon Rose
Browse files

Trying to re-write conftest

parent a34932b0
No related branches found
No related tags found
No related merge requests found
from pathlib import Path
from random import choice
from string import ascii_lowercase
import pytest
from git import Repo
from .utils import TEST_MODULE_NAME
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
@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
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)
Note that a number of necessary variables are expected to be present in the environment
"""
try:
params = request.param
except AttributeError:
params = {}
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
test_dir.mkdir(parents=True)
config_file = """
self.makefile = self.path / f"{name}.Makefile"
makefile = f"""
TOP:=$(CURDIR)
E3_MODULE_NAME:={name}
E3_MODULE_SRC_PATH:={module_path}
E3_MODULE_MAKEFILE:={name}.Makefile
include $(REQUIRE_CONFIG)/CONFIG
include $(REQUIRE_CONFIG)/RULES_SITEMODS
"""
with open(self.path / "Makefile", "w") as f:
f.write(makefile)
module_makefile = """
module_makefile = """
where_am_I := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
include $(E3_REQUIRE_TOOLS)/driver.makefile
TEMPLATES += {templates}
SOURCES += {sources}
DBDS += {dbds}
HEADERS += {headers}
"""
with open(self.path / f"{name}.Makefile", "w") as f:
f.write(module_makefile)
# TODO: This should not be necessary, but it is for patching.
Repo.init(wrapper_dir)
with open(wrapper_dir / "Makefile", "w") as f:
f.write(config_file)
if include_dbd:
self.add_file("test.dbd")
make_vars = {"templates": "", "sources": "", "dbds": "", "headers": ""}
make_vars.update(**params)
with open(wrapper_dir / f"{TEST_MODULE_NAME}.Makefile", "w") as f:
f.write(module_makefile.format(**make_vars))
Repo.init(self.path)
with open(wrapper_dir / TEST_MODULE_PATH / "test.dbd", "w") as f:
pass
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}")
return wrapper_dir
@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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment