1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "Private/UnitTests/BsFileSystemTestSuite.h"
4
5#include "Debug/BsDebug.h"
6#include "Error/BsException.h"
7#include "FileSystem/BsFileSystem.h"
8
9#include <algorithm>
10#include <fstream>
11
12namespace bs
13{
14 const String testDirectoryName = "FileSystemTestDirectory/";
15
16 void createFile(Path path, String content)
17 {
18 std::ofstream fs;
19 fs.open(path.toPlatformString().c_str());
20 fs << content;
21 fs.close();
22 }
23
24 void createEmptyFile(Path path)
25 {
26 createFile(path, "");
27 }
28
29 String readFile(Path path)
30 {
31 String content;
32 std::ifstream fs;
33 fs.open(path.toPlatformString().c_str());
34 fs >> content;
35 fs.close();
36 return content;
37 }
38
39 void FileSystemTestSuite::startUp()
40 {
41 mTestDirectory = FileSystem::getWorkingDirectoryPath() + testDirectoryName;
42 if (FileSystem::exists(mTestDirectory))
43 {
44 BS_EXCEPT(InternalErrorException,
45 String("Directory '") + testDirectoryName
46 + "' should not already exist; you should remove it manually.");
47 }
48 else
49 {
50 FileSystem::createDir(mTestDirectory);
51 BS_TEST_ASSERT_MSG(FileSystem::exists(mTestDirectory), "FileSystemTestSuite::startUp(): test directory creation failed");
52 }
53 }
54
55 void FileSystemTestSuite::shutDown()
56 {
57 FileSystem::remove(mTestDirectory, true);
58 if (FileSystem::exists(mTestDirectory))
59 {
60 LOGERR("FileSystemTestSuite failed to delete '" + mTestDirectory.toString()
61 + "', you should remove it manually.");
62 }
63 }
64
65 FileSystemTestSuite::FileSystemTestSuite()
66 {
67 BS_ADD_TEST(FileSystemTestSuite::testExists_yes_file);
68 BS_ADD_TEST(FileSystemTestSuite::testExists_yes_dir);
69 BS_ADD_TEST(FileSystemTestSuite::testExists_no);
70 BS_ADD_TEST(FileSystemTestSuite::testGetFileSize_zero);
71 BS_ADD_TEST(FileSystemTestSuite::testGetFileSize_not_zero);
72 BS_ADD_TEST(FileSystemTestSuite::testIsFile_yes);
73 BS_ADD_TEST(FileSystemTestSuite::testIsFile_no);
74 BS_ADD_TEST(FileSystemTestSuite::testIsDirectory_yes);
75 BS_ADD_TEST(FileSystemTestSuite::testIsDirectory_no);
76 BS_ADD_TEST(FileSystemTestSuite::testRemove_file);
77 BS_ADD_TEST(FileSystemTestSuite::testRemove_directory);
78 BS_ADD_TEST(FileSystemTestSuite::testMove);
79 BS_ADD_TEST(FileSystemTestSuite::testMove_overwrite_existing);
80 BS_ADD_TEST(FileSystemTestSuite::testMove_no_overwrite_existing);
81 BS_ADD_TEST(FileSystemTestSuite::testCopy);
82 BS_ADD_TEST(FileSystemTestSuite::testCopy_overwrite_existing);
83 BS_ADD_TEST(FileSystemTestSuite::testCopy_no_overwrite_existing);
84 BS_ADD_TEST(FileSystemTestSuite::testGetChildren);
85 BS_ADD_TEST(FileSystemTestSuite::testGetLastModifiedTime);
86 BS_ADD_TEST(FileSystemTestSuite::testGetTempDirectoryPath);
87 }
88
89 void FileSystemTestSuite::testExists_yes_file()
90 {
91 Path path = mTestDirectory + "plop";
92 createEmptyFile(path);
93 BS_TEST_ASSERT(FileSystem::exists(path));
94 FileSystem::remove(path);
95 }
96
97 void FileSystemTestSuite::testExists_yes_dir()
98 {
99 Path path = mTestDirectory + "plop/";
100 FileSystem::createDir(path);
101 BS_TEST_ASSERT(FileSystem::exists(path));
102 FileSystem::remove(path);
103 }
104
105 void FileSystemTestSuite::testExists_no()
106 {
107 BS_TEST_ASSERT(!FileSystem::exists(Path("this-file-does-not-exist")));
108 }
109
110 void FileSystemTestSuite::testGetFileSize_zero()
111 {
112 Path path = mTestDirectory + "file-size-test-1";
113 createEmptyFile(path);
114 BS_TEST_ASSERT(FileSystem::getFileSize(path) == 0);
115 FileSystem::remove(path);
116 }
117
118 void FileSystemTestSuite::testGetFileSize_not_zero()
119 {
120 Path path = mTestDirectory + "file-size-test-2";
121 createFile(path, "0123456789");
122 BS_TEST_ASSERT(FileSystem::getFileSize(path) == 10);
123 FileSystem::remove(path);
124 }
125
126 void FileSystemTestSuite::testIsFile_yes()
127 {
128 Path path = mTestDirectory + "some-file-1";
129 createEmptyFile(path);
130 BS_TEST_ASSERT(FileSystem::isFile(path));
131 }
132
133 void FileSystemTestSuite::testIsFile_no()
134 {
135 Path path = mTestDirectory + "some-directory-1/";
136 FileSystem::createDir(path);
137 BS_TEST_ASSERT(!FileSystem::isFile(path));
138 }
139
140 void FileSystemTestSuite::testIsDirectory_yes()
141 {
142 Path path = mTestDirectory + "some-directory-2/";
143 FileSystem::createDir(path);
144 BS_TEST_ASSERT(FileSystem::isDirectory(path));
145 }
146
147 void FileSystemTestSuite::testIsDirectory_no()
148 {
149 Path path = mTestDirectory + "some-file-2";
150 createEmptyFile(path);
151 BS_TEST_ASSERT(!FileSystem::isDirectory(path));
152 }
153
154 void FileSystemTestSuite::testRemove_file()
155 {
156 Path path = mTestDirectory + "file-to-remove";
157 createEmptyFile(path);
158 BS_TEST_ASSERT(FileSystem::exists(path));
159 FileSystem::remove(path);
160 BS_TEST_ASSERT(!FileSystem::exists(path));
161 }
162
163 void FileSystemTestSuite::testRemove_directory()
164 {
165 Path path = mTestDirectory + "directory-to-remove/";
166 FileSystem::createDir(path);
167 BS_TEST_ASSERT(FileSystem::exists(path));
168 FileSystem::remove(path, true);
169 BS_TEST_ASSERT(!FileSystem::exists(path));
170 }
171
172 void FileSystemTestSuite::testMove()
173 {
174 Path source = mTestDirectory + "move-source-1";
175 Path destination = mTestDirectory + "move-destination-1";
176 createFile(source, "move-data-source-1");
177 BS_TEST_ASSERT(FileSystem::exists(source));
178 BS_TEST_ASSERT(!FileSystem::exists(destination));
179 FileSystem::move(source, destination);
180 BS_TEST_ASSERT(!FileSystem::exists(source));
181 BS_TEST_ASSERT(FileSystem::exists(destination));
182 BS_TEST_ASSERT(readFile(destination) == "move-data-source-1");
183 }
184
185 void FileSystemTestSuite::testMove_overwrite_existing()
186 {
187 Path source = mTestDirectory + "move-source-2";
188 Path destination = mTestDirectory + "move-destination-2";
189 createFile(source, "move-data-source-2");
190 createFile(destination, "move-data-destination-2");
191 BS_TEST_ASSERT(FileSystem::exists(source));
192 BS_TEST_ASSERT(FileSystem::exists(destination));
193 FileSystem::move(source, destination, true);
194 BS_TEST_ASSERT(!FileSystem::exists(source));
195 BS_TEST_ASSERT(FileSystem::exists(destination));
196 BS_TEST_ASSERT(readFile(destination) == "move-data-source-2");
197 }
198
199 void FileSystemTestSuite::testMove_no_overwrite_existing()
200 {
201 Path source = mTestDirectory + "move-source-3";
202 Path destination = mTestDirectory + "move-destination-3";
203 createFile(source, "move-data-source-3");
204 createFile(destination, "move-data-destination-3");
205 BS_TEST_ASSERT(FileSystem::exists(source));
206 BS_TEST_ASSERT(FileSystem::exists(destination));
207 FileSystem::move(source, destination, false);
208 BS_TEST_ASSERT(FileSystem::exists(source));
209 BS_TEST_ASSERT(FileSystem::exists(destination));
210 BS_TEST_ASSERT(readFile(destination) == "move-data-destination-3");
211 }
212
213 void FileSystemTestSuite::testCopy()
214 {
215 Path source = mTestDirectory + "copy-source-1";
216 Path destination = mTestDirectory + "copy-destination-1";
217 createFile(source, "copy-data-source-1");
218 BS_TEST_ASSERT(FileSystem::exists(source));
219 BS_TEST_ASSERT(!FileSystem::exists(destination));
220 FileSystem::copy(source, destination);
221 BS_TEST_ASSERT(FileSystem::exists(source));
222 BS_TEST_ASSERT(FileSystem::exists(destination));
223 BS_TEST_ASSERT(readFile(source) == "copy-data-source-1");
224 BS_TEST_ASSERT(readFile(destination) == "copy-data-source-1");
225 }
226
227 void FileSystemTestSuite::testCopy_overwrite_existing()
228 {
229 Path source = mTestDirectory + "copy-source-2";
230 Path destination = mTestDirectory + "copy-destination-2";
231 createFile(source, "copy-data-source-2");
232 createFile(destination, "copy-data-destination-2");
233 BS_TEST_ASSERT(FileSystem::exists(source));
234 BS_TEST_ASSERT(FileSystem::exists(destination));
235 FileSystem::copy(source, destination, true);
236 BS_TEST_ASSERT(FileSystem::exists(source));
237 BS_TEST_ASSERT(FileSystem::exists(destination));
238 BS_TEST_ASSERT(readFile(source) == "copy-data-source-2");
239 BS_TEST_ASSERT(readFile(destination) == "copy-data-source-2");
240 }
241
242 void FileSystemTestSuite::testCopy_no_overwrite_existing()
243 {
244 Path source = mTestDirectory + "copy-source-3";
245 Path destination = mTestDirectory + "copy-destination-3";
246 createFile(source, "copy-data-source-3");
247 createFile(destination, "copy-data-destination-3");
248 BS_TEST_ASSERT(FileSystem::exists(source));
249 BS_TEST_ASSERT(FileSystem::exists(destination));
250 FileSystem::copy(source, destination, false);
251 BS_TEST_ASSERT(FileSystem::exists(source));
252 BS_TEST_ASSERT(FileSystem::exists(destination));
253 BS_TEST_ASSERT(readFile(source) == "copy-data-source-3");
254 BS_TEST_ASSERT(readFile(destination) == "copy-data-destination-3");
255 }
256
257
258#define CONTAINS(v, e) (std::find(v.begin(), v.end(), e) != v.end())
259
260 void FileSystemTestSuite::testGetChildren()
261 {
262 Path path = mTestDirectory + "get-children-test/";
263 FileSystem::createDir(path);
264 FileSystem::createDir(path + "foo/");
265 FileSystem::createDir(path + "bar/");
266 FileSystem::createDir(path + "baz/");
267 createEmptyFile(path + "ga");
268 createEmptyFile(path + "bu");
269 createEmptyFile(path + "zo");
270 createEmptyFile(path + "meu");
271 Vector<Path> files, directories;
272 FileSystem::getChildren(path, files, directories);
273 BS_TEST_ASSERT(files.size() == 4);
274 BS_TEST_ASSERT(CONTAINS(files, path + "ga"));
275 BS_TEST_ASSERT(CONTAINS(files, path + "bu"));
276 BS_TEST_ASSERT(CONTAINS(files, path + "zo"));
277 BS_TEST_ASSERT(CONTAINS(files, path + "meu"));
278 BS_TEST_ASSERT(directories.size() == 3);
279 BS_TEST_ASSERT(CONTAINS(directories, path + "foo"));
280 BS_TEST_ASSERT(CONTAINS(directories, path + "bar"));
281 BS_TEST_ASSERT(CONTAINS(directories, path + "baz"));
282 }
283
284 void FileSystemTestSuite::testGetLastModifiedTime()
285 {
286 std::time_t beforeTime;
287 time(&beforeTime);
288
289 Path path = mTestDirectory + "blah1234";
290 createFile(path, "blah");
291 std::time_t mtime = FileSystem::getLastModifiedTime(path);
292 BS_TEST_ASSERT(mtime >= beforeTime);
293 BS_TEST_ASSERT(mtime <= beforeTime + 10);
294 }
295
296 void FileSystemTestSuite::testGetTempDirectoryPath()
297 {
298 Path path = FileSystem::getTempDirectoryPath();
299 /* No judging. */
300 BS_TEST_ASSERT(!path.toString().empty());
301 }
302}
303