iceberg-cpp
Loading...
Searching...
No Matches
temp_file_test_base.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
22#include <filesystem>
23#include <format>
24#include <fstream>
25#include <random>
26#include <string>
27#include <string_view>
28#include <vector>
29
30#include <gtest/gtest.h>
31
32namespace iceberg {
33
68class TempFileTestBase : public ::testing::Test {
69 protected:
70 void SetUp() override {
71 // Base class setup is empty, but provided for derived classes to call
72 }
73
74 void TearDown() override {
75 // Clean up all temporary files and directories created during the test
76 for (const auto& path : created_temp_files_) {
77 std::error_code ec;
78 if (std::filesystem::is_directory(path, ec)) {
79 std::filesystem::remove_all(path, ec);
80 } else {
81 std::filesystem::remove(path, ec);
82 }
83 }
84 }
85
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
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
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
119 std::string TestInfo() const {
120 if (const auto info = ::testing::UnitTest::GetInstance()->current_test_info(); info) {
121 std::string result = std::format("{}_{}", info->test_suite_name(), info->name());
122 // Replace slashes (from parameterized tests) with underscores to avoid path issues
123 for (auto& c : result) {
124 if (c == '/') {
125 c = '_';
126 }
127 }
128 return result;
129 }
130 return "unknown_test";
131 }
132
134 std::string CreateNewTempFilePath() {
135 std::string filepath = GenerateUniqueTempFilePath();
136 created_temp_files_.push_back(filepath);
137 return filepath;
138 }
139
142 std::string CreateNewTempFilePathWithSuffix(const std::string& suffix) {
143 std::string filepath = GenerateUniqueTempFilePathWithSuffix(suffix);
144 created_temp_files_.push_back(filepath);
145 return filepath;
146 }
147
149 std::string CreateTempDirectory() {
150 std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
151 std::string dir_name =
152 std::format("iceberg_test_dir_{}_{}", TestInfo(), GenerateRandomString(8));
153 std::filesystem::path dir_path = temp_dir / dir_name;
154
155 std::error_code ec;
156 std::filesystem::create_directory(dir_path, ec);
157 if (ec) {
158 throw std::runtime_error(
159 std::format("Failed to create temporary directory: {}", ec.message()));
160 }
161
162 created_temp_files_.push_back(dir_path.string());
163 return dir_path.string();
164 }
165
167 void WriteContentToFile(const std::string& path, const std::string& content) {
168 std::ofstream file(path, std::ios::binary);
169 if (!file) {
170 throw std::runtime_error(std::format("Failed to open file for writing: {}", path));
171 }
172 file.write(content.data(), content.size());
173 if (!file) {
174 throw std::runtime_error(std::format("Failed to write to file: {}", path));
175 }
176 }
177
179 std::string CreateTempFileWithContent(const std::string& content) {
180 std::string path = CreateNewTempFilePath();
181 WriteContentToFile(path, content);
182 return path;
183 }
184
185 std::vector<std::string> created_temp_files_;
186};
187
188} // namespace iceberg
Definition temp_file_test_base.h:68
std::string CreateTempFileWithContent(const std::string &content)
Creates a new temporary file with the given content.
Definition temp_file_test_base.h:179
std::string GenerateUniqueTempFilePathWithSuffix(const std::string &suffix)
Create a temporary filepath with the specified suffix/extension.
Definition temp_file_test_base.h:95
void WriteContentToFile(const std::string &path, const std::string &content)
Creates a file with the given content at the specified path.
Definition temp_file_test_base.h:167
std::string CreateNewTempFilePath()
Creates a new temporary filepath and registers it for cleanup.
Definition temp_file_test_base.h:134
std::string CreateTempDirectory()
Creates a temporary directory and registers it for cleanup.
Definition temp_file_test_base.h:149
std::string CreateNewTempFilePathWithSuffix(const std::string &suffix)
Create a temporary filepath with the specified suffix and registers it for cleanup.
Definition temp_file_test_base.h:142
std::string GenerateRandomString(size_t length) const
Helper to generate a random alphanumeric string for unique filenames.
Definition temp_file_test_base.h:103
std::string GenerateUniqueTempFilePath() const
Generates a unique temporary filepath that works across platforms.
Definition temp_file_test_base.h:87
std::string TestInfo() const
Get the test name for inclusion in the filename.
Definition temp_file_test_base.h:119