FileTest.cpp 11 KB

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