@@ -56,35 +56,32 @@ void TestFileManager::testOpenFile_invalid()
56
56
57
57
void TestFileManager::testRenamePath ()
58
58
{
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. " );
61
61
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 ();
68
67
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);
70
70
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." );
76
74
}
77
75
78
76
void TestFileManager::testDeleteFile ()
79
77
{
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 ();
88
85
89
86
QVERIFY2 (QFile::exists (tempFilePath), " Temporary file should exist before deletion." );
90
87
@@ -101,98 +98,71 @@ void TestFileManager::testDeleteFile()
101
98
102
99
void TestFileManager::testDeleteDir ()
103
100
{
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);
110
106
111
- QVERIFY2 (QFile:: exists (directory ), " Test directory should exist before deletion." );
107
+ QVERIFY2 (QFileInfo (dirPath). exists (), " Test directory should exist before deletion." );
112
108
113
109
QFileSystemModel *model = tree->getModel ();
114
110
QVERIFY2 (model, " Tree model should not be null." );
115
111
116
- QModelIndex index = model->index (directory );
112
+ QModelIndex index = model->index (dirPath );
117
113
QVERIFY2 (index.isValid (), " Model index should be valid for the test directory." );
118
114
119
115
FileManager::getInstance ().deletePath (QFileInfo (model->filePath (index)));
120
116
121
- QVERIFY2 (!QFile::exists (directory ), " Directory should be deleted." );
117
+ QVERIFY2 (!QFile::exists (dirPath ), " Directory should be deleted." );
122
118
}
123
119
124
120
void TestFileManager::testNewFile ()
125
121
{
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." );
133
124
125
+ QString folderPath = tempDir.path ();
134
126
OperationResult fileCreated = FileManager::getInstance ().newFile (QFileInfo (folderPath), " newFileTest1.c" );
135
127
136
128
QVERIFY2 (fileCreated.success , " New file should be created." );
137
129
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." );
144
130
}
145
131
146
132
void TestFileManager::testNewFolder ()
147
133
{
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." );
155
136
137
+ QString folderPath = tempDir.path ();
156
138
OperationResult folderCreated = FileManager::getInstance ().newFolder (QFileInfo (folderPath), " newDirTest" );
157
139
158
140
QVERIFY2 (folderCreated.success , " New folder should be created." );
159
141
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." );
163
142
}
164
143
165
144
void TestFileManager::testNewFolderFail ()
166
145
{
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 ();
168
150
OperationResult folderCreated = FileManager::getInstance ().newFolder (QFileInfo (folderPath), " " );
169
151
170
152
QVERIFY2 (!folderCreated.success , " Folder creation should fail." );
171
-
172
- QDir (folderPath).removeRecursively ();
173
- QVERIFY2 (!QDir (folderPath).exists (), " Directory should not exist after deletion." );
174
153
}
175
154
176
155
void TestFileManager::testDuplicatePath ()
177
156
{
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);
180
162
181
163
OperationResult pathDuplicated = FileManager::getInstance ().duplicatePath (QFileInfo (basePath));
182
164
183
165
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 ();
196
166
}
197
167
198
168
QTEST_MAIN (TestFileManager)
0 commit comments