Newer
Older
import os
import subprocess
import time
from pathlib import Path
from random import choice
from string import ascii_lowercase
# Random module name
TEST_MODULE_NAME = "test_mod_" + "".join(choice(ascii_lowercase) for _ in range(16))
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
EPICS_BASE = os.environ.get("EPICS_BASE")
E3_REQUIRE_VERSION = os.environ.get("E3_REQUIRE_VERSION")
CONFIG = f"""
TOP:=$(CURDIR)
# To configure require
EPICS_BASE:=$(__EPICS_BASE_LOCATION)
E3_REQUIRE_VERSION:=$(__REQUIRE_VERSION)
E3_REQUIRE_LOCATION := $(EPICS_BASE)/require/$(E3_REQUIRE_VERSION)
REQUIRE_CONFIG := $(E3_REQUIRE_LOCATION)/configure
# To configure the modules
EPICS_MODULE_NAME:={TEST_MODULE_NAME}
E3_MODULE_VERSION:=$(__DEBUG_VERSION)
E3_MODULE_NAME:=$(EPICS_MODULE_NAME)
E3_MODULE_SRC_PATH:=$(EPICS_MODULE_NAME)
E3_MODULE_MAKEFILE:=$(EPICS_MODULE_NAME).Makefile
include $(REQUIRE_CONFIG)/CONFIG
include $(REQUIRE_CONFIG)/RULES_SITEMODS
"""
MODULE_MAKEFILE = """
where_am_I := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
include $(E3_REQUIRE_TOOLS)/driver.makefile
TEMPLATES += {templates}
SOURCES += {sources}
DBDS += {dbds}
HEADERS += {headers}
"""
def run_make(path, *args, **kwargs):
test_env = os.environ.copy()
test_env["__EPICS_BASE_LOCATION"] = EPICS_BASE
test_env["__REQUIRE_VERSION"] = E3_REQUIRE_VERSION
for kw in kwargs:
test_env[kw] = kwargs[kw]
make_cmd = ["make", "-C", path] + list(args)
p = subprocess.Popen(
make_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=test_env
)
outs, errs = p.communicate()
return p.returncode, outs, errs
def run_ioc_get_output(version, cell_path):
with IOC("-r", f"{TEST_MODULE_NAME},{version}", "-l", cell_path) as ioc:
time.sleep(1)
return ioc.proc.returncode, ioc.outs, ioc.errs
@pytest.fixture
def wrapper(tmpdir, request):
wrapper_dir = Path(tmpdir / "wrapper")
test_dir = wrapper_dir / TEST_MODULE_NAME
test_dir.mkdir(parents=True)
# 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)
with open(wrapper_dir / f"{TEST_MODULE_NAME}.Makefile", "w") as f:
f.write(MODULE_MAKEFILE.format(**request.param))
with open(wrapper_dir / TEST_MODULE_NAME / "test.dbd", "w") as f:
f.write("")
yield wrapper_dir