Changeset View
Changeset View
Standalone View
Standalone View
cfe/trunk/unittests/Basic/FileManagerTest.cpp
Show First 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
// Before any virtual file is added, no virtual directory exists. | // Before any virtual file is added, no virtual directory exists. | ||||
TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) { | TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) { | ||||
// An empty FakeStatCache causes all stat calls made by the | // An empty FakeStatCache causes all stat calls made by the | ||||
// FileManager to report "file/directory doesn't exist". This | // FileManager to report "file/directory doesn't exist". This | ||||
// avoids the possibility of the result of this test being affected | // avoids the possibility of the result of this test being affected | ||||
// by what's in the real file system. | // by what's in the real file system. | ||||
manager.addStatCache(llvm::make_unique<FakeStatCache>()); | manager.setStatCache(llvm::make_unique<FakeStatCache>()); | ||||
EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo")); | EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo")); | ||||
EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir")); | EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir")); | ||||
EXPECT_EQ(nullptr, manager.getDirectory("virtual")); | EXPECT_EQ(nullptr, manager.getDirectory("virtual")); | ||||
} | } | ||||
// When a virtual file is added, all of its ancestors should be created. | // When a virtual file is added, all of its ancestors should be created. | ||||
TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) { | TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) { | ||||
// Fake an empty real file system. | // Fake an empty real file system. | ||||
manager.addStatCache(llvm::make_unique<FakeStatCache>()); | manager.setStatCache(llvm::make_unique<FakeStatCache>()); | ||||
manager.getVirtualFile("virtual/dir/bar.h", 100, 0); | manager.getVirtualFile("virtual/dir/bar.h", 100, 0); | ||||
EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo")); | EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo")); | ||||
const DirectoryEntry *dir = manager.getDirectory("virtual/dir"); | const DirectoryEntry *dir = manager.getDirectory("virtual/dir"); | ||||
ASSERT_TRUE(dir != nullptr); | ASSERT_TRUE(dir != nullptr); | ||||
EXPECT_EQ("virtual/dir", dir->getName()); | EXPECT_EQ("virtual/dir", dir->getName()); | ||||
Show All 11 Lines | |||||
#ifdef _WIN32 | #ifdef _WIN32 | ||||
const char *DirName = "C:."; | const char *DirName = "C:."; | ||||
const char *FileName = "C:test"; | const char *FileName = "C:test"; | ||||
statCache->InjectDirectory(DirName, 44); | statCache->InjectDirectory(DirName, 44); | ||||
statCache->InjectFile(FileName, 45); | statCache->InjectFile(FileName, 45); | ||||
#endif | #endif | ||||
manager.addStatCache(std::move(statCache)); | manager.setStatCache(std::move(statCache)); | ||||
const FileEntry *file = manager.getFile("/tmp/test"); | const FileEntry *file = manager.getFile("/tmp/test"); | ||||
ASSERT_TRUE(file != nullptr); | ASSERT_TRUE(file != nullptr); | ||||
ASSERT_TRUE(file->isValid()); | ASSERT_TRUE(file->isValid()); | ||||
EXPECT_EQ("/tmp/test", file->getName()); | EXPECT_EQ("/tmp/test", file->getName()); | ||||
const DirectoryEntry *dir = file->getDir(); | const DirectoryEntry *dir = file->getDir(); | ||||
ASSERT_TRUE(dir != nullptr); | ASSERT_TRUE(dir != nullptr); | ||||
EXPECT_EQ("/tmp", dir->getName()); | EXPECT_EQ("/tmp", dir->getName()); | ||||
#ifdef _WIN32 | #ifdef _WIN32 | ||||
file = manager.getFile(FileName); | file = manager.getFile(FileName); | ||||
ASSERT_TRUE(file != NULL); | ASSERT_TRUE(file != NULL); | ||||
dir = file->getDir(); | dir = file->getDir(); | ||||
ASSERT_TRUE(dir != NULL); | ASSERT_TRUE(dir != NULL); | ||||
EXPECT_EQ(DirName, dir->getName()); | EXPECT_EQ(DirName, dir->getName()); | ||||
#endif | #endif | ||||
} | } | ||||
// getFile() returns non-NULL if a virtual file exists at the given path. | // getFile() returns non-NULL if a virtual file exists at the given path. | ||||
TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) { | TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) { | ||||
// Fake an empty real file system. | // Fake an empty real file system. | ||||
manager.addStatCache(llvm::make_unique<FakeStatCache>()); | manager.setStatCache(llvm::make_unique<FakeStatCache>()); | ||||
manager.getVirtualFile("virtual/dir/bar.h", 100, 0); | manager.getVirtualFile("virtual/dir/bar.h", 100, 0); | ||||
const FileEntry *file = manager.getFile("virtual/dir/bar.h"); | const FileEntry *file = manager.getFile("virtual/dir/bar.h"); | ||||
ASSERT_TRUE(file != nullptr); | ASSERT_TRUE(file != nullptr); | ||||
ASSERT_TRUE(file->isValid()); | ASSERT_TRUE(file->isValid()); | ||||
EXPECT_EQ("virtual/dir/bar.h", file->getName()); | EXPECT_EQ("virtual/dir/bar.h", file->getName()); | ||||
const DirectoryEntry *dir = file->getDir(); | const DirectoryEntry *dir = file->getDir(); | ||||
ASSERT_TRUE(dir != nullptr); | ASSERT_TRUE(dir != nullptr); | ||||
EXPECT_EQ("virtual/dir", dir->getName()); | EXPECT_EQ("virtual/dir", dir->getName()); | ||||
} | } | ||||
// getFile() returns different FileEntries for different paths when | // getFile() returns different FileEntries for different paths when | ||||
// there's no aliasing. | // there's no aliasing. | ||||
TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) { | TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) { | ||||
// Inject two fake files into the file system. Different inodes | // Inject two fake files into the file system. Different inodes | ||||
// mean the files are not symlinked together. | // mean the files are not symlinked together. | ||||
auto statCache = llvm::make_unique<FakeStatCache>(); | auto statCache = llvm::make_unique<FakeStatCache>(); | ||||
statCache->InjectDirectory(".", 41); | statCache->InjectDirectory(".", 41); | ||||
statCache->InjectFile("foo.cpp", 42); | statCache->InjectFile("foo.cpp", 42); | ||||
statCache->InjectFile("bar.cpp", 43); | statCache->InjectFile("bar.cpp", 43); | ||||
manager.addStatCache(std::move(statCache)); | manager.setStatCache(std::move(statCache)); | ||||
const FileEntry *fileFoo = manager.getFile("foo.cpp"); | const FileEntry *fileFoo = manager.getFile("foo.cpp"); | ||||
const FileEntry *fileBar = manager.getFile("bar.cpp"); | const FileEntry *fileBar = manager.getFile("bar.cpp"); | ||||
ASSERT_TRUE(fileFoo != nullptr); | ASSERT_TRUE(fileFoo != nullptr); | ||||
ASSERT_TRUE(fileFoo->isValid()); | ASSERT_TRUE(fileFoo->isValid()); | ||||
ASSERT_TRUE(fileBar != nullptr); | ASSERT_TRUE(fileBar != nullptr); | ||||
ASSERT_TRUE(fileBar->isValid()); | ASSERT_TRUE(fileBar->isValid()); | ||||
EXPECT_NE(fileFoo, fileBar); | EXPECT_NE(fileFoo, fileBar); | ||||
} | } | ||||
// getFile() returns NULL if neither a real file nor a virtual file | // getFile() returns NULL if neither a real file nor a virtual file | ||||
// exists at the given path. | // exists at the given path. | ||||
TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) { | TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) { | ||||
// Inject a fake foo.cpp into the file system. | // Inject a fake foo.cpp into the file system. | ||||
auto statCache = llvm::make_unique<FakeStatCache>(); | auto statCache = llvm::make_unique<FakeStatCache>(); | ||||
statCache->InjectDirectory(".", 41); | statCache->InjectDirectory(".", 41); | ||||
statCache->InjectFile("foo.cpp", 42); | statCache->InjectFile("foo.cpp", 42); | ||||
manager.addStatCache(std::move(statCache)); | manager.setStatCache(std::move(statCache)); | ||||
// Create a virtual bar.cpp file. | // Create a virtual bar.cpp file. | ||||
manager.getVirtualFile("bar.cpp", 200, 0); | manager.getVirtualFile("bar.cpp", 200, 0); | ||||
const FileEntry *file = manager.getFile("xyz.txt"); | const FileEntry *file = manager.getFile("xyz.txt"); | ||||
EXPECT_EQ(nullptr, file); | EXPECT_EQ(nullptr, file); | ||||
} | } | ||||
Show All 30 Lines | |||||
// getFile() returns the same FileEntry for real files that are aliases. | // getFile() returns the same FileEntry for real files that are aliases. | ||||
TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) { | TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) { | ||||
// Inject two real files with the same inode. | // Inject two real files with the same inode. | ||||
auto statCache = llvm::make_unique<FakeStatCache>(); | auto statCache = llvm::make_unique<FakeStatCache>(); | ||||
statCache->InjectDirectory("abc", 41); | statCache->InjectDirectory("abc", 41); | ||||
statCache->InjectFile("abc/foo.cpp", 42); | statCache->InjectFile("abc/foo.cpp", 42); | ||||
statCache->InjectFile("abc/bar.cpp", 42); | statCache->InjectFile("abc/bar.cpp", 42); | ||||
manager.addStatCache(std::move(statCache)); | manager.setStatCache(std::move(statCache)); | ||||
EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp")); | EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp")); | ||||
} | } | ||||
// getFile() returns the same FileEntry for virtual files that have | // getFile() returns the same FileEntry for virtual files that have | ||||
// corresponding real files that are aliases. | // corresponding real files that are aliases. | ||||
TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) { | TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) { | ||||
// Inject two real files with the same inode. | // Inject two real files with the same inode. | ||||
auto statCache = llvm::make_unique<FakeStatCache>(); | auto statCache = llvm::make_unique<FakeStatCache>(); | ||||
statCache->InjectDirectory("abc", 41); | statCache->InjectDirectory("abc", 41); | ||||
statCache->InjectFile("abc/foo.cpp", 42); | statCache->InjectFile("abc/foo.cpp", 42); | ||||
statCache->InjectFile("abc/bar.cpp", 42); | statCache->InjectFile("abc/bar.cpp", 42); | ||||
manager.addStatCache(std::move(statCache)); | manager.setStatCache(std::move(statCache)); | ||||
ASSERT_TRUE(manager.getVirtualFile("abc/foo.cpp", 100, 0)->isValid()); | ASSERT_TRUE(manager.getVirtualFile("abc/foo.cpp", 100, 0)->isValid()); | ||||
ASSERT_TRUE(manager.getVirtualFile("abc/bar.cpp", 200, 0)->isValid()); | ASSERT_TRUE(manager.getVirtualFile("abc/bar.cpp", 200, 0)->isValid()); | ||||
EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp")); | EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp")); | ||||
} | } | ||||
TEST_F(FileManagerTest, addRemoveStatCache) { | |||||
manager.addStatCache(llvm::make_unique<FakeStatCache>()); | |||||
auto statCacheOwner = llvm::make_unique<FakeStatCache>(); | |||||
auto *statCache = statCacheOwner.get(); | |||||
manager.addStatCache(std::move(statCacheOwner)); | |||||
manager.addStatCache(llvm::make_unique<FakeStatCache>()); | |||||
manager.removeStatCache(statCache); | |||||
} | |||||
// getFile() Should return the same entry as getVirtualFile if the file actually | // getFile() Should return the same entry as getVirtualFile if the file actually | ||||
// is a virtual file, even if the name is not exactly the same (but is after | // is a virtual file, even if the name is not exactly the same (but is after | ||||
// normalisation done by the file system, like on Windows). This can be checked | // normalisation done by the file system, like on Windows). This can be checked | ||||
// here by checking the size. | // here by checking the size. | ||||
TEST_F(FileManagerTest, getVirtualFileWithDifferentName) { | TEST_F(FileManagerTest, getVirtualFileWithDifferentName) { | ||||
// Inject fake files into the file system. | // Inject fake files into the file system. | ||||
auto statCache = llvm::make_unique<FakeStatCache>(); | auto statCache = llvm::make_unique<FakeStatCache>(); | ||||
statCache->InjectDirectory("c:\\tmp", 42); | statCache->InjectDirectory("c:\\tmp", 42); | ||||
statCache->InjectFile("c:\\tmp\\test", 43); | statCache->InjectFile("c:\\tmp\\test", 43); | ||||
manager.addStatCache(std::move(statCache)); | manager.setStatCache(std::move(statCache)); | ||||
// Inject the virtual file: | // Inject the virtual file: | ||||
const FileEntry *file1 = manager.getVirtualFile("c:\\tmp\\test", 123, 1); | const FileEntry *file1 = manager.getVirtualFile("c:\\tmp\\test", 123, 1); | ||||
ASSERT_TRUE(file1 != nullptr); | ASSERT_TRUE(file1 != nullptr); | ||||
ASSERT_TRUE(file1->isValid()); | ASSERT_TRUE(file1->isValid()); | ||||
EXPECT_EQ(43U, file1->getUniqueID().getFile()); | EXPECT_EQ(43U, file1->getUniqueID().getFile()); | ||||
EXPECT_EQ(123, file1->getSize()); | EXPECT_EQ(123, file1->getSize()); | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | #endif | ||||
FileSystemOptions Opts; | FileSystemOptions Opts; | ||||
FileManager Manager(Opts, FS); | FileManager Manager(Opts, FS); | ||||
// Inject fake files into the file system. | // Inject fake files into the file system. | ||||
auto statCache = llvm::make_unique<FakeStatCache>(); | auto statCache = llvm::make_unique<FakeStatCache>(); | ||||
statCache->InjectDirectory("/tmp", 42); | statCache->InjectDirectory("/tmp", 42); | ||||
statCache->InjectFile("/tmp/test", 43); | statCache->InjectFile("/tmp/test", 43); | ||||
Manager.addStatCache(std::move(statCache)); | Manager.setStatCache(std::move(statCache)); | ||||
// Check for real path. | // Check for real path. | ||||
const FileEntry *file = Manager.getVirtualFile("/tmp/test", 123, 1); | const FileEntry *file = Manager.getVirtualFile("/tmp/test", 123, 1); | ||||
ASSERT_TRUE(file != nullptr); | ASSERT_TRUE(file != nullptr); | ||||
ASSERT_TRUE(file->isValid()); | ASSERT_TRUE(file->isValid()); | ||||
SmallString<64> ExpectedResult = CustomWorkingDir; | SmallString<64> ExpectedResult = CustomWorkingDir; | ||||
llvm::sys::path::append(ExpectedResult, "tmp", "test"); | llvm::sys::path::append(ExpectedResult, "tmp", "test"); | ||||
EXPECT_EQ(file->tryGetRealPathName(), ExpectedResult); | EXPECT_EQ(file->tryGetRealPathName(), ExpectedResult); | ||||
} | } | ||||
} // anonymous namespace | } // anonymous namespace |