blob: b5df814ba884c963e87976d867ca86aa146da348 [file] [log] [blame]
Zhanyong Wanbc402ab2011-02-15 21:30:27 +00001//===- unittests/Basic/FileMangerTest.cpp ------------ FileManger tests ---===//
Zhanyong Wane1dd3e22011-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 Carruth320d9662012-12-04 09:45:34 +000010#include "clang/Basic/FileManager.h"
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000011#include "clang/Basic/FileSystemOptions.h"
12#include "clang/Basic/FileSystemStatCache.h"
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000013#include "gtest/gtest.h"
Alp Toker1d257e12014-06-04 03:28:55 +000014#include "llvm/Config/llvm-config.h"
Zhanyong Wane1dd3e22011-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 Espindolaf8f91b82013-08-01 21:42:11 +000028 llvm::StringMap<FileData, llvm::BumpPtrAllocator> StatCalls;
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000029
30 void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000031 FileData Data;
Ben Langmuird066d4c2014-02-28 21:16:07 +000032 Data.Name = Path;
33 Data.Size = 0;
34 Data.ModTime = 0;
35 Data.UniqueID = llvm::sys::fs::UniqueID(1, INode);
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000036 Data.IsDirectory = !IsFile;
Ben Langmuird066d4c2014-02-28 21:16:07 +000037 Data.IsNamedPipe = false;
38 Data.InPCH = false;
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000039 StatCalls[Path] = Data;
Zhanyong Wane1dd3e22011-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 Espindolaf8f91b82013-08-01 21:42:11 +000054 virtual LookupResult getStat(const char *Path, FileData &Data, bool isFile,
Ben Langmuirc8130a72014-02-20 21:59:23 +000055 vfs::File **F, vfs::FileSystem &FS) {
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000056 if (StatCalls.count(Path) != 0) {
Rafael Espindolaf8f91b82013-08-01 21:42:11 +000057 Data = StatCalls[Path];
Zhanyong Wane1dd3e22011-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);
Craig Topper416fa342014-06-08 08:38:12 +000079 ASSERT_TRUE(file != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000080
81 const DirectoryEntry *dir = file->getDir();
Craig Topper416fa342014-06-08 08:38:12 +000082 ASSERT_TRUE(dir != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000083 EXPECT_STREQ(".", dir->getName());
84
85 file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
Craig Topper416fa342014-06-08 08:38:12 +000086 ASSERT_TRUE(file != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000087
88 dir = file->getDir();
Craig Topper416fa342014-06-08 08:38:12 +000089 ASSERT_TRUE(dir != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +000090 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
Craig Topper416fa342014-06-08 08:38:12 +0000101 EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo"));
102 EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir"));
103 EXPECT_EQ(nullptr, manager.getDirectory("virtual"));
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000104}
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);
Craig Topper416fa342014-06-08 08:38:12 +0000112 EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo"));
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000113
114 const DirectoryEntry *dir = manager.getDirectory("virtual/dir");
Craig Topper416fa342014-06-08 08:38:12 +0000115 ASSERT_TRUE(dir != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000116 EXPECT_STREQ("virtual/dir", dir->getName());
117
118 dir = manager.getDirectory("virtual");
Craig Topper416fa342014-06-08 08:38:12 +0000119 ASSERT_TRUE(dir != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000120 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 Espindolaee305462013-07-29 15:47:24 +0000129
Hans Wennborg501eadb2014-03-12 16:07:46 +0000130#ifdef LLVM_ON_WIN32
Rafael Espindolaee305462013-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 Wane1dd3e22011-02-11 18:44:49 +0000137 manager.addStatCache(statCache);
138
139 const FileEntry *file = manager.getFile("/tmp/test");
Craig Topper416fa342014-06-08 08:38:12 +0000140 ASSERT_TRUE(file != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000141 EXPECT_STREQ("/tmp/test", file->getName());
142
143 const DirectoryEntry *dir = file->getDir();
Craig Topper416fa342014-06-08 08:38:12 +0000144 ASSERT_TRUE(dir != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000145 EXPECT_STREQ("/tmp", dir->getName());
Rafael Espindolaee305462013-07-29 15:47:24 +0000146
Hans Wennborg501eadb2014-03-12 16:07:46 +0000147#ifdef LLVM_ON_WIN32
Rafael Espindolaee305462013-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 Wane1dd3e22011-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");
Craig Topper416fa342014-06-08 08:38:12 +0000164 ASSERT_TRUE(file != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000165 EXPECT_STREQ("virtual/dir/bar.h", file->getName());
166
167 const DirectoryEntry *dir = file->getDir();
Craig Topper416fa342014-06-08 08:38:12 +0000168 ASSERT_TRUE(dir != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000169 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");
Craig Topper416fa342014-06-08 08:38:12 +0000185 ASSERT_TRUE(fileFoo != nullptr);
186 ASSERT_TRUE(fileBar != nullptr);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000187 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");
Craig Topper416fa342014-06-08 08:38:12 +0000203 EXPECT_EQ(nullptr, file);
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000204}
205
206// The following tests apply to Unix-like system only.
207
Hans Wennborg501eadb2014-03-12 16:07:46 +0000208#ifndef LLVM_ON_WIN32
Zhanyong Wane1dd3e22011-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
Hans Wennborg501eadb2014-03-12 16:07:46 +0000238#endif // !LLVM_ON_WIN32
Zhanyong Wane1dd3e22011-02-11 18:44:49 +0000239
240} // anonymous namespace