FileTest.cpp 11 KB

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