FileTest.cpp 11 KB

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