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