FileManagerTest.cpp 8.5 KB

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