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