diff --git a/modules/util/include/func_test_util.hpp b/modules/util/include/func_test_util.hpp index 9a4e7132..6cae3028 100644 --- a/modules/util/include/func_test_util.hpp +++ b/modules/util/include/func_test_util.hpp @@ -61,6 +61,8 @@ class BaseRunFuncTests : public ::testing::TestWithParam #include +#include #include #include +#include #include +#include #include +#include +#include #include #ifdef __GNUG__ # include @@ -17,6 +23,9 @@ # pragma warning(disable : 4459) #endif +#include + +#include #include /// @brief JSON namespace used for settings and config parsing. @@ -90,4 +99,57 @@ inline std::shared_ptr InitJSONPtr() { bool IsUnderMpirun(); +namespace test { + +[[nodiscard]] inline std::string SanitizeToken(std::string_view token_sv) { + std::string token{token_sv}; + auto is_allowed = [](char c) { + return std::isalnum(static_cast(c)) || c == '_' || c == '-' || c == '.'; + }; + std::ranges::replace(token, ' ', '_'); + for (char &ch : token) { + if (!is_allowed(ch)) { + ch = '_'; + } + } + return token; +} + +class ScopedPerTestEnv { + public: + explicit ScopedPerTestEnv(const std::string &token) + : set_uid_("PPC_TEST_UID", token), set_tmp_("PPC_TEST_TMPDIR", CreateTmpDir(token)) {} + + private: + static std::string CreateTmpDir(const std::string &token) { + namespace fs = std::filesystem; + const fs::path tmp = fs::temp_directory_path() / (std::string("ppc_test_") + token); + std::error_code ec; + fs::create_directories(tmp, ec); + (void)ec; + return tmp.string(); + } + + env::detail::set_scoped_environment_variable set_uid_; + env::detail::set_scoped_environment_variable set_tmp_; +}; + +[[nodiscard]] inline std::string MakeCurrentGTestToken(std::string_view fallback_name) { + const auto *unit = ::testing::UnitTest::GetInstance(); + const auto *info = (unit != nullptr) ? unit->current_test_info() : nullptr; + std::ostringstream os; + if (info != nullptr) { + os << info->test_suite_name() << "." << info->name(); + } else { + os << fallback_name; + } + return SanitizeToken(os.str()); +} + +inline ScopedPerTestEnv MakePerTestEnvForCurrentGTest(std::string_view fallback_name) { + return ScopedPerTestEnv(MakeCurrentGTestToken(fallback_name)); +} + +} // namespace test + } // namespace ppc::util diff --git a/scripts/run_tests.py b/scripts/run_tests.py index a6f75a75..ea2ea3bb 100755 --- a/scripts/run_tests.py +++ b/scripts/run_tests.py @@ -142,7 +142,7 @@ def run_processes(self, additional_mpi_args): self.__run_exec( mpi_running + [str(self.work_dir / "ppc_func_tests")] - + self.__get_gtest_settings(1, "_" + task_type) + + self.__get_gtest_settings(1, "_" + task_type + "_") ) def run_performance(self): @@ -152,13 +152,13 @@ def run_performance(self): self.__run_exec( mpi_running + [str(self.work_dir / "ppc_perf_tests")] - + self.__get_gtest_settings(1, "_" + task_type) + + self.__get_gtest_settings(1, "_" + task_type + "_") ) for task_type in ["omp", "seq", "stl", "tbb"]: self.__run_exec( [str(self.work_dir / "ppc_perf_tests")] - + self.__get_gtest_settings(1, "_" + task_type) + + self.__get_gtest_settings(1, "_" + task_type + "_") )