30#include <gtest/gtest.h>
70 void SetUp()
override {
74 void TearDown()
override {
76 for (
const auto& path : created_temp_files_) {
78 if (std::filesystem::is_directory(path, ec)) {
79 std::filesystem::remove_all(path, ec);
81 std::filesystem::remove(path, ec);
88 std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
89 std::string file_name =
91 return (temp_dir / file_name).string();
96 std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
97 std::string file_name =
99 return (temp_dir / file_name).string();
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));
111 result.reserve(length);
112 for (
size_t i = 0; i < length; ++i) {
113 result += chars[dist(gen)];
120 if (
const auto info = ::testing::UnitTest::GetInstance()->current_test_info(); info) {
121 std::string result = std::format(
"{}_{}", info->test_suite_name(), info->name());
123 for (
auto& c : result) {
130 return "unknown_test";
136 created_temp_files_.push_back(filepath);
144 created_temp_files_.push_back(filepath);
150 std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
151 std::string dir_name =
153 std::filesystem::path dir_path = temp_dir / dir_name;
156 std::filesystem::create_directory(dir_path, ec);
158 throw std::runtime_error(
159 std::format(
"Failed to create temporary directory: {}", ec.message()));
162 created_temp_files_.push_back(dir_path.string());
163 return dir_path.string();
168 std::ofstream file(path, std::ios::binary);
170 throw std::runtime_error(std::format(
"Failed to open file for writing: {}", path));
172 file.write(content.data(), content.size());
174 throw std::runtime_error(std::format(
"Failed to write to file: {}", path));
185 std::vector<std::string> created_temp_files_;
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