FileManagerTest.cpp 7.8 KB

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