FileManagerTest.cpp 7.8 KB

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