1 //===- unittests/Basic/FileMangerTest.cpp ------------ FileManger tests ---===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 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/Support/Path.h" 14 #include "llvm/Support/VirtualFileSystem.h" 15 #include "gtest/gtest.h" 16 17 using namespace llvm; 18 using namespace clang; 19 20 namespace { 21 22 // Used to create a fake file system for running the tests with such 23 // that the tests are not affected by the structure/contents of the 24 // file system on the machine running the tests. 25 class FakeStatCache : public FileSystemStatCache { 26 private: 27 // Maps a file/directory path to its desired stat result. Anything 28 // not in this map is considered to not exist in the file system. 29 llvm::StringMap<llvm::vfs::Status, llvm::BumpPtrAllocator> StatCalls; 30 31 void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile, 32 const char *StatPath) { 33 #ifndef _WIN32 34 SmallString<128> NormalizedPath(Path); 35 llvm::sys::path::native(NormalizedPath); 36 Path = NormalizedPath.c_str(); 37 38 SmallString<128> NormalizedStatPath; 39 if (StatPath) { 40 NormalizedStatPath = StatPath; 41 llvm::sys::path::native(NormalizedStatPath); 42 StatPath = NormalizedStatPath.c_str(); 43 } 44 #endif 45 46 if (!StatPath) 47 StatPath = Path; 48 49 auto fileType = IsFile ? 50 llvm::sys::fs::file_type::regular_file : 51 llvm::sys::fs::file_type::directory_file; 52 llvm::vfs::Status Status(StatPath, llvm::sys::fs::UniqueID(1, INode), 53 /*MTime*/{}, /*User*/0, /*Group*/0, 54 /*Size*/0, fileType, 55 llvm::sys::fs::perms::all_all); 56 StatCalls[Path] = Status; 57 } 58 59 public: 60 // Inject a file with the given inode value to the fake file system. 61 void InjectFile(const char *Path, ino_t INode, 62 const char *StatPath = nullptr) { 63 InjectFileOrDirectory(Path, INode, /*IsFile=*/true, StatPath); 64 } 65 66 // Inject a directory with the given inode value to the fake file system. 67 void InjectDirectory(const char *Path, ino_t INode) { 68 InjectFileOrDirectory(Path, INode, /*IsFile=*/false, nullptr); 69 } 70 71 // Implement FileSystemStatCache::getStat(). 72 std::error_code getStat(StringRef Path, llvm::vfs::Status &Status, 73 bool isFile, 74 std::unique_ptr<llvm::vfs::File> *F, 75 llvm::vfs::FileSystem &FS) override { 76 #ifndef _WIN32 77 SmallString<128> NormalizedPath(Path); 78 llvm::sys::path::native(NormalizedPath); 79 Path = NormalizedPath.c_str(); 80 #endif 81 82 if (StatCalls.count(Path) != 0) { 83 Status = StatCalls[Path]; 84 return std::error_code(); 85 } 86 87 return std::make_error_code(std::errc::no_such_file_or_directory); 88 } 89 }; 90 91 // The test fixture. 92 class FileManagerTest : public ::testing::Test { 93 protected: 94 FileManagerTest() : manager(options) { 95 } 96 97 FileSystemOptions options; 98 FileManager manager; 99 }; 100 101 // When a virtual file is added, its getDir() field is set correctly 102 // (not NULL, correct name). 103 TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) { 104 const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0); 105 ASSERT_TRUE(file != nullptr); 106 107 const DirectoryEntry *dir = file->getDir(); 108 ASSERT_TRUE(dir != nullptr); 109 EXPECT_EQ(".", dir->getName()); 110 111 file = manager.getVirtualFile("x/y/z.cpp", 42, 0); 112 ASSERT_TRUE(file != nullptr); 113 114 dir = file->getDir(); 115 ASSERT_TRUE(dir != nullptr); 116 EXPECT_EQ("x/y", dir->getName()); 117 } 118 119 // Before any virtual file is added, no virtual directory exists. 120 TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) { 121 // An empty FakeStatCache causes all stat calls made by the 122 // FileManager to report "file/directory doesn't exist". This 123 // avoids the possibility of the result of this test being affected 124 // by what's in the real file system. 125 manager.setStatCache(std::make_unique<FakeStatCache>()); 126 127 ASSERT_FALSE(manager.getDirectory("virtual/dir/foo")); 128 ASSERT_FALSE(manager.getDirectory("virtual/dir")); 129 ASSERT_FALSE(manager.getDirectory("virtual")); 130 } 131 132 // When a virtual file is added, all of its ancestors should be created. 133 TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) { 134 // Fake an empty real file system. 135 manager.setStatCache(std::make_unique<FakeStatCache>()); 136 137 manager.getVirtualFile("virtual/dir/bar.h", 100, 0); 138 ASSERT_FALSE(manager.getDirectory("virtual/dir/foo")); 139 140 auto dir = manager.getDirectory("virtual/dir"); 141 ASSERT_TRUE(dir); 142 EXPECT_EQ("virtual/dir", (*dir)->getName()); 143 144 dir = manager.getDirectory("virtual"); 145 ASSERT_TRUE(dir); 146 EXPECT_EQ("virtual", (*dir)->getName()); 147 } 148 149 // getFile() returns non-NULL if a real file exists at the given path. 150 TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) { 151 // Inject fake files into the file system. 152 auto statCache = std::make_unique<FakeStatCache>(); 153 statCache->InjectDirectory("/tmp", 42); 154 statCache->InjectFile("/tmp/test", 43); 155 156 #ifdef _WIN32 157 const char *DirName = "C:."; 158 const char *FileName = "C:test"; 159 statCache->InjectDirectory(DirName, 44); 160 statCache->InjectFile(FileName, 45); 161 #endif 162 163 manager.setStatCache(std::move(statCache)); 164 165 auto file = manager.getFile("/tmp/test"); 166 ASSERT_TRUE(file); 167 ASSERT_TRUE((*file)->isValid()); 168 EXPECT_EQ("/tmp/test", (*file)->getName()); 169 170 const DirectoryEntry *dir = (*file)->getDir(); 171 ASSERT_TRUE(dir != nullptr); 172 EXPECT_EQ("/tmp", dir->getName()); 173 174 #ifdef _WIN32 175 file = manager.getFile(FileName); 176 ASSERT_TRUE(file); 177 178 dir = (*file)->getDir(); 179 ASSERT_TRUE(dir != NULL); 180 EXPECT_EQ(DirName, dir->getName()); 181 #endif 182 } 183 184 // getFile() returns non-NULL if a virtual file exists at the given path. 185 TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) { 186 // Fake an empty real file system. 187 manager.setStatCache(std::make_unique<FakeStatCache>()); 188 189 manager.getVirtualFile("virtual/dir/bar.h", 100, 0); 190 auto file = manager.getFile("virtual/dir/bar.h"); 191 ASSERT_TRUE(file); 192 ASSERT_TRUE((*file)->isValid()); 193 EXPECT_EQ("virtual/dir/bar.h", (*file)->getName()); 194 195 const DirectoryEntry *dir = (*file)->getDir(); 196 ASSERT_TRUE(dir != nullptr); 197 EXPECT_EQ("virtual/dir", dir->getName()); 198 } 199 200 // getFile() returns different FileEntries for different paths when 201 // there's no aliasing. 202 TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) { 203 // Inject two fake files into the file system. Different inodes 204 // mean the files are not symlinked together. 205 auto statCache = std::make_unique<FakeStatCache>(); 206 statCache->InjectDirectory(".", 41); 207 statCache->InjectFile("foo.cpp", 42); 208 statCache->InjectFile("bar.cpp", 43); 209 manager.setStatCache(std::move(statCache)); 210 211 auto fileFoo = manager.getFile("foo.cpp"); 212 auto fileBar = manager.getFile("bar.cpp"); 213 ASSERT_TRUE(fileFoo); 214 ASSERT_TRUE((*fileFoo)->isValid()); 215 ASSERT_TRUE(fileBar); 216 ASSERT_TRUE((*fileBar)->isValid()); 217 EXPECT_NE(*fileFoo, *fileBar); 218 } 219 220 // getFile() returns an error if neither a real file nor a virtual file 221 // exists at the given path. 222 TEST_F(FileManagerTest, getFileReturnsErrorForNonexistentFile) { 223 // Inject a fake foo.cpp into the file system. 224 auto statCache = std::make_unique<FakeStatCache>(); 225 statCache->InjectDirectory(".", 41); 226 statCache->InjectFile("foo.cpp", 42); 227 statCache->InjectDirectory("MyDirectory", 49); 228 manager.setStatCache(std::move(statCache)); 229 230 // Create a virtual bar.cpp file. 231 manager.getVirtualFile("bar.cpp", 200, 0); 232 233 auto file = manager.getFile("xyz.txt"); 234 ASSERT_FALSE(file); 235 ASSERT_EQ(file.getError(), std::errc::no_such_file_or_directory); 236 237 auto readingDirAsFile = manager.getFile("MyDirectory"); 238 ASSERT_FALSE(readingDirAsFile); 239 ASSERT_EQ(readingDirAsFile.getError(), std::errc::is_a_directory); 240 241 auto readingFileAsDir = manager.getDirectory("foo.cpp"); 242 ASSERT_FALSE(readingFileAsDir); 243 ASSERT_EQ(readingFileAsDir.getError(), std::errc::not_a_directory); 244 } 245 246 // The following tests apply to Unix-like system only. 247 248 #ifndef _WIN32 249 250 // getFile() returns the same FileEntry for real files that are aliases. 251 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) { 252 // Inject two real files with the same inode. 253 auto statCache = std::make_unique<FakeStatCache>(); 254 statCache->InjectDirectory("abc", 41); 255 statCache->InjectFile("abc/foo.cpp", 42); 256 statCache->InjectFile("abc/bar.cpp", 42); 257 manager.setStatCache(std::move(statCache)); 258 259 auto f1 = manager.getFile("abc/foo.cpp"); 260 auto f2 = manager.getFile("abc/bar.cpp"); 261 262 EXPECT_EQ(f1 ? *f1 : nullptr, 263 f2 ? *f2 : nullptr); 264 265 // Check that getFileRef also does the right thing. 266 auto r1 = manager.getFileRef("abc/foo.cpp"); 267 auto r2 = manager.getFileRef("abc/bar.cpp"); 268 ASSERT_FALSE(!r1); 269 ASSERT_FALSE(!r2); 270 271 EXPECT_EQ("abc/foo.cpp", r1->getName()); 272 EXPECT_EQ("abc/bar.cpp", r2->getName()); 273 EXPECT_EQ((f1 ? *f1 : nullptr), &r1->getFileEntry()); 274 EXPECT_EQ((f2 ? *f2 : nullptr), &r2->getFileEntry()); 275 } 276 277 TEST_F(FileManagerTest, getFileRefReturnsCorrectNameForDifferentStatPath) { 278 // Inject files with the same inode, but where some files have a stat that 279 // gives a different name. This is adding coverage for stat behaviour 280 // triggered by the RedirectingFileSystem for 'use-external-name' that 281 // FileManager::getFileRef has special logic for. 282 auto StatCache = std::make_unique<FakeStatCache>(); 283 StatCache->InjectDirectory("dir", 40); 284 StatCache->InjectFile("dir/f1.cpp", 41); 285 StatCache->InjectFile("dir/f1-alias.cpp", 41, "dir/f1.cpp"); 286 StatCache->InjectFile("dir/f2.cpp", 42); 287 StatCache->InjectFile("dir/f2-alias.cpp", 42, "dir/f2.cpp"); 288 289 // This unintuitive rename-the-file-on-stat behaviour supports how the 290 // RedirectingFileSystem VFS layer responds to stats. However, even if you 291 // have two layers, you should only get a single filename back. As such the 292 // following stat cache behaviour is not supported (the correct stat entry 293 // for a double-redirection would be "dir/f1.cpp") and the getFileRef below 294 // should assert. 295 StatCache->InjectFile("dir/f1-alias-alias.cpp", 41, "dir/f1-alias.cpp"); 296 297 manager.setStatCache(std::move(StatCache)); 298 299 // With F1, test accessing the non-redirected name first. 300 auto F1 = manager.getFileRef("dir/f1.cpp"); 301 auto F1Alias = manager.getFileRef("dir/f1-alias.cpp"); 302 auto F1Alias2 = manager.getFileRef("dir/f1-alias.cpp"); 303 ASSERT_FALSE(!F1); 304 ASSERT_FALSE(!F1Alias); 305 ASSERT_FALSE(!F1Alias2); 306 EXPECT_EQ("dir/f1.cpp", F1->getName()); 307 EXPECT_EQ("dir/f1.cpp", F1->getFileEntry().getName()); 308 EXPECT_EQ("dir/f1.cpp", F1Alias->getName()); 309 EXPECT_EQ("dir/f1.cpp", F1Alias2->getName()); 310 EXPECT_EQ(&F1->getFileEntry(), &F1Alias->getFileEntry()); 311 EXPECT_EQ(&F1->getFileEntry(), &F1Alias2->getFileEntry()); 312 313 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST 314 EXPECT_DEATH((void)manager.getFileRef("dir/f1-alias-alias.cpp"), 315 "filename redirected to a non-canonical filename?"); 316 #endif 317 318 // With F2, test accessing the redirected name first. 319 auto F2Alias = manager.getFileRef("dir/f2-alias.cpp"); 320 auto F2 = manager.getFileRef("dir/f2.cpp"); 321 auto F2Alias2 = manager.getFileRef("dir/f2-alias.cpp"); 322 ASSERT_FALSE(!F2); 323 ASSERT_FALSE(!F2Alias); 324 ASSERT_FALSE(!F2Alias2); 325 EXPECT_EQ("dir/f2.cpp", F2->getName()); 326 EXPECT_EQ("dir/f2.cpp", F2->getFileEntry().getName()); 327 EXPECT_EQ("dir/f2.cpp", F2Alias->getName()); 328 EXPECT_EQ("dir/f2.cpp", F2Alias2->getName()); 329 EXPECT_EQ(&F2->getFileEntry(), &F2Alias->getFileEntry()); 330 EXPECT_EQ(&F2->getFileEntry(), &F2Alias2->getFileEntry()); 331 } 332 333 // getFile() returns the same FileEntry for virtual files that have 334 // corresponding real files that are aliases. 335 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) { 336 // Inject two real files with the same inode. 337 auto statCache = std::make_unique<FakeStatCache>(); 338 statCache->InjectDirectory("abc", 41); 339 statCache->InjectFile("abc/foo.cpp", 42); 340 statCache->InjectFile("abc/bar.cpp", 42); 341 manager.setStatCache(std::move(statCache)); 342 343 ASSERT_TRUE(manager.getVirtualFile("abc/foo.cpp", 100, 0)->isValid()); 344 ASSERT_TRUE(manager.getVirtualFile("abc/bar.cpp", 200, 0)->isValid()); 345 346 auto f1 = manager.getFile("abc/foo.cpp"); 347 auto f2 = manager.getFile("abc/bar.cpp"); 348 349 EXPECT_EQ(f1 ? *f1 : nullptr, 350 f2 ? *f2 : nullptr); 351 } 352 353 TEST_F(FileManagerTest, getFileRefEquality) { 354 auto StatCache = std::make_unique<FakeStatCache>(); 355 StatCache->InjectDirectory("dir", 40); 356 StatCache->InjectFile("dir/f1.cpp", 41); 357 StatCache->InjectFile("dir/f1-also.cpp", 41); 358 StatCache->InjectFile("dir/f1-redirect.cpp", 41, "dir/f1.cpp"); 359 StatCache->InjectFile("dir/f2.cpp", 42); 360 manager.setStatCache(std::move(StatCache)); 361 362 auto F1 = manager.getFileRef("dir/f1.cpp"); 363 auto F1Again = manager.getFileRef("dir/f1.cpp"); 364 auto F1Also = manager.getFileRef("dir/f1-also.cpp"); 365 auto F1Redirect = manager.getFileRef("dir/f1-redirect.cpp"); 366 auto F2 = manager.getFileRef("dir/f2.cpp"); 367 368 // Check Expected<FileEntryRef> for error. 369 ASSERT_FALSE(!F1); 370 ASSERT_FALSE(!F1Also); 371 ASSERT_FALSE(!F1Again); 372 ASSERT_FALSE(!F1Redirect); 373 ASSERT_FALSE(!F2); 374 375 // Check names. 376 EXPECT_EQ("dir/f1.cpp", F1->getName()); 377 EXPECT_EQ("dir/f1.cpp", F1Again->getName()); 378 EXPECT_EQ("dir/f1-also.cpp", F1Also->getName()); 379 EXPECT_EQ("dir/f1.cpp", F1Redirect->getName()); 380 EXPECT_EQ("dir/f2.cpp", F2->getName()); 381 382 // Compare against FileEntry*. 383 EXPECT_EQ(&F1->getFileEntry(), *F1); 384 EXPECT_EQ(*F1, &F1->getFileEntry()); 385 EXPECT_NE(&F2->getFileEntry(), *F1); 386 EXPECT_NE(*F1, &F2->getFileEntry()); 387 388 // Compare using ==. 389 EXPECT_EQ(*F1, *F1Also); 390 EXPECT_EQ(*F1, *F1Again); 391 EXPECT_EQ(*F1, *F1Redirect); 392 EXPECT_EQ(*F1Also, *F1Redirect); 393 EXPECT_NE(*F2, *F1); 394 EXPECT_NE(*F2, *F1Also); 395 EXPECT_NE(*F2, *F1Again); 396 EXPECT_NE(*F2, *F1Redirect); 397 398 // Compare using isSameRef. 399 EXPECT_TRUE(F1->isSameRef(*F1Again)); 400 EXPECT_TRUE(F1->isSameRef(*F1Redirect)); 401 EXPECT_FALSE(F1->isSameRef(*F1Also)); 402 EXPECT_FALSE(F1->isSameRef(*F2)); 403 } 404 405 // getFile() Should return the same entry as getVirtualFile if the file actually 406 // is a virtual file, even if the name is not exactly the same (but is after 407 // normalisation done by the file system, like on Windows). This can be checked 408 // here by checking the size. 409 TEST_F(FileManagerTest, getVirtualFileWithDifferentName) { 410 // Inject fake files into the file system. 411 auto statCache = std::make_unique<FakeStatCache>(); 412 statCache->InjectDirectory("c:\\tmp", 42); 413 statCache->InjectFile("c:\\tmp\\test", 43); 414 415 manager.setStatCache(std::move(statCache)); 416 417 // Inject the virtual file: 418 const FileEntry *file1 = manager.getVirtualFile("c:\\tmp\\test", 123, 1); 419 ASSERT_TRUE(file1 != nullptr); 420 ASSERT_TRUE(file1->isValid()); 421 EXPECT_EQ(43U, file1->getUniqueID().getFile()); 422 EXPECT_EQ(123, file1->getSize()); 423 424 // Lookup the virtual file with a different name: 425 auto file2 = manager.getFile("c:/tmp/test", 100, 1); 426 ASSERT_TRUE(file2); 427 ASSERT_TRUE((*file2)->isValid()); 428 // Check that it's the same UFE: 429 EXPECT_EQ(file1, *file2); 430 EXPECT_EQ(43U, (*file2)->getUniqueID().getFile()); 431 // Check that the contents of the UFE are not overwritten by the entry in the 432 // filesystem: 433 EXPECT_EQ(123, (*file2)->getSize()); 434 } 435 436 #endif // !_WIN32 437 438 TEST_F(FileManagerTest, makeAbsoluteUsesVFS) { 439 SmallString<64> CustomWorkingDir; 440 #ifdef _WIN32 441 CustomWorkingDir = "C:"; 442 #else 443 CustomWorkingDir = "/"; 444 #endif 445 llvm::sys::path::append(CustomWorkingDir, "some", "weird", "path"); 446 447 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( 448 new llvm::vfs::InMemoryFileSystem); 449 // setCurrentworkingdirectory must finish without error. 450 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); 451 452 FileSystemOptions Opts; 453 FileManager Manager(Opts, FS); 454 455 SmallString<64> Path("a/foo.cpp"); 456 457 SmallString<64> ExpectedResult(CustomWorkingDir); 458 llvm::sys::path::append(ExpectedResult, Path); 459 460 ASSERT_TRUE(Manager.makeAbsolutePath(Path)); 461 EXPECT_EQ(Path, ExpectedResult); 462 } 463 464 // getVirtualFile should always fill the real path. 465 TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) { 466 SmallString<64> CustomWorkingDir; 467 #ifdef _WIN32 468 CustomWorkingDir = "C:/"; 469 #else 470 CustomWorkingDir = "/"; 471 #endif 472 473 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( 474 new llvm::vfs::InMemoryFileSystem); 475 // setCurrentworkingdirectory must finish without error. 476 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); 477 478 FileSystemOptions Opts; 479 FileManager Manager(Opts, FS); 480 481 // Inject fake files into the file system. 482 auto statCache = std::make_unique<FakeStatCache>(); 483 statCache->InjectDirectory("/tmp", 42); 484 statCache->InjectFile("/tmp/test", 43); 485 486 Manager.setStatCache(std::move(statCache)); 487 488 // Check for real path. 489 const FileEntry *file = Manager.getVirtualFile("/tmp/test", 123, 1); 490 ASSERT_TRUE(file != nullptr); 491 ASSERT_TRUE(file->isValid()); 492 SmallString<64> ExpectedResult = CustomWorkingDir; 493 494 llvm::sys::path::append(ExpectedResult, "tmp", "test"); 495 EXPECT_EQ(file->tryGetRealPathName(), ExpectedResult); 496 } 497 498 TEST_F(FileManagerTest, getFileDontOpenRealPath) { 499 SmallString<64> CustomWorkingDir; 500 #ifdef _WIN32 501 CustomWorkingDir = "C:/"; 502 #else 503 CustomWorkingDir = "/"; 504 #endif 505 506 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( 507 new llvm::vfs::InMemoryFileSystem); 508 // setCurrentworkingdirectory must finish without error. 509 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); 510 511 FileSystemOptions Opts; 512 FileManager Manager(Opts, FS); 513 514 // Inject fake files into the file system. 515 auto statCache = std::make_unique<FakeStatCache>(); 516 statCache->InjectDirectory("/tmp", 42); 517 statCache->InjectFile("/tmp/test", 43); 518 519 Manager.setStatCache(std::move(statCache)); 520 521 // Check for real path. 522 auto file = Manager.getFile("/tmp/test", /*OpenFile=*/false); 523 ASSERT_TRUE(file); 524 ASSERT_TRUE((*file)->isValid()); 525 SmallString<64> ExpectedResult = CustomWorkingDir; 526 527 llvm::sys::path::append(ExpectedResult, "tmp", "test"); 528 EXPECT_EQ((*file)->tryGetRealPathName(), ExpectedResult); 529 } 530 531 TEST_F(FileManagerTest, getBypassFile) { 532 SmallString<64> CustomWorkingDir; 533 #ifdef _WIN32 534 CustomWorkingDir = "C:/"; 535 #else 536 CustomWorkingDir = "/"; 537 #endif 538 539 auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>( 540 new llvm::vfs::InMemoryFileSystem); 541 // setCurrentworkingdirectory must finish without error. 542 ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir)); 543 544 FileSystemOptions Opts; 545 FileManager Manager(Opts, FS); 546 547 // Inject fake files into the file system. 548 auto Cache = std::make_unique<FakeStatCache>(); 549 Cache->InjectDirectory("/tmp", 42); 550 Cache->InjectFile("/tmp/test", 43); 551 Manager.setStatCache(std::move(Cache)); 552 553 // Set up a virtual file with a different size than FakeStatCache uses. 554 const FileEntry *File = Manager.getVirtualFile("/tmp/test", /*Size=*/10, 0); 555 ASSERT_TRUE(File); 556 const FileEntry &FE = *File; 557 EXPECT_TRUE(FE.isValid()); 558 EXPECT_EQ(FE.getSize(), 10); 559 560 // Calling a second time should not affect the UID or size. 561 unsigned VirtualUID = FE.getUID(); 562 EXPECT_EQ( 563 &FE, 564 &expectedToOptional(Manager.getFileRef("/tmp/test"))->getFileEntry()); 565 EXPECT_EQ(FE.getUID(), VirtualUID); 566 EXPECT_EQ(FE.getSize(), 10); 567 568 // Bypass the file. 569 llvm::Optional<FileEntryRef> BypassRef = 570 Manager.getBypassFile(File->getLastRef()); 571 ASSERT_TRUE(BypassRef); 572 EXPECT_TRUE(BypassRef->isValid()); 573 EXPECT_EQ("/tmp/test", BypassRef->getName()); 574 575 // Check that it's different in the right ways. 576 EXPECT_NE(&BypassRef->getFileEntry(), File); 577 EXPECT_NE(BypassRef->getUID(), VirtualUID); 578 EXPECT_NE(BypassRef->getSize(), FE.getSize()); 579 580 // The virtual file should still be returned when searching. 581 EXPECT_EQ( 582 &FE, 583 &expectedToOptional(Manager.getFileRef("/tmp/test"))->getFileEntry()); 584 } 585 586 } // anonymous namespace 587