blob: f8ce50d531e737d47edd95ce3bdf9ece6fc95c43 [file] [log] [blame]
Zhanyong Wan483651c2011-02-15 21:30:27 +00001//===- unittests/Basic/FileMangerTest.cpp ------------ FileManger tests ---===//
Zhanyong Wan9b555ea2011-02-11 18:44:49 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth1050e8b2012-12-04 09:45:34 +000010#include "clang/Basic/FileManager.h"
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000011#include "clang/Basic/FileSystemOptions.h"
12#include "clang/Basic/FileSystemStatCache.h"
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000013#include "gtest/gtest.h"
14
15using namespace llvm;
16using namespace clang;
17
18namespace {
19
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.
23class FakeStatCache : public FileSystemStatCache {
24private:
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.
Rafael Espindola0fda0f72013-08-01 21:42:11 +000027 llvm::StringMap<FileData, llvm::BumpPtrAllocator> StatCalls;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000028
29 void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +000030 FileData Data;
31 memset(&Data, 0, sizeof(FileData));
32 llvm::sys::fs::UniqueID ID(1, INode);
33 Data.UniqueID = ID;
34 Data.IsDirectory = !IsFile;
35 StatCalls[Path] = Data;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000036 }
37
38public:
39 // Inject a file with the given inode value to the fake file system.
40 void InjectFile(const char *Path, ino_t INode) {
41 InjectFileOrDirectory(Path, INode, /*IsFile=*/true);
42 }
43
44 // Inject a directory with the given inode value to the fake file system.
45 void InjectDirectory(const char *Path, ino_t INode) {
46 InjectFileOrDirectory(Path, INode, /*IsFile=*/false);
47 }
48
49 // Implement FileSystemStatCache::getStat().
Rafael Espindola0fda0f72013-08-01 21:42:11 +000050 virtual LookupResult getStat(const char *Path, FileData &Data, bool isFile,
51 int *FileDescriptor) {
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000052 if (StatCalls.count(Path) != 0) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +000053 Data = StatCalls[Path];
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000054 return CacheExists;
55 }
56
57 return CacheMissing; // This means the file/directory doesn't exist.
58 }
59};
60
61// The test fixture.
62class FileManagerTest : public ::testing::Test {
63 protected:
64 FileManagerTest() : manager(options) {
65 }
66
67 FileSystemOptions options;
68 FileManager manager;
69};
70
71// When a virtual file is added, its getDir() field is set correctly
72// (not NULL, correct name).
73TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
74 const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0);
75 ASSERT_TRUE(file != NULL);
76
77 const DirectoryEntry *dir = file->getDir();
78 ASSERT_TRUE(dir != NULL);
79 EXPECT_STREQ(".", dir->getName());
80
81 file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
82 ASSERT_TRUE(file != NULL);
83
84 dir = file->getDir();
85 ASSERT_TRUE(dir != NULL);
86 EXPECT_STREQ("x/y", dir->getName());
87}
88
89// Before any virtual file is added, no virtual directory exists.
90TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) {
91 // An empty FakeStatCache causes all stat calls made by the
92 // FileManager to report "file/directory doesn't exist". This
93 // avoids the possibility of the result of this test being affected
94 // by what's in the real file system.
95 manager.addStatCache(new FakeStatCache);
96
97 EXPECT_EQ(NULL, manager.getDirectory("virtual/dir/foo"));
98 EXPECT_EQ(NULL, manager.getDirectory("virtual/dir"));
99 EXPECT_EQ(NULL, manager.getDirectory("virtual"));
100}
101
102// When a virtual file is added, all of its ancestors should be created.
103TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) {
104 // Fake an empty real file system.
105 manager.addStatCache(new FakeStatCache);
106
107 manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
108 EXPECT_EQ(NULL, manager.getDirectory("virtual/dir/foo"));
109
110 const DirectoryEntry *dir = manager.getDirectory("virtual/dir");
111 ASSERT_TRUE(dir != NULL);
112 EXPECT_STREQ("virtual/dir", dir->getName());
113
114 dir = manager.getDirectory("virtual");
115 ASSERT_TRUE(dir != NULL);
116 EXPECT_STREQ("virtual", dir->getName());
117}
118
119// getFile() returns non-NULL if a real file exists at the given path.
120TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
121 // Inject fake files into the file system.
122 FakeStatCache *statCache = new FakeStatCache;
123 statCache->InjectDirectory("/tmp", 42);
124 statCache->InjectFile("/tmp/test", 43);
Rafael Espindola146d57f2013-07-29 15:47:24 +0000125
126#ifdef _WIN32
127 const char *DirName = "C:.";
128 const char *FileName = "C:test";
129 statCache->InjectDirectory(DirName, 44);
130 statCache->InjectFile(FileName, 45);
131#endif
132
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000133 manager.addStatCache(statCache);
134
135 const FileEntry *file = manager.getFile("/tmp/test");
136 ASSERT_TRUE(file != NULL);
137 EXPECT_STREQ("/tmp/test", file->getName());
138
139 const DirectoryEntry *dir = file->getDir();
140 ASSERT_TRUE(dir != NULL);
141 EXPECT_STREQ("/tmp", dir->getName());
Rafael Espindola146d57f2013-07-29 15:47:24 +0000142
143#ifdef _WIN32
144 file = manager.getFile(FileName);
145 ASSERT_TRUE(file != NULL);
146
147 dir = file->getDir();
148 ASSERT_TRUE(dir != NULL);
149 EXPECT_STREQ(DirName, dir->getName());
150#endif
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000151}
152
153// getFile() returns non-NULL if a virtual file exists at the given path.
154TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
155 // Fake an empty real file system.
156 manager.addStatCache(new FakeStatCache);
157
158 manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
159 const FileEntry *file = manager.getFile("virtual/dir/bar.h");
160 ASSERT_TRUE(file != NULL);
161 EXPECT_STREQ("virtual/dir/bar.h", file->getName());
162
163 const DirectoryEntry *dir = file->getDir();
164 ASSERT_TRUE(dir != NULL);
165 EXPECT_STREQ("virtual/dir", dir->getName());
166}
167
168// getFile() returns different FileEntries for different paths when
169// there's no aliasing.
170TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) {
171 // Inject two fake files into the file system. Different inodes
172 // mean the files are not symlinked together.
173 FakeStatCache *statCache = new FakeStatCache;
174 statCache->InjectDirectory(".", 41);
175 statCache->InjectFile("foo.cpp", 42);
176 statCache->InjectFile("bar.cpp", 43);
177 manager.addStatCache(statCache);
178
179 const FileEntry *fileFoo = manager.getFile("foo.cpp");
180 const FileEntry *fileBar = manager.getFile("bar.cpp");
181 ASSERT_TRUE(fileFoo != NULL);
182 ASSERT_TRUE(fileBar != NULL);
183 EXPECT_NE(fileFoo, fileBar);
184}
185
186// getFile() returns NULL if neither a real file nor a virtual file
187// exists at the given path.
188TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) {
189 // Inject a fake foo.cpp into the file system.
190 FakeStatCache *statCache = new FakeStatCache;
191 statCache->InjectDirectory(".", 41);
192 statCache->InjectFile("foo.cpp", 42);
193 manager.addStatCache(statCache);
194
195 // Create a virtual bar.cpp file.
196 manager.getVirtualFile("bar.cpp", 200, 0);
197
198 const FileEntry *file = manager.getFile("xyz.txt");
199 EXPECT_EQ(NULL, file);
200}
201
202// The following tests apply to Unix-like system only.
203
NAKAMURA Takumi8e27eb72011-06-24 14:10:29 +0000204#ifndef _WIN32
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000205
206// getFile() returns the same FileEntry for real files that are aliases.
207TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
208 // Inject two real files with the same inode.
209 FakeStatCache *statCache = new FakeStatCache;
210 statCache->InjectDirectory("abc", 41);
211 statCache->InjectFile("abc/foo.cpp", 42);
212 statCache->InjectFile("abc/bar.cpp", 42);
213 manager.addStatCache(statCache);
214
215 EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
216}
217
218// getFile() returns the same FileEntry for virtual files that have
219// corresponding real files that are aliases.
220TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
221 // Inject two real files with the same inode.
222 FakeStatCache *statCache = new FakeStatCache;
223 statCache->InjectDirectory("abc", 41);
224 statCache->InjectFile("abc/foo.cpp", 42);
225 statCache->InjectFile("abc/bar.cpp", 42);
226 manager.addStatCache(statCache);
227
228 manager.getVirtualFile("abc/foo.cpp", 100, 0);
229 manager.getVirtualFile("abc/bar.cpp", 200, 0);
230
231 EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
232}
233
NAKAMURA Takumi8e27eb72011-06-24 14:10:29 +0000234#endif // !_WIN32
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000235
236} // anonymous namespace