FileManagerTest.cpp [plain text]
#include "clang/Basic/FileSystemOptions.h"
#include "clang/Basic/FileSystemStatCache.h"
#include "clang/Basic/FileManager.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace clang;
namespace {
class FakeStatCache : public FileSystemStatCache {
private:
llvm::StringMap<struct stat, llvm::BumpPtrAllocator> StatCalls;
void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) {
struct stat statBuf;
memset(&statBuf, 0, sizeof(statBuf));
statBuf.st_dev = 1;
#ifndef _WIN32 // struct stat has no st_ino field on Windows.
statBuf.st_ino = INode;
#endif
statBuf.st_mode = IsFile ? (0777 | S_IFREG) : (0777 | S_IFDIR); StatCalls[Path] = statBuf;
}
public:
void InjectFile(const char *Path, ino_t INode) {
InjectFileOrDirectory(Path, INode, true);
}
void InjectDirectory(const char *Path, ino_t INode) {
InjectFileOrDirectory(Path, INode, false);
}
virtual LookupResult getStat(const char *Path, struct stat &StatBuf,
int *FileDescriptor) {
if (StatCalls.count(Path) != 0) {
StatBuf = StatCalls[Path];
return CacheExists;
}
return CacheMissing; }
};
class FileManagerTest : public ::testing::Test {
protected:
FileManagerTest() : manager(options) {
}
FileSystemOptions options;
FileManager manager;
};
TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0);
ASSERT_TRUE(file != NULL);
const DirectoryEntry *dir = file->getDir();
ASSERT_TRUE(dir != NULL);
EXPECT_STREQ(".", dir->getName());
file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
ASSERT_TRUE(file != NULL);
dir = file->getDir();
ASSERT_TRUE(dir != NULL);
EXPECT_STREQ("x/y", dir->getName());
}
TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) {
manager.addStatCache(new FakeStatCache);
EXPECT_EQ(NULL, manager.getDirectory("virtual/dir/foo"));
EXPECT_EQ(NULL, manager.getDirectory("virtual/dir"));
EXPECT_EQ(NULL, manager.getDirectory("virtual"));
}
TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) {
manager.addStatCache(new FakeStatCache);
manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
EXPECT_EQ(NULL, manager.getDirectory("virtual/dir/foo"));
const DirectoryEntry *dir = manager.getDirectory("virtual/dir");
ASSERT_TRUE(dir != NULL);
EXPECT_STREQ("virtual/dir", dir->getName());
dir = manager.getDirectory("virtual");
ASSERT_TRUE(dir != NULL);
EXPECT_STREQ("virtual", dir->getName());
}
TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
FakeStatCache *statCache = new FakeStatCache;
statCache->InjectDirectory("/tmp", 42);
statCache->InjectFile("/tmp/test", 43);
manager.addStatCache(statCache);
const FileEntry *file = manager.getFile("/tmp/test");
ASSERT_TRUE(file != NULL);
EXPECT_STREQ("/tmp/test", file->getName());
const DirectoryEntry *dir = file->getDir();
ASSERT_TRUE(dir != NULL);
EXPECT_STREQ("/tmp", dir->getName());
}
TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
manager.addStatCache(new FakeStatCache);
manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
const FileEntry *file = manager.getFile("virtual/dir/bar.h");
ASSERT_TRUE(file != NULL);
EXPECT_STREQ("virtual/dir/bar.h", file->getName());
const DirectoryEntry *dir = file->getDir();
ASSERT_TRUE(dir != NULL);
EXPECT_STREQ("virtual/dir", dir->getName());
}
TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) {
FakeStatCache *statCache = new FakeStatCache;
statCache->InjectDirectory(".", 41);
statCache->InjectFile("foo.cpp", 42);
statCache->InjectFile("bar.cpp", 43);
manager.addStatCache(statCache);
const FileEntry *fileFoo = manager.getFile("foo.cpp");
const FileEntry *fileBar = manager.getFile("bar.cpp");
ASSERT_TRUE(fileFoo != NULL);
ASSERT_TRUE(fileBar != NULL);
EXPECT_NE(fileFoo, fileBar);
}
TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) {
FakeStatCache *statCache = new FakeStatCache;
statCache->InjectDirectory(".", 41);
statCache->InjectFile("foo.cpp", 42);
manager.addStatCache(statCache);
manager.getVirtualFile("bar.cpp", 200, 0);
const FileEntry *file = manager.getFile("xyz.txt");
EXPECT_EQ(NULL, file);
}
#ifndef _WIN32
TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
FakeStatCache *statCache = new FakeStatCache;
statCache->InjectDirectory("abc", 41);
statCache->InjectFile("abc/foo.cpp", 42);
statCache->InjectFile("abc/bar.cpp", 42);
manager.addStatCache(statCache);
EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
}
TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
FakeStatCache *statCache = new FakeStatCache;
statCache->InjectDirectory("abc", 41);
statCache->InjectFile("abc/foo.cpp", 42);
statCache->InjectFile("abc/bar.cpp", 42);
manager.addStatCache(statCache);
manager.getVirtualFile("abc/foo.cpp", 100, 0);
manager.getVirtualFile("abc/bar.cpp", 200, 0);
EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
}
#endif // !_WIN32
}