Zhanyong Wan | bc402ab | 2011-02-15 21:30:27 +0000 | [diff] [blame] | 1 | //===- unittests/Basic/FileMangerTest.cpp ------------ FileManger tests ---===// |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 320d966 | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 10 | #include "clang/Basic/FileManager.h" |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 11 | #include "clang/Basic/FileSystemOptions.h" |
| 12 | #include "clang/Basic/FileSystemStatCache.h" |
Ilya Biryukov | 47035c0 | 2017-08-02 07:25:24 +0000 | [diff] [blame] | 13 | #include "clang/Basic/VirtualFileSystem.h" |
Benjamin Kramer | 33335df | 2015-03-01 21:36:40 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" |
Erik Verbruggen | dfffaf5 | 2017-03-28 09:18:05 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Path.h" |
Chandler Carruth | 575bc3ba | 2015-01-14 11:23:58 +0000 | [diff] [blame] | 16 | #include "gtest/gtest.h" |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace clang; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | // Used to create a fake file system for running the tests with such |
| 24 | // that the tests are not affected by the structure/contents of the |
| 25 | // file system on the machine running the tests. |
| 26 | class FakeStatCache : public FileSystemStatCache { |
| 27 | private: |
| 28 | // Maps a file/directory path to its desired stat result. Anything |
| 29 | // not in this map is considered to not exist in the file system. |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 30 | llvm::StringMap<FileData, llvm::BumpPtrAllocator> StatCalls; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 31 | |
| 32 | void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) { |
Nico Weber | 1865df4 | 2018-04-27 19:11:14 +0000 | [diff] [blame] | 33 | #ifndef _WIN32 |
Erik Verbruggen | dfffaf5 | 2017-03-28 09:18:05 +0000 | [diff] [blame] | 34 | SmallString<128> NormalizedPath(Path); |
| 35 | llvm::sys::path::native(NormalizedPath); |
| 36 | Path = NormalizedPath.c_str(); |
| 37 | #endif |
| 38 | |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 39 | FileData Data; |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 40 | Data.Name = Path; |
| 41 | Data.Size = 0; |
| 42 | Data.ModTime = 0; |
| 43 | Data.UniqueID = llvm::sys::fs::UniqueID(1, INode); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 44 | Data.IsDirectory = !IsFile; |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 45 | Data.IsNamedPipe = false; |
| 46 | Data.InPCH = false; |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 47 | StatCalls[Path] = Data; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | public: |
| 51 | // Inject a file with the given inode value to the fake file system. |
| 52 | void InjectFile(const char *Path, ino_t INode) { |
| 53 | InjectFileOrDirectory(Path, INode, /*IsFile=*/true); |
| 54 | } |
| 55 | |
| 56 | // Inject a directory with the given inode value to the fake file system. |
| 57 | void InjectDirectory(const char *Path, ino_t INode) { |
| 58 | InjectFileOrDirectory(Path, INode, /*IsFile=*/false); |
| 59 | } |
| 60 | |
| 61 | // Implement FileSystemStatCache::getStat(). |
Mehdi Amini | 0df59d8 | 2016-10-11 07:31:29 +0000 | [diff] [blame] | 62 | LookupResult getStat(StringRef Path, FileData &Data, bool isFile, |
Benjamin Kramer | 4674175 | 2014-07-08 16:07:39 +0000 | [diff] [blame] | 63 | std::unique_ptr<vfs::File> *F, |
| 64 | vfs::FileSystem &FS) override { |
Nico Weber | 1865df4 | 2018-04-27 19:11:14 +0000 | [diff] [blame] | 65 | #ifndef _WIN32 |
Erik Verbruggen | dfffaf5 | 2017-03-28 09:18:05 +0000 | [diff] [blame] | 66 | SmallString<128> NormalizedPath(Path); |
| 67 | llvm::sys::path::native(NormalizedPath); |
| 68 | Path = NormalizedPath.c_str(); |
| 69 | #endif |
| 70 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 71 | if (StatCalls.count(Path) != 0) { |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 72 | Data = StatCalls[Path]; |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 73 | return CacheExists; |
| 74 | } |
| 75 | |
| 76 | return CacheMissing; // This means the file/directory doesn't exist. |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | // The test fixture. |
| 81 | class FileManagerTest : public ::testing::Test { |
| 82 | protected: |
| 83 | FileManagerTest() : manager(options) { |
| 84 | } |
| 85 | |
| 86 | FileSystemOptions options; |
| 87 | FileManager manager; |
| 88 | }; |
| 89 | |
| 90 | // When a virtual file is added, its getDir() field is set correctly |
| 91 | // (not NULL, correct name). |
| 92 | TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) { |
| 93 | const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 94 | ASSERT_TRUE(file != nullptr); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 95 | |
| 96 | const DirectoryEntry *dir = file->getDir(); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 97 | ASSERT_TRUE(dir != nullptr); |
Mehdi Amini | 0df59d8 | 2016-10-11 07:31:29 +0000 | [diff] [blame] | 98 | EXPECT_EQ(".", dir->getName()); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 99 | |
| 100 | file = manager.getVirtualFile("x/y/z.cpp", 42, 0); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 101 | ASSERT_TRUE(file != nullptr); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 102 | |
| 103 | dir = file->getDir(); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 104 | ASSERT_TRUE(dir != nullptr); |
Mehdi Amini | 0df59d8 | 2016-10-11 07:31:29 +0000 | [diff] [blame] | 105 | EXPECT_EQ("x/y", dir->getName()); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Before any virtual file is added, no virtual directory exists. |
| 109 | TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) { |
| 110 | // An empty FakeStatCache causes all stat calls made by the |
| 111 | // FileManager to report "file/directory doesn't exist". This |
| 112 | // avoids the possibility of the result of this test being affected |
| 113 | // by what's in the real file system. |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 114 | manager.addStatCache(llvm::make_unique<FakeStatCache>()); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 115 | |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 116 | EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo")); |
| 117 | EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir")); |
| 118 | EXPECT_EQ(nullptr, manager.getDirectory("virtual")); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // When a virtual file is added, all of its ancestors should be created. |
| 122 | TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) { |
| 123 | // Fake an empty real file system. |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 124 | manager.addStatCache(llvm::make_unique<FakeStatCache>()); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 125 | |
| 126 | manager.getVirtualFile("virtual/dir/bar.h", 100, 0); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 127 | EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo")); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 128 | |
| 129 | const DirectoryEntry *dir = manager.getDirectory("virtual/dir"); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 130 | ASSERT_TRUE(dir != nullptr); |
Mehdi Amini | 0df59d8 | 2016-10-11 07:31:29 +0000 | [diff] [blame] | 131 | EXPECT_EQ("virtual/dir", dir->getName()); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 132 | |
| 133 | dir = manager.getDirectory("virtual"); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 134 | ASSERT_TRUE(dir != nullptr); |
Mehdi Amini | 0df59d8 | 2016-10-11 07:31:29 +0000 | [diff] [blame] | 135 | EXPECT_EQ("virtual", dir->getName()); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // getFile() returns non-NULL if a real file exists at the given path. |
| 139 | TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) { |
| 140 | // Inject fake files into the file system. |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 141 | auto statCache = llvm::make_unique<FakeStatCache>(); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 142 | statCache->InjectDirectory("/tmp", 42); |
| 143 | statCache->InjectFile("/tmp/test", 43); |
Rafael Espindola | ee30546 | 2013-07-29 15:47:24 +0000 | [diff] [blame] | 144 | |
Nico Weber | 1865df4 | 2018-04-27 19:11:14 +0000 | [diff] [blame] | 145 | #ifdef _WIN32 |
Rafael Espindola | ee30546 | 2013-07-29 15:47:24 +0000 | [diff] [blame] | 146 | const char *DirName = "C:."; |
| 147 | const char *FileName = "C:test"; |
| 148 | statCache->InjectDirectory(DirName, 44); |
| 149 | statCache->InjectFile(FileName, 45); |
| 150 | #endif |
| 151 | |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 152 | manager.addStatCache(std::move(statCache)); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 153 | |
| 154 | const FileEntry *file = manager.getFile("/tmp/test"); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 155 | ASSERT_TRUE(file != nullptr); |
Erik Verbruggen | dfffaf5 | 2017-03-28 09:18:05 +0000 | [diff] [blame] | 156 | ASSERT_TRUE(file->isValid()); |
Mehdi Amini | 004b9c7 | 2016-10-10 22:52:47 +0000 | [diff] [blame] | 157 | EXPECT_EQ("/tmp/test", file->getName()); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 158 | |
| 159 | const DirectoryEntry *dir = file->getDir(); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 160 | ASSERT_TRUE(dir != nullptr); |
Mehdi Amini | 0df59d8 | 2016-10-11 07:31:29 +0000 | [diff] [blame] | 161 | EXPECT_EQ("/tmp", dir->getName()); |
Rafael Espindola | ee30546 | 2013-07-29 15:47:24 +0000 | [diff] [blame] | 162 | |
Nico Weber | 1865df4 | 2018-04-27 19:11:14 +0000 | [diff] [blame] | 163 | #ifdef _WIN32 |
Rafael Espindola | ee30546 | 2013-07-29 15:47:24 +0000 | [diff] [blame] | 164 | file = manager.getFile(FileName); |
| 165 | ASSERT_TRUE(file != NULL); |
| 166 | |
| 167 | dir = file->getDir(); |
| 168 | ASSERT_TRUE(dir != NULL); |
Mehdi Amini | 0df59d8 | 2016-10-11 07:31:29 +0000 | [diff] [blame] | 169 | EXPECT_EQ(DirName, dir->getName()); |
Rafael Espindola | ee30546 | 2013-07-29 15:47:24 +0000 | [diff] [blame] | 170 | #endif |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // getFile() returns non-NULL if a virtual file exists at the given path. |
| 174 | TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) { |
| 175 | // Fake an empty real file system. |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 176 | manager.addStatCache(llvm::make_unique<FakeStatCache>()); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 177 | |
| 178 | manager.getVirtualFile("virtual/dir/bar.h", 100, 0); |
| 179 | const FileEntry *file = manager.getFile("virtual/dir/bar.h"); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 180 | ASSERT_TRUE(file != nullptr); |
Erik Verbruggen | dfffaf5 | 2017-03-28 09:18:05 +0000 | [diff] [blame] | 181 | ASSERT_TRUE(file->isValid()); |
Mehdi Amini | 004b9c7 | 2016-10-10 22:52:47 +0000 | [diff] [blame] | 182 | EXPECT_EQ("virtual/dir/bar.h", file->getName()); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 183 | |
| 184 | const DirectoryEntry *dir = file->getDir(); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 185 | ASSERT_TRUE(dir != nullptr); |
Mehdi Amini | 0df59d8 | 2016-10-11 07:31:29 +0000 | [diff] [blame] | 186 | EXPECT_EQ("virtual/dir", dir->getName()); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // getFile() returns different FileEntries for different paths when |
| 190 | // there's no aliasing. |
| 191 | TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) { |
| 192 | // Inject two fake files into the file system. Different inodes |
| 193 | // mean the files are not symlinked together. |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 194 | auto statCache = llvm::make_unique<FakeStatCache>(); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 195 | statCache->InjectDirectory(".", 41); |
| 196 | statCache->InjectFile("foo.cpp", 42); |
| 197 | statCache->InjectFile("bar.cpp", 43); |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 198 | manager.addStatCache(std::move(statCache)); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 199 | |
| 200 | const FileEntry *fileFoo = manager.getFile("foo.cpp"); |
| 201 | const FileEntry *fileBar = manager.getFile("bar.cpp"); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 202 | ASSERT_TRUE(fileFoo != nullptr); |
Erik Verbruggen | dfffaf5 | 2017-03-28 09:18:05 +0000 | [diff] [blame] | 203 | ASSERT_TRUE(fileFoo->isValid()); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 204 | ASSERT_TRUE(fileBar != nullptr); |
Erik Verbruggen | dfffaf5 | 2017-03-28 09:18:05 +0000 | [diff] [blame] | 205 | ASSERT_TRUE(fileBar->isValid()); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 206 | EXPECT_NE(fileFoo, fileBar); |
| 207 | } |
| 208 | |
| 209 | // getFile() returns NULL if neither a real file nor a virtual file |
| 210 | // exists at the given path. |
| 211 | TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) { |
| 212 | // Inject a fake foo.cpp into the file system. |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 213 | auto statCache = llvm::make_unique<FakeStatCache>(); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 214 | statCache->InjectDirectory(".", 41); |
| 215 | statCache->InjectFile("foo.cpp", 42); |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 216 | manager.addStatCache(std::move(statCache)); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 217 | |
| 218 | // Create a virtual bar.cpp file. |
| 219 | manager.getVirtualFile("bar.cpp", 200, 0); |
| 220 | |
| 221 | const FileEntry *file = manager.getFile("xyz.txt"); |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 222 | EXPECT_EQ(nullptr, file); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // The following tests apply to Unix-like system only. |
| 226 | |
Nico Weber | 1865df4 | 2018-04-27 19:11:14 +0000 | [diff] [blame] | 227 | #ifndef _WIN32 |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 228 | |
| 229 | // getFile() returns the same FileEntry for real files that are aliases. |
| 230 | TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) { |
| 231 | // Inject two real files with the same inode. |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 232 | auto statCache = llvm::make_unique<FakeStatCache>(); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 233 | statCache->InjectDirectory("abc", 41); |
| 234 | statCache->InjectFile("abc/foo.cpp", 42); |
| 235 | statCache->InjectFile("abc/bar.cpp", 42); |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 236 | manager.addStatCache(std::move(statCache)); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 237 | |
| 238 | EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp")); |
| 239 | } |
| 240 | |
| 241 | // getFile() returns the same FileEntry for virtual files that have |
| 242 | // corresponding real files that are aliases. |
| 243 | TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) { |
| 244 | // Inject two real files with the same inode. |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 245 | auto statCache = llvm::make_unique<FakeStatCache>(); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 246 | statCache->InjectDirectory("abc", 41); |
| 247 | statCache->InjectFile("abc/foo.cpp", 42); |
| 248 | statCache->InjectFile("abc/bar.cpp", 42); |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 249 | manager.addStatCache(std::move(statCache)); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 250 | |
Erik Verbruggen | dfffaf5 | 2017-03-28 09:18:05 +0000 | [diff] [blame] | 251 | ASSERT_TRUE(manager.getVirtualFile("abc/foo.cpp", 100, 0)->isValid()); |
| 252 | ASSERT_TRUE(manager.getVirtualFile("abc/bar.cpp", 200, 0)->isValid()); |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 253 | |
| 254 | EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp")); |
| 255 | } |
| 256 | |
David Blaikie | 23430cc | 2014-08-11 21:29:24 +0000 | [diff] [blame] | 257 | TEST_F(FileManagerTest, addRemoveStatCache) { |
| 258 | manager.addStatCache(llvm::make_unique<FakeStatCache>()); |
| 259 | auto statCacheOwner = llvm::make_unique<FakeStatCache>(); |
| 260 | auto *statCache = statCacheOwner.get(); |
| 261 | manager.addStatCache(std::move(statCacheOwner)); |
| 262 | manager.addStatCache(llvm::make_unique<FakeStatCache>()); |
| 263 | manager.removeStatCache(statCache); |
| 264 | } |
| 265 | |
Erik Verbruggen | dfffaf5 | 2017-03-28 09:18:05 +0000 | [diff] [blame] | 266 | // getFile() Should return the same entry as getVirtualFile if the file actually |
| 267 | // is a virtual file, even if the name is not exactly the same (but is after |
| 268 | // normalisation done by the file system, like on Windows). This can be checked |
| 269 | // here by checkng the size. |
| 270 | TEST_F(FileManagerTest, getVirtualFileWithDifferentName) { |
| 271 | // Inject fake files into the file system. |
| 272 | auto statCache = llvm::make_unique<FakeStatCache>(); |
| 273 | statCache->InjectDirectory("c:\\tmp", 42); |
| 274 | statCache->InjectFile("c:\\tmp\\test", 43); |
| 275 | |
| 276 | manager.addStatCache(std::move(statCache)); |
| 277 | |
| 278 | // Inject the virtual file: |
| 279 | const FileEntry *file1 = manager.getVirtualFile("c:\\tmp\\test", 123, 1); |
| 280 | ASSERT_TRUE(file1 != nullptr); |
| 281 | ASSERT_TRUE(file1->isValid()); |
| 282 | EXPECT_EQ(43U, file1->getUniqueID().getFile()); |
| 283 | EXPECT_EQ(123, file1->getSize()); |
| 284 | |
| 285 | // Lookup the virtual file with a different name: |
| 286 | const FileEntry *file2 = manager.getFile("c:/tmp/test", 100, 1); |
| 287 | ASSERT_TRUE(file2 != nullptr); |
| 288 | ASSERT_TRUE(file2->isValid()); |
| 289 | // Check that it's the same UFE: |
| 290 | EXPECT_EQ(file1, file2); |
| 291 | EXPECT_EQ(43U, file2->getUniqueID().getFile()); |
| 292 | // Check that the contents of the UFE are not overwritten by the entry in the |
| 293 | // filesystem: |
| 294 | EXPECT_EQ(123, file2->getSize()); |
| 295 | } |
| 296 | |
Nico Weber | 1865df4 | 2018-04-27 19:11:14 +0000 | [diff] [blame] | 297 | #endif // !_WIN32 |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 298 | |
Ilya Biryukov | 47035c0 | 2017-08-02 07:25:24 +0000 | [diff] [blame] | 299 | TEST_F(FileManagerTest, makeAbsoluteUsesVFS) { |
| 300 | SmallString<64> CustomWorkingDir; |
Nico Weber | 1865df4 | 2018-04-27 19:11:14 +0000 | [diff] [blame] | 301 | #ifdef _WIN32 |
Ilya Biryukov | 47035c0 | 2017-08-02 07:25:24 +0000 | [diff] [blame] | 302 | CustomWorkingDir = "C:"; |
| 303 | #else |
| 304 | CustomWorkingDir = "/"; |
| 305 | #endif |
| 306 | llvm::sys::path::append(CustomWorkingDir, "some", "weird", "path"); |
| 307 | |
| 308 | auto FS = |
| 309 | IntrusiveRefCntPtr<vfs::InMemoryFileSystem>(new vfs::InMemoryFileSystem); |
| 310 | // setCurrentworkingdirectory must finish without error. |
| 311 | ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); |
| 312 | |
| 313 | FileSystemOptions Opts; |
| 314 | FileManager Manager(Opts, FS); |
| 315 | |
| 316 | SmallString<64> Path("a/foo.cpp"); |
| 317 | |
| 318 | SmallString<64> ExpectedResult(CustomWorkingDir); |
| 319 | llvm::sys::path::append(ExpectedResult, Path); |
| 320 | |
| 321 | ASSERT_TRUE(Manager.makeAbsolutePath(Path)); |
| 322 | EXPECT_EQ(Path, ExpectedResult); |
| 323 | } |
| 324 | |
Zhanyong Wan | e1dd3e2 | 2011-02-11 18:44:49 +0000 | [diff] [blame] | 325 | } // anonymous namespace |