From 0887dbcca63e1ade29faab5e408dc550bae8402a Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Fri, 1 Nov 2024 15:27:40 +0100
Subject: [PATCH] Add tests for stop function

---
 src/utils/Stop.cpp   |  2 ++
 tests/CMakeLists.txt |  1 +
 tests/test_Stop.cpp  | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+)
 create mode 100644 tests/test_Stop.cpp

diff --git a/src/utils/Stop.cpp b/src/utils/Stop.cpp
index c789beb3a..593094cc3 100644
--- a/src/utils/Stop.cpp
+++ b/src/utils/Stop.cpp
@@ -30,6 +30,7 @@ stop()
     }
 
 #ifdef PUGS_HAS_SLURM
+    // LCOV_EXCL_START
     char* env = getenv("SLURM_JOB_ID");
     if (env != nullptr) {
       slurm_init(nullptr);
@@ -41,6 +42,7 @@ stop()
 
       slurm_fini();
     }
+    // LCOV_EXCL_STOP
 #endif   // PUGS_HAS_SLURM
   }
 
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 5b7ff6b06..87cac1920 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -147,6 +147,7 @@ add_executable (unit_tests
   test_SourceLocation.cpp
   test_SquareGaussQuadrature.cpp
   test_SquareTransformation.cpp
+  test_Stop.cpp
   test_SymbolTable.cpp
   test_Table.cpp
   test_TetrahedronGaussQuadrature.cpp
diff --git a/tests/test_Stop.cpp b/tests/test_Stop.cpp
new file mode 100644
index 000000000..4e5d62049
--- /dev/null
+++ b/tests/test_Stop.cpp
@@ -0,0 +1,36 @@
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/matchers/catch_matchers_all.hpp>
+
+#include <utils/ExecutionStatManager.hpp>
+#include <utils/Stop.hpp>
+
+#include <chrono>
+#include <filesystem>
+#include <fstream>
+
+// clazy:excludeall=non-pod-global-static
+
+TEST_CASE("Stop", "[utils]")
+{
+  ExecutionStatManager::create();
+  auto stop_file = std::filesystem::current_path() / "stop";
+  std::filesystem::remove(stop_file);
+
+  REQUIRE(not std::filesystem::exists(stop_file));
+  REQUIRE(not stop());
+
+  std::ofstream{stop_file.c_str()}.put('a');
+  REQUIRE(std::filesystem::exists(stop_file));
+
+  std::filesystem::file_time_type ftime = std::filesystem::last_write_time(stop_file);
+
+  using namespace std::chrono_literals;
+  std::filesystem::last_write_time(stop_file, ftime - 1h);
+
+  REQUIRE(not stop());
+
+  std::filesystem::last_write_time(stop_file, ftime);
+  REQUIRE(stop());
+
+  ExecutionStatManager::destroy();
+}
-- 
GitLab