Skip to content

Commit 42df3e2

Browse files
committed
[VirtualFileSystem] Add unit test for vfs::YAMLVFSWriter
Add a unit test for vfs::YAMLVFSWriter. This patch exposes an issue in the writer: when we call addFileMapping with a directory, the VFS writer will emit it as a regular file, causing any of the nested files or directories to not be found.
1 parent 8140f6b commit 42df3e2

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

llvm/unittests/Support/VirtualFileSystemTest.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,3 +2188,59 @@ TEST_F(VFSFromYAMLTest, WorkingDirectoryFallthroughInvalid) {
21882188
Status = FS->status("foo/a");
21892189
ASSERT_TRUE(Status.getError());
21902190
}
2191+
2192+
TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest) {
2193+
ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
2194+
ScopedDir _a(TestDirectory + "/a");
2195+
ScopedFile _ab(TestDirectory + "/a/b", "");
2196+
ScopedDir _c(TestDirectory + "/c");
2197+
ScopedFile _cd(TestDirectory + "/c/d", "");
2198+
ScopedDir _e(TestDirectory + "/e");
2199+
ScopedDir _ef(TestDirectory + "/e/f");
2200+
ScopedDir _g(TestDirectory + "/g");
2201+
ScopedFile _h(TestDirectory + "/h", "");
2202+
2203+
// This test exposes a bug/shortcoming in the YAMLVFSWriter. Below we call
2204+
// addFileMapping for _a and _e, which causes _ab and _ef not to exists in
2205+
// the deserialized file system, because _a and _e got emitted as regular
2206+
// files. The counter example is _c, if we only call addFileMapping for _cd,
2207+
// things work as expected.
2208+
2209+
vfs::YAMLVFSWriter VFSWriter;
2210+
VFSWriter.addFileMapping(_a.Path, "//root/a");
2211+
VFSWriter.addFileMapping(_ab.Path, "//root/a/b");
2212+
VFSWriter.addFileMapping(_cd.Path, "//root/c/d");
2213+
VFSWriter.addFileMapping(_e.Path, "//root/e");
2214+
VFSWriter.addFileMapping(_ef.Path, "//root/e/f");
2215+
VFSWriter.addFileMapping(_g.Path, "//root/g");
2216+
VFSWriter.addFileMapping(_h.Path, "//root/h");
2217+
2218+
std::string Buffer;
2219+
raw_string_ostream OS(Buffer);
2220+
VFSWriter.write(OS);
2221+
OS.flush();
2222+
2223+
IntrusiveRefCntPtr<ErrorDummyFileSystem> Lower(new ErrorDummyFileSystem());
2224+
Lower->addDirectory("//root/");
2225+
Lower->addDirectory("//root/a");
2226+
Lower->addRegularFile("//root/a/b");
2227+
Lower->addDirectory("//root/b");
2228+
Lower->addDirectory("//root/c");
2229+
Lower->addRegularFile("//root/c/d");
2230+
Lower->addDirectory("//root/e");
2231+
Lower->addDirectory("//root/e/f");
2232+
Lower->addDirectory("//root/g");
2233+
Lower->addRegularFile("//root/h");
2234+
2235+
IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLRawString(Buffer, Lower);
2236+
ASSERT_TRUE(FS.get() != nullptr);
2237+
2238+
EXPECT_TRUE(FS->exists(_a.Path));
2239+
EXPECT_FALSE(FS->exists(_ab.Path)); // FIXME: See explanation above.
2240+
EXPECT_TRUE(FS->exists(_c.Path));
2241+
EXPECT_TRUE(FS->exists(_cd.Path));
2242+
EXPECT_TRUE(FS->exists(_e.Path));
2243+
EXPECT_FALSE(FS->exists(_ef.Path)); // FIXME: See explanation above.
2244+
EXPECT_TRUE(FS->exists(_g.Path));
2245+
EXPECT_TRUE(FS->exists(_h.Path));
2246+
}

0 commit comments

Comments
 (0)