Browse Source

Fixed File class

- fixed "list" and "listFiles" method by fixing
absolute file path of current parsed file
Patrick 4 years ago
parent
commit
9f1e84e930
2 changed files with 23 additions and 16 deletions
  1. 5 3
      source/io/File.cpp
  2. 18 13
      test/cases/io/FileTest.cpp

+ 5 - 3
source/io/File.cpp

@@ -273,9 +273,10 @@ std::list<std::string> ls_std::File::_listUnix(const std::string &_path, bool wi
   struct dirent* directoryEntity;
   std::string parent = ls_std::File::_getParent(_path);
   std::string absolutePath {};
+  const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
 
   while((directoryEntity = readdir(directory)) != nullptr) {
-    absolutePath = parent + directoryEntity->d_name;
+    absolutePath = _path + separator + directoryEntity->d_name;
 
     if(withDirectories) {
       filesInDirectory.emplace_back(absolutePath);
@@ -297,12 +298,13 @@ std::list<std::string> ls_std::File::_listWindows(const std::string &_path, bool
   WIN32_FIND_DATA data {};
   HANDLE hFind;
   std::string parent = ls_std::File::_getParent(_path);
-  std::string pattern {_path + ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator() + "*"};
+  const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
+  std::string pattern {_path + separator + "*"};
   std::string absolutePath {};
 
   if((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE) {
     do {
-      absolutePath = parent + data.cFileName;
+      absolutePath = _path + separator + data.cFileName;
 
       if(withDirectories) {
         filesInDirectory.emplace_back(absolutePath);

+ 18 - 13
test/cases/io/FileTest.cpp

@@ -148,8 +148,13 @@ namespace {
 
   TEST_F(FileTest, isFile)
   {
+    const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
+
     ls_std::File file {this->fileLocation};
     ASSERT_TRUE(file.isFile());
+
+    ls_std::File file2 {TestHelper::getResourcesFolderLocation() + "list_test" + separator + "bla.txt"};
+    ASSERT_TRUE(file2.isFile());
   }
 
   TEST_F(FileTest, isFileNegative)
@@ -169,25 +174,26 @@ namespace {
     ls_std::File file {TestHelper::getResourcesFolderLocation() + "list_test"};
     std::list<std::string> filesInDirectory = file.list();
     std::string expectedFile {};
+    const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
 
     auto filesIterator = filesInDirectory.begin();
 
     ASSERT_FALSE(filesInDirectory.empty());
     ASSERT_EQ(7, filesInDirectory.size());
 
-    expectedFile = file.getParent() + ".";
+    expectedFile = file.getAbsoluteFilePath() + separator + ".";
     ASSERT_TRUE((ls_std::STLUtils<std::list<std::string>, std::string>::contains(filesInDirectory, expectedFile)));
-    expectedFile = file.getParent() + "..";
+    expectedFile = file.getAbsoluteFilePath() + separator + "..";
     ASSERT_TRUE((ls_std::STLUtils<std::list<std::string>, std::string>::contains(filesInDirectory, expectedFile)));
-    expectedFile = file.getParent() + "another_file.txt";
+    expectedFile = file.getAbsoluteFilePath() + separator + "another_file.txt";
     ASSERT_TRUE((ls_std::STLUtils<std::list<std::string>, std::string>::contains(filesInDirectory, expectedFile)));
-    expectedFile = file.getParent() + "bla.txt";
+    expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
     ASSERT_TRUE((ls_std::STLUtils<std::list<std::string>, std::string>::contains(filesInDirectory, expectedFile)));
-    expectedFile = file.getParent() + "hello.txt";
+    expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
     ASSERT_TRUE((ls_std::STLUtils<std::list<std::string>, std::string>::contains(filesInDirectory, expectedFile)));
-    expectedFile = file.getParent() + "list_test_sub";
+    expectedFile = file.getAbsoluteFilePath() + separator + "list_test_sub";
     ASSERT_TRUE((ls_std::STLUtils<std::list<std::string>, std::string>::contains(filesInDirectory, expectedFile)));
-    expectedFile = file.getParent() + ".hidden_file.txt";
+    expectedFile = file.getAbsoluteFilePath() + separator + ".hidden_file.txt";
     ASSERT_TRUE((ls_std::STLUtils<std::list<std::string>, std::string>::contains(filesInDirectory, expectedFile)));
   }
 
@@ -196,19 +202,18 @@ namespace {
     ls_std::File file {TestHelper::getResourcesFolderLocation() + "list_test"};
     std::list<std::string> filesInDirectory = file.listFiles();
     std::string expectedFile {};
-
-    auto filesIterator = filesInDirectory.begin();
+    const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
 
     ASSERT_FALSE(filesInDirectory.empty());
     ASSERT_EQ(4, filesInDirectory.size());
 
-    expectedFile = file.getParent() + "another_file.txt";
+    expectedFile = file.getAbsoluteFilePath() + separator + "another_file.txt";
     ASSERT_TRUE((ls_std::STLUtils<std::list<std::string>, std::string>::contains(filesInDirectory, expectedFile)));
-    expectedFile = file.getParent() + "bla.txt";
+    expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
     ASSERT_TRUE((ls_std::STLUtils<std::list<std::string>, std::string>::contains(filesInDirectory, expectedFile)));
-    expectedFile = file.getParent() + "hello.txt";
+    expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
     ASSERT_TRUE((ls_std::STLUtils<std::list<std::string>, std::string>::contains(filesInDirectory, expectedFile)));
-    expectedFile = file.getParent() + ".hidden_file.txt";
+    expectedFile = file.getAbsoluteFilePath() + separator + ".hidden_file.txt";
     ASSERT_TRUE((ls_std::STLUtils<std::list<std::string>, std::string>::contains(filesInDirectory, expectedFile)));
   }