From 7e63c2e7fabaabf8a5e4545a1a56c10550f03792 Mon Sep 17 00:00:00 2001
From: Simon Rose <simon.rose@ess.eu>
Date: Mon, 29 Nov 2021 17:15:13 +0100
Subject: [PATCH] Added test for header installation

---
 tests/test_build.py | 32 ++++++++++++++++++++++++++++----
 tests/test_e3.py    |  6 +++---
 tests/utils.py      | 17 ++++++++++++++++-
 3 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/tests/test_build.py b/tests/test_build.py
index 477f02c6..2303daa6 100644
--- a/tests/test_build.py
+++ b/tests/test_build.py
@@ -1,8 +1,9 @@
 import re
+from pathlib import Path
 
 import pytest
 
-from .utils import TEST_MODULE_NAME, run_make
+from .utils import TEST_MODULE_NAME, get_env_var, run_make
 
 MODULE_VERSION = "0.0.0+0"
 MODULE_VERSION_NO_BUILD = "0.0.0"
@@ -56,7 +57,7 @@ def test_patch(wrapper):
         E3_MODULE_VERSION=MODULE_VERSION,
     )
     assert rc == 0
-    assert "You are in the local source mode" in outs.decode("utf-8")
+    assert "You are in the local source mode" in outs
 
     rc, _, _ = run_make(wrapper, "patch", E3_MODULE_VERSION=MODULE_VERSION)
     assert rc == 0
@@ -85,7 +86,7 @@ def test_missing_dbd_file(wrapper):
     assert rc == 2
     assert re.search(
         RE_MISSING_FILE.format(filename=re.escape("../nonexistent.dbd")),
-        errs.decode("utf-8"),
+        errs,
     )
 
 
@@ -100,7 +101,7 @@ def test_missing_source_file(wrapper):
     assert rc == 2
     assert re.search(
         RE_MISSING_FILE.format(filename=re.escape("nonexistent.o")),
-        errs.decode("utf-8"),
+        errs,
     )
 
 
@@ -111,3 +112,26 @@ def test_missing_requirement(wrapper):
     rc, _, errs = run_make(wrapper, "build")
     assert rc == 2
     assert "REQUIRED module 'foo' version '' does not exist" in errs.decode("utf-8")
+
+
+def test_header_install_location(wrapper):
+    subdir = wrapper / TEST_MODULE_NAME / "db" / "subdir"
+    subdir.mkdir(parents=True)
+
+    extensions = ["h", "hpp", "hxx", "hh"]
+    with open(wrapper / f"{TEST_MODULE_NAME}.Makefile", "a") as f:
+        for ext in extensions:
+            f.write(f"HEADERS += db/subdir/header.{ext}\n")
+        f.write("KEEP_HEADER_SUBDIRS += db\n")
+
+    for extension in extensions:
+        with open(subdir / f"header.{extension}", "w") as f:
+            pass
+
+    rc, _, _ = run_make(wrapper, "cellinstall")
+    assert rc == 0
+
+    cell_path = get_env_var(wrapper, "E3_MODULES_INSTALL_LOCATION")
+
+    for ext in extensions:
+        assert (Path(cell_path) / "include" / "subdir" / f"header.{ext}").is_file()
diff --git a/tests/test_e3.py b/tests/test_e3.py
index d1cfa8b8..4784ee96 100644
--- a/tests/test_e3.py
+++ b/tests/test_e3.py
@@ -11,7 +11,7 @@ from .utils import run_make
 def test_loc(wrapper):
     rc, _, errs = run_make(wrapper, "build", E3_MODULE_SRC_PATH="test-loc")
     assert rc == 2
-    assert 'Local source mode "-loc" has been deprecated' in errs.decode("utf-8")
+    assert 'Local source mode "-loc" has been deprecated' in errs
 
 
 def test_sitelibs(wrapper):
@@ -34,11 +34,11 @@ include $(REQUIRE_CONFIG)/RULES_DEV
         )
     rc, _, errs = run_make(wrapper, "build")
     assert rc == 2
-    assert "RULES_E3 should only be loaded from RULES_SITEMODS" in errs.decode("utf-8")
+    assert "RULES_E3 should only be loaded from RULES_SITEMODS" in errs
 
 
 def test_incorrect_module_name(wrapper):
     module_name = "ADCore"
     rc, _, errs = run_make(wrapper, "build", E3_MODULE_NAME=module_name)
     assert rc == 2
-    assert f"E3_MODULE_NAME '{module_name}' is not valid" in errs.decode("utf-8")
+    assert f"E3_MODULE_NAME '{module_name}' is not valid" in errs
diff --git a/tests/utils.py b/tests/utils.py
index 14b69171..8b23542f 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -49,12 +49,27 @@ def run_make(path, *args, **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
+        make_cmd,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE,
+        env=test_env,
+        encoding="utf-8",
     )
     outs, errs = p.communicate()
     return p.returncode, outs, errs
 
 
+def get_env_var(path, var: str):
+    """
+    Fetch an environment variable from the module build environment
+    """
+    _, out, _ = run_make(path, "cellvars")
+    for line in out.split("\n"):
+        if line.startswith(var):
+            return line.split("=")[1].strip()
+    assert False
+
+
 def run_ioc_get_output(version, cell_path):
     """
     Run an IOC and try to load the test module
-- 
GitLab