|
31 | 31 |
|
32 | 32 | namespace iceberg { |
33 | 33 |
|
34 | | -/// \brief Get the test name for inclusion in the filename |
35 | | -inline std::string TestInfo() { |
36 | | - if (const auto info = ::testing::UnitTest::GetInstance()->current_test_info(); info) { |
37 | | - return std::format("{}_{}", info->test_suite_name(), info->name()); |
38 | | - } |
39 | | - return "unknown_test"; |
40 | | -} |
41 | | - |
42 | | -/// \brief Helper to generate a random alphanumeric string for unique filenames |
43 | | -inline std::string GenerateRandomString(size_t length) { |
44 | | - const std::string_view chars = |
45 | | - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
46 | | - std::random_device rd; |
47 | | - std::mt19937 gen(rd()); |
48 | | - std::uniform_int_distribution<> dist(0, static_cast<int>(chars.size() - 1)); |
49 | | - |
50 | | - std::string result; |
51 | | - result.reserve(length); |
52 | | - for (size_t i = 0; i < length; ++i) { |
53 | | - result += chars[dist(gen)]; |
54 | | - } |
55 | | - return result; |
56 | | -} |
57 | | - |
58 | | -/// \brief Generates a unique temporary filepath that works across platforms |
59 | | -inline std::string GenerateUniqueTempFilePath() { |
60 | | - std::filesystem::path temp_dir = std::filesystem::temp_directory_path(); |
61 | | - std::string file_name = |
62 | | - std::format("iceberg_test_{}_{}.tmp", TestInfo(), GenerateRandomString(8)); |
63 | | - return (temp_dir / file_name).string(); |
64 | | -} |
65 | | - |
66 | | -/// \brief Create a temporary filepath with the specified suffix/extension |
67 | | -inline std::string GenerateUniqueTempFilePathWithSuffix(const std::string& suffix) { |
68 | | - std::filesystem::path temp_dir = std::filesystem::temp_directory_path(); |
69 | | - std::string file_name = |
70 | | - std::format("iceberg_test_{}_{}{}", TestInfo(), GenerateRandomString(8), suffix); |
71 | | - return (temp_dir / file_name).string(); |
72 | | -} |
73 | | - |
74 | 34 | /// A base class for tests that need to create and manage temporary files. |
75 | 35 | /// Provides utilities for creating platform-independent temporary files |
76 | 36 | /// and ensures proper cleanup after tests run. |
@@ -123,6 +83,46 @@ class TempFileTestBase : public ::testing::Test { |
123 | 83 | } |
124 | 84 | } |
125 | 85 |
|
| 86 | + /// \brief Generates a unique temporary filepath that works across platforms |
| 87 | + std::string GenerateUniqueTempFilePath() const { |
| 88 | + std::filesystem::path temp_dir = std::filesystem::temp_directory_path(); |
| 89 | + std::string file_name = |
| 90 | + std::format("iceberg_test_{}_{}.tmp", TestInfo(), GenerateRandomString(8)); |
| 91 | + return (temp_dir / file_name).string(); |
| 92 | + } |
| 93 | + |
| 94 | + /// \brief Create a temporary filepath with the specified suffix/extension |
| 95 | + std::string GenerateUniqueTempFilePathWithSuffix(const std::string& suffix) { |
| 96 | + std::filesystem::path temp_dir = std::filesystem::temp_directory_path(); |
| 97 | + std::string file_name = |
| 98 | + std::format("iceberg_test_{}_{}{}", TestInfo(), GenerateRandomString(8), suffix); |
| 99 | + return (temp_dir / file_name).string(); |
| 100 | + } |
| 101 | + |
| 102 | + /// \brief Helper to generate a random alphanumeric string for unique filenames |
| 103 | + std::string GenerateRandomString(size_t length) const { |
| 104 | + const std::string_view chars = |
| 105 | + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 106 | + std::random_device rd; |
| 107 | + std::mt19937 gen(rd()); |
| 108 | + std::uniform_int_distribution<> dist(0, static_cast<int>(chars.size() - 1)); |
| 109 | + |
| 110 | + std::string result; |
| 111 | + result.reserve(length); |
| 112 | + for (size_t i = 0; i < length; ++i) { |
| 113 | + result += chars[dist(gen)]; |
| 114 | + } |
| 115 | + return result; |
| 116 | + } |
| 117 | + |
| 118 | + /// \brief Get the test name for inclusion in the filename |
| 119 | + std::string TestInfo() const { |
| 120 | + if (const auto info = ::testing::UnitTest::GetInstance()->current_test_info(); info) { |
| 121 | + return std::format("{}_{}", info->test_suite_name(), info->name()); |
| 122 | + } |
| 123 | + return "unknown_test"; |
| 124 | + } |
| 125 | + |
126 | 126 | /// \brief Creates a new temporary filepath and registers it for cleanup |
127 | 127 | std::string CreateNewTempFilePath() { |
128 | 128 | std::string filepath = GenerateUniqueTempFilePath(); |
|
0 commit comments