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-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. ASSERT_FALSE(filesInDirectory.empty());
  218. ASSERT_EQ(7, filesInDirectory.size());
  219. expectedFile = file.getAbsoluteFilePath() + separator + ".";
  220. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  221. expectedFile = file.getAbsoluteFilePath() + separator + "..";
  222. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  223. expectedFile = file.getAbsoluteFilePath() + separator + "another-file.txt";
  224. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  225. expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
  226. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  227. expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
  228. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  229. expectedFile = file.getAbsoluteFilePath() + separator + "list-test-sub";
  230. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  231. expectedFile = file.getAbsoluteFilePath() + separator + ".hidden-file.txt";
  232. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  233. }
  234. TEST_F(FileTest, listFiles)
  235. {
  236. File file{TestHelper::getResourcesFolderLocation() + "list-test"};
  237. list<string> filesInDirectory = file.listFiles();
  238. string expectedFile{};
  239. const char separator = FilePathSeparator::get();
  240. ASSERT_FALSE(filesInDirectory.empty());
  241. ASSERT_EQ(4, filesInDirectory.size());
  242. expectedFile = file.getAbsoluteFilePath() + separator + "another-file.txt";
  243. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  244. expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
  245. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  246. expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
  247. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  248. expectedFile = file.getAbsoluteFilePath() + separator + ".hidden-file.txt";
  249. ASSERT_TRUE(FileTest::hasFileInDirectory(filesInDirectory, expectedFile));
  250. }
  251. TEST_F(FileTest, makeDirectory)
  252. {
  253. File directory{TestHelper::getResourcesFolderLocation() + "testDir"};
  254. ASSERT_FALSE(directory.exists());
  255. directory.makeDirectory();
  256. ASSERT_TRUE(directory.exists());
  257. directory.remove();
  258. ASSERT_FALSE(directory.exists());
  259. }
  260. TEST_F(FileTest, makeDirectory_directory_already_exists)
  261. {
  262. File directory{TestHelper::getResourcesFolderLocation() + "list-test"};
  263. EXPECT_THROW(
  264. {
  265. try
  266. {
  267. directory.makeDirectory();
  268. }
  269. catch (const FileOperationException &_exception)
  270. {
  271. throw;
  272. }
  273. },
  274. FileOperationException);
  275. }
  276. TEST_F(FileTest, makeDirectories)
  277. {
  278. File directory{TestHelper::getResourcesFolderLocation() + "testDir/sub/tmp/bla"};
  279. ASSERT_FALSE(directory.exists());
  280. directory.makeDirectories();
  281. ASSERT_TRUE(directory.exists());
  282. // clean up
  283. directory.remove();
  284. directory = File(TestHelper::getResourcesFolderLocation() + "testDir/sub/tmp");
  285. directory.remove();
  286. directory = File(TestHelper::getResourcesFolderLocation() + "testDir/sub");
  287. directory.remove();
  288. directory = File(TestHelper::getResourcesFolderLocation() + "testDir");
  289. directory.remove();
  290. }
  291. TEST_F(FileTest, remove)
  292. {
  293. string fileName = TestHelper::getResourcesFolderLocation() + "removable-file.txt";
  294. File file{fileName};
  295. file.createNewFile();
  296. ASSERT_TRUE(file.exists());
  297. file.remove();
  298. ASSERT_FALSE(file.exists());
  299. }
  300. TEST_F(FileTest, renameTo)
  301. {
  302. string currentName = TestHelper::getResourcesFolderLocation() + "tmp-rename-to.txt";
  303. string newName = TestHelper::getResourcesFolderLocation() + "tmp-rename-to-better-name.txt";
  304. File file{currentName};
  305. file.createNewFile();
  306. ASSERT_TRUE(file.exists());
  307. ASSERT_STREQ(currentName.c_str(), file.getAbsoluteFilePath().c_str());
  308. file.renameTo(newName);
  309. ASSERT_TRUE(file.exists());
  310. ASSERT_STREQ(newName.c_str(), file.getAbsoluteFilePath().c_str());
  311. file.remove();
  312. }
  313. TEST_F(FileTest, reset)
  314. {
  315. File file{this->fileLocation};
  316. ASSERT_TRUE(file.exists());
  317. file.reset(TestHelper::getResourcesFolderLocation() + "list-test/hello.txt");
  318. ASSERT_TRUE(file.exists());
  319. }
  320. }