66#include < gtest/gtest.h>
77#include < spdlog/sinks/basic_file_sink.h>
88#include < spdlog/spdlog.h>
9+ #include < unistd.h>
910
1011#include < cstdio>
1112#include < filesystem>
1213#include < fstream>
14+ #include < stdexcept>
1315#include < string>
1416
1517namespace {
1618
19+ /* *
20+ * @brief Generate a unique temporary file path using mkstemp
21+ * @return Temporary file path
22+ */
23+ std::string GenerateTempFilePath () {
24+ char temp_template[] = " /tmp/mygramdb_test_XXXXXX" ;
25+ int fd = mkstemp (temp_template);
26+ if (fd == -1 ) {
27+ throw std::runtime_error (" Failed to create temporary file" );
28+ }
29+ close (fd);
30+ unlink (temp_template); // Remove the file, we just need the path
31+ return std::string (temp_template);
32+ }
33+
1734/* *
1835 * @brief Read entire file content
1936 */
@@ -32,8 +49,8 @@ std::string ReadFileContent(const std::string& filepath) {
3249 * @brief Test basic file logging
3350 */
3451TEST (FileLoggingTest, BasicFileLogging) {
35- const char * temp_file = std::tmpnam ( nullptr );
36- std::string log_file = std::string ( temp_file) + " .log" ;
52+ std::string temp_file = GenerateTempFilePath ( );
53+ std::string log_file = temp_file + " .log" ;
3754
3855 // Ensure file doesn't exist
3956 std::filesystem::remove (log_file);
@@ -66,8 +83,8 @@ TEST(FileLoggingTest, BasicFileLogging) {
6683 * @brief Test file logging with directory creation
6784 */
6885TEST (FileLoggingTest, DirectoryCreation) {
69- const char * temp_dir = std::tmpnam ( nullptr );
70- std::string log_dir = std::string ( temp_dir) + " _logs" ;
86+ std::string temp_dir = GenerateTempFilePath ( );
87+ std::string log_dir = temp_dir + " _logs" ;
7188 std::string log_file = log_dir + " /test.log" ;
7289
7390 // Ensure directory doesn't exist
@@ -105,8 +122,8 @@ TEST(FileLoggingTest, DirectoryCreation) {
105122 * @brief Test file logging with multiple messages
106123 */
107124TEST (FileLoggingTest, MultipleMessages) {
108- const char * temp_file = std::tmpnam ( nullptr );
109- std::string log_file = std::string ( temp_file) + " .log" ;
125+ std::string temp_file = GenerateTempFilePath ( );
126+ std::string log_file = temp_file + " .log" ;
110127
111128 std::filesystem::remove (log_file);
112129
@@ -141,11 +158,11 @@ TEST(FileLoggingTest, MultipleMessages) {
141158 * @brief Test file logging with nested directory path
142159 */
143160TEST (FileLoggingTest, NestedDirectoryPath) {
144- const char * temp_base = std::tmpnam ( nullptr );
145- std::string log_dir = std::string ( temp_base) + " _logs/subdir1/subdir2" ;
161+ std::string temp_base = GenerateTempFilePath ( );
162+ std::string log_dir = temp_base + " _logs/subdir1/subdir2" ;
146163 std::string log_file = log_dir + " /nested.log" ;
147164
148- std::filesystem::remove_all (std::string ( temp_base) + " _logs" );
165+ std::filesystem::remove_all (temp_base + " _logs" );
149166
150167 try {
151168 // Create nested directories
@@ -167,7 +184,7 @@ TEST(FileLoggingTest, NestedDirectoryPath) {
167184
168185 // Cleanup
169186 spdlog::drop (" test_logger_nested" );
170- std::filesystem::remove_all (std::string ( temp_base) + " _logs" );
187+ std::filesystem::remove_all (temp_base + " _logs" );
171188 } catch (const spdlog::spdlog_ex& ex) {
172189 FAIL () << " spdlog exception: " << ex.what ();
173190 }
@@ -177,8 +194,8 @@ TEST(FileLoggingTest, NestedDirectoryPath) {
177194 * @brief Test switching from stdout to file logger
178195 */
179196TEST (FileLoggingTest, SwitchToFileLogger) {
180- const char * temp_file = std::tmpnam ( nullptr );
181- std::string log_file = std::string ( temp_file) + " .log" ;
197+ std::string temp_file = GenerateTempFilePath ( );
198+ std::string log_file = temp_file + " .log" ;
182199
183200 std::filesystem::remove (log_file);
184201
0 commit comments