[VirtualFileSystem] Add unit test for vfs::YAMLVFSWriter
authorJonas Devlieghere <jonas@devlieghere.com>
Tue, 24 Mar 2020 00:47:18 +0000 (17:47 -0700)
committerJonas Devlieghere <jonas@devlieghere.com>
Tue, 24 Mar 2020 01:49:06 +0000 (18:49 -0700)
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.

llvm/unittests/Support/VirtualFileSystemTest.cpp

index acd5264..8ac454b 100644 (file)
@@ -2188,3 +2188,59 @@ TEST_F(VFSFromYAMLTest, WorkingDirectoryFallthroughInvalid) {
   Status = FS->status("foo/a");
   ASSERT_TRUE(Status.getError());
 }
+
+TEST_F(VFSFromYAMLTest, YAMLVFSWriterTest) {
+  ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
+  ScopedDir _a(TestDirectory + "/a");
+  ScopedFile _ab(TestDirectory + "/a/b", "");
+  ScopedDir _c(TestDirectory + "/c");
+  ScopedFile _cd(TestDirectory + "/c/d", "");
+  ScopedDir _e(TestDirectory + "/e");
+  ScopedDir _ef(TestDirectory + "/e/f");
+  ScopedDir _g(TestDirectory + "/g");
+  ScopedFile _h(TestDirectory + "/h", "");
+
+  // This test exposes a bug/shortcoming in the YAMLVFSWriter. Below we call
+  // addFileMapping for _a and _e, which causes _ab and _ef not to exists in
+  // the deserialized file system, because _a and _e got emitted as regular
+  // files. The counter example is _c, if we only call addFileMapping for _cd,
+  // things work as expected.
+
+  vfs::YAMLVFSWriter VFSWriter;
+  VFSWriter.addFileMapping(_a.Path, "//root/a");
+  VFSWriter.addFileMapping(_ab.Path, "//root/a/b");
+  VFSWriter.addFileMapping(_cd.Path, "//root/c/d");
+  VFSWriter.addFileMapping(_e.Path, "//root/e");
+  VFSWriter.addFileMapping(_ef.Path, "//root/e/f");
+  VFSWriter.addFileMapping(_g.Path, "//root/g");
+  VFSWriter.addFileMapping(_h.Path, "//root/h");
+
+  std::string Buffer;
+  raw_string_ostream OS(Buffer);
+  VFSWriter.write(OS);
+  OS.flush();
+
+  IntrusiveRefCntPtr<ErrorDummyFileSystem> Lower(new ErrorDummyFileSystem());
+  Lower->addDirectory("//root/");
+  Lower->addDirectory("//root/a");
+  Lower->addRegularFile("//root/a/b");
+  Lower->addDirectory("//root/b");
+  Lower->addDirectory("//root/c");
+  Lower->addRegularFile("//root/c/d");
+  Lower->addDirectory("//root/e");
+  Lower->addDirectory("//root/e/f");
+  Lower->addDirectory("//root/g");
+  Lower->addRegularFile("//root/h");
+
+  IntrusiveRefCntPtr<vfs::FileSystem> FS = getFromYAMLRawString(Buffer, Lower);
+  ASSERT_TRUE(FS.get() != nullptr);
+
+  EXPECT_TRUE(FS->exists(_a.Path));
+  EXPECT_FALSE(FS->exists(_ab.Path)); // FIXME: See explanation above.
+  EXPECT_TRUE(FS->exists(_c.Path));
+  EXPECT_TRUE(FS->exists(_cd.Path));
+  EXPECT_TRUE(FS->exists(_e.Path));
+  EXPECT_FALSE(FS->exists(_ef.Path)); // FIXME: See explanation above.
+  EXPECT_TRUE(FS->exists(_g.Path));
+  EXPECT_TRUE(FS->exists(_h.Path));
+}