[VFS] Add an in-memory file system implementation.
This is a simple file system tree of memory buffers that can be filled by a
client. In conjunction with an OverlayFS it can be used to make virtual
files accessible right next to physical files. This can be used as a
replacement for the virtual file handling in FileManager and which I intend
to remove eventually.
llvm-svn: 249315
diff --git a/clang/unittests/Basic/VirtualFileSystemTest.cpp b/clang/unittests/Basic/VirtualFileSystemTest.cpp
index 13ae501..4e5ec87 100644
--- a/clang/unittests/Basic/VirtualFileSystemTest.cpp
+++ b/clang/unittests/Basic/VirtualFileSystemTest.cpp
@@ -515,6 +515,73 @@
}
}
+class InMemoryFileSystemTest : public ::testing::Test {
+protected:
+ clang::vfs::InMemoryFileSystem FS;
+};
+
+TEST_F(InMemoryFileSystemTest, IsEmpty) {
+ auto Stat = FS.status("/a");
+ ASSERT_EQ(errc::no_such_file_or_directory, Stat.getError()) << FS.toString();
+ Stat = FS.status("/");
+ ASSERT_EQ(errc::no_such_file_or_directory, Stat.getError()) << FS.toString();
+}
+
+TEST_F(InMemoryFileSystemTest, WindowsPath) {
+ FS.addFile("c:/windows/system128/foo.cpp", 0, MemoryBuffer::getMemBuffer(""));
+ auto Stat = FS.status("c:");
+ ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
+ Stat = FS.status("c:/windows/system128/foo.cpp");
+ ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
+ FS.addFile("d:/windows/foo.cpp", 0, MemoryBuffer::getMemBuffer(""));
+ Stat = FS.status("d:/windows/foo.cpp");
+ ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
+}
+
+TEST_F(InMemoryFileSystemTest, OverlayFile) {
+ FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
+ auto Stat = FS.status("/");
+ ASSERT_FALSE(Stat.getError()) << Stat.getError() << FS.toString();
+ Stat = FS.status("/a");
+ ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
+ ASSERT_EQ("/a", Stat->getName());
+}
+
+TEST_F(InMemoryFileSystemTest, OpenFileForRead) {
+ FS.addFile("/a", 0, MemoryBuffer::getMemBuffer("a"));
+ auto File = FS.openFileForRead("/a");
+ ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
+ File = FS.openFileForRead("/a"); // Open again.
+ ASSERT_EQ("a", (*(*File)->getBuffer("ignored"))->getBuffer());
+ File = FS.openFileForRead("/");
+ ASSERT_EQ(errc::invalid_argument, File.getError()) << FS.toString();
+ File = FS.openFileForRead("/b");
+ ASSERT_EQ(errc::no_such_file_or_directory, File.getError()) << FS.toString();
+}
+
+TEST_F(InMemoryFileSystemTest, DirectoryIteration) {
+ FS.addFile("/a", 0, MemoryBuffer::getMemBuffer(""));
+ FS.addFile("/b/c", 0, MemoryBuffer::getMemBuffer(""));
+
+ std::error_code EC;
+ vfs::directory_iterator I = FS.dir_begin("/", EC);
+ ASSERT_FALSE(EC);
+ ASSERT_EQ("/a", I->getName());
+ I.increment(EC);
+ ASSERT_FALSE(EC);
+ ASSERT_EQ("/b", I->getName());
+ I.increment(EC);
+ ASSERT_FALSE(EC);
+ ASSERT_EQ(vfs::directory_iterator(), I);
+
+ I = FS.dir_begin("/b", EC);
+ ASSERT_FALSE(EC);
+ ASSERT_EQ("/b/c", I->getName());
+ I.increment(EC);
+ ASSERT_FALSE(EC);
+ ASSERT_EQ(vfs::directory_iterator(), I);
+}
+
// NOTE: in the tests below, we use '//root/' as our root directory, since it is
// a legal *absolute* path on Windows as well as *nix.
class VFSFromYAMLTest : public ::testing::Test {