FileTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-15
  6. * Changed: 2023-04-13
  7. *
  8. * */
  9. #include <classes/TestHelper.hpp>
  10. #include <gtest/gtest.h>
  11. #include <ls-std/ls-std-core.hpp>
  12. #include <ls-std/ls-std-io.hpp>
  13. using ls::std::core::FileOperationException;
  14. using ls::std::io::File;
  15. using ls::std::io::FilePathSeparator;
  16. using ls::std::test::TestHelper;
  17. using std::list;
  18. using std::string;
  19. using testing::Test;
  20. namespace
  21. {
  22. class FileTest : public Test
  23. {
  24. public:
  25. FileTest() = default;
  26. ~FileTest() override = default;
  27. string fileLocation = TestHelper::getResourcesFolderLocation() + "simple.txt";
  28. static bool hasFileInDirectory(const list<string> &_fileNamesInDirectory, const string &_fileName)
  29. {
  30. return find(_fileNamesInDirectory.begin(), _fileNamesInDirectory.end(), _fileName) != _fileNamesInDirectory.end();
  31. }
  32. };
  33. // comparison operators
  34. TEST_F(FileTest, operator_equals)
  35. {
  36. File file{this->fileLocation};
  37. File file2{this->fileLocation};
  38. File file3{"/home/bla/text.txt"};
  39. File file4{"\\home/bla\\text.txt"};
  40. ASSERT_TRUE(file == file2);
  41. ASSERT_TRUE(file2 == file);
  42. ASSERT_TRUE(file3 == file4);
  43. ASSERT_TRUE(file4 == file3);
  44. }
  45. TEST_F(FileTest, operator_not_equals)
  46. {
  47. File file{this->fileLocation};
  48. File file2{TestHelper::getResourcesFolderLocation() + "app.exe"};
  49. ASSERT_TRUE(file != file2);
  50. ASSERT_TRUE(file2 != file);
  51. }
  52. // additional functionality
  53. TEST_F(FileTest, canExecute)
  54. {
  55. #ifdef _WIN32
  56. File executableFile{TestHelper::getResourcesFolderLocation() + "app.exe"};
  57. #endif
  58. #if defined(unix) || defined(__APPLE__)
  59. File executableFile{TestHelper::getResourcesFolderLocation() + "app"};
  60. #endif
  61. ASSERT_TRUE(executableFile.canExecute());
  62. }
  63. TEST_F(FileTest, canExecute_not_executable)
  64. {
  65. File file{this->fileLocation};
  66. ASSERT_FALSE(file.canExecute());
  67. }
  68. TEST_F(FileTest, canRead)
  69. {
  70. File readableFile{this->fileLocation};
  71. ASSERT_TRUE(readableFile.canRead());
  72. }
  73. TEST_F(FileTest, canRead_file_does_not_exist)
  74. {
  75. File file{""};
  76. EXPECT_THROW(
  77. {
  78. try
  79. {
  80. bool isReadable = file.canRead();
  81. }
  82. catch (const FileOperationException &_exception)
  83. {
  84. throw;
  85. }
  86. },
  87. FileOperationException);
  88. }
  89. TEST_F(FileTest, canWrite)
  90. {
  91. File readableFile{this->fileLocation};
  92. ASSERT_TRUE(readableFile.canWrite());
  93. }
  94. TEST_F(FileTest, canWrite_not_writable)
  95. {
  96. #if defined(unix) || defined(__APPLE__)
  97. File noWritableFile{TestHelper::getResourcesFolderLocation() + "no-writable.txt"};
  98. ASSERT_FALSE(noWritableFile.canWrite());
  99. #endif
  100. #ifdef _WIN32
  101. File noWritableFile{TestHelper::getResourcesFolderLocation() + "no-writable-windows.txt"};
  102. ASSERT_FALSE(noWritableFile.canWrite());
  103. #endif
  104. }
  105. TEST_F(FileTest, createNewFile)
  106. {
  107. File file{TestHelper::getResourcesFolderLocation() + "tmp.txt"};
  108. ASSERT_FALSE(file.exists());
  109. file.createNewFile();
  110. ASSERT_TRUE(file.exists());
  111. file.remove();
  112. ASSERT_FALSE(file.exists());
  113. }
  114. TEST_F(FileTest, createNewFile_file_does_already_exist)
  115. {
  116. File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  117. EXPECT_THROW(
  118. {
  119. try
  120. {
  121. file.createNewFile();
  122. }
  123. catch (const FileOperationException &_exception)
  124. {
  125. string expected = _exception.getName() + R"lit( thrown - file ")lit" + TestHelper::getResourcesFolderLocation() + R"lit(simple.txt" could not be created!)lit";
  126. string actual = _exception.what();
  127. ASSERT_STREQ(expected.c_str(), actual.c_str());
  128. throw;
  129. }
  130. },
  131. FileOperationException);
  132. }
  133. TEST_F(FileTest, exists)
  134. {
  135. File file{this->fileLocation};
  136. File directory{TestHelper::getResourcesFolderLocation()};
  137. ASSERT_TRUE(file.exists());
  138. ASSERT_TRUE(directory.exists());
  139. }
  140. TEST_F(FileTest, exists_does_not_exist)
  141. {
  142. File file{TestHelper::getResourcesFolderLocation() + "bla.txt"};
  143. ASSERT_FALSE(file.exists());
  144. }
  145. TEST_F(FileTest, getAbsoluteFilePath)
  146. {
  147. File file{this->fileLocation};
  148. ASSERT_STREQ(this->fileLocation.c_str(), file.getAbsoluteFilePath().c_str());
  149. string s = {FilePathSeparator::get()};
  150. string wrongFilePath = "home" + s + s + s + "user" + s + "bla" + s + s + "sub_folder";
  151. string expectedFilePath = "home" + s + "user" + s + "bla" + s + "sub_folder";
  152. File anotherFile{wrongFilePath};
  153. ASSERT_STREQ(expectedFilePath.c_str(), anotherFile.getAbsoluteFilePath().c_str());
  154. }
  155. TEST_F(FileTest, getName)
  156. {
  157. File file{this->fileLocation};
  158. File executableFile{TestHelper::getResourcesFolderLocation() + "app.exe"};
  159. File anotherFile{"bla.txt"};
  160. File directory{TestHelper::getResourcesFolderLocation()};
  161. ASSERT_STREQ("simple.txt", file.getName().c_str());
  162. ASSERT_STREQ("app.exe", executableFile.getName().c_str());
  163. ASSERT_STREQ("bla.txt", anotherFile.getName().c_str());
  164. ASSERT_STREQ("resources", directory.getName().c_str());
  165. }
  166. TEST_F(FileTest, getParent)
  167. {
  168. File file{this->fileLocation};
  169. ASSERT_STREQ(TestHelper::getResourcesFolderLocation().c_str(), file.getParent().c_str());
  170. }
  171. TEST_F(FileTest, getWorkingDirectory)
  172. {
  173. string workingDirectory = File::getWorkingDirectory();
  174. ASSERT_FALSE(workingDirectory.empty());
  175. }
  176. TEST_F(FileTest, getSize)
  177. {
  178. File file{this->fileLocation};
  179. size_t size = file.getSize();
  180. ASSERT_TRUE(size == 7 || size == 8); // different OS specific new lines
  181. }
  182. TEST_F(FileTest, isDirectory)
  183. {
  184. File directory{TestHelper::getResourcesFolderLocation()};
  185. ASSERT_TRUE(directory.isDirectory());
  186. }
  187. TEST_F(FileTest, isDirectory_is_a_file)
  188. {
  189. File file{this->fileLocation};
  190. ASSERT_FALSE(file.isDirectory());
  191. }
  192. TEST_F(FileTest, isFile)
  193. {
  194. const char separator = FilePathSeparator::get();
  195. File file{this->fileLocation};
  196. ASSERT_TRUE(file.isFile());
  197. File file2{TestHelper::getResourcesFolderLocation() + "list-test" + separator + "bla.txt"};
  198. ASSERT_TRUE(file2.isFile());
  199. }
  200. TEST_F(FileTest, isFile_is_a_directory)
  201. {
  202. File directory{TestHelper::getResourcesFolderLocation()};
  203. ASSERT_FALSE(directory.isFile());
  204. }
  205. TEST_F(FileTest, lastModified)
  206. {
  207. File file{this->fileLocation};
  208. ASSERT_TRUE(file.lastModified() > 1590000000);
  209. }
  210. TEST_F(FileTest, list)
  211. {
  212. File file{TestHelper::getResourcesFolderLocation() + "list-test"};
  213. list<string> filesInDirectory = file.list();
  214. string expectedFile{};
  215. const char separator = FilePathSeparator::get();
  216. ASSERT_FALSE(filesInDirectory.empty());
  217. ASSERT_EQ(7, filesInDirectory.size());
  218. expectedFile = file.getAbsoluteFilePath() + separator + ".";
  219. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  220. expectedFile = file.getAbsoluteFilePath() + separator + "..";
  221. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  222. expectedFile = file.getAbsoluteFilePath() + separator + "another-file.txt";
  223. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  224. expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
  225. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  226. expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
  227. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  228. expectedFile = file.getAbsoluteFilePath() + separator + "list-test-sub";
  229. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  230. expectedFile = file.getAbsoluteFilePath() + separator + ".hidden-file.txt";
  231. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  232. }
  233. TEST_F(FileTest, listFiles)
  234. {
  235. File file{TestHelper::getResourcesFolderLocation() + "list-test"};
  236. list<string> filesInDirectory = file.listFiles();
  237. string expectedFile{};
  238. const char separator = FilePathSeparator::get();
  239. ASSERT_FALSE(filesInDirectory.empty());
  240. ASSERT_EQ(4, filesInDirectory.size());
  241. expectedFile = file.getAbsoluteFilePath() + separator + "another-file.txt";
  242. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  243. expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
  244. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  245. expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
  246. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  247. expectedFile = file.getAbsoluteFilePath() + separator + ".hidden-file.txt";
  248. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  249. }
  250. TEST_F(FileTest, makeDirectory)
  251. {
  252. File directory{TestHelper::getResourcesFolderLocation() + "testDir"};
  253. ASSERT_FALSE(directory.exists());
  254. directory.makeDirectory();
  255. ASSERT_TRUE(directory.exists());
  256. directory.remove();
  257. ASSERT_FALSE(directory.exists());
  258. }
  259. TEST_F(FileTest, makeDirectory_directory_already_exists)
  260. {
  261. File directory{TestHelper::getResourcesFolderLocation() + "list-test"};
  262. EXPECT_THROW(
  263. {
  264. try
  265. {
  266. directory.makeDirectory();
  267. }
  268. catch (const FileOperationException &_exception)
  269. {
  270. string expected = _exception.getName() + R"lit( thrown - directory ")lit" + TestHelper::getResourcesFolderLocation() + R"lit(list-test" could not be created!)lit";
  271. string actual = _exception.what();
  272. ASSERT_STREQ(expected.c_str(), actual.c_str());
  273. throw;
  274. }
  275. },
  276. FileOperationException);
  277. }
  278. TEST_F(FileTest, makeDirectories)
  279. {
  280. File directory{TestHelper::getResourcesFolderLocation() + "testDir/sub/tmp/bla"};
  281. ASSERT_FALSE(directory.exists());
  282. directory.makeDirectories();
  283. ASSERT_TRUE(directory.exists());
  284. // clean up
  285. directory.remove();
  286. directory = File(TestHelper::getResourcesFolderLocation() + "testDir/sub/tmp");
  287. directory.remove();
  288. directory = File(TestHelper::getResourcesFolderLocation() + "testDir/sub");
  289. directory.remove();
  290. directory = File(TestHelper::getResourcesFolderLocation() + "testDir");
  291. directory.remove();
  292. }
  293. TEST_F(FileTest, remove)
  294. {
  295. string fileName = TestHelper::getResourcesFolderLocation() + "removable-file.txt";
  296. File file{fileName};
  297. file.createNewFile();
  298. ASSERT_TRUE(file.exists());
  299. file.remove();
  300. ASSERT_FALSE(file.exists());
  301. }
  302. TEST_F(FileTest, renameTo)
  303. {
  304. string currentName = TestHelper::getResourcesFolderLocation() + "tmp-rename-to.txt";
  305. string newName = TestHelper::getResourcesFolderLocation() + "tmp-rename-to-better-name.txt";
  306. File file{currentName};
  307. file.createNewFile();
  308. ASSERT_TRUE(file.exists());
  309. ASSERT_STREQ(currentName.c_str(), file.getAbsoluteFilePath().c_str());
  310. file.renameTo(newName);
  311. ASSERT_TRUE(file.exists());
  312. ASSERT_STREQ(newName.c_str(), file.getAbsoluteFilePath().c_str());
  313. file.remove();
  314. }
  315. TEST_F(FileTest, reset)
  316. {
  317. File file{this->fileLocation};
  318. ASSERT_TRUE(file.exists());
  319. file.reset(TestHelper::getResourcesFolderLocation() + "list-test/hello.txt");
  320. ASSERT_TRUE(file.exists());
  321. }
  322. }