FileTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-15
  6. * Changed: 2023-03-25
  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. throw;
  126. }
  127. },
  128. FileOperationException);
  129. }
  130. TEST_F(FileTest, exists)
  131. {
  132. File file{this->fileLocation};
  133. File directory{TestHelper::getResourcesFolderLocation()};
  134. ASSERT_TRUE(file.exists());
  135. ASSERT_TRUE(directory.exists());
  136. }
  137. TEST_F(FileTest, exists_does_not_exist)
  138. {
  139. File file{TestHelper::getResourcesFolderLocation() + "bla.txt"};
  140. ASSERT_FALSE(file.exists());
  141. }
  142. TEST_F(FileTest, getAbsoluteFilePath)
  143. {
  144. File file{this->fileLocation};
  145. ASSERT_STREQ(this->fileLocation.c_str(), file.getAbsoluteFilePath().c_str());
  146. string s = {FilePathSeparator::get()};
  147. string wrongFilePath = "home" + s + s + s + "user" + s + "bla" + s + s + "sub_folder";
  148. string expectedFilePath = "home" + s + "user" + s + "bla" + s + "sub_folder";
  149. File anotherFile{wrongFilePath};
  150. ASSERT_STREQ(expectedFilePath.c_str(), anotherFile.getAbsoluteFilePath().c_str());
  151. }
  152. TEST_F(FileTest, getName)
  153. {
  154. File file{this->fileLocation};
  155. File executableFile{TestHelper::getResourcesFolderLocation() + "app.exe"};
  156. File anotherFile{"bla.txt"};
  157. File directory{TestHelper::getResourcesFolderLocation()};
  158. ASSERT_STREQ("simple.txt", file.getName().c_str());
  159. ASSERT_STREQ("app.exe", executableFile.getName().c_str());
  160. ASSERT_STREQ("bla.txt", anotherFile.getName().c_str());
  161. ASSERT_STREQ("resources", directory.getName().c_str());
  162. }
  163. TEST_F(FileTest, getParent)
  164. {
  165. File file{this->fileLocation};
  166. ASSERT_STREQ(TestHelper::getResourcesFolderLocation().c_str(), file.getParent().c_str());
  167. }
  168. TEST_F(FileTest, getWorkingDirectory)
  169. {
  170. string workingDirectory = File::getWorkingDirectory();
  171. ASSERT_FALSE(workingDirectory.empty());
  172. }
  173. TEST_F(FileTest, getSize)
  174. {
  175. File file{this->fileLocation};
  176. size_t size = file.getSize();
  177. ASSERT_TRUE(size == 7 || size == 8); // different OS specific new lines
  178. }
  179. TEST_F(FileTest, isDirectory)
  180. {
  181. File directory{TestHelper::getResourcesFolderLocation()};
  182. ASSERT_TRUE(directory.isDirectory());
  183. }
  184. TEST_F(FileTest, isDirectory_is_a_file)
  185. {
  186. File file{this->fileLocation};
  187. ASSERT_FALSE(file.isDirectory());
  188. }
  189. TEST_F(FileTest, isFile)
  190. {
  191. const char separator = FilePathSeparator::get();
  192. File file{this->fileLocation};
  193. ASSERT_TRUE(file.isFile());
  194. File file2{TestHelper::getResourcesFolderLocation() + "list-test" + separator + "bla.txt"};
  195. ASSERT_TRUE(file2.isFile());
  196. }
  197. TEST_F(FileTest, isFile_is_a_directory)
  198. {
  199. File directory{TestHelper::getResourcesFolderLocation()};
  200. ASSERT_FALSE(directory.isFile());
  201. }
  202. TEST_F(FileTest, lastModified)
  203. {
  204. File file{this->fileLocation};
  205. ASSERT_TRUE(file.lastModified() > 1590000000);
  206. }
  207. TEST_F(FileTest, list)
  208. {
  209. File file{TestHelper::getResourcesFolderLocation() + "list-test"};
  210. list<string> filesInDirectory = file.list();
  211. string expectedFile{};
  212. const char separator = FilePathSeparator::get();
  213. ASSERT_FALSE(filesInDirectory.empty());
  214. ASSERT_EQ(7, filesInDirectory.size());
  215. expectedFile = file.getAbsoluteFilePath() + separator + ".";
  216. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  217. expectedFile = file.getAbsoluteFilePath() + separator + "..";
  218. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  219. expectedFile = file.getAbsoluteFilePath() + separator + "another-file.txt";
  220. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  221. expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
  222. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  223. expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
  224. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  225. expectedFile = file.getAbsoluteFilePath() + separator + "list-test-sub";
  226. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  227. expectedFile = file.getAbsoluteFilePath() + separator + ".hidden-file.txt";
  228. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  229. }
  230. TEST_F(FileTest, listFiles)
  231. {
  232. File file{TestHelper::getResourcesFolderLocation() + "list-test"};
  233. list<string> filesInDirectory = file.listFiles();
  234. string expectedFile{};
  235. const char separator = FilePathSeparator::get();
  236. ASSERT_FALSE(filesInDirectory.empty());
  237. ASSERT_EQ(4, filesInDirectory.size());
  238. expectedFile = file.getAbsoluteFilePath() + separator + "another-file.txt";
  239. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  240. expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
  241. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  242. expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
  243. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  244. expectedFile = file.getAbsoluteFilePath() + separator + ".hidden-file.txt";
  245. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  246. }
  247. TEST_F(FileTest, makeDirectory)
  248. {
  249. File directory{TestHelper::getResourcesFolderLocation() + "testDir"};
  250. ASSERT_FALSE(directory.exists());
  251. directory.makeDirectory();
  252. ASSERT_TRUE(directory.exists());
  253. directory.remove();
  254. ASSERT_FALSE(directory.exists());
  255. }
  256. TEST_F(FileTest, makeDirectory_directory_already_exists)
  257. {
  258. File directory{TestHelper::getResourcesFolderLocation() + "list-test"};
  259. EXPECT_THROW(
  260. {
  261. try
  262. {
  263. directory.makeDirectory();
  264. }
  265. catch (const FileOperationException &_exception)
  266. {
  267. throw;
  268. }
  269. },
  270. FileOperationException);
  271. }
  272. TEST_F(FileTest, makeDirectories)
  273. {
  274. File directory{TestHelper::getResourcesFolderLocation() + "testDir/sub/tmp/bla"};
  275. ASSERT_FALSE(directory.exists());
  276. directory.makeDirectories();
  277. ASSERT_TRUE(directory.exists());
  278. // clean up
  279. directory.remove();
  280. directory = File(TestHelper::getResourcesFolderLocation() + "testDir/sub/tmp");
  281. directory.remove();
  282. directory = File(TestHelper::getResourcesFolderLocation() + "testDir/sub");
  283. directory.remove();
  284. directory = File(TestHelper::getResourcesFolderLocation() + "testDir");
  285. directory.remove();
  286. }
  287. TEST_F(FileTest, remove)
  288. {
  289. string fileName = TestHelper::getResourcesFolderLocation() + "removable-file.txt";
  290. File file{fileName};
  291. file.createNewFile();
  292. ASSERT_TRUE(file.exists());
  293. file.remove();
  294. ASSERT_FALSE(file.exists());
  295. }
  296. TEST_F(FileTest, renameTo)
  297. {
  298. string currentName = TestHelper::getResourcesFolderLocation() + "tmp-rename-to.txt";
  299. string newName = TestHelper::getResourcesFolderLocation() + "tmp-rename-to-better-name.txt";
  300. File file{currentName};
  301. file.createNewFile();
  302. ASSERT_TRUE(file.exists());
  303. ASSERT_STREQ(currentName.c_str(), file.getAbsoluteFilePath().c_str());
  304. file.renameTo(newName);
  305. ASSERT_TRUE(file.exists());
  306. ASSERT_STREQ(newName.c_str(), file.getAbsoluteFilePath().c_str());
  307. file.remove();
  308. }
  309. TEST_F(FileTest, reset)
  310. {
  311. File file{this->fileLocation};
  312. ASSERT_TRUE(file.exists());
  313. file.reset(TestHelper::getResourcesFolderLocation() + "list-test/hello.txt");
  314. ASSERT_TRUE(file.exists());
  315. }
  316. }