@@ -56,35 +56,32 @@ void TestFileManager::testOpenFile_invalid()
5656
5757void TestFileManager::testRenamePath ()
5858{
59- QString originalFileName = QDir::temp (). filePath ( " testFile.cpp " ) ;
60- QString newFileName = QDir::temp (). filePath ( " renamedTestFile.cpp " );
59+ QTemporaryDir tempDir ;
60+ QVERIFY2 (tempDir. isValid (), " Temporary directory should be valid. " );
6161
62- QFile originalFile (originalFileName);
63- if (!originalFile.exists () && !originalFile.open (QIODevice::WriteOnly))
64- {
65- QFAIL (" Failed to create test file." );
66- }
67- originalFile.close ();
62+ QString originalFilePath = tempDir.path () + " /testFile.cpp" ;
63+ QFile file (originalFilePath);
64+ QVERIFY2 (file.open (QIODevice::WriteOnly), " File should be created successfully." );
65+ file.write (" // test content" );
66+ file.close ();
6867
69- OperationResult fileRenamed = FileManager::getInstance ().renamePath (QFileInfo (originalFileName), newFileName);
68+ QString newFilePath = tempDir.path () + " /renamedTestFile.cpp" ;
69+ OperationResult fileRenamed = FileManager::getInstance ().renamePath (QFileInfo (originalFilePath), newFilePath);
7070
71- QVERIFY2 (fileRenamed.success , " File should be renamed successfully." );
72- QVERIFY2 (QFile::exists (newFileName), " Renamed file should exist." );
73- QVERIFY2 (!QFile::exists (originalFileName), " Original file should no longer exist." );
74-
75- QFile::remove (newFileName);
71+ QVERIFY2 (fileRenamed.success , fileRenamed.message .c_str ());
72+ QVERIFY2 (QFile::exists (newFilePath), " Renamed file should exist." );
73+ QVERIFY2 (!QFile::exists (originalFilePath), " Original file should no longer exist." );
7674}
7775
7876void TestFileManager::testDeleteFile ()
7977{
80- QString tempFilePath = QDir::temp ().filePath (" testDeleteFile.cpp" );
81- QFile tempFile (tempFilePath);
82- if (!tempFile.open (QIODevice::WriteOnly))
83- {
84- QFAIL (" Failed to create temporary test file for deletion." );
85- }
86- tempFile.write (" // test content" );
87- tempFile.close ();
78+ QTemporaryDir tempDir;
79+ QVERIFY2 (tempDir.isValid (), " Temporary directory should be valid." );
80+
81+ QString tempFilePath = tempDir.path () + " /testDeleteFile.cpp" ;
82+ QFile file (tempFilePath);
83+ QVERIFY2 (file.open (QIODevice::WriteOnly), " Temporary file should be created." );
84+ file.close ();
8885
8986 QVERIFY2 (QFile::exists (tempFilePath), " Temporary file should exist before deletion." );
9087
@@ -101,98 +98,71 @@ void TestFileManager::testDeleteFile()
10198
10299void TestFileManager::testDeleteDir ()
103100{
104- QString directory = QDir::temp ().absolutePath () + " /testDeleteDir" ;
105- QDir tempDir (directory);
106- if (!tempDir.exists () && !tempDir.mkpath (" ." ))
107- {
108- QFAIL (" Failed to create test directory." );
109- }
101+ QTemporaryDir tempDir;
102+ QVERIFY2 (tempDir.isValid (), " Temporary directory should be valid." );
103+
104+ QString dirPath = tempDir.path () + " /testDeleteDir" ;
105+ QDir ().mkdir (dirPath);
110106
111- QVERIFY2 (QFile:: exists (directory ), " Test directory should exist before deletion." );
107+ QVERIFY2 (QFileInfo (dirPath). exists (), " Test directory should exist before deletion." );
112108
113109 QFileSystemModel *model = tree->getModel ();
114110 QVERIFY2 (model, " Tree model should not be null." );
115111
116- QModelIndex index = model->index (directory );
112+ QModelIndex index = model->index (dirPath );
117113 QVERIFY2 (index.isValid (), " Model index should be valid for the test directory." );
118114
119115 FileManager::getInstance ().deletePath (QFileInfo (model->filePath (index)));
120116
121- QVERIFY2 (!QFile::exists (directory ), " Directory should be deleted." );
117+ QVERIFY2 (!QFile::exists (dirPath ), " Directory should be deleted." );
122118}
123119
124120void TestFileManager::testNewFile ()
125121{
126- QString folderPath = QDir::temp ().absolutePath () + " /testNewDir" ;
127- QDir dir (folderPath);
128- if (!dir.exists ())
129- {
130- dir.mkpath (" ." );
131- }
132- QVERIFY2 (QFile::exists (folderPath), " Temporary directory should exist." );
122+ QTemporaryDir tempDir;
123+ QVERIFY2 (tempDir.isValid (), " Temporary directory should be valid." );
133124
125+ QString folderPath = tempDir.path ();
134126 OperationResult fileCreated = FileManager::getInstance ().newFile (QFileInfo (folderPath), " newFileTest1.c" );
135127
136128 QVERIFY2 (fileCreated.success , " New file should be created." );
137129 QVERIFY2 (QFile::exists (folderPath + " /newFileTest1.c" ), " Newly created file should exist." );
138-
139- QFile::remove (folderPath + " /newFileTest1.c" );
140- QVERIFY2 (!QFile::exists (folderPath + " /newFileTest1.c" ), " Newly created file should be deleted." );
141-
142- dir.removeRecursively ();
143- QVERIFY2 (!QDir (folderPath).exists (), " Directory should not exist after deletion." );
144130}
145131
146132void TestFileManager::testNewFolder ()
147133{
148- QString folderPath = QDir::temp ().absolutePath () + " /testNewDir" ;
149- QDir dir (folderPath);
150- if (!dir.exists ())
151- {
152- dir.mkpath (" ." );
153- }
154- QVERIFY2 (QFile::exists (folderPath), " Temporary directory should exist." );
134+ QTemporaryDir tempDir;
135+ QVERIFY2 (tempDir.isValid (), " Temporary directory should be valid." );
155136
137+ QString folderPath = tempDir.path ();
156138 OperationResult folderCreated = FileManager::getInstance ().newFolder (QFileInfo (folderPath), " newDirTest" );
157139
158140 QVERIFY2 (folderCreated.success , " New folder should be created." );
159141 QVERIFY2 (QFile::exists (folderPath + " /newDirTest" ), " Newly created folder should exist." );
160-
161- dir.removeRecursively ();
162- QVERIFY2 (!QDir (folderPath).exists (), " Directory should not exist after deletion." );
163142}
164143
165144void TestFileManager::testNewFolderFail ()
166145{
167- QString folderPath = QDir::temp ().absolutePath () + " ../testNewDir" ;
146+ QTemporaryDir tempDir;
147+ QVERIFY2 (tempDir.isValid (), " Temporary directory should be valid." );
148+
149+ QString folderPath = tempDir.path ();
168150 OperationResult folderCreated = FileManager::getInstance ().newFolder (QFileInfo (folderPath), " " );
169151
170152 QVERIFY2 (!folderCreated.success , " Folder creation should fail." );
171-
172- QDir (folderPath).removeRecursively ();
173- QVERIFY2 (!QDir (folderPath).exists (), " Directory should not exist after deletion." );
174153}
175154
176155void TestFileManager::testDuplicatePath ()
177156{
178- QString basePath = QDir::temp ().absolutePath () + " /testDuplicateDir" ;
179- QDir ().mkpath (basePath);
157+ QTemporaryDir tempDir;
158+ QVERIFY2 (tempDir.isValid (), " Temporary directory should be valid." );
159+
160+ QString basePath = tempDir.path () + " /testDuplicateDir" ;
161+ QDir ().mkdir (basePath);
180162
181163 OperationResult pathDuplicated = FileManager::getInstance ().duplicatePath (QFileInfo (basePath));
182164
183165 QVERIFY2 (pathDuplicated.success , " Path should be duplicated successfully." );
184-
185- QDir tempDir (QDir::tempPath ());
186- QStringList entries = tempDir.entryList (QDir::Dirs | QDir::NoDotAndDotDot);
187- for (const QString &entry : entries)
188- {
189- if (entry.startsWith (" testDuplicateDir_copy" ))
190- {
191- QDir (tempDir.absoluteFilePath (entry)).removeRecursively ();
192- }
193- }
194-
195- QDir (basePath).removeRecursively ();
196166}
197167
198168QTEST_MAIN (TestFileManager)
0 commit comments