From 73bbc0738068c47c244dacc1dc682b5b953dfab7 Mon Sep 17 00:00:00 2001
From: Simon Rose <simon.rose@ess.eu>
Date: Thu, 13 Jan 2022 09:51:10 +0100
Subject: [PATCH] Removed E3_CELL_PATH environment override

This was needed since we had to have a way for the test suites to override this value. However,
this can also be acheived by writing to configure/CONFIG_CELL.local. This was added in a
refactoring of run_make to make more clear what variables can be passed there.
---
 configure/E3/CONFIG_CELL |  2 +-
 tests/conftest.py        |  8 ++++++++
 tests/test_build.py      | 20 ++++++++++----------
 tests/test_e3.py         |  2 +-
 tests/test_versions.py   |  2 +-
 tests/utils.py           | 11 ++++++++---
 6 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/configure/E3/CONFIG_CELL b/configure/E3/CONFIG_CELL
index 742bd076..4dafb395 100644
--- a/configure/E3/CONFIG_CELL
+++ b/configure/E3/CONFIG_CELL
@@ -2,7 +2,7 @@
 # TOP is e3-MODULENAME
 ifneq (,$(findstring cell,$(MAKECMDGOALS)))
 ## Default is e3-MODULENAME/cellMods
-E3_CELL_PATH?=$(TOP)/cellMods
+E3_CELL_PATH=$(TOP)/cellMods
 ##
 ## Allow local file in $(TOP)/configure
 ##       local file in $(TOP)/../
diff --git a/tests/conftest.py b/tests/conftest.py
index 3e98fcef..73ecb72b 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -19,6 +19,9 @@ class Wrapper:
         self.module_dir = self.path / module_path
         self.module_dir.mkdir(parents=True)
 
