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