From 9e5c2556b340b16725c4ad9ac3f93ffce28cf68b Mon Sep 17 00:00:00 2001
From: Simon Rose <simon.rose@ess.eu>
Date: Tue, 6 Sep 2022 14:45:40 +0200
Subject: [PATCH] E3-731: Add tests for double installs

---
 require-ess/tools/driver.makefile | 11 +++----
 tests/test_build.py               | 49 +++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 5 deletions(-)

diff --git a/require-ess/tools/driver.makefile b/require-ess/tools/driver.makefile
index c439a9b1..eb811fa2 100644
--- a/require-ess/tools/driver.makefile
+++ b/require-ess/tools/driver.makefile
@@ -401,13 +401,14 @@ else
 
 ifeq ($(shell echo "${LIBVERSION}" | grep -v -E "^$(VERSIONREGEX)\$$"),)
 install:: build
-	@test ! -d ${MODULE_LOCATION}/lib/${T_A} || \
-        (echo -e "Error: ${MODULE_LOCATION}/lib/${T_A} already exists.\nNote: If you really want to overwrite then uninstall first."; false)
+	$(if $(wildcard ${MODULE_LOCATION}/lib/${T_A}),$(error ${MODULE_LOCATION}/lib/${T_A} already exists. If you really want to overwrite then uninstall first.))
+
 else
 install:: build
-	@test ! -d ${MODULE_LOCATION}/lib/${T_A} || \
-        (echo -e "Warning: Re-installing ${MODULE_LOCATION}/lib/${T_A}"; \
-        $(RMDIR) ${MODULE_LOCATION}/lib/${T_A})
+	$(if $(wildcard ${MODULE_LOCATION}/lib/${T_A}),\
+      $(warning Re-installing ${MODULE_LOCATION}/lib/${T_A})\
+      $(RMDIR) ${MODULE_LOCATION}/lib/${T_A}\
+    )
 endif
 
 install build debug:: O.${EPICSVERSION}_${T_A}
diff --git a/tests/test_build.py b/tests/test_build.py
index 2f3c47f9..2c036771 100644
--- a/tests/test_build.py
+++ b/tests/test_build.py
@@ -591,3 +591,52 @@ def test_build_fails_if_nth_architecture_fails(wrapper: Wrapper, archs, failing_
         RE_MISSING_FILE.format(filename=re.escape(f"nonexistent_{failing_arch}.o")),
         errs,
     )
+
+
+def test_double_install_fails(wrapper: Wrapper):
+    RE_ERR_MOD_VER_EXISTS = ".*{module}/{version}.* already exists"
+
+    rc, *_ = wrapper.run_make("install", module_version=MODULE_VERSION)
+    assert rc == 0
+
+    rc, _, errs = wrapper.run_make("install", module_version=MODULE_VERSION)
+    assert rc == 2
+    assert re.search(
+        RE_ERR_MOD_VER_EXISTS.format(
+            module=re.escape(wrapper.name), version=re.escape(MODULE_VERSION)
+        ),
+        errs,
+    )
+
+
+def test_double_install_test_version_succeeds(wrapper: Wrapper):
+    RE_WARN_MOD_VER_EXISTS = "Re-installing .*{module}/{version}.*"
+
+    test_version = "test"
+    wrapper.write_dot_local_data("CONFIG_MODULE", {"E3_MODULE_VERSION": test_version})
+    cell_path = wrapper.get_env_var("E3_MODULES_INSTALL_LOCATION")
+
+    wrapper.add_file("header.h")
+
+    rc, _, errs = wrapper.run_make("install")
+    assert rc == 0
+    assert not re.search(
+        RE_WARN_MOD_VER_EXISTS.format(
+            module=re.escape(wrapper.name), version=test_version
+        ),
+        errs,
+    )
+
+    assert not (Path(cell_path) / "include" / "header.h").is_file()
+
+    wrapper.add_var_to_module_makefile("HEADERS", "header.h")
+
+    rc, _, errs = wrapper.run_make("install")
+    assert rc == 0
+    assert re.search(
+        RE_WARN_MOD_VER_EXISTS.format(
+            module=re.escape(wrapper.name), version=test_version
+        ),
+        errs,
+    )
+    assert (Path(cell_path) / "include" / "header.h").is_file()
-- 
GitLab