blob: 9df85329a4d471eed915b9e2d780c35610b3ca28 [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"
Stephen Hines651f13c2014-04-23 16:59:28 -070014#include "llvm/Config/config.h"
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000015
16using namespace llvm;
17using namespace clang;
18
19namespace {
20
21// Used to create a fake file system for running the tests with such
22// that the tests are not affected by the structure/contents of the
23// file system on the machine running the tests.
24class FakeStatCache : public FileSystemStatCache {
25private:
26 // Maps a file/directory path to its desired stat result. Anything
27 // not in this map is considered to not exist in the file system.
Rafael Espindola0fda0f72013-08-01 21:42:11 +000028 llvm::StringMap<FileData, llvm::BumpPtrAllocator> StatCalls;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000029
30 void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +000031 FileData Data;
Stephen Hines651f13c2014-04-23 16:59:28 -070032 Data.Name = Path;
33 Data.Size = 0;
34 Data.ModTime = 0;
35 Data.UniqueID = llvm::sys::fs::UniqueID(1, INode);
Rafael Espindola0fda0f72013-08-01 21:42:11 +000036 Data.IsDirectory = !IsFile;
Stephen Hines651f13c2014-04-23 16:59:28 -070037 Data.IsNamedPipe = false;
38 Data.InPCH = false;
Rafael Espindola0fda0f72013-08-01 21:42:11 +000039 StatCalls[Path] = Data;
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000040 }
41
42public:
43 // Inject a file with the given inode value to the fake file system.
44 void InjectFile(const char *Path, ino_t INode) {
45 InjectFileOrDirectory(Path, INode, /*IsFile=*/true);
46 }
47
48 // Inject a directory with the given inode value to the fake file system.
49 void InjectDirectory(const char *Path, ino_t INode) {
50 InjectFileOrDirectory(Path, INode, /*IsFile=*/false);
51 }
52
53 // Implement FileSystemStatCache::getStat().
Rafael Espindola0fda0f72013-08-01 21:42:11 +000054 virtual LookupResult getStat(const char *Path, FileData &Data, bool isFile,
Stephen Hines651f13c2014-04-23 16:59:28 -070055 vfs::File **F, vfs::FileSystem &FS) {
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000056 if (StatCalls.count(Path) != 0) {
Rafael Espindola0fda0f72013-08-01 21:42:11 +000057 Data = StatCalls[Path];
Zhanyong Wan9b555ea2011-02-11 18:44:49 +000058 return CacheExists;
59 }
60
61 return CacheMissing; // This means the file/directory doesn't exist.
62 }
63};
64
65// The test fixture.
66class FileManagerTest : public ::testing::Test {
67 protected:
68 FileManagerTest() : manager(options) {
69 }
70
71 FileSystemOptions options;
72 FileManager manager;
73};
74
75// When a virtual file is added, its getDir() field is set correctly
76// (not NULL, correct name).
77TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
78 const FileEntry *file = manager.getVirtualFile("foo.cpp", 42, 0);
79 ASSERT_TRUE(file != NULL);
80
81 const DirectoryEntry *dir = file->getDir();
82 ASSERT_TRUE(dir != NULL);
83 EXPECT_STREQ(".", dir->getName());
84
85 file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
86 ASSERT_TRUE(file != NULL);
87
88 dir = file->getDir();
89 ASSERT_TRUE(dir != NULL);
90 EXPECT_STREQ("x/y", dir->getName());
91}
92
93// Before any virtual file is added, no virtual directory exists.
94TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) {
95 // An empty FakeStatCache causes all stat calls made by the
96 // FileManager to report "file/directory doesn't exist". This
97 // avoids the possibility of the result of this test being affected
98 // by what's in the real file system.
99 manager.addStatCache(new FakeStatCache);
100
101 EXPECT_EQ(NULL, manager.getDirectory("virtual/dir/foo"));
102 EXPECT_EQ(NULL, manager.getDirectory("virtual/dir"));
103 EXPECT_EQ(NULL, manager.getDirectory("virtual"));
104}
105
106// When a virtual file is added, all of its ancestors should be created.
107TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) {
108 // Fake an empty real file system.
109 manager.addStatCache(new FakeStatCache);
110
111 manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
112 EXPECT_EQ(NULL, manager.getDirectory("virtual/dir/foo"));
113
114 const DirectoryEntry *dir = manager.getDirectory("virtual/dir");
115 ASSERT_TRUE(dir != NULL);
116 EXPECT_STREQ("virtual/dir", dir->getName());
117
118 dir = manager.getDirectory("virtual");
119 ASSERT_TRUE(dir != NULL);
120 EXPECT_STREQ("virtual", dir->getName());
121}
122
123// getFile() returns non-NULL if a real file exists at the given path.
124TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
125 // Inject fake files into the file system.
126 FakeStatCache *statCache = new FakeStatCache;
127 statCache->InjectDirectory("/tmp", 42);
128 statCache->InjectFile("/tmp/test", 43);
Rafael Espindola146d57f2013-07-29 15:47:24 +0000129
Stephen Hines651f13c2014-04-23 16:59:28 -0700130#ifdef LLVM_ON_WIN32
Rafael Espindola146d57f2013-07-29 15:47:24 +0000131 const char *DirName = "C:.";
132 const char *FileName = "C:test";
133 statCache->InjectDirectory(DirName, 44);
134 statCache->InjectFile(FileName, 45);
135#endif
136
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000137 manager.addStatCache(statCache);
138
139 const FileEntry *file = manager.getFile("/tmp/test");
140 ASSERT_TRUE(file != NULL);
141 EXPECT_STREQ("/tmp/test", file->getName());
142
143 const DirectoryEntry *dir = file->getDir();
144 ASSERT_TRUE(dir != NULL);
145 EXPECT_STREQ("/tmp", dir->getName());
Rafael Espindola146d57f2013-07-29 15:47:24 +0000146
Stephen Hines651f13c2014-04-23 16:59:28 -0700147#ifdef LLVM_ON_WIN32
Rafael Espindola146d57f2013-07-29 15:47:24 +0000148 file = manager.getFile(FileName);
149 ASSERT_TRUE(file != NULL);
150
151 dir = file->getDir();
152 ASSERT_TRUE(dir != NULL);
153 EXPECT_STREQ(DirName, dir->getName());
154#endif
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000155}
156
157// getFile() returns non-NULL if a virtual file exists at the given path.
158TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
159 // Fake an empty real file system.
160 manager.addStatCache(new FakeStatCache);
161
162 manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
163 const FileEntry *file = manager.getFile("virtual/dir/bar.h");
164 ASSERT_TRUE(file != NULL);
165 EXPECT_STREQ("virtual/dir/bar.h", file->getName());
166
167 const DirectoryEntry *dir = file->getDir();
168 ASSERT_TRUE(dir != NULL);
169 EXPECT_STREQ("virtual/dir", dir->getName());
170}
171
172// getFile() returns different FileEntries for different paths when
173// there's no aliasing.
174TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) {
175 // Inject two fake files into the file system. Different inodes
176 // mean the files are not symlinked together.
177 FakeStatCache *statCache = new FakeStatCache;
178 statCache->InjectDirectory(".", 41);
179 statCache->InjectFile("foo.cpp", 42);
180 statCache->InjectFile("bar.cpp", 43);
181 manager.addStatCache(statCache);
182
183 const FileEntry *fileFoo = manager.getFile("foo.cpp");
184 const FileEntry *fileBar = manager.getFile("bar.cpp");
185 ASSERT_TRUE(fileFoo != NULL);
186 ASSERT_TRUE(fileBar != NULL);
187 EXPECT_NE(fileFoo, fileBar);
188}
189
190// getFile() returns NULL if neither a real file nor a virtual file
191// exists at the given path.
192TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) {
193 // Inject a fake foo.cpp into the file system.
194 FakeStatCache *statCache = new FakeStatCache;
195 statCache->InjectDirectory(".", 41);
196 statCache->InjectFile("foo.cpp", 42);
197 manager.addStatCache(statCache);
198
199 // Create a virtual bar.cpp file.
200 manager.getVirtualFile("bar.cpp", 200, 0);
201
202 const FileEntry *file = manager.getFile("xyz.txt");
203 EXPECT_EQ(NULL, file);
204}
205
206// The following tests apply to Unix-like system only.
207
Stephen Hines651f13c2014-04-23 16:59:28 -0700208#ifndef LLVM_ON_WIN32
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000209
210// getFile() returns the same FileEntry for real files that are aliases.
211TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
212 // Inject two real files with the same inode.
213 FakeStatCache *statCache = new FakeStatCache;
214 statCache->InjectDirectory("abc", 41);
215 statCache->InjectFile("abc/foo.cpp", 42);
216 statCache->InjectFile("abc/bar.cpp", 42);
217 manager.addStatCache(statCache);
218
219 EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
220}
221
222// getFile() returns the same FileEntry for virtual files that have
223// corresponding real files that are aliases.
224TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) {
225 // Inject two real files with the same inode.
226 FakeStatCache *statCache = new FakeStatCache;
227 statCache->InjectDirectory("abc", 41);
228 statCache->InjectFile("abc/foo.cpp", 42);
229 statCache->InjectFile("abc/bar.cpp", 42);
230 manager.addStatCache(statCache);
231
232 manager.getVirtualFile("abc/foo.cpp", 100, 0);
233 manager.getVirtualFile("abc/bar.cpp", 200, 0);
234
235 EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp"));
236}
237
Stephen Hines651f13c2014-04-23 16:59:28 -0700238#endif // !LLVM_ON_WIN32
Zhanyong Wan9b555ea2011-02-11 18:44:49 +0000239
240} // anonymous namespace