+        self.config_dir = self.path / "configure"
+        self.config_dir.mkdir()
+
         self.makefile = self.path / f"{name}.Makefile"
 
         makefile_contents = f"""
@@ -50,6 +53,11 @@ include $(E3_REQUIRE_TOOLS)/driver.makefile
     def add_file(self, name):
         (self.module_dir / name).touch()
 
+    def write_local_data(self, config_file, **kwargs):
+        with open(self.config_dir / f"{config_file}.local", "w") as f:
+            for var, value in kwargs.items():
+                f.write(f"{var} = {value}\n")
+
     def add_var_to_makefile(self, makefile_var, value, modifier="+"):
         with open(self.makefile, "a") as f:
             f.write(f"{makefile_var} {modifier}= {value}\n")
diff --git a/tests/test_build.py b/tests/test_build.py
index a7932e64..3427b3c5 100644
--- a/tests/test_build.py
+++ b/tests/test_build.py
@@ -45,23 +45,23 @@ def test_patch(wrapper):
     rc, outs, _ = run_make(
         wrapper,
         "init",
-        E3_MODULE_VERSION=MODULE_VERSION,
+        module_version=MODULE_VERSION,
     )
     assert rc == 0
     assert "You are in the local source mode" in outs
 
-    rc, _, _ = run_make(wrapper, "patch", E3_MODULE_VERSION=MODULE_VERSION)
+    rc, _, _ = run_make(wrapper, "patch", module_version=MODULE_VERSION)
     assert rc == 0
     with open(db_path, "r") as f:
         db_contents = f.read()
     assert 'field(DESC, "OK")' in db_contents
     assert "Bad" not in db_contents
 
-    rc, _, _ = run_make(wrapper, "build", E3_MODULE_VERSION=MODULE_VERSION)
+    rc, _, _ = run_make(wrapper, "build", module_version=MODULE_VERSION)
     assert rc == 0
     assert any((wrapper.module_dir).glob("O.*"))
 
-    rc, _, _ = run_make(wrapper, "cellinstall", E3_MODULE_VERSION=MODULE_VERSION)
+    rc, _, _ = run_make(wrapper, "cellinstall", module_version=MODULE_VERSION)
     assert rc == 0
     assert any((wrapper.path / "cellMods").glob("**/*.db"))
 
@@ -134,12 +134,12 @@ def test_updated_dependencies(wrappers):
     rc, *_ = run_make(
         wrapper_dep,
         "cellinstall",
-        E3_CELL_PATH=cell_path,
-        E3_MODULE_VERSION=old_version,
+        module_version=old_version,
+        cell_path=cell_path,
     )
     assert rc == 0
 
-    rc, *_ = run_make(wrapper_main, "cellinstall", E3_MODULE_VERSION=old_version)
+    rc, *_ = run_make(wrapper_main, "cellinstall", module_version=old_version)
     assert rc == 0
 
     new_version = "1.0.0+0"
@@ -147,8 +147,8 @@ def test_updated_dependencies(wrappers):
     rc, *_ = run_make(
         wrapper_dep,
         "cellinstall",
-        E3_CELL_PATH=cell_path,
-        E3_MODULE_VERSION=new_version,
+        module_version=new_version,
+        cell_path=cell_path,
     )
     assert rc == 0
 
@@ -156,7 +156,7 @@ def test_updated_dependencies(wrappers):
         f"{wrapper_dep.name}_VERSION", new_version, modifier=""
     )
 
-    rc, *_ = run_make(wrapper_main, "cellinstall", E3_MODULE_VERSION=new_version)
+    rc, *_ = run_make(wrapper_main, "cellinstall", module_version=new_version)
     assert rc == 0
 
     rc, o, _ = run_ioc_get_output(
diff --git a/tests/test_e3.py b/tests/test_e3.py
index a02e1672..8de05489 100644
--- a/tests/test_e3.py
+++ b/tests/test_e3.py
@@ -39,6 +39,6 @@ def test_incorrect_module_name(wrappers):
     module_name = "ADCore"
 
     wrapper = wrappers.get(name=module_name)
-    rc, _, errs = run_make(wrapper, "build", E3_MODULE_NAME=module_name)
+    rc, _, errs = run_make(wrapper, "build", module_name=module_name)
     assert rc == 2
     assert f"E3_MODULE_NAME '{module_name}' is not valid" in errs
diff --git a/tests/test_versions.py b/tests/test_versions.py
index f8d1b7ba..4866df72 100644
--- a/tests/test_versions.py
+++ b/tests/test_versions.py
@@ -40,7 +40,7 @@ def test_version(wrapper, requested, expected, installed):
             wrapper,
             "clean",
             "cellinstall",
-            E3_MODULE_VERSION=version,
+            module_version=version,
         )
         assert returncode == 0
 
diff --git a/tests/utils.py b/tests/utils.py
index de790a3d..17e06803 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -3,6 +3,7 @@ import subprocess
 import time
 from pathlib import Path
 
+from .conftest import Wrapper
 from run_iocsh import IOC
 
 EPICS_BASE = os.environ.get("EPICS_BASE")
@@ -28,13 +29,17 @@ def set_env_vars(env: dict) -> dict:
     return env
 
 
-def run_make(wrapper, *args, **kwargs):
+def run_make(wrapper: Wrapper, *args, module_name: str = "", module_version: str = "", cell_path: str=""):
     """
     Attempt to run `make <args>` in the specified path with <kwargs> as environment variables
     """
     test_env = set_env_vars(os.environ.copy())
-    for kw in kwargs:
-        test_env[kw] = kwargs[kw]
+    if module_name:
+        test_env["E3_MODULE_NAME"] = module_name
+    if module_version:
+        test_env["E3_MODULE_VERSION"] = module_version
+    if cell_path:
+        wrapper.write_local_data("CONFIG_CELL", E3_CELL_PATH=cell_path)
     make_cmd = ["make", "-C", wrapper.path] + list(args)
     p = subprocess.Popen(
         make_cmd,
-- 
GitLab