1 //===- unittests/Basic/FileMangerTest.cpp ------------ FileManger tests ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Basic/FileManager.h"
11 #include "clang/Basic/FileSystemOptions.h"
12 #include "clang/Basic/FileSystemStatCache.h"
13 #include "gtest/gtest.h"
16 using namespace clang;
20 // Used to create a fake file system for running the tests with such
21 // that the tests are not affected by the structure/contents of the
22 // file system on the machine running the tests.
23 class FakeStatCache : public FileSystemStatCache {
25 // Maps a file/directory path to its desired stat result. Anything
26 // not in this map is considered to not exist in the file system.
27 llvm::StringMap<struct stat, llvm::BumpPtrAllocator> StatCalls;
29 void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) {
31 memset(&statBuf, 0, sizeof(statBuf));
33 #ifndef _WIN32 // struct stat has no st_ino field on Windows.
34 statBuf.st_ino = INode;
36 statBuf.st_mode = IsFile ? (0777 | S_IFREG) // a regular file
37 : (0777 | S_IFDIR); // a directory
38 StatCalls[Path] = statBuf;
42 // Inject a file with the given inode value to the fake file system.
43 void InjectFile(const char *Path, ino_t INode) {
44 InjectFileOrDirectory(Path, INode, /*IsFile=*/true);
47 // Inject a directory with the given inode value to the fake file system.
48 void InjectDirectory(const char *Path, ino_t INode) {
49 InjectFileOrDirectory(Path, INode, /*IsFile=*/false);
52 // Implement FileSystemStatCache::getStat().
53 virtual LookupResult getStat(const char *Path, struct stat &StatBuf,
54 bool isFile, int *FileDescriptor) {
55 if (StatCalls.count(Path) != 0) {
56 StatBuf = StatCalls[Path];
60 return CacheMissing; // This means the file/directory doesn't exist.
65 class FileManagerTest : public ::testing::Test {
67 FileManagerTest() : manager(options) {
70 FileSystemOptions options;
74 // When a virtual file is added, its getDir() field is set correctly
75 // (not NULL, correct name).
76 TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
77 const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0);
78 ASSERT_TRUE(file != NULL);
80 const DirectoryEntry *dir = file->getDir();
81 ASSERT_TRUE(dir != NULL);
82 EXPECT_STREQ(".", dir->getName());
84 file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
85 ASSERT_TRUE(file != NULL);
88 ASSERT_TRUE(dir != NULL);
89 EXPECT_STREQ("x/y", dir->getName());
92 // Before any virtual file is added, no virtual directory exists.
93 TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) {
94 // An empty FakeStatCache causes all stat calls made by the
95 // FileManager to report "file/directory doesn't exist". This
96 // avoids the possibility of the result of this test being affected
97 // by what's in the real file system.
98 manager.addStatCache(new FakeStatCache);
100 EXPECT_EQ(NULL, manager.getDirectory("virtual/dir/foo"));
101 EXPECT_EQ(NULL, manager.getDirectory("virtual/dir"));
102 EXPECT_EQ(NULL, manager.getDirectory("virtual"));
105 // When a virtual file is added, all of its ancestors should be created.
106 TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) {
107 // Fake an empty real file system.
108 manager.addStatCache(new FakeStatCache);
110 manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
111 EXPECT_EQ(NULL, manager.getDirectory("virtual/dir/foo"));
113 const DirectoryEntry *dir = manager.getDirectory("virtual/dir");
114 ASSERT_TRUE(dir != NULL);
115 EXPECT_STREQ("virtual/dir", dir->getName());
117 dir = manager.getDirectory("virtual");
118 ASSERT_TRUE(dir != NULL);
119 EXPECT_STREQ("virtual", dir->getName());
122 // getFile() returns non-NULL if a real file exists at the given path.
123 TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
124 // Inject fake files into the file system.
125 FakeStatCache *statCache = new FakeStatCache;
126 statCache->InjectDirectory("/tmp", 42);
127 statCache->InjectFile("/tmp/test", 43);
128 manager.addStatCache(statCache);
130 const FileEntry *file = manager.getFile("/tmp/test");
131 ASSERT_TRUE(file != NULL);
132 EXPECT_STREQ("/tmp/test", file->getName());
134 const DirectoryEntry *dir = file->getDir();
135 ASSERT_TRUE(dir != NULL);
136 EXPECT_STREQ("/tmp", dir->getName());
139 // getFile() returns non-NULL if a virtual file exists at the given path.
140 TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
141 // Fake an empty real file system.
142 manager.addStatCache(new FakeStatCache);
144 manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
145 const FileEntry *file = manager.getFile("virtual/dir/bar.h");
146 ASSERT_TRUE(file != NULL);
147 EXPECT_STREQ("virtual/dir/bar.h", file->getName());
149 const DirectoryEntry *dir = file->getDir();
150 ASSERT_TRUE(dir != NULL);
151 EXPECT_STREQ("virtual/dir", dir->getName());
154 // getFile() returns different FileEntries for different paths when
155 // there's no aliasing.
156 TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) {
157 // Inject two fake files into the file system. Different inodes
158 // mean the files are not symlinked together.
159 FakeStatCache *statCache = new FakeStatCache;
160 statCache->InjectDirectory(".", 41);
161 statCache->InjectFile("foo.cpp", 42);
162 statCache->InjectFile("bar.cpp", 43);
163 manager.addStatCache(statCache);
165 const FileEntry *fileFoo = manager.getFile("foo.cpp");
166 const FileEntry *fileBar = manager.getFile("bar.cpp");
167 ASSERT_TRUE(fileFoo != NULL);
168 ASSERT_TRUE(fileBar != NULL);
169 EXPECT_NE(fileFoo, fileBar);
172 // getFile() returns NULL if neither a real file nor a virtual file
173 // exists at the given path.
174 TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) {
175 // Inject a fake foo.cpp into the file system.
176 FakeStatCache *statCache = new FakeStatCache;
177 statCache->InjectDirectory(".", 41);
178 statCache->InjectFile("foo.cpp", 42);
179 manager.addStatCache(statCache);
181 // Create a virtual bar.cpp file.
182 manager.getVirtualFile("bar.cpp", 200, 0);
184 const FileEntry *file = manager.getFile("xyz.txt");
185 EXPECT_EQ(NULL, file);
188 // The following tests apply to Unix-like system only.
192 // getFile() returns the same FileEntry for real files that are aliases.
193 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
194 // Inject two real files with the same inode.
195 FakeStatCache *statCache = new FakeStatCache;
196 statCache->InjectDirectory("abc", 41);
197 statCache->InjectFile("abc/foo.cpp", 42);
198 statCache->InjectFile("abc/bar.cpp", 42);
199 manager.addStatCache(statCache);
201 EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
204 // getFile() returns the same FileEntry for virtual files that have
205 // corresponding real files that are aliases.
206 TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
207 // Inject two real files with the same inode.
208 FakeStatCache *statCache = new FakeStatCache;
209 statCache->InjectDirectory("abc", 41);
210 statCache->InjectFile("abc/foo.cpp", 42);
211 statCache->InjectFile("abc/bar.cpp", 42);
212 manager.addStatCache(statCache);
214 manager.getVirtualFile("abc/foo.cpp", 100, 0);
215 manager.getVirtualFile("abc/bar.cpp", 200, 0);
217 EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
222 } // anonymous namespace