FileManagerTest.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //===- unittests/Basic/FileMangerTest.cpp ------------ FileManger tests ---===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/Basic/FileManager.h"
  10. #include "clang/Basic/FileSystemOptions.h"
  11. #include "clang/Basic/FileSystemStatCache.h"
  12. #include "gtest/gtest.h"
  13. #include "llvm/Config/llvm-config.h"
  14. using namespace llvm;
  15. using namespace clang;
  16. namespace {
  17. // Used to create a fake file system for running the tests with such
  18. // that the tests are not affected by the structure/contents of the
  19. // file system on the machine running the tests.
  20. class FakeStatCache : public FileSystemStatCache {
  21. private:
  22. // Maps a file/directory path to its desired stat result. Anything
  23. // not in this map is considered to not exist in the file system.
  24. llvm::StringMap<FileData, llvm::BumpPtrAllocator> StatCalls;
  25. void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) {
  26. FileData Data;
  27. Data.Name = Path;
  28. Data.Size = 0;
  29. Data.ModTime = 0;
  30. Data.UniqueID = llvm::sys::fs::UniqueID(1, INode);
  31. Data.IsDirectory = !IsFile;
  32. Data.IsNamedPipe = false;
  33. Data.InPCH = false;
  34. StatCalls[Path] = Data;
  35. }
  36. public:
  37. // Inject a file with the given inode value to the fake file system.
  38. void InjectFile(const char *Path, ino_t INode) {
  39. InjectFileOrDirectory(Path, INode, /*IsFile=*/true);
  40. }
  41. // Inject a directory with the given inode value to the fake file system.
  42. void InjectDirectory(const char *Path, ino_t INode) {
  43. InjectFileOrDirectory(Path, INode, /*IsFile=*/false);
  44. }
  45. // Implement FileSystemStatCache::getStat().
  46. LookupResult getStat(const char *Path, FileData &Data, bool isFile,
  47. std::unique_ptr<vfs::File> *F,
  48. vfs::FileSystem &FS) override {
  49. if (StatCalls.count(Path) != 0) {
  50. Data = StatCalls[Path];
  51. return CacheExists;
  52. }
  53. return CacheMissing; // This means the file/directory doesn't exist.
  54. }
  55. };
  56. // The test fixture.
  57. class FileManagerTest : public ::testing::Test {
  58. protected:
  59. FileManagerTest() : manager(options) {
  60. }
  61. FileSystemOptions options;
  62. FileManager manager;
  63. };
  64. // When a virtual file is added, its getDir() field is set correctly
  65. // (not NULL, correct name).
  66. TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
  67. const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0);
  68. ASSERT_TRUE(file != nullptr);
  69. const DirectoryEntry *dir = file->getDir();
  70. ASSERT_TRUE(dir != nullptr);
  71. EXPECT_STREQ(".", dir->getName());
  72. file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
  73. ASSERT_TRUE(file != nullptr);
  74. dir = file->getDir();
  75. ASSERT_TRUE(dir != nullptr);
  76. EXPECT_STREQ("x/y", dir->getName());
  77. }
  78. // Before any virtual file is added, no virtual directory exists.
  79. TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) {
  80. // An empty FakeStatCache causes all stat calls made by the
  81. // FileManager to report "file/directory doesn't exist". This
  82. // avoids the possibility of the result of this test being affected
  83. // by what's in the real file system.
  84. manager.addStatCache(llvm::make_unique<FakeStatCache>());
  85. EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo"));
  86. EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir"));
  87. EXPECT_EQ(nullptr, manager.getDirectory("virtual"));
  88. }
  89. // When a virtual file is added, all of its ancestors should be created.
  90. TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) {
  91. // Fake an empty real file system.
  92. manager.addStatCache(llvm::make_unique<FakeStatCache>());
  93. manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
  94. EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo"));
  95. const DirectoryEntry *dir = manager.getDirectory("virtual/dir");
  96. ASSERT_TRUE(dir != nullptr);
  97. EXPECT_STREQ("virtual/dir", dir->getName());
  98. dir = manager.getDirectory("virtual");
  99. ASSERT_TRUE(dir != nullptr);
  100. EXPECT_STREQ("virtual", dir->getName());
  101. }
  102. // getFile() returns non-NULL if a real file exists at the given path.
  103. TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
  104. // Inject fake files into the file system.
  105. auto statCache = llvm::make_unique<FakeStatCache>();
  106. statCache->InjectDirectory("/tmp", 42);
  107. statCache->InjectFile("/tmp/test", 43);
  108. #ifdef LLVM_ON_WIN32
  109. const char *DirName = "C:.";
  110. const char *FileName = "C:test";
  111. statCache->InjectDirectory(DirName, 44);
  112. statCache->InjectFile(FileName, 45);
  113. #endif
  114. manager.addStatCache(std::move(statCache));
  115. const FileEntry *file = manager.getFile("/tmp/test");
  116. ASSERT_TRUE(file != nullptr);
  117. EXPECT_STREQ("/tmp/test", file->getName());
  118. const DirectoryEntry *dir = file->getDir();
  119. ASSERT_TRUE(dir != nullptr);
  120. EXPECT_STREQ("/tmp", dir->getName());
  121. #ifdef LLVM_ON_WIN32
  122. file = manager.getFile(FileName);
  123. ASSERT_TRUE(file != NULL);
  124. dir = file->getDir();
  125. ASSERT_TRUE(dir != NULL);
  126. EXPECT_STREQ(DirName, dir->getName());
  127. #endif
  128. }
  129. // getFile() returns non-NULL if a virtual file exists at the given path.
  130. TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
  131. // Fake an empty real file system.
  132. manager.addStatCache(llvm::make_unique<FakeStatCache>());
  133. manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
  134. const FileEntry *file = manager.getFile("virtual/dir/bar.h");
  135. ASSERT_TRUE(file != nullptr);
  136. EXPECT_STREQ("virtual/dir/bar.h", file->getName());
  137. const DirectoryEntry *dir = file->getDir();
  138. ASSERT_TRUE(dir != nullptr);
  139. EXPECT_STREQ("virtual/dir", dir->getName());
  140. }
  141. // getFile() returns different FileEntries for different paths when
  142. // there's no aliasing.
  143. TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) {
  144. // Inject two fake files into the file system. Different inodes
  145. // mean the files are not symlinked together.
  146. auto statCache = llvm::make_unique<FakeStatCache>();
  147. statCache->InjectDirectory(".", 41);
  148. statCache->InjectFile("foo.cpp", 42);
  149. statCache->InjectFile("bar.cpp", 43);
  150. manager.addStatCache(std::move(statCache));
  151. const FileEntry *fileFoo = manager.getFile("foo.cpp");
  152. const FileEntry *fileBar = manager.getFile("bar.cpp");
  153. ASSERT_TRUE(fileFoo != nullptr);
  154. ASSERT_TRUE(fileBar != nullptr);
  155. EXPECT_NE(fileFoo, fileBar);
  156. }
  157. // getFile() returns NULL if neither a real file nor a virtual file
  158. // exists at the given path.
  159. TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) {
  160. // Inject a fake foo.cpp into the file system.
  161. auto statCache = llvm::make_unique<FakeStatCache>();
  162. statCache->InjectDirectory(".", 41);
  163. statCache->InjectFile("foo.cpp", 42);
  164. manager.addStatCache(std::move(statCache));
  165. // Create a virtual bar.cpp file.
  166. manager.getVirtualFile("bar.cpp", 200, 0);
  167. const FileEntry *file = manager.getFile("xyz.txt");
  168. EXPECT_EQ(nullptr, file);
  169. }
  170. // The following tests apply to Unix-like system only.
  171. #ifndef LLVM_ON_WIN32
  172. // getFile() returns the same FileEntry for real files that are aliases.
  173. TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
  174. // Inject two real files with the same inode.
  175. auto statCache = llvm::make_unique<FakeStatCache>();
  176. statCache->InjectDirectory("abc", 41);
  177. statCache->InjectFile("abc/foo.cpp", 42);
  178. statCache->InjectFile("abc/bar.cpp", 42);
  179. manager.addStatCache(std::move(statCache));
  180. EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
  181. }
  182. // getFile() returns the same FileEntry for virtual files that have
  183. // corresponding real files that are aliases.
  184. TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
  185. // Inject two real files with the same inode.
  186. auto statCache = llvm::make_unique<FakeStatCache>();
  187. statCache->InjectDirectory("abc", 41);
  188. statCache->InjectFile("abc/foo.cpp", 42);
  189. statCache->InjectFile("abc/bar.cpp", 42);
  190. manager.addStatCache(std::move(statCache));
  191. manager.getVirtualFile("abc/foo.cpp", 100, 0);
  192. manager.getVirtualFile("abc/bar.cpp", 200, 0);
  193. EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
  194. }
  195. TEST_F(FileManagerTest, addRemoveStatCache) {
  196. manager.addStatCache(llvm::make_unique<FakeStatCache>());
  197. auto statCacheOwner = llvm::make_unique<FakeStatCache>();
  198. auto *statCache = statCacheOwner.get();
  199. manager.addStatCache(std::move(statCacheOwner));
  200. manager.addStatCache(llvm::make_unique<FakeStatCache>());
  201. manager.removeStatCache(statCache);
  202. }
  203. #endif // !LLVM_ON_WIN32
  204. } // anonymous namespace