-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
454 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Including Headers | ||
|
||
`src/fs.cc` and `src/fs.h` | ||
|
||
- `.cc` must have the fn impl - (non-inline) or Visibility is Private | ||
- `.cc` must include the Header def file `#include <fs.h>` | ||
|
||
## Files that need Access to Funcs | ||
|
||
- Simply add the `.cc` file to the `Add Executable` so that CMake will compile | ||
it along with the Files | ||
|
||
```bash | ||
add_executable(${TEST_EXECUTABLE} tests/${TEST_EXECUTABLE}.cc src/fs.cc) | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
#include "fs.h" | ||
#include <llvm/Support/FileSystem.h> | ||
#include <llvm/Support/FormatVariadic.h> | ||
#include <llvm/Support/Path.h> | ||
|
||
auto getFilePaths(const llvm::Twine &directoryPath) -> llvm::Expected<std::vector<std::string>> { | ||
std::error_code ERR; | ||
llvm::sys::fs::directory_iterator dirIt(directoryPath, ERR); | ||
|
||
if (ERR) { | ||
return llvm::make_error<llvm::StringError>(ERR.message(), ERR); | ||
} | ||
|
||
llvm::sys::fs::directory_iterator dirEnd; | ||
std::vector<std::string> filePaths; | ||
|
||
for (; dirIt != dirEnd && !ERR; dirIt.increment(ERR)) { | ||
if (ERR) { | ||
return llvm::make_error<llvm::StringError>(ERR.message(), ERR); | ||
} | ||
filePaths.push_back(dirIt->path()); | ||
} | ||
|
||
return filePaths; | ||
} | ||
auto getFileInfo(const llvm::Twine &Path) -> llvm::Expected<llvm::sys::fs::file_status> { | ||
llvm::sys::fs::file_status Status; | ||
if (std::error_code ERR = llvm::sys::fs::status(Path, Status)) { | ||
return llvm::make_error<llvm::StringError>(ERR.message(), ERR); | ||
} | ||
return Status; | ||
} | ||
|
||
auto createDirectories(const llvm::Twine &path) -> llvm::Expected<bool> { | ||
if (auto err = llvm::sys::fs::create_directories(path); err) { | ||
return llvm::make_error<llvm::StringError>(err.message(), err); | ||
} | ||
return true; | ||
} | ||
|
||
auto createDirectoryForFile(const llvm::Twine &filePath) -> llvm::Expected<bool> { | ||
llvm::SmallString<256> fullPathStorage; | ||
filePath.toVector(fullPathStorage); | ||
|
||
llvm::StringRef fullPathRef(fullPathStorage); | ||
|
||
llvm::StringRef directoryPathRef = llvm::sys::path::parent_path(fullPathRef); | ||
|
||
llvm::SmallString<256> directoryPath(directoryPathRef); | ||
|
||
if (llvm::sys::fs::exists(directoryPath)) { | ||
return true; | ||
} | ||
|
||
if (auto err = llvm::sys::fs::create_directories(directoryPath); err) { | ||
llvm::errs() << "Error creating directory '" << directoryPath << "': " << err.message() << "\n"; | ||
return llvm::make_error<llvm::StringError>( | ||
llvm::formatv("Error creating directory {0}: {1}", directoryPath.str(), err.message()), err); | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// fs.h | ||
#ifndef FS_H | ||
#define FS_H | ||
|
||
#include <llvm/Support/FileSystem.h> | ||
|
||
auto getFilePaths(const llvm::Twine &directoryPath) -> llvm::Expected<std::vector<std::string>>; | ||
|
||
auto getFileInfo(const llvm::Twine &Path) -> llvm::Expected<llvm::sys::fs::file_status>; | ||
|
||
auto createDirectories(const llvm::Twine &path) -> llvm::Expected<bool>; | ||
|
||
auto createDirectoryForFile(const llvm::Twine &filePath) -> llvm::Expected<bool>; | ||
|
||
#endif // FS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include <gtest/gtest.h> | ||
#include <llvm/Support/Error.h> | ||
#include <src/fs.h> | ||
|
||
template <typename T> | ||
auto ErrorExists(llvm::Expected<T> &expected, const std::string &errorMessage) -> bool { | ||
if (!expected) { | ||
llvm::Error err = expected.takeError(); | ||
std::cerr << "Assert Failure:" << errorMessage << ": " << llvm::toString(std::move(err)) << '\n'; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
class LLVMFsTests: public ::testing::Test { | ||
protected: | ||
void SetUp() override { | ||
} | ||
|
||
void TearDown() override { | ||
} | ||
|
||
public: | ||
const std::string validFolder = "../../images"; | ||
const std::string emptyFolder = "../../empty"; | ||
}; | ||
|
||
TEST_F(LLVMFsTests, GetFilePathsEmpty) { | ||
auto result = getFilePaths(emptyFolder); | ||
|
||
ASSERT_FALSE(ErrorExists(result, "Expected valid result, but got an error")); | ||
|
||
if (!result) { | ||
llvm::Error err = result.takeError(); | ||
llvm::consumeError(std::move(err)); | ||
FAIL() << "Expected valid result, but got error."; | ||
} | ||
ASSERT_TRUE(result->empty()) << "Expected empty directory."; | ||
} | ||
|
||
TEST_F(LLVMFsTests, GetFilePaths) { | ||
auto result = getFilePaths(validFolder); | ||
ASSERT_FALSE(ErrorExists(result, "Valid Folder expected no Error")); | ||
|
||
auto emptyPathResult = getFilePaths(emptyFolder); | ||
ASSERT_FALSE(ErrorExists(emptyPathResult, "Empty Dir should cause no Errors")); | ||
|
||
auto invalidPathResult = getFilePaths("path/to/non/existing/directory"); | ||
|
||
ASSERT_TRUE(ErrorExists(invalidPathResult, "Invalid Dir should return an Error")); | ||
} | ||
|
||
TEST_F(LLVMFsTests, GetFileInfo) { | ||
auto fileInfoResult = getFileInfo(validFolder + "/imgtext.jpeg"); | ||
ASSERT_FALSE(ErrorExists(fileInfoResult, "File Info Valid")); | ||
|
||
auto directoryInfoResult = getFileInfo(validFolder); | ||
ASSERT_FALSE(ErrorExists(directoryInfoResult, "Dir Info Valid")); | ||
|
||
auto nonExistentResult = getFileInfo("/path/to/non/existent/file.txt"); | ||
ASSERT_TRUE(ErrorExists(nonExistentResult, "Non Existent should Return an Error")); | ||
} | ||
|
||
TEST_F(LLVMFsTests, DirectoryCreationTests) { | ||
auto existingDirResult = createDirectories(validFolder); | ||
|
||
ASSERT_FALSE(ErrorExists(existingDirResult, "Creating existing directory should not error")); | ||
ASSERT_TRUE(*existingDirResult); | ||
|
||
auto newDirResult = createDirectories("llvmtest"); | ||
|
||
ASSERT_FALSE(ErrorExists(newDirResult, "Creating new directory should succeed")); | ||
ASSERT_TRUE(*newDirResult); | ||
|
||
auto fileInNewDir = createDirectoryForFile("llvmfiletest/mock.txt"); | ||
ASSERT_FALSE(ErrorExists(fileInNewDir, "Creating directory for new file should succeed")); | ||
ASSERT_TRUE(*fileInNewDir); | ||
|
||
auto fileInExistingDir = createDirectoryForFile("llvmfiletest/mock.txt"); | ||
ASSERT_FALSE(ErrorExists(fileInExistingDir, "Creating directory for an existing file should still succeed")); | ||
ASSERT_TRUE(*fileInExistingDir); | ||
} | ||
|
||
auto main(int argc, char **argv) -> int { | ||
testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
Oops, something went wrong.