|
1 | 1 | #pragma once |
2 | 2 |
|
| 3 | +#include <algorithm> |
3 | 4 | #include <atomic> |
| 5 | +#include <cctype> |
4 | 6 | #include <cstdint> |
5 | 7 | #include <cstdlib> |
| 8 | +#include <filesystem> |
6 | 9 | #include <memory> |
| 10 | +#include <sstream> |
7 | 11 | #include <string> |
| 12 | +#include <string_view> |
| 13 | +#include <system_error> |
8 | 14 | #include <typeinfo> |
9 | 15 | #ifdef __GNUG__ |
10 | 16 | # include <cxxabi.h> |
|
17 | 23 | # pragma warning(disable : 4459) |
18 | 24 | #endif |
19 | 25 |
|
| 26 | +#include <gtest/gtest.h> |
| 27 | + |
| 28 | +#include <libenvpp/detail/environment.hpp> |
20 | 29 | #include <nlohmann/json.hpp> |
21 | 30 |
|
22 | 31 | /// @brief JSON namespace used for settings and config parsing. |
@@ -90,4 +99,57 @@ inline std::shared_ptr<nlohmann::json> InitJSONPtr() { |
90 | 99 |
|
91 | 100 | bool IsUnderMpirun(); |
92 | 101 |
|
| 102 | +namespace test { |
| 103 | + |
| 104 | +[[nodiscard]] inline std::string SanitizeToken(std::string_view token_sv) { |
| 105 | + std::string token{token_sv}; |
| 106 | + auto is_allowed = [](char c) { |
| 107 | + return std::isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '-' || c == '.'; |
| 108 | + }; |
| 109 | + std::ranges::replace(token, ' ', '_'); |
| 110 | + for (char &ch : token) { |
| 111 | + if (!is_allowed(ch)) { |
| 112 | + ch = '_'; |
| 113 | + } |
| 114 | + } |
| 115 | + return token; |
| 116 | +} |
| 117 | + |
| 118 | +class ScopedPerTestEnv { |
| 119 | + public: |
| 120 | + explicit ScopedPerTestEnv(const std::string &token) |
| 121 | + : set_uid_("PPC_TEST_UID", token), set_tmp_("PPC_TEST_TMPDIR", CreateTmpDir(token)) {} |
| 122 | + |
| 123 | + private: |
| 124 | + static std::string CreateTmpDir(const std::string &token) { |
| 125 | + namespace fs = std::filesystem; |
| 126 | + const fs::path tmp = fs::temp_directory_path() / (std::string("ppc_test_") + token); |
| 127 | + std::error_code ec; |
| 128 | + fs::create_directories(tmp, ec); |
| 129 | + (void)ec; |
| 130 | + return tmp.string(); |
| 131 | + } |
| 132 | + |
| 133 | + env::detail::set_scoped_environment_variable set_uid_; |
| 134 | + env::detail::set_scoped_environment_variable set_tmp_; |
| 135 | +}; |
| 136 | + |
| 137 | +[[nodiscard]] inline std::string MakeCurrentGTestToken(std::string_view fallback_name) { |
| 138 | + const auto *unit = ::testing::UnitTest::GetInstance(); |
| 139 | + const auto *info = (unit != nullptr) ? unit->current_test_info() : nullptr; |
| 140 | + std::ostringstream os; |
| 141 | + if (info != nullptr) { |
| 142 | + os << info->test_suite_name() << "." << info->name(); |
| 143 | + } else { |
| 144 | + os << fallback_name; |
| 145 | + } |
| 146 | + return SanitizeToken(os.str()); |
| 147 | +} |
| 148 | + |
| 149 | +inline ScopedPerTestEnv MakePerTestEnvForCurrentGTest(std::string_view fallback_name) { |
| 150 | + return ScopedPerTestEnv(MakeCurrentGTestToken(fallback_name)); |
| 151 | +} |
| 152 | + |
| 153 | +} // namespace test |
| 154 | + |
93 | 155 | } // namespace ppc::util |
0 commit comments