2011-02-16 05:30:27 +08:00
|
|
|
//===- unittests/Basic/FileMangerTest.cpp ------------ FileManger tests ---===//
|
2011-02-12 02:44:49 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2011-02-12 02:44:49 +08:00
|
|
|
#include "clang/Basic/FileSystemOptions.h"
|
|
|
|
#include "clang/Basic/FileSystemStatCache.h"
|
2015-03-02 05:36:40 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-03-28 17:18:05 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-10-10 21:27:25 +08:00
|
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
2015-01-14 19:23:58 +08:00
|
|
|
#include "gtest/gtest.h"
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Used to create a fake file system for running the tests with such
|
|
|
|
// that the tests are not affected by the structure/contents of the
|
|
|
|
// file system on the machine running the tests.
|
|
|
|
class FakeStatCache : public FileSystemStatCache {
|
|
|
|
private:
|
|
|
|
// Maps a file/directory path to its desired stat result. Anything
|
|
|
|
// not in this map is considered to not exist in the file system.
|
2013-08-02 05:42:11 +08:00
|
|
|
llvm::StringMap<FileData, llvm::BumpPtrAllocator> StatCalls;
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) {
|
2018-04-28 03:11:14 +08:00
|
|
|
#ifndef _WIN32
|
2017-03-28 17:18:05 +08:00
|
|
|
SmallString<128> NormalizedPath(Path);
|
|
|
|
llvm::sys::path::native(NormalizedPath);
|
|
|
|
Path = NormalizedPath.c_str();
|
|
|
|
#endif
|
|
|
|
|
2013-08-02 05:42:11 +08:00
|
|
|
FileData Data;
|
2014-03-01 05:16:07 +08:00
|
|
|
Data.Name = Path;
|
|
|
|
Data.Size = 0;
|
|
|
|
Data.ModTime = 0;
|
|
|
|
Data.UniqueID = llvm::sys::fs::UniqueID(1, INode);
|
2013-08-02 05:42:11 +08:00
|
|
|
Data.IsDirectory = !IsFile;
|
2014-03-01 05:16:07 +08:00
|
|
|
Data.IsNamedPipe = false;
|
|
|
|
Data.InPCH = false;
|
2013-08-02 05:42:11 +08:00
|
|
|
StatCalls[Path] = Data;
|
2011-02-12 02:44:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Inject a file with the given inode value to the fake file system.
|
|
|
|
void InjectFile(const char *Path, ino_t INode) {
|
|
|
|
InjectFileOrDirectory(Path, INode, /*IsFile=*/true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject a directory with the given inode value to the fake file system.
|
|
|
|
void InjectDirectory(const char *Path, ino_t INode) {
|
|
|
|
InjectFileOrDirectory(Path, INode, /*IsFile=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement FileSystemStatCache::getStat().
|
2016-10-11 15:31:29 +08:00
|
|
|
LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
|
2018-10-10 21:27:25 +08:00
|
|
|
std::unique_ptr<llvm::vfs::File> *F,
|
|
|
|
llvm::vfs::FileSystem &FS) override {
|
2018-04-28 03:11:14 +08:00
|
|
|
#ifndef _WIN32
|
2017-03-28 17:18:05 +08:00
|
|
|
SmallString<128> NormalizedPath(Path);
|
|
|
|
llvm::sys::path::native(NormalizedPath);
|
|
|
|
Path = NormalizedPath.c_str();
|
|
|
|
#endif
|
|
|
|
|
2011-02-12 02:44:49 +08:00
|
|
|
if (StatCalls.count(Path) != 0) {
|
2013-08-02 05:42:11 +08:00
|
|
|
Data = StatCalls[Path];
|
2011-02-12 02:44:49 +08:00
|
|
|
return CacheExists;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CacheMissing; // This means the file/directory doesn't exist.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The test fixture.
|
|
|
|
class FileManagerTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
FileManagerTest() : manager(options) {
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystemOptions options;
|
|
|
|
FileManager manager;
|
|
|
|
};
|
|
|
|
|
|
|
|
// When a virtual file is added, its getDir() field is set correctly
|
|
|
|
// (not NULL, correct name).
|
|
|
|
TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
|
|
|
|
const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0);
|
2014-06-08 16:38:12 +08:00
|
|
|
ASSERT_TRUE(file != nullptr);
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
const DirectoryEntry *dir = file->getDir();
|
2014-06-08 16:38:12 +08:00
|
|
|
ASSERT_TRUE(dir != nullptr);
|
2016-10-11 15:31:29 +08:00
|
|
|
EXPECT_EQ(".", dir->getName());
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
|
2014-06-08 16:38:12 +08:00
|
|
|
ASSERT_TRUE(file != nullptr);
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
dir = file->getDir();
|
2014-06-08 16:38:12 +08:00
|
|
|
ASSERT_TRUE(dir != nullptr);
|
2016-10-11 15:31:29 +08:00
|
|
|
EXPECT_EQ("x/y", dir->getName());
|
2011-02-12 02:44:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Before any virtual file is added, no virtual directory exists.
|
|
|
|
TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) {
|
|
|
|
// An empty FakeStatCache causes all stat calls made by the
|
|
|
|
// FileManager to report "file/directory doesn't exist". This
|
|
|
|
// avoids the possibility of the result of this test being affected
|
|
|
|
// by what's in the real file system.
|
2014-08-12 05:29:24 +08:00
|
|
|
manager.addStatCache(llvm::make_unique<FakeStatCache>());
|
2011-02-12 02:44:49 +08:00
|
|
|
|
2014-06-08 16:38:12 +08:00
|
|
|
EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo"));
|
|
|
|
EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir"));
|
|
|
|
EXPECT_EQ(nullptr, manager.getDirectory("virtual"));
|
2011-02-12 02:44:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// When a virtual file is added, all of its ancestors should be created.
|
|
|
|
TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) {
|
|
|
|
// Fake an empty real file system.
|
2014-08-12 05:29:24 +08:00
|
|
|
manager.addStatCache(llvm::make_unique<FakeStatCache>());
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
|
2014-06-08 16:38:12 +08:00
|
|
|
EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo"));
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
const DirectoryEntry *dir = manager.getDirectory("virtual/dir");
|
2014-06-08 16:38:12 +08:00
|
|
|
ASSERT_TRUE(dir != nullptr);
|
2016-10-11 15:31:29 +08:00
|
|
|
EXPECT_EQ("virtual/dir", dir->getName());
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
dir = manager.getDirectory("virtual");
|
2014-06-08 16:38:12 +08:00
|
|
|
ASSERT_TRUE(dir != nullptr);
|
2016-10-11 15:31:29 +08:00
|
|
|
EXPECT_EQ("virtual", dir->getName());
|
2011-02-12 02:44:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// getFile() returns non-NULL if a real file exists at the given path.
|
|
|
|
TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
|
|
|
|
// Inject fake files into the file system.
|
2014-08-12 05:29:24 +08:00
|
|
|
auto statCache = llvm::make_unique<FakeStatCache>();
|
2011-02-12 02:44:49 +08:00
|
|
|
statCache->InjectDirectory("/tmp", 42);
|
|
|
|
statCache->InjectFile("/tmp/test", 43);
|
2013-07-29 23:47:24 +08:00
|
|
|
|
2018-04-28 03:11:14 +08:00
|
|
|
#ifdef _WIN32
|
2013-07-29 23:47:24 +08:00
|
|
|
const char *DirName = "C:.";
|
|
|
|
const char *FileName = "C:test";
|
|
|
|
statCache->InjectDirectory(DirName, 44);
|
|
|
|
statCache->InjectFile(FileName, 45);
|
|
|
|
#endif
|
|
|
|
|
2014-08-12 05:29:24 +08:00
|
|
|
manager.addStatCache(std::move(statCache));
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
const FileEntry *file = manager.getFile("/tmp/test");
|
2014-06-08 16:38:12 +08:00
|
|
|
ASSERT_TRUE(file != nullptr);
|
2017-03-28 17:18:05 +08:00
|
|
|
ASSERT_TRUE(file->isValid());
|
2016-10-11 06:52:47 +08:00
|
|
|
EXPECT_EQ("/tmp/test", file->getName());
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
const DirectoryEntry *dir = file->getDir();
|
2014-06-08 16:38:12 +08:00
|
|
|
ASSERT_TRUE(dir != nullptr);
|
2016-10-11 15:31:29 +08:00
|
|
|
EXPECT_EQ("/tmp", dir->getName());
|
2013-07-29 23:47:24 +08:00
|
|
|
|
2018-04-28 03:11:14 +08:00
|
|
|
#ifdef _WIN32
|
2013-07-29 23:47:24 +08:00
|
|
|
file = manager.getFile(FileName);
|
|
|
|
ASSERT_TRUE(file != NULL);
|
|
|
|
|
|
|
|
dir = file->getDir();
|
|
|
|
ASSERT_TRUE(dir != NULL);
|
2016-10-11 15:31:29 +08:00
|
|
|
EXPECT_EQ(DirName, dir->getName());
|
2013-07-29 23:47:24 +08:00
|
|
|
#endif
|
2011-02-12 02:44:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// getFile() returns non-NULL if a virtual file exists at the given path.
|
|
|
|
TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
|
|
|
|
// Fake an empty real file system.
|
2014-08-12 05:29:24 +08:00
|
|
|
manager.addStatCache(llvm::make_unique<FakeStatCache>());
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
|
|
|
|
const FileEntry *file = manager.getFile("virtual/dir/bar.h");
|
2014-06-08 16:38:12 +08:00
|
|
|
ASSERT_TRUE(file != nullptr);
|
2017-03-28 17:18:05 +08:00
|
|
|
ASSERT_TRUE(file->isValid());
|
2016-10-11 06:52:47 +08:00
|
|
|
EXPECT_EQ("virtual/dir/bar.h", file->getName());
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
const DirectoryEntry *dir = file->getDir();
|
2014-06-08 16:38:12 +08:00
|
|
|
ASSERT_TRUE(dir != nullptr);
|
2016-10-11 15:31:29 +08:00
|
|
|
EXPECT_EQ("virtual/dir", dir->getName());
|
2011-02-12 02:44:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// getFile() returns different FileEntries for different paths when
|
|
|
|
// there's no aliasing.
|
|
|
|
TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) {
|
|
|
|
// Inject two fake files into the file system. Different inodes
|
|
|
|
// mean the files are not symlinked together.
|
2014-08-12 05:29:24 +08:00
|
|
|
auto statCache = llvm::make_unique<FakeStatCache>();
|
2011-02-12 02:44:49 +08:00
|
|
|
statCache->InjectDirectory(".", 41);
|
|
|
|
statCache->InjectFile("foo.cpp", 42);
|
|
|
|
statCache->InjectFile("bar.cpp", 43);
|
2014-08-12 05:29:24 +08:00
|
|
|
manager.addStatCache(std::move(statCache));
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
const FileEntry *fileFoo = manager.getFile("foo.cpp");
|
|
|
|
const FileEntry *fileBar = manager.getFile("bar.cpp");
|
2014-06-08 16:38:12 +08:00
|
|
|
ASSERT_TRUE(fileFoo != nullptr);
|
2017-03-28 17:18:05 +08:00
|
|
|
ASSERT_TRUE(fileFoo->isValid());
|
2014-06-08 16:38:12 +08:00
|
|
|
ASSERT_TRUE(fileBar != nullptr);
|
2017-03-28 17:18:05 +08:00
|
|
|
ASSERT_TRUE(fileBar->isValid());
|
2011-02-12 02:44:49 +08:00
|
|
|
EXPECT_NE(fileFoo, fileBar);
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFile() returns NULL if neither a real file nor a virtual file
|
|
|
|
// exists at the given path.
|
|
|
|
TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) {
|
|
|
|
// Inject a fake foo.cpp into the file system.
|
2014-08-12 05:29:24 +08:00
|
|
|
auto statCache = llvm::make_unique<FakeStatCache>();
|
2011-02-12 02:44:49 +08:00
|
|
|
statCache->InjectDirectory(".", 41);
|
|
|
|
statCache->InjectFile("foo.cpp", 42);
|
2014-08-12 05:29:24 +08:00
|
|
|
manager.addStatCache(std::move(statCache));
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
// Create a virtual bar.cpp file.
|
|
|
|
manager.getVirtualFile("bar.cpp", 200, 0);
|
|
|
|
|
|
|
|
const FileEntry *file = manager.getFile("xyz.txt");
|
2014-06-08 16:38:12 +08:00
|
|
|
EXPECT_EQ(nullptr, file);
|
2011-02-12 02:44:49 +08:00
|
|
|
}
|
|
|
|
|
2018-11-19 21:37:46 +08:00
|
|
|
// When calling getFile(OpenFile=false); getFile(OpenFile=true) the file is
|
|
|
|
// opened for the second call.
|
|
|
|
TEST_F(FileManagerTest, getFileDefersOpen) {
|
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS(
|
|
|
|
new llvm::vfs::InMemoryFileSystem());
|
|
|
|
FS->addFile("/tmp/test", 0, llvm::MemoryBuffer::getMemBufferCopy("test"));
|
|
|
|
FS->addFile("/tmp/testv", 0, llvm::MemoryBuffer::getMemBufferCopy("testv"));
|
|
|
|
FileManager manager(options, FS);
|
|
|
|
|
|
|
|
const FileEntry *file = manager.getFile("/tmp/test", /*OpenFile=*/false);
|
|
|
|
ASSERT_TRUE(file != nullptr);
|
|
|
|
ASSERT_TRUE(file->isValid());
|
|
|
|
// "real path name" reveals whether the file was actually opened.
|
2018-12-01 01:10:11 +08:00
|
|
|
EXPECT_FALSE(file->isOpenForTests());
|
2018-11-19 21:37:46 +08:00
|
|
|
|
|
|
|
file = manager.getFile("/tmp/test", /*OpenFile=*/true);
|
|
|
|
ASSERT_TRUE(file != nullptr);
|
|
|
|
ASSERT_TRUE(file->isValid());
|
2018-12-01 01:10:11 +08:00
|
|
|
EXPECT_TRUE(file->isOpenForTests());
|
2018-11-19 21:37:46 +08:00
|
|
|
|
|
|
|
// However we should never try to open a file previously opened as virtual.
|
|
|
|
ASSERT_TRUE(manager.getVirtualFile("/tmp/testv", 5, 0));
|
|
|
|
ASSERT_TRUE(manager.getFile("/tmp/testv", /*OpenFile=*/false));
|
|
|
|
file = manager.getFile("/tmp/testv", /*OpenFile=*/true);
|
2018-12-01 01:10:11 +08:00
|
|
|
EXPECT_FALSE(file->isOpenForTests());
|
2018-11-19 21:37:46 +08:00
|
|
|
}
|
|
|
|
|
2011-02-12 02:44:49 +08:00
|
|
|
// The following tests apply to Unix-like system only.
|
|
|
|
|
2018-04-28 03:11:14 +08:00
|
|
|
#ifndef _WIN32
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
// getFile() returns the same FileEntry for real files that are aliases.
|
|
|
|
TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
|
|
|
|
// Inject two real files with the same inode.
|
2014-08-12 05:29:24 +08:00
|
|
|
auto statCache = llvm::make_unique<FakeStatCache>();
|
2011-02-12 02:44:49 +08:00
|
|
|
statCache->InjectDirectory("abc", 41);
|
|
|
|
statCache->InjectFile("abc/foo.cpp", 42);
|
|
|
|
statCache->InjectFile("abc/bar.cpp", 42);
|
2014-08-12 05:29:24 +08:00
|
|
|
manager.addStatCache(std::move(statCache));
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFile() returns the same FileEntry for virtual files that have
|
|
|
|
// corresponding real files that are aliases.
|
|
|
|
TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
|
|
|
|
// Inject two real files with the same inode.
|
2014-08-12 05:29:24 +08:00
|
|
|
auto statCache = llvm::make_unique<FakeStatCache>();
|
2011-02-12 02:44:49 +08:00
|
|
|
statCache->InjectDirectory("abc", 41);
|
|
|
|
statCache->InjectFile("abc/foo.cpp", 42);
|
|
|
|
statCache->InjectFile("abc/bar.cpp", 42);
|
2014-08-12 05:29:24 +08:00
|
|
|
manager.addStatCache(std::move(statCache));
|
2011-02-12 02:44:49 +08:00
|
|
|
|
2017-03-28 17:18:05 +08:00
|
|
|
ASSERT_TRUE(manager.getVirtualFile("abc/foo.cpp", 100, 0)->isValid());
|
|
|
|
ASSERT_TRUE(manager.getVirtualFile("abc/bar.cpp", 200, 0)->isValid());
|
2011-02-12 02:44:49 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
|
|
|
|
}
|
|
|
|
|
2014-08-12 05:29:24 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-28 17:18:05 +08:00
|
|
|
// 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
|
|
|
|
// normalisation done by the file system, like on Windows). This can be checked
|
2018-07-24 11:34:15 +08:00
|
|
|
// here by checking the size.
|
2017-03-28 17:18:05 +08:00
|
|
|
TEST_F(FileManagerTest, getVirtualFileWithDifferentName) {
|
|
|
|
// Inject fake files into the file system.
|
|
|
|
auto statCache = llvm::make_unique<FakeStatCache>();
|
|
|
|
statCache->InjectDirectory("c:\\tmp", 42);
|
|
|
|
statCache->InjectFile("c:\\tmp\\test", 43);
|
|
|
|
|
|
|
|
manager.addStatCache(std::move(statCache));
|
|
|
|
|
|
|
|
// Inject the virtual file:
|
|
|
|
const FileEntry *file1 = manager.getVirtualFile("c:\\tmp\\test", 123, 1);
|
|
|
|
ASSERT_TRUE(file1 != nullptr);
|
|
|
|
ASSERT_TRUE(file1->isValid());
|
|
|
|
EXPECT_EQ(43U, file1->getUniqueID().getFile());
|
|
|
|
EXPECT_EQ(123, file1->getSize());
|
|
|
|
|
|
|
|
// Lookup the virtual file with a different name:
|
|
|
|
const FileEntry *file2 = manager.getFile("c:/tmp/test", 100, 1);
|
|
|
|
ASSERT_TRUE(file2 != nullptr);
|
|
|
|
ASSERT_TRUE(file2->isValid());
|
|
|
|
// Check that it's the same UFE:
|
|
|
|
EXPECT_EQ(file1, file2);
|
|
|
|
EXPECT_EQ(43U, file2->getUniqueID().getFile());
|
|
|
|
// Check that the contents of the UFE are not overwritten by the entry in the
|
|
|
|
// filesystem:
|
|
|
|
EXPECT_EQ(123, file2->getSize());
|
|
|
|
}
|
|
|
|
|
2018-04-28 03:11:14 +08:00
|
|
|
#endif // !_WIN32
|
2011-02-12 02:44:49 +08:00
|
|
|
|
2017-08-02 15:25:24 +08:00
|
|
|
TEST_F(FileManagerTest, makeAbsoluteUsesVFS) {
|
|
|
|
SmallString<64> CustomWorkingDir;
|
2018-04-28 03:11:14 +08:00
|
|
|
#ifdef _WIN32
|
2017-08-02 15:25:24 +08:00
|
|
|
CustomWorkingDir = "C:";
|
|
|
|
#else
|
|
|
|
CustomWorkingDir = "/";
|
|
|
|
#endif
|
|
|
|
llvm::sys::path::append(CustomWorkingDir, "some", "weird", "path");
|
|
|
|
|
2018-10-10 21:27:25 +08:00
|
|
|
auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
|
|
|
|
new llvm::vfs::InMemoryFileSystem);
|
2017-08-02 15:25:24 +08:00
|
|
|
// setCurrentworkingdirectory must finish without error.
|
|
|
|
ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
|
|
|
|
|
|
|
|
FileSystemOptions Opts;
|
|
|
|
FileManager Manager(Opts, FS);
|
|
|
|
|
|
|
|
SmallString<64> Path("a/foo.cpp");
|
|
|
|
|
|
|
|
SmallString<64> ExpectedResult(CustomWorkingDir);
|
|
|
|
llvm::sys::path::append(ExpectedResult, Path);
|
|
|
|
|
|
|
|
ASSERT_TRUE(Manager.makeAbsolutePath(Path));
|
|
|
|
EXPECT_EQ(Path, ExpectedResult);
|
|
|
|
}
|
|
|
|
|
2018-12-01 01:10:11 +08:00
|
|
|
// getVirtualFile should always fill the real path.
|
|
|
|
TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) {
|
2018-12-08 07:50:05 +08:00
|
|
|
SmallString<64> CustomWorkingDir;
|
|
|
|
#ifdef _WIN32
|
|
|
|
CustomWorkingDir = "C:/";
|
|
|
|
#else
|
|
|
|
CustomWorkingDir = "/";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
|
|
|
|
new llvm::vfs::InMemoryFileSystem);
|
|
|
|
// setCurrentworkingdirectory must finish without error.
|
|
|
|
ASSERT_TRUE(!FS->setCurrentWorkingDirectory(CustomWorkingDir));
|
|
|
|
|
|
|
|
FileSystemOptions Opts;
|
|
|
|
FileManager Manager(Opts, FS);
|
|
|
|
|
2018-12-01 01:10:11 +08:00
|
|
|
// Inject fake files into the file system.
|
|
|
|
auto statCache = llvm::make_unique<FakeStatCache>();
|
|
|
|
statCache->InjectDirectory("/tmp", 42);
|
|
|
|
statCache->InjectFile("/tmp/test", 43);
|
2018-12-08 07:50:05 +08:00
|
|
|
|
|
|
|
Manager.addStatCache(std::move(statCache));
|
2018-12-01 01:10:11 +08:00
|
|
|
|
|
|
|
// Check for real path.
|
2018-12-08 07:50:05 +08:00
|
|
|
const FileEntry *file = Manager.getVirtualFile("/tmp/test", 123, 1);
|
2018-12-01 01:10:11 +08:00
|
|
|
ASSERT_TRUE(file != nullptr);
|
|
|
|
ASSERT_TRUE(file->isValid());
|
2018-12-08 07:50:05 +08:00
|
|
|
SmallString<64> ExpectedResult = CustomWorkingDir;
|
|
|
|
|
2018-12-01 02:36:31 +08:00
|
|
|
llvm::sys::path::append(ExpectedResult, "tmp", "test");
|
|
|
|
EXPECT_EQ(file->tryGetRealPathName(), ExpectedResult);
|
2018-12-01 01:10:11 +08:00
|
|
|
}
|
|
|
|
|
2011-02-12 02:44:49 +08:00
|
|
|
} // anonymous namespace
